// JavaScript Document

// Cross-browser automated event firing
function fireEvent(element,event){
	if (document.createEventObject){
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt)
	}
	else{
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
}

var shareMenuMinimized = new Boolean();
	shareMenuMinimized = true;
	
var blogMinimized = new Boolean();
	blogMinimized = true;
	
var infoMinimized = new Boolean();
	infoMinimized = true;
	
var storeMinimized = new Boolean();
	storeMinimized = true;
	
var musicMinimized = new Boolean();
	musicMinimized = true;

var liveMinimized = new Boolean();
	liveMinimized = true;

var blogPageOffset;
var sharePadding;

$(document).ready(function() {
	$('#link_share').click(function() {
		var $slidingShareElement = $('#slidingShareElement');
		if(shareMenuMinimized) {
			shareMenuMinimized = false;
			maximizeShareSection();
			$slidingShareElement.animate({marginLeft:0});	
		} else {
			shareMenuMinimized = true;
			var callbackCheck = 1;
			$slidingShareElement.animate({marginLeft:291},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	$('#link_live').click(function() {
		var $slidingLiveElement = $('#slidingLiveElement');
		if(liveMinimized) {
			liveMinimized = false;
			maximizeLiveSection();
			$slidingLiveElement.animate({marginLeft:0});	
		} else {
			liveMinimized = true;
			var callbackCheck = 1;
			$slidingLiveElement.animate({marginLeft:391},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	$('#link_blog').click(function() {
		var $slidingBlogElement = $('#slidingBlogElement');
		if(blogMinimized) {
			blogMinimized = false;
			maximizeBlogSection();
			isInit = false;
			$slidingBlogElement.animate({marginLeft:0});	
		} else {
			blogMinimized = true;
			var callbackCheck = 1;
			$slidingBlogElement.animate({marginLeft:(blogPageOffset * -1)},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	$('#link_info').click(function() {
		var $slidingInfoElement = $('#slidingInfoElement');
		if(infoMinimized) {
			infoMinimized = false;
			maximizeInfoSection();
			$slidingInfoElement.animate({marginLeft:0});	
		} else {
			infoMinimized = true;
			var callbackCheck = 1;
			$slidingInfoElement.animate({marginLeft:-295},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	$('#link_store').click(function() {
		var $slidingStoreElement = $('#slidingStoreElement');
		if(storeMinimized) {
			storeMinimized = false;
			maximizeStoreSection();
			$slidingStoreElement.animate({marginLeft:0});	
		} else {
			storeMinimized = true;
			var callbackCheck = 1;
			$slidingStoreElement.animate({marginLeft:-295},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	$('#link_music').click(function() {
		var $slidingMusicElement = $('#slidingMusicElement');
		if(musicMinimized) {
			musicMinimized = false;
			maximizeMusicSection();
			$slidingMusicElement.animate({marginLeft:0});	
		} else {
			musicMinimized = true;
			var callbackCheck = 1;
			$slidingMusicElement.animate({marginLeft:-540},{complete:function(){
				if (! --callbackCheck) { setUpInitialView(); }
			}});
		}
	});
	
	var headerHeight = 130;
	var footerHeight = 102;
	var linkHeight = 40;
	var linkWidth = 89;
	
	var numElements = 3;
	var numElementsRight = 2;
	var elementPadding = 10;
	var topContainerPadding;
	var topContainerPaddingRight;

	var pageWidth;
	var pageHeight;
	
	if( typeof( window.innerWidth ) == 'number' ) { 
		//Non-IE 
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight; 
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
		//IE 6+ in 'standards compliant mode' 
		pageWidth = document.documentElement.clientWidth; 
		pageHeight = document.documentElement.clientHeight; 
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 
		//IE 4 compatible 
		pageWidth = document.body.clientWidth; 
		pageHeight = document.body.clientHeight; 
	}
	
	blogPageOffset = pageWidth * .6;

	function maximizeBlogSection() {
		var blogPadding = 20;
		
		var blogWindowHeight = pageHeight - headerHeight - footerHeight - (blogPadding * 2);

		var blogSlide = document.getElementById("blogSlide");		
			blogSlide.style.height = blogWindowHeight + 'px';
			blogSlide.style.top = headerHeight + blogPadding + 'px';
			blogSlide.style.zIndex = 10;
		
		var blogWindowBackground = document.getElementById("blogWindowBackground");
			blogWindowBackground.style.height = blogWindowHeight + 'px';
	
		var innerBlogWindow = document.getElementById("blogWindowInnerContent");
			innerBlogWindow.style.height  = (blogWindowHeight - blogPadding * 2) + 'px';
			innerBlogWindow.style.width = blogPageOffset - 60 + 'px';
			
		var blogLinkElement = document.getElementById("link_blog");
			blogLinkElement.style.top = (topContainerPadding - blogPadding) + 'px';
		
		var settings = { showArrows: true };
		var pane = $('#blogWindowInnerContent');		
			pane.jScrollPane(settings);
		var api = pane.data('jsp');
			api.reinitialise();
	};
	
	function maximizeStoreSection() {
		var storePadding = 20;
		
		var storeWindowHeight = pageHeight - headerHeight - footerHeight - (storePadding * 2);

		var storeSlide = document.getElementById("storeSlide");		
			storeSlide.style.height = storeWindowHeight + 'px';
			storeSlide.style.top = headerHeight + storePadding + 'px';
			storeSlide.style.zIndex = 10;
		
		var storeWindowBackground = document.getElementById("storeWindowBackground");
			storeWindowBackground.style.height = storeWindowHeight + 'px';
	
		var storeBlogWindow = document.getElementById("storeWindowInnerContent");
			storeBlogWindow.style.height = (storeWindowHeight - storePadding) + 'px';
	
		var storeLinkElement = document.getElementById("link_store");
			storeLinkElement.style.top = (topContainerPadding - storePadding + (linkHeight + elementPadding) * 2) + 'px';
	}

	function maximizeMusicSection() {
		var musicPadding = 20;
		
		var musicWindowHeight = pageHeight - headerHeight - footerHeight - (musicPadding * 2);

		var musicSlide = document.getElementById("musicSlide");		
			musicSlide.style.height = musicWindowHeight + 'px';
			musicSlide.style.top = headerHeight + musicPadding + 'px';
			musicSlide.style.zIndex = 10;
		
		var musicWindowBackground = document.getElementById("musicWindowBackground");
			musicWindowBackground.style.height = musicWindowHeight + 'px';
	
		var musicBlogWindow = document.getElementById("musicWindowInnerContent");
			musicBlogWindow.style.height = (musicWindowHeight - musicPadding - 10) + 'px';
	
		var musicLinkElement = document.getElementById("link_music");
			musicLinkElement.style.top = (topContainerPadding - musicPadding + (linkHeight + elementPadding) * 1) + 'px';
			
		var settings = { showArrows: true };
		var secondaryPane = $('#musicWindowInnerContent');
			secondaryPane.jScrollPane(settings);
		var secondaryAPI = secondaryPane.data('jsp');
			secondaryAPI.reinitialise();
	}

	function maximizeInfoSection() {
		var infoPadding = 20;
		
		var infoWindowHeight = pageHeight - headerHeight - footerHeight - (infoPadding * 2);

		var infoSlide = document.getElementById("infoSlide");		
			infoSlide.style.height = infoWindowHeight + 'px';
			infoSlide.style.top = headerHeight + infoPadding + 'px';
			infoSlide.style.zIndex = 10;
		
		var infoWindowBackground = document.getElementById("infoWindowBackground");
			infoWindowBackground.style.height = infoWindowHeight + 'px';
	
		var infoBlogWindow = document.getElementById("infoWindowInnerContent");
			infoBlogWindow.style.height = (infoWindowHeight - infoPadding - 30) + 'px';

		var infoLinkElement = document.getElementById("link_info");
			infoLinkElement.style.top = (topContainerPadding - infoPadding + (linkHeight + elementPadding) * 2) + 'px';
	}
	
	function maximizeShareSection() {
		var shareSlide = document.getElementById("shareSlide");		
			shareSlide.style.height = 78 + 'px';
			shareSlide.style.top = ((pageHeight - headerHeight - footerHeight) - 128) / 2 + 'px';
	
		var shareWindowBackground = document.getElementById("shareWindowBackground");
			shareWindowBackground.style.height = 78 + 'px';
	
		var shareWindowInnerContent = document.getElementById("shareWindowInnerContent");
			shareWindowInnerContent.style.height = 46 + 'px';

		var shareLinkElement = document.getElementById("link_share");
			shareLinkElement.style.top = (78 - 40) / 2 + 'px';
	}

	function maximizeLiveSection() {
		var livePadding = 20;
		var liveWindowHeight = pageHeight - headerHeight - footerHeight - (livePadding * 4);
	
		var liveSlide = document.getElementById("liveSlide");		
			liveSlide.style.height = liveWindowHeight + 'px';
			liveSlide.style.top = headerHeight + 40 + 'px';
		
		var liveWindowBackground = document.getElementById("liveWindowBackground");
			liveWindowBackground.style.height = liveWindowHeight + 'px';
	
		var liveWindowInnerContent = document.getElementById("liveWindowInnerContent");
			liveWindowInnerContent.style.height = liveWindowHeight - 50 + 'px';

		var liveLinkElement = document.getElementById("link_live");
			liveLinkElement.style.top = (topContainerPadding - livePadding + (linkHeight + elementPadding) * 1) + 5 + 'px';
		
	}

	var isInit = true;

	// Each element is absolutely positioned proportionally down the page, according to an even distribution of the number of elements and the height of the active page
	function setUpInitialView() {
		
		topContainerPadding = ((pageHeight - headerHeight - footerHeight) - ((numElements * linkHeight) + ((numElements - 1) * elementPadding))) / 2;

		// BLOG
		if(blogMinimized || isInit) {
			var blogSlide = document.getElementById("blogSlide");
				blogSlide.style.height = linkHeight + 'px';
				blogSlide.style.top = headerHeight + topContainerPadding + 'px';
				blogSlide.style.zIndex = 1;
				blogSlide.style.width = blogPageOffset + 100 + 'px';
	
			var slidingBlogElement = document.getElementById("slidingBlogElement");
				slidingBlogElement.style.width = blogPageOffset + 100 + 'px';
	
			var blogWindowBackground = document.getElementById("blogWindowBackground");
				blogWindowBackground.style.height = linkHeight + 'px';
				blogWindowBackground.style.width = (pageWidth * .6) + 'px';
		
			var innerBlogWindow = document.getElementById("blogWindowInnerContent");
				innerBlogWindow.style.height = linkHeight + 'px';
		
			var blogLinkElement = document.getElementById("link_blog");
				blogLinkElement.style.top = '0px';
		}
	
		// MUSIC
		if(musicMinimized || isInit) {
			var musicSlide = document.getElementById("musicSlide");
				musicSlide.style.height = linkHeight + 'px';
				musicSlide.style.top = headerHeight + topContainerPadding + (linkHeight + elementPadding) * 1 + 'px';
				musicSlide.style.zIndex = 2;
					
			var musicWindowBackground = document.getElementById("musicWindowBackground");
				musicWindowBackground.style.height = linkHeight + 'px';
			
			var innerMusicWindow = document.getElementById("musicWindowInnerContent");
				innerMusicWindow.style.height = linkHeight + 'px';
					
			var musicLinkElement = document.getElementById("link_music");
				musicLinkElement.style.top = '0px';
		}
		/*** STORE
		var storeSlide = document.getElementById("storeSlide");
			storeSlide.style.height = linkHeight + 'px';
			storeSlide.style.top = headerHeight + topContainerPadding + (linkHeight + elementPadding) * 2 + 'px';
			storeSlide.style.zIndex = 3;
				
		var storeWindowBackground = document.getElementById("storeWindowBackground");
			storeWindowBackground.style.height = linkHeight + 'px';
		
		var innerStoreWindow = document.getElementById("storeWindowInnerContent");
			innerStoreWindow.style.height = linkHeight + 'px';
				
		var storeLinkElement = document.getElementById("link_store");
			storeLinkElement.style.top = '0px';
		***/

		// INFO
		if(infoMinimized || isInit) {
			var infoSlide = document.getElementById("infoSlide");
				infoSlide.style.height = linkHeight + 'px';
				infoSlide.style.top = headerHeight + topContainerPadding + (linkHeight + elementPadding) * 2 + 'px';
				infoSlide.style.zIndex = 4;
					
			var infoWindowBackground = document.getElementById("infoWindowBackground");
				infoWindowBackground.style.height = linkHeight + 'px';
			
			var innerInfoWindow = document.getElementById("infoWindowInnerContent");
				innerInfoWindow.style.height = linkHeight + 'px';
					
			var infoLinkElement = document.getElementById("link_info");
				infoLinkElement.style.top = '0px';
		}

		sharePadding = ((pageHeight - headerHeight - footerHeight) - ((numElementsRight * linkHeight) + ((numElementsRight - 1) * elementPadding))) / 2;

		// SHARE
		if(shareMenuMinimized || isInit) {
			var shareSlide = document.getElementById("shareSlide");
				shareSlide.style.height = linkHeight + 'px';
				shareSlide.style.top = sharePadding + 'px';
				shareSlide.style.zIndex = 5;
	
			var shareWindowBackground = document.getElementById("shareWindowBackground");
				shareWindowBackground.style.height = linkHeight + 'px';
			
			var innerShareWindow = document.getElementById("shareWindowInnerContent");
				innerShareWindow.style.height = linkHeight + 'px';
					
			var shareLinkElement = document.getElementById("link_share");
				shareLinkElement.style.top = '0px';
		}

		/* LIVE */
		if(liveMinimized || isInit) {
			var liveSlide = document.getElementById("liveSlide");
				liveSlide.style.height = linkHeight + 'px';
				liveSlide.style.top = headerHeight + sharePadding + linkHeight + elementPadding + 'px';
				liveSlide.style.zIndex = 6;
	
			var liveWindowBackground = document.getElementById("liveWindowBackground");
				liveWindowBackground.style.height = linkHeight + 'px';
			
			var innerLiveWindow = document.getElementById("liveWindowInnerContent");
				innerLiveWindow.style.height = linkHeight + 'px';
					
			var liveLinkElement = document.getElementById("link_live");
				liveLinkElement.style.top = '0px';
		}
		
	};


	setUpInitialView();
	
	setTimeout("fireEvent(link_blog,'click')", 1000);
	
});

$(window).load(function() {    
	var theWindow        = $(window),
		$bg              = $("#bg"),
		aspectRatio      = $bg.width() / $bg.height();

	function resizeBg() {
		if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
			$bg
				.removeClass()
				.addClass('bgheight');
		} else {
			$bg
				.removeClass()
				.addClass('bgwidth');
		}
	}

	theWindow.resize(function() {
			resizeBg();
	}).trigger("resize");	
});
