$(document).ready(function() {
	var root =  $(".scrollable").scrollable({
						circular: true, 
						speed: 1000,
						onSeek:function(){			
							if(this.getIndex()==(this.getSize()-16)){
								this.seekTo(0);
							}						
						}
					}).autoscroll({autoplay: false, interval:2500 });	
	window.api = root.data("scrollable");	
	checkAutoScroll();
});

function checkAutoScroll(){
	if(api.getSize()>17){
		api.play();
	}	
}

function setCenterImage(imgPath){
	$("#bodyImage").html("<img border='0' alt='' src='" + imgPath + "' />").hide().fadeIn();
	
}

function processZoomImages(path){	
	$("a[class^=zoomImage]").each(
		function(){
			var obj=$(this);
			var thumbsizeArr,thumbwidth,thumbheight,urlParts,server,relpath,query			
			if(obj.attr("rel").indexOf("x")>1){
				thumbsize = obj.attr("rel");
				thumbsizeArr = thumbsize.split("x");
			}else{
				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
			});
		}
	)

	$("a[class^=tradiImage]").each(
		function(){
			var obj=$(this);
			var thumbsizeArr,thumbwidth,thumbheight,urlParts,server,relpath,query			
			if(obj.attr("rel").indexOf("x")>1){
				thumbsize = obj.attr("rel");
				thumbsizeArr = thumbsize.split("x");
			}else{
				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"
			});			
		}
	)
}


/*****************************
/* algemene functies
/****************************/

function get_windowHeight()
{
   var myWidth = 0, myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' )
   { //Non-IE
      myHeight = window.innerHeight;
   }
   else
   {
      if ( document.documentElement && ( document.documentElement.clientWidth ||    document.documentElement.clientHeight ) )
   { //IE 6+ in 'standards compliant mode'
      myHeight = document.documentElement.clientHeight;
   }
   else
   {
      if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
      { //IE 4 compatible
         myHeight = document.body.clientHeight;
      }
   }
}
   return myHeight;
}

function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
