$(document).ready(function(){

	//-------------------------open browser window fullscreen------------		
	function FullScreen(){
				this.moveTo(0,0);
				resizeTo(screen.availWidth,screen.availHeight);
	}
	
	FullScreen();

//------------------------FONT SIZE--------------------------------------

	//font.setup(); 
	//if(!font-family.isInstalled('KiNfoNt1Regular'))$('body').css('font-size','75.0%');
	
	



	//--------------------musicBox position----------------------------
	function musicBoxPosition(){
		var windowWidth = $(window).width();
		var musicBoxWidth = $("#fullscreenFooter").width();
		var x = windowWidth / 2;
		var y = musicBoxWidth / 2;
		var z = x - y;
		var b = z - 20;
		$('#fullscreenFooter').css("left",b);
		if (b <= 0){
			$('#fullscreenFooter').css("left",0);
		}
	
	}
	musicBoxPosition();
	
	//--------------------photoBox position----------------------------
	function photoBoxPosition(){
		var windowWidth = $(window).width();
		var photoBoxWidth = $("#photoBox").width();
		var x = windowWidth / 2;
		var y = photoBoxWidth / 2;
		var z = x - y;
		var b = z - 100;
		$('#photoBox').css("left",b);		
		if (b <= 120){
			$('#photoBox').css("left",100);
		}

	}
	photoBoxPosition();

	$(function(){ 
        	$("#littleColors").click(function(event){
        		event.preventDefault();        		
        		$('#fullscreenFooter').find("span:last").remove();
        		showLoaderGif();        		
        		hidePlayer();
        		musicBoxPosition();
        		littleColors();
        		hideGif();				
			});			
	});	
	$(function(){ 
        	$("#sheSaidNothing").click(function(event){
        		event.preventDefault();       		
        		$('#fullscreenFooter').find("span:last").remove();
        		showLoaderGif();        		
        		hidePlayer();
        		musicBoxPosition();
        		sheSaidNothing();
        		hideGif();				
			});			
	});	
	$(function(){ 
        	$("#NotComeAgain").click(function(event){
        		event.preventDefault();        		
        		$('#fullscreenFooter').find("span:last").remove();
        		showLoaderGif();        		
        		hidePlayer();
        		musicBoxPosition();
        		NotComeAgain();
        		hideGif();				
			});			
	});	
	$(function(){ 
        	$("#MadameButterfly").click(function(event){
				event.preventDefault();
				$('#fullscreenFooter').find("span:last").remove();
				showLoaderGif();
				hidePlayer();
				musicBoxPosition();
				MadameButterfly();
				hideGif();				
			});
	});	
	$(function(){ 
        	$("#PapaGeno").click(function(event){
        		event.preventDefault();
        		$('#fullscreenFooter').find("span:last").remove();
				showLoaderGif();
				hidePlayer();
				musicBoxPosition();
				PapaGeno();
				hideGif();				
			});
	});

	$(function(){ 
        	$("#StationRMX").click(function(event){
        		event.preventDefault();        		
        		$('#fullscreenFooter').find("span:last").remove();
        		showLoaderGif();        		
        		hidePlayer();
        		musicBoxPosition();
        		StationRMX();
        		hideGif();				
			});			
	});


function littleColors(){
				
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F29778090&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" width="800px" src="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F29778090&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
		
		
		
	}

	
function sheSaidNothing(){
				
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F547475&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" width="800px" src="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F547475&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
	}	

function NotComeAgain(){
				
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fkin-not-come-again&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" width="800px" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fkin-not-come-again&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
		
		
		
	}

function MadameButterfly(){
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fmadame-butterfly&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" width="800px" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fmadame-butterfly&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
		
		
	}
	
function PapaGeno(){
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fpapageno-1&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" width="800px" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fhawken1%2Fpapageno-1&amp;show_comments=true&amp;auto_play=true&amp;color=ff0919" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
	}
	
	
	
function StationRMX(){
		$('#fullscreenFooter').append('<span><object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fkin%2Fgirl-at-the-station-kin-paramaterial-remix&amp;show_comments=true&amp;auto_play=true&amp;color=ff7700"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fkin%2Fgirl-at-the-station-kin-paramaterial-remix&amp;show_comments=true&amp;auto_play=true&amp;color=ff7700" type="application/x-shockwave-flash" width="100%"></embed> </object></span>');
	}	

	
function showLoaderGif() {
		$('#centerBox').html('<div id="loaderGif"></div>');  
		$('#loaderGif').fadeIn('normal');
	}
function hideGif(){
   		$('#loaderGif').fadeOut('slow',showPlayer);
 	}

function showPlayer(){
		$('#fullscreenFooter').slideDown(3000);
	}
function hidePlayer(){
		$('#fullscreenFooter').hide();
	}

	$(function(){ 
		$("#musicLink").click(function(event){
			event.preventDefault();
			$('#musicBox').toggle();
			if( $('#musicBox').css('display') == 'none' ) {
				$('#musicLink').html('+ Music');
				hidePlayer();
				$('#fullscreenFooter').html('');
  			}
			if( $('#musicBox').css('display') == 'block' ) {

				$('#musicLink').html('- Music');
 			}			
		});
	});
	$(function(){ 
		$("#videoLink").click(function(event){
			event.preventDefault();
			$('#videoBox').toggle();
			
			if( $('#videoBox').css('display') == 'none' ) {
				$('#videoLink').html('+ Video');
  			}
			if( $('#videoBox').css('display') == 'block' ) {

				$('#videoLink').html('- Video');
 			}		
		});
	});
	
	$(function(){
		$('#photoLink').click(function(event){
			event.preventDefault();
			showLoaderGif();
			$('#photoBox').toggle();
			$('#photoBox').ajaxStop(hideGif());
			if( $('#photoBox').css('display') == 'none' ) {
				$('#photoLink').html('+ Photo');
  			}
			else {
				$('#photoLink').html('- Photo');
 			}
				
		});
	});
	//------------------class = "draggable" --------------------------	
	$(function(){
		$(".draggable").draggable({ containment: 'window' });
	});
	//----------------window resize----------------------------
	$(window).resize(function() {
		musicBoxPosition();
		photoBoxPosition();					  		
	});


	//-----------------background color pulse------------------------
	$('#news').pulse({
        backgroundColors: ['#29ABE2','#ED1E79','#F15A24','#42210B','#22B573'],
        speed: 3000
    });
	

	//----------------moving dolls-----------------------------------
	/*	
		function switch13(){
  			$("#kinDollID5").css('backgroundPosition', '0px 140px');  		
  		}
  		
  		function switch14(){
  			$("#kinDollID5").css('backgroundPosition', '0px 70px');
  		}
  		
  		function switch15(){
  			$("#kinDollID5").css('backgroundPosition', '0px 0px');  		
  		}
  		
  		function switch16(){
  			$("#kinDollID5").css('backgroundPosition', '0px 210px');
  		}


  		function switch13141516(){
  			
  			setTimeout(function(){switch13();}, 40400);
  			setTimeout(function(){switch14();}, 40800);
  			setTimeout(function(){switch13();}, 41200);
  			setTimeout(function(){switch14();}, 41600);
  			setTimeout(function(){switch13();}, 42000);
  			setTimeout(function(){switch14();}, 42400);
  			setTimeout(function(){switch13();}, 42800);
  			setTimeout(function(){switch14();}, 43200);
  			setTimeout(function(){switch13();}, 43600);
  			setTimeout(function(){switch14();}, 44000);
  			setTimeout(function(){switch13();}, 44400);
  			setTimeout(function(){switch14();}, 44800);
  			setTimeout(function(){switch13();}, 45200);
  			setTimeout(function(){switch14();}, 45600);
  			setTimeout(function(){switch13();}, 46000);
  			setTimeout(function(){switch16();}, 46400);
  			setTimeout(function(){switch13();}, 46800);
  			setTimeout(function(){switch14();}, 47200);
  			setTimeout(function(){switch13();}, 47600);
  			setTimeout(function(){switch14();}, 48000);
  			setTimeout(function(){switch13();}, 48400);
  			setTimeout(function(){switch14();}, 48800);
  			setTimeout(function(){switch13();}, 49200);
  			setTimeout(function(){switch14();}, 49600);
  			setTimeout(function(){switch13();}, 50000);
  			setTimeout(function(){switch14();}, 50400);
  			setTimeout(function(){switch13();}, 50800);
  			setTimeout(function(){switch14();}, 51200);
  			setTimeout(function(){switch13();}, 51600);
  			setTimeout(function(){switch16();}, 52000);
  			setTimeout(function(){switch13();}, 52400);
  			setTimeout(function(){switch14();}, 52800);
  			setTimeout(function(){switch13();}, 53200);
  			setTimeout(function(){switch16();}, 53600);
  			setTimeout(function(){switch13();}, 54000);
  			setTimeout(function(){switch14();}, 54400);
  			setTimeout(function(){switch15();}, 54800);
  			setTimeout(function(){switch14();}, 55200);
  			setTimeout(function(){switch13();}, 55600);
  			setTimeout(function(){switch14();}, 56000);
  			setTimeout(function(){switch15();}, 56400);
  			setTimeout(function(){switch14();}, 56800);
  			setTimeout(function(){switch13();}, 56400);
  			setTimeout(function(){switch16();}, 56800);
			setTimeout(function(){switch13();}, 57200);
  			setTimeout(function(){switch14();}, 57600);
			setTimeout(function(){switch15();}, 58000);
  			setTimeout(function(){switch14();}, 58400);
			setTimeout(function(){switch13();}, 58800);
  			setTimeout(function(){switch16();}, 59200);
			setTimeout(function(){switch13();}, 59600);
  			setTimeout(function(){switch14();}, 60000);
			setTimeout(function(){switch15();}, 60400);
  		}

		
		$("#kinDollID5").delay(40000).animate({'left': '+=16%'},23000),switch13141516();
	
		$("#kinDollID5").delay(7000).animate({'left': '+=40%'},25000),switch1516();
		
	
			function switch1516(){
  			
  			setTimeout(function(){switch15();}, 70400);
  			setTimeout(function(){switch16();}, 70800);
  			setTimeout(function(){switch15();}, 71200);
  			setTimeout(function(){switch16();}, 71600);
  			setTimeout(function(){switch15();}, 72000);
  			setTimeout(function(){switch16();}, 72400);
  			setTimeout(function(){switch15();}, 72800);
  			setTimeout(function(){switch16();}, 73200);
  			setTimeout(function(){switch15();}, 73600);
  			setTimeout(function(){switch16();}, 74000);
  			setTimeout(function(){switch15();}, 74400);
  			setTimeout(function(){switch16();}, 74800);
  			setTimeout(function(){switch15();}, 75200);
  			setTimeout(function(){switch16();}, 75600);
  			setTimeout(function(){switch15();}, 76000);
  			setTimeout(function(){switch16();}, 76400);
  			setTimeout(function(){switch15();}, 76800);
  			setTimeout(function(){switch16();}, 77200);
  			setTimeout(function(){switch15();}, 77600);
  			setTimeout(function(){switch16();}, 78000);
  			setTimeout(function(){switch15();}, 78400);
  			setTimeout(function(){switch16();}, 78800);
  			setTimeout(function(){switch15();}, 79200);
  			setTimeout(function(){switch16();}, 79600);
  			setTimeout(function(){switch15();}, 80000);
  			setTimeout(function(){switch16();}, 80400);
  			setTimeout(function(){switch15();}, 80800);
  			setTimeout(function(){switch16();}, 81200);
  			setTimeout(function(){switch15();}, 81600);
  			setTimeout(function(){switch16();}, 82000);
  			setTimeout(function(){switch15();}, 82400);
  			setTimeout(function(){switch16();}, 82800);
  			setTimeout(function(){switch15();}, 83200);
  			setTimeout(function(){switch16();}, 83600);
  			setTimeout(function(){switch15();}, 84000);
  			setTimeout(function(){switch16();}, 84400);
  			setTimeout(function(){switch15();}, 84800);
  			setTimeout(function(){switch16();}, 85200);
  			setTimeout(function(){switch15();}, 85600);
  			setTimeout(function(){switch16();}, 86000);
  			setTimeout(function(){switch15();}, 86400);
  			setTimeout(function(){switch16();}, 86800);
  			setTimeout(function(){switch15();}, 86400);
  			setTimeout(function(){switch16();}, 86800);
			setTimeout(function(){switch15();}, 87200);
  			setTimeout(function(){switch16();}, 87600);
			setTimeout(function(){switch15();}, 88000);
  			setTimeout(function(){switch16();}, 88400);
			setTimeout(function(){switch15();}, 88800);
  			setTimeout(function(){switch16();}, 89200);
			setTimeout(function(){switch15();}, 89600);
  			setTimeout(function(){switch16();}, 90000);
			setTimeout(function(){switch15();}, 90400);
  		}
*/




			
$(window).load(function(){  
//initialize after images are loaded 
 
function extractUrl(input)
{
 // remove quotes and wrapping url()
 return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
}

$("div.dolls").each(function() { 
	dollID = $(this).attr("id");
	imgURL = extractUrl($(this).css("background-image"));
	var str = $(this).next("div").attr("class");
	var substr = str.split(' ');	
	var startPosition = $.trim(substr[0]);
	var startDirection = $.trim(substr[1]);
	var walkDistance = $.trim(substr[2]);
	var walkDir = $.trim(substr[3]);
	if (walkDir == 'left'){
		var walkDirection = "-=";
	}
	else{
		var walkDirection = "+=";
	}
	var timeBeforeStart = parseInt(substr[4]);
	var walkTime = parseInt(substr[5]);
	var movementSpeed = parseInt(substr[6]);
	var imgHeight = parseInt(substr[7]);
	var imgwidth = parseInt(substr[8]);
	var spriteImgColumns = parseInt(substr[9]);
	var spriteImgRows = parseInt(substr[10]);
	//alert(substr[11]);
	
	//alert("från PHP till jquery:  startPosition = " + startPosition + " startDirection = " + startDirection + " walkDist = " + walkDist + " walkDir = " + walkDir + " timeBeforeStart = " + timeBeforeStart + " walkTime = " + walkTime + " movementSpeed = " + movementSpeed + " imgHeight = " + imgHeight + " imgwidth = " + imgwidth + " spriteImgColumns = " + spriteImgColumns + " spriteImgRows = " + spriteImgRows);

	var dollObject = new dOll("#"+dollID,imgURL,startPosition,startDirection,walkDistance,walkDirection,timeBeforeStart,walkTime,movementSpeed,imgHeight,imgwidth,spriteImgColumns,spriteImgRows);
	
	var zz = 11;
	if(substr[zz]!=null){
	
		
		function nextDoll(){
		//alert(substr[zz]);
			var walkDistance = $.trim(substr[zz]);
			var walkDir = $.trim(substr[zz+1]);
			if (walkDir == 'left'){
				var walkDirection = "-=";
			}
			else{
				var walkDirection = "+=";
			}
			var timeBeforeStart = parseInt(substr[zz+2]);
			var walkTime = parseInt(substr[zz+3]);
	//alert(walkDistance + walkDirection + timeBeforeStart + walkTime);
	
		var dollObject = new dOll("#"+dollID,imgURL,startPosition,startDirection,walkDistance,walkDirection,timeBeforeStart,walkTime,movementSpeed,imgHeight,imgwidth,spriteImgColumns,spriteImgRows);
			zz=zz+4;
			if(substr[zz]!=null){
				nextDoll();
			}
		}
		nextDoll();
	}		
});
	
	function dOll (dollID,imgURL,startPosition,startDirection,walkDistance,walkDirection,timeBeforeStart,walkTime,movementSpeed,imgHeight,imgWidth,spriteImgColumns,spriteImgRows) {
	
		this.dollID=dollID;
		this.imgURL=imgURL;
		this.startPosition=startPosition;
		this.startDirection=startDirection;
		this.walkDistance=walkDistance;
		this.walkDirection=walkDirection;
		this.timeBeforeStart=timeBeforeStart;
		this.walkTime=walkTime;
		this.movementSpeed=movementSpeed;
		this.imgHeight=imgHeight;
		this.imgWidth=imgWidth;
		this.spriteImgColumns=spriteImgColumns;
		this.spriteImgRows=spriteImgRows;
		
		//alert("dollID : " + this.dollID + " imgURL : " + this.imgURL + " startPosition : " + this.startPosition + " startDirection : " + this.startDirection + " walkDistance : " + this.walkDistance + " walkDirection : " + this.walkDirection + " timeBeforeStart : " + this.timeBeforeStart + " walkTime : " + this.walkTime + " movementSpeed : " + this.movementSpeed + " imgHeight : " + this.imgHeight + " imgWidth : " + this.imgWidth + " spriteImgColumns : " + this.spriteImgColumns + " spriteImgRows : " + this.spriteImgRows);
		
		e = imgWidth / spriteImgColumns;
		f = imgHeight / spriteImgRows;
		if(walkDirection == "+=") {
		//alert("test");
			var bgPosNeutral = '0px 0px';
			var bgPosBowed = '0px ' + f + 'px';
		}
		else {
			var bgPosNeutral = e + 'px 0px';
			var bgPosBowed = e + 'px ' + f + 'px';
		}
			//$(this).css("background-image", "url("+imgURL+")");
			$(dollID).css('backgroundPosition', bgPosNeutral);
			setTimeout(function(){walkDoll();}, timeBeforeStart);
			var cc = $(dollID);
			
			function waggleDollStraight(){
	  			$(cc).css('backgroundPosition', bgPosNeutral);
	  		}
	  		function waggleDollBowed(){
	  			$(cc).css('backgroundPosition', bgPosBowed);
	  		}
	  		var ff = 0;
	  		var a = 1000;
			var t;
	  		function timedCount() {
				if(ff == 0){
					waggleDollBowed();
					ff=1;
				}
				else{
					waggleDollStraight();
					ff=0;
				}
				a = a + movementSpeed;
				if(a <= walkTime) {
				//alert(movementSpeed);
					t=setTimeout(function(){timedCount();}, movementSpeed);
				}
				else {
					clearTimeout(t);
					waggleDollStraight();
				}
			}
	  		function walkDoll(){
	  			//alert("walkDoll " + walkTime);
	  			$(cc).animate({'left': walkDirection + walkDistance},walkTime);
				timedCount();
	  		}
	}
});
  	/*
  	$("#musicLink").airport([ '+ Music','+ 音乐','+ Muziek','+ Mizik' ]);
  	$("#musicLink").airport([ '+ Music','+ 音乐','+ Muziek','+ Mizik' ]);
  	$("#videoLink").airport([ '+ Video','+ 视频','+ Videyo','+ Wideo' ]);
  	$("#photoLink").airport([ '+ Photo','+ 照片','+ Foto','+ Ritratt' ]);
  	*/	
  	
  	
  	var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (agentID) {

    // do something special
    //alert("iphone-ipod-ipad");
        detectOrientation();
	window.onorientationchange = detectOrientation;
	function detectOrientation(){
		if(typeof window.onorientationchange != 'undefined'){
			if ( orientation == 0 ) {
				 //Do Something In Portrait Mode
				 
				 $('#supersize').resizenow();
				// startDolls();
			}
			else if ( orientation == 90 ) {
				 //Do Something In Landscape Mode
				 $('#supersize').resizenow();
				// startDolls();
			}
			else if ( orientation == -90 ) {
				 //Do Something In Landscape Mode
				 $('#supersize').resizenow();
				// startDolls();
			}
			else if ( orientation == 180 ) {
				 //Do Something In Landscape Mode
				 
				 $('#supersize').resizenow();
				// startDolls();
			}
		}
	} 
}

  	
  	
  	
  	
});	
