////////// google youtube iframe api code //////////
 //Load player api asynchronously.
    var tag = document.createElement('script');
    tag.src = "http://www.youtube.com/player_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    var ytPlayer = new Array();
    function onYouTubePlayerAPIReady() {
    	
    	//isolt youtube-player
    	 ytPlayer[0] = new YT.Player('yt-isolt', {
          height: '368',
          width: '448',
          videoId: 'x8QkAnn4ImY?hl=sv&fs=1',
          events: {
            'onStateChange': onPlayerStateChange
          }
        })
    	
    	//babel youtube-player
        ytPlayer[1] = new YT.Player('yt-babel', {
          height: '315',
          width: '420',
          videoId: 'EX47n9Ad3h4',
          events: {
            'onStateChange': onPlayerStateChange
          }
        });
        
        //underground youtube-player
        ytPlayer[2] = new YT.Player('yt-underground', {
          height: '315',
          width: '420',
          videoId: 'vWRGB7urz7Y',
          events: {
            'onStateChange': onPlayerStateChange
          }
        });
        
        //porsche youtube-player
        ytPlayer[3] = new YT.Player('yt-porsche', {
          height: '315',
          width: '560',
          videoId: 'Kh3A1ERsvY4',
          events: {
            'onStateChange': onPlayerStateChange
          }
        });
    }
    /*
    function onPlayerReady(evt) {
        evt.target.playVideo();
    }
    */
function onPlayerStateChange(evt) {
	if (evt.data == YT.PlayerState.PLAYING) {
		stopAllSoundcloud();
		stopOtherVideoPlayers(evt.target);
	}
}
    
function stopAllVideoPlayers() {
	for (var i=0; i<ytPlayer.length; i++)
		ytPlayer[i].stopVideo();
}
    
function stopOtherVideoPlayers(target) {
	for (var i=0; i<ytPlayer.length; i++) {// go through all yt-players
		if (target != ytPlayer[i])// pause players that are not the target here
			ytPlayer[i].pauseVideo();
		}
}

//////////////////// END google youtube iframe api code ///////////////



var boxes=5;
var boxIsOpen=new Array(); 
for (var i=0; i<boxes; i++) {//boxes 1-5 represented by idx 0-4
	boxIsOpen[i] = false;
	}
var mainView = true;

function openBox(i) {
	var h1=document.getElementById('box'+i).style.height;
	if (h1 !="4em") {
		closeBox(i);
	}
		
	var h2=document.getElementById('inside_box'+i).offsetHeight;
	if (h1 == "4em" && screen.width >= 481) {
		$(function() {
    		$('#box'+i).animate({height:h2}, 'slow', function() {
    			if (i != 1) {
    				$.scrollTo('#box'+i, '350');
    				$('#returnButton').css('top','18px');
    				$('#shutupButton').css('top','68px');
    				//$('#box'+i).css('background-color','#323232');
    				if (i == 5)
    					$('#box'+i).css('background-color','#242424');
    				} else if (i == 1) {
    					$('#returnButton').css('top','55px');
    					$('#shutupButton').css('top','105px');
    					//$('#box'+i).css('background-color','#2f2f2f');
    				}
    			$('#returnButton').fadeTo(1500, 0.7);
    			$('#shutupButton').fadeTo(1500, 0.7);
  			});
  		});
  		mainView=false;
    	boxIsOpen[i-1] = true;
		hideTweet();
		} else if (h1 == "4em" && screen.width <=480) {
    		//$('#box'+i).css('height',h2);
    		$("#box"+i).animate({height:h2}, 100);
    			if (i != 1) {
    				$('#returnButton').css('top','18px');
    				$('#shutupButton').css('top','68px');
    				} else if (i == 1) {
    					$('#returnButton').css('top','55px');
    					$('#shutupButton').css('top','105px');
    				}
    			$('#returnButton').fadeTo(1500, 0.7);
    			$('#shutupButton').fadeTo(1500, 0.7);
  
  		mainView=false;
    	boxIsOpen[i-1] = true;
		hideTweet();
			
			
			
		}	
			
		/*	
		} else //end if
			closeBox(i);*/
}

function returnToMain () {
	if (!mainView) {
	for (var i=1; i<=boxes; i++) {
		closeBox(i);
		boxIsOpen[i-1]=false;
		}//end for
		$('#returnButton').css('top:18px');
		$(function() {
			$('#returnButton').animate({opacity: .0}, 0, function() {
			diplayTweet();
			});
		});
		shutup();
		mainView = true;
	}//end if
}

function shutup() {
	stopAllSoundcloud();
	stopAllVideoPlayers();
}

function closeBox(i) {
	if (screen.width >=481) {
	$(function() {
		//$.scPlayer.stopAll(); //om alla players ska stanna när en box stängs
		$("#box"+i).animate({height:"4em"}, 1000);
	});
	boxIsOpen[i-1]=false;
	if (i==5)
    	document.getElementById('box'+i).style.backgroundColor="#292929";
	else if (i<5)
		document.getElementById('box'+i).style.backgroundColor="#333";
	
	mainView = true;
	for (var j=0; j<boxes; j++) {
		if (boxIsOpen[j]) // is there an open box?
			mainView=false;  
		}//end for

	if (mainView) { // if there were no open boxes
		$(function() {
			$('#returnButton').animate({opacity: .0}, 600, function() {
			//todo: om ingen sc-player spelar
				$('#shutupButton').animate({opacity: .0}, 'fast');
			});
		});
		stopAllSoundcloud();
		stopAllVideoPlayers();
		displayTweet();
		}
	} else if (screen.width <=480) {
		//$('#box'+i).css('height', '4em');
		$("#box"+i).animate({height:"4em"}, 20);
		boxIsOpen[i-1]=false;
		mainView = true;
		for (var j=0; j<boxes; j++) {
			if (boxIsOpen[j]) // is there an open box?
				mainView=false;  
			}//end for
		if (mainView) // if there were no open boxes
			stopAllSoundcloud();
	}
}

function stopAllSoundcloud() {
	$(function() {
	$.scPlayer.stopAll();
	});
}

function displayTweet() {
	$('div.tweet').animate({left:0}, 1200);
}
function hideTweet() {
	$('div.tweet').animate({left:'-5000px'}, 800);
}

function changeHeightOfBoxes() {//adjust height of 'boxes' (div's). triggered by window resize
	for (var i=1; i<boxes; i++) {
		var h1=document.getElementById("box"+i).style.height;
		if (h1!="4em") {
			var h2=document.getElementById("inside_box"+i).offsetHeight;
			$(function() {
   				$("#box"+i).animate({height:h2},80);
   			});
   			}// end if
		} //end for
}

function hideLoadingLayer() {
	$(function() {
		$('#loading_layer').fadeOut();
		$('#all').fadeIn();
	});
	displayTweet();
}

function mouseover_likebutton() {
	document.getElementById("likebutton").style.opacity='1';
}

function mouseout_likebutton() {
	document.getElementById("likebutton").style.opacity='.5';
}



