var galleryBGList = [];
var galleryBGIndex = 0;
var galleryBGImage = null;
var galleryBGTitle = null;

var pageBGSoundFile = null;
var pageBGSoundStatus = null;
var soundSWF = null;
var soundDiv = null;
var soundBtn = null;
var imgBackground = null;

function SetPanelsHeight(){
	var newHeight = Math.max( $('.Panel2').innerHeight(), $(window).height() ) - 80;
	$('.Panel').height( newHeight );
}

/*
function ResizeBGImage(){
	if(imgBackground!=undefined){ // skip
	}else{ 
		if( !imgBackground.aspectRatio ) {
			if( imgBackground.offsetWidth && imgBackground.offsetHeight ) {
				imgBackground.aspectRatio = imgBackground.offsetWidth / imgBackground.offsetHeight;
			} else {
				return;
			}
		}	
		var Width = window.innerWidth || document.body.clientWidth;
		var Height = window.innerHeight || document.body.clientHeight;
		alert( Width + ' ' + Height );
		if( ( Width/Height) > imgBackground.aspectRatio ) {
			imgBackground.style.width = '100%';
			imgBackground.style.height = '';
		} else {
			imgBackground.style.width = '';
			imgBackground.style.height = '100%';
		}	 
	}
}

function ResizeBGImageOld() {
	var Background = document.getElementById( 'BackgroundImage' );
	if( Background ) {
	if( !Background.aspectRatio ) {
	if( Background.offsetWidth && Background.offsetHeight ) {
	Background.aspectRatio = Background.offsetWidth / Background.offsetHeight;
	} else {
	return;
	}
	}
	var Width = window.innerWidth || document.body.clientWidth;
	var Height = window.innerHeight || document.body.clientHeight;
	if( ( Width/Height) > Background.aspectRatio ) {
	Background.style.width = '100%';
	Background.style.height = '';
	} else {
	Background.style.width = '';
	Background.style.height = '100%';
	}
	} else {
	//No background ID found
	}
	} 
*/

function ResizeBGImage2(){
	alert('oud');
}
function ResizeBGImage(){
	alert('oud');
}
		
function ResizeBGImageNew(){	
	try{
		if(galleryBGImage!=undefined) {
			var leftPanelWidth = 190;
			var Height = $(window).height();//window.innerHeight || document.body.clientHeight;
			var Width = $(window).width() - leftPanelWidth;//(window.innerWidth || document.body.clientWidth) - leftPanelWidth;
			var ImgRatio = galleryBGImage.height() / galleryBGImage.width();
			var windowRatio = Height / Width;
			
			// alert('img h:' + galleryBGImage.height() + '-w:'+ galleryBGImage.width() + ', win h:' + Height+ '-w:'+Width);
			if(windowRatio < ImgRatio) {				
				// heigher
				galleryBGImage.width('');
				galleryBGImage.height(Height);
				
				var footerHeight = Height - galleryBGImage.height();
				var panelWidth = Width - galleryBGImage.width();
				
				galleryFooter.hide();
				galleryRight.show();
				galleryRight.width(panelWidth);				
			} else {
				//alert(Height + ' '+ Width + ', '+galleryBGImage.height()+ ' ' + galleryBGImage.width());
				
				// wider 
				galleryBGImage.width(Width);
				galleryBGImage.height('');
				
				var footerHeight = Height - galleryBGImage.height();
				var panelWidth = (Width  - leftPanelWidth) - galleryBGImage.width();
				galleryRight.hide();
				galleryFooter.show();				
				galleryFooter.height(footerHeight);
				galleryOpenText.height(footerHeight);
				// imgBackground.style.width = '100%';
			}
			
			
		} else { // no bg id			
		}
	} catch(er) {
	}	
}

$(document).ready(function(){
	// gallery
	galleryBGImage = $('#BackgroundImage');	
	galleryBGTitle = $('#galleryBGTitle');
	galleryFooter = $('#FooterPanel');
	galleryRight = $('#RightPanel');
	galleryOpenText = $('#OpenText');
	
	galleryBGImage.bind('load', function() {
		ResizeBGImageNew();	
	}).each(function() {
	  if(this.complete) $(this).load();
	});

	
	// sound
	soundDiv = $('#jplayerSound');	
	soundBtn = $('#sound_btn');
	
	//InitBGSound();	
		
	$( window ).bind( 'resize load', function() {
		ResizeBGImageNew();
		SetPanelsHeight();
	});
});



function GetHTMLElement(elementId){
	var element = $(elementId);	
	if(element == undefined || element.length == 0){
		throw "Could not find HTML-element with id '"+elementId+"'!";		
	}else{ // ok
	}
	return element;	
}


function InitBGSound(){
	if(pageBGSoundFile == undefined || pageBGSoundStatus == undefined ||
			pageBGSoundFile == null	 || pageBGSoundStatus == null || soundDiv == undefined)
		return; // invalid settings
	
	soundDiv.jPlayer({
		ready: function() {
	      $(this).jPlayer("setMedia", {
	    	  mp3: pageBGSoundFile
			});		
	      HandleBGSound();
		},
		play: function() {					
			$(this).jPlayer("pauseOthers");
		},
		swfPath: websitePath+'js/jquery/jQuery.jPlayer.2.0.0.source',
		supplied: 'mp3'				
	});	
}

function ToggleBGSound(){
	if(pageBGSoundFile == undefined || pageBGSoundStatus == undefined ||
			pageBGSoundFile == null	 || pageBGSoundStatus == null || soundDiv == undefined)
		return; // invalid settings
	
	pageBGSoundStatus = (pageBGSoundStatus=='on')?'off':'on';
	HandleBGSound();
	UpdateBGSoundSetting(pageBGSoundStatus);
}

function HandleBGSound(){
	if(pageBGSoundFile == undefined || pageBGSoundStatus == undefined ||
		pageBGSoundFile == null	 || pageBGSoundStatus == null || soundDiv == undefined)
		return; // invalid settings
	
	switch (pageBGSoundStatus){
		case 'on':						
			soundDiv.jPlayer("play");			
			if(soundBtn!=undefined){
				soundBtn.removeClass('Disabled');
			}	
			break;
			
		case 'off':			
		default:
			soundDiv.jPlayer("stop");
			if(soundBtn!=undefined){
				soundBtn.addClass('Disabled');
			}		
			break;
	}	 
}

	
function ChangeGalleryBG(action){
	if(galleryBGList == undefined || galleryBGList.length == 0 || galleryBGImage == undefined || galleryBGTitle == undefined)
		return; // invalid settings or no gallery images
	switch (action){		
		case 'f':
			if(galleryBGList.length>0){
				galleryBGIndex = 0;				
			}else{ //skip
				return;
			}
			break;
		case 'n':
			if((galleryBGIndex + 1) < galleryBGList.length){
				galleryBGIndex += 1;				
			}else{ // show first
				galleryBGIndex = 0;				
			}
			break;
		case 'p':
			if((galleryBGIndex -1) >= 0){
				galleryBGIndex -= 1;				
			}else{ // show last
				galleryBGIndex = galleryBGList.length -1;	
			}
			break;			
		case 'l':
			if(galleryBGList.length>0){
				galleryBGIndex = galleryBGList.length -1;				
			}else{ //skip
				return;
			}
			break;
		default:
			alert('action not supported');
			return;
	}
	
	galleryBGImage.attr('src', galleryBGList[galleryBGIndex]['file']);
	galleryBGTitle.html(galleryBGList[galleryBGIndex]['title']);
}

	

function SlidePanel(special){
	//Current status
	var Position = $('.Panel2').position();
	
	if(special == undefined){
		if( Position.left > 0 ){
			var newTop = '0';
			var newPosition = '-91';
		} else {
			var newTop = '-21';
			var newPosition = '200';
		}
	}else{ // special page
		if( Position.left > 0 ){
			var newTop = '0';
			var newPosition = '-514';
		} else {
			var newTop = '-21';
			var newPosition = '200';
		}
		
	}		
	$( '#Open' ).toggle();
	$( '#Close' ).toggle();
	//Animate
	$('.Panel2').animate({
		left: newPosition
	}, {
		duration: 500,
		queue: false,
		specialEasing: {
			width: 'easeInQuad'
		},
		complete: function() {
			
			//completed
		}
	});
	
	$( "#TopBar" ).animate({
		top: newTop
	}, {
		duration: 500,
		queue: false,
		specialEasing: {
			width: 'easeInQuad'
		},
		complete: function() {
			//completed
		}
	});
	
	return false;
}

function SlidePanels(){
	//Current status
	var Position = $('.Panel2').position();
	if( Position.left > 0 ){
		var newTop = '0';
		var newPosition1 = '-133';
		var newPosition2 = '-516';
	} else {
		var newTop = '-21';
		var newPosition1 = '200';
		var newPosition2 = '200';
	}
	$( '#Open' ).toggle();
	$( '#Close' ).toggle();
	//Animate
	$('.Panel2').animate({
		left: newPosition1
	}, {
		duration: 500,
		queue: false,
		specialEasing: {
			width: 'easeInQuad'
		},
		complete: function() {
			
			//completed
		}
	});
	$('.Panel3').animate({
		left: newPosition2
	}, {
		duration: 500,
		queue: false,
		specialEasing: {
			width: 'easeInQuad'
		},
		complete: function() {
			
			//completed
		}
	});
	$( "#TopBar" ).animate({
		top: newTop
	}, {
		duration: 500,
		queue: false,
		specialEasing: {
			width: 'easeInQuad'
		},
		complete: function() {
			//completed
		}
	});
	
	return false;
}



