/**
 * Variabels voor de overlay
 */
var modalOpen = function(hash){
	hash.w.html("<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" style=\"height:440px;\" border=\"0\"><tr><td width=\"100%\" align=\"center\" style=\"height:100%;vertical-align:middle;\">Bezig met laden...<br /><img src=\"/site/" + xs_site + "/design/graphics/loader.gif\" alt=\"\" /></td></tr></table>").show();
};
var modalClose=function(hash) { hash.w.hide(); hash.w.empty(); hash.o.remove(); };
var modalLoad=function(hash) { initOverlay(); };

/**
 * functie word aangeroepen vanuit de overlay
 */
function initOverlay(){
	$("#iframePage").load( function() {
		$("#loadingTable").hide();
		$("#iframeContainer").removeAttr('style');
	});
}

function closeOverlay(){
	$('#overlayWindow').jqmHide();
}

function ambianceAnimate(){
	$("#ambiancecutter").animate({
		width: 670
	}, { queue:false, duration:750, complete: function(){
		//als submenu er is deze naar beneden vallen
		if($("#submenu ul").length)
		{
			$("#submenu").slideDown("slow");
		}

		//show content container
		$("#content").fadeIn(750);

		//activate ambiance interval
		activateAmbianceRotate();
	}});
}

function setHeightContent(){
	if($("#content").height() > 327)
	{
		var newHeight = $("#main_header").height() + $("#content").height() + 10;
		$("#main").css({minHeight:newHeight, height:newHeight});
	}
}

function activateAmbianceRotate(){
	//activate ambiance interval
	setTimeout ( "ambianceRotate()", 2000 );
}

function ambianceRotate(){
	//create new image
	imArrayNum++;
	if(imArrayNum >= imArray.length){
		//reset to begin
		imArrayNum = 0;
	}
	var img = new Image();
	$(img).load(function(){
		//add new image into contaier
		$('ul#ambiancerotate').append('<li><img src="' + $(this).attr('src') + '" width="963" height="235" alt="" /></li>');
		$("ul#ambiancerotate").animate({left : -963}, 3000, function(){

			//reset container
			$('ul#ambiancerotate li:first').remove();
			$("ul#ambiancerotate").css('left',0);

			//rotate image again
			activateAmbianceRotate();
		});
	})
	.attr("src", imArray[imArrayNum]);
}

$(document).ready(function() {
	//png fix
	$("img[src$='.png']").ifixpng();

	//set height of content block
	if( typeof(imArrayNum) != 'undefined' )
	{
		if( $("#content").length )
		{
			//set content height
			setHeightContent();

			//animeer ambiance als geen submenu selected is
			if($("#submenu ul li.buttonsub_selected").length)
			{
				//er is een submenu selected - animatie niet meer laten zien
				$("#ambiancecutter").width(670);
				$("#submenu").show();
				$("#content").show();
				activateAmbianceRotate();
			}
			else
			{
				ambianceAnimate();
			}
		}
		else
		{
			//content wide
			if($("#submenu ul").length)
			{
				if($("#submenu ul li.buttonsub_selected").length)
				{
					$("#submenu").show();
				}
				else
				{
					$("#submenu").slideDown("slow");
				}
			}

			activateAmbianceRotate();
		}
	}

	/**
	 * Image gallery
	 */
	if( $('#photos').length )
	{
		$('#photos').galleryView({
			panel_width: 800,
			panel_height: 376,
			frame_width: 100,
			frame_height: 100,
			nav_theme: 'light',
			easing: 'easeInOutBack',
			transition_interval: 3000,
			transition_speed: 1000
		});
	}

	/**
	 * trigger overlay window op class .triggerModal
	 */
	$('.triggerOverlay').click(function(){
		var iframeurl = $(this).attr("href");
		$('#overlayWindow').jqm({toTop:true, ajax:'/site/' + xs_site + '/iframe.php?xs_site=' + xs_site + '&iframeurl=' + encodeURI(iframeurl), trigger: false, onShow: modalOpen, modal: false, onHide: modalClose, onLoad: modalLoad });
		$('#overlayWindow').jqmShow();
		return false;
	});

});
