$(document).ready(function(){	
	$('#overlay').click(hideContainers);
	$('.folderTree').each(function(index){
		$(this).fileTree({  
			root: $(this).attr("rel"),  
			script: '/admin/js/jqueryFileTree/connectors/jqueryFileTree.asp',  
			expandSpeed: 300,  
			collapseSpeed: 300,  
			multiFolder: true 
		}, function(file) {  
			window.open(file,'Download');  
		});
	});
	$('.slideshow').cycle({
		
		fx: 'fade', 

		random:1
	});	
	$("#fancybox-title").hide();
});

function loadGallery(galleryId,menuId,websiteId){

	if(galleryId){		
		$.ajax({
			url: '/templates/oranje/includes/fotoboek.asp',
			data:{galleryId:galleryId,menuId:menuId,websiteId:websiteId},
			//ajaxStart:showBusyOverlay(),
			success: function(data) {
				//hideBusyOverlay();
				$("#main").html(data);
			},
			dataType: "html"
		});
	}
};
function processZoomImages(path){

	$("a.zoomImage").each(
		function(){
			var obj=$(this);
			var thumbsize = obj.attr("rel");
			var thumbsizeArr = obj.attr("rel").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
			});
		}
	)
}
function showPictures(id, urls, body) {
	var swf = new SWFObject("templates/matexi/swf/pictures.swf", id, body ? "395" : "378", body ? "255" : "203", "9", "#FFFFFF", true);
	for(var i = 0; i < urls.length; i++) {
		swf.addVariable("pictureUrl"+i, urls[i]);
	}
	if(!body) {
		swf.addVariable("showShadow", true);
		swf.addVariable("showCorner", true);
	}
	swf.write(id);
}

function showBusyOverlay() {
	updateBusyOverlayPosition();
	$('#busyoverlay').show();	
}

function hideBusyOverlay(){
	$('#busyoverlay').hide();
}

function hideSidebar() {
	$("#sidebarCell").hide();
}

function showSidebar() {
	$("#sidebarCell").show();
}

function updateSelectedCities(cities) {
	$("gemeenteId").value = cities.join("; ");
}

function closeMap() {
	hideKaart();
}

function closeVerkaveling() {
	hideVerkaveling();
}

function search(zoektype,postcode) {
	hideKaart();
	zoek(zoektype,postcode);
}

function stretchBodyCell(){	
	var dochoogte = $(document).height()
	var headerhoogte = 179;
	var introhoogte = $("#introCell").height();
	var creditshoogte = $("#creditsCell").height();
	$("#bodyCell").height(dochoogte - headerhoogte - introhoogte - creditshoogte);	
}

function updateSelectedProvincieProjecten(prov){
	SWFAddress.setValue("/projecten/?provincie=" + prov);
}
function updateSelectedProvincieProjectenRef(prov){
	SWFAddress.setValue("/projecten/?provincie=" + prov + "&isReferentie=1");
}

function updateSelectedProvincieSfeerwoningen(prov){
	SWFAddress.setValue("/sfeerwoningen/?provincie=" + prov);
}

function updateOverlaySize() {
	var arrayPageSize = getPageSize();
	$('#overlay').width(arrayPageSize[0]);
	$('#overlay').height(arrayPageSize[1]);
	$('#overlay').show();
}

function updateBusyOverlayPosition() {
	var arrayPageSize = getPageSize();
	$('#busyoverlay').css("left", Math.round((arrayPageSize[0]-156)/2));
	$('#busyoverlay').css("top", Math.round((arrayPageSize[1]-8)/2));
}

function hideContainers(){
	try{
		hideKaart();
	}catch(e){
	}
	try{
		hideVerkaveling();
	}catch(e){
	}	
	try{
		hideKijkboek();
		SWFAddress.setValue("/");
	}catch(e){}
}
function hideKaart() {
	$('#overlay').hide();
	$('#kaartContainer').html='';
	$('#kaartContainer').remove();
	$('select', 'object', 'embed').each(function(){$(this).show()});
}

function hideKijkboek() {
	$('#overlay').hide();	
	$('#kijkboekContainer').html='';
	$('#kijkboekLinkContainer').html='';
	$('#kijkboekContainer').remove();
	$('#kijkboekLinkContainer').remove();
	$('select', 'object', 'embed').each(function(){$(this).show()});
}

function hideVerkaveling() {
	$('#overlay').hide();
	$('#verkavelingContainer').html='';
	$('#verkavelingContainer').remove();	
	$('select', 'object', 'embed').each(function(){$(this).show()});
}

function showOverlay(){
	$('select', 'object', 'embed').each(function(){$(this).hide()});
	updateOverlaySize();
	$('#overlay').show();
}


function loadCalendar(params){
	$.ajax({
		url: '/templates/oranje/includes/includeCalendarEvents.asp',
		data:params,
		ajaxStart:showBusyOverlay(),
		success: function(data) {
			hideBusyOverlay();
			$("#main").html(data);
		},
		dataType: "html"
	});
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

/*****************************
/* 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];
}
