/*****************************
/* algemene functies
/****************************/
	
function processZoomImages(path){

	$("a[class^=zoomImage]").each(
		function(i){
			var obj=$(this);
			var thumbsizeArr,thumbwidth,thumbheight,urlParts,server,relpath,query			

				thumbsize = obj.attr("class").split("_")[1];
				thumbsizeArr = thumbsize.split("x");

			var thumbwidth = thumbsizeArr[0];
			var thumbheight = thumbsizeArr[1];
			var urlParts = /^(https?:\/\/.+?)?(\/.+?)(\?.*?)?$/.exec(obj.attr("href")); 
			var server = urlParts[1]; // maybe be '' depending on the browser 
			var relpath = urlParts[2]; 
			var query = urlParts[3]; 
			$.ajax({
				  url: path,
				  data : {
				  	  file : relpath,
					  width : thumbwidth,
					  height : thumbheight
				  },
				  success: function(data) {
					obj.html("<img src='"  + data.thumb + "' alt='' />");
				  },
				  dataType:"json"
			});
			obj.fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	200, 
				'speedOut'		:	200, 
				'overlayShow'	:	false
			});
		}
	)
}
jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
jQuery.fn.morphTo = function (left,top,width,speed) 
	{        
		$(this).animate(
			{ 
				width : width,
				left: left,
				top: top
			}, 
			speed
		);         
		return this;     
	} 
	
function moveline(s1,s2,delay,speed,init){
	try{
	var pos = s2.offset();
	var l = pos.left;
	var t = pos.top+s2.height();
	var w = s2.width();
	if(init){
		s1.clearQueue().hide().morphTo(l,t,w,speed).show();		
	}else{
		s1.clearQueue().show().wait(delay).morphTo(l,t,w,speed);		
	}
	}catch(e){}
}
	
function stretchBodyCell(){
	var dochoogte = $(document).height();
	var headerhoogte = $("#headerCell").outerHeight();
	var creditshoogte = $("#creditsCell").outerHeight();
	$("#bodycontent").css("min-height",(dochoogte - creditshoogte - headerhoogte)+"px");	
	if($(window).data("isLoaded"))
		moveline($("#menuline"),$("#menu li.selected a"),500,0,false);
}

/*****************************
/* algemene functies
/****************************/
	
function galleryLoading(startstop){
	if(startstop){
		$("#gallery").html("");
		$("#prevBtn").remove();
		$("#nextBtn").remove();
		$("#gallery").css("background","url(/templates/"+websiteTemplate+"/img/ajax-loader.gif) no-repeat scroll center center");
	}else{
		$("#gallery").css("background","none");
	}
}	

function createPopupWindow(html,name,width,height) {
	var n=open
	( 
	html, name, 
	'width='+width+',height='+height
	+',left='+(screen.width -width )/2
	+',top=' +(screen.height-height)/2
	+',status=0,menubar=0,toolbar=0,scrollbars=0,resizable=1'
	);
	n.focus();
}

function getPageSize() {
	return [$(document).width(),$(document).height()];
}

function hideTitle(){
	$('#pageTitle').hide();
}

function parseEmail(){
	$(".email").map(function(){
		$(this).html($(this).html().replace("[a]","@"));
		$(this).html($(this).html().replace("[p]","."));
		$(this).html("<a href=\"mail"+"to:" + $(this).html() + "\">" + $(this).html() + "</a>");	
		$(this).removeClass("email");
	});
}
//https://picasaweb.google.com/lh/sredir?uname=108616619175293659633&target=ALBUM&id=5654818862242300833&authkey=Gv1sRgCJO7prbols-xBQ&invite=CLLEivII&feat=email
/*****************************
/* SWFADDRESS
/****************************/

function handleChange(event) {
	if(SWFAddress.getPathNames()[0]!="slideshow"){
		$("#browsable").hide();
		$(".browse").hide();
	}
	switch(SWFAddress.getPathNames()[0]){
		case "agents":
			$.ajax({
				url: "/templates/"+websiteTemplate+"/includes/includeAgenten.asp",
				success: function(data) {
					$("#bodycontent").html(data);
					$( "#accordion" ).accordion({
						autoHeight: true,
						navigation: true
					}).position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});;
					
				},
				dataType:"html"
			});					
			break;
		case "news":
			$("#text,#bodycontent .menuCell").hide();
			$("#listItems").show().position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
			break;
		case "shops":
			var taal=language;
			var flashvars = {taal:taal};
				var params = {
				wmode:"opaque",
				allowfullscreen:"false",
				menu: "false",
				bgcolor: "#000000"
			};
			var attributes = {};
			attributes.id="onionikFlash";
			$("#bodycontent").html("<div id='flashwrapper'><div id='flash'></div></div>");
			swfobject.embedSWF("oni-onik.swf", "flash", "988px", "548px", "10", "js/expressInstall.swf", flashvars, params, attributes);
			$("#flashwrapper").position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
			break;
		case "slideshow":
			$("#text").hide();
			$("#gallery").show().position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
			galleryLoading(true);
			var	folder=SWFAddress.getPathNames()[1];
			$.ajax({
				  url: "/templates/"+websiteTemplate+"/includes/getImagesInFolderJson.asp",
				  data : {
				  	  folder:folder
				  },
				  success: function(data) {
				  	var j=0;
					$("#gallery").html("<ul id='slider'></ul>");
					for(i=0;i<data.images.length;i++){
						$("#slider").append("<li><a rel='slideshow' class='zoomImage_644x430' href='/write/images/slideshows/"+folder+"/"+data.images[i]+"' rel='foto'>"+data.images[i]+"</a></li>");
					}
					processZoomImages(zoomImageCreator);
					$("#gallery").easySlider({
						prevId: 'prevBtn',
						prevText: 'Vorige',
						nextId: 'nextBtn',	
						nextText: 'Volgende',
						controlsShow: true,
						controlsBefore: '',
						controlsAfter: '',	
						controlsFade: true,
						firstId: 'firstBtn',
						firstText: 'First',
						firstShow: false,
						lastId: 'lastBtn',	
						lastText: 'Last',
						lastShow: false,				
						vertical: false,
						speed: 800,
						auto: true,
						pause: 2000,
						continuous: false					
					}).position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
					$("#prevBtn").position({of:$("#gallery"),my:"left center",at:"left center",offset:"30 0",collision:"none"});			
					$("#nextBtn").position({of:$("#gallery"),my:"right center",at:"right center",offset:"-30 0",collision:"none"});			
					galleryLoading(false);
				},
				dataType:"json"
			});		

			break;
		case "slideshowgrid":
			$("#text").hide();
			$("#gallery").show().position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
			galleryLoading(true);
			var	folder=SWFAddress.getPathNames()[1];
			$.ajax({
				  url: "/templates/"+websiteTemplate+"/includes/getImagesInFolderJson.asp",
				  data : {
				  	  folder:folder
				  },
				  success: function(data) {
				  	var j=0;
					$("#gallery").html("<ul id='slider'></ul>").position({of:$("#bodycontent"),my:"center center",at:"center center",collision:"none"});
					var slideshowId = "images" + j;
					$("#slider").append("<li id='"+slideshowId+"'></li>");
					for(i=0;i<data.images.length;i++){
						$("#"+slideshowId).append("<a rel='slideshow' class='zoomImage_136x136' href='/write/images/slideshows/"+folder+"/"+data.images[i]+"' rel='foto'>"+data.images[i]+"</a>");
						if((i+1)!=data.images.length)
							if(((i+1)%12)==0){
								j++;
								slideshowId = "images" + j;
								$("#slider").append("<li id='"+slideshowId+"'></li>");
							}
					}
					
					processZoomImages(zoomImageCreator);
					$("#gallery").easySlider({
						prevId: 'prevBtn',
						prevText: 'Vorige',
						nextId: 'nextBtn',	
						nextText: 'Volgende',
						controlsShow: true,
						controlsBefore: '',
						controlsAfter: '',	
						controlsFade: true,
						firstId: 'firstBtn',
						firstText: 'First',
						firstShow: false,
						lastId: 'lastBtn',	
						lastText: 'Last',
						lastShow: false,				
						vertical: false,
						speed: 800,
						auto: false,
						pause: 2000,
						continuous: false					
					});
					$("#prevBtn").position({of:$("#gallery"),my:"left center",at:"left center",offset:"-45 0",collision:"none"});			
					$("#nextBtn").position({of:$("#gallery"),my:"right center",at:"right center",offset:"45 0",collision:"none"});		
					galleryLoading(false);		
				},
				dataType:"json"
			});				
		
			break;						
		default:

		
	}

}
jQuery.fn.wait = function (MiliSeconds) {         $(this).animate({ opacity: '+=0' }, MiliSeconds);         return this;     } 
$(function(){
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
	processZoomImages(zoomImageCreator);
	$(window).resize(function(){
		stretchBodyCell();
	});	
	stretchBodyCell();
	parseEmail();

});
$(window).load(function(){
	moveline($("#menuline"),$("#menu li.selected a"),500,0,true);
	$(this).data("isLoaded",true);
})


