
var pageID;

/*
 * Default domReady event handler.
 * Called on all pages.
 */
window.addEvent('domready', function() {
    pageID = document.body.getAttribute('id');
    eval(pageID+"_init()"); 
    enableExternalLinks(); 
    initCtrl();     
});

/* 
 * Place holder functions for general domready 
 */
var front_init = function() {}
var langfront_init = function() {}
var area_init = function() {}
var advice_init = function() {}
var contact_init = function() {}

/*
 * Send to a friend
 */
var sendmail_init = function() {
	
}


/*
 * Property list initialization function
 */
var properties_init = function() {
	//make entire item clickable
	$$('.caseitem').each(function(item){
		var urlStr = item.getChildren()[0].getChildren()[0].get('href');
		item.addEvent('mouseenter', function(){ item.addClass('caseitem_over');});
		item.addEvent('mouseleave', function(){	item.removeClass('caseitem_over'); });
		item.addEvent('click', function(){	location.href=urlStr;});	
	});	
}
/*
 * Initilization for case detail page
 */
var case_init = function() {
	var counter = 1;
	var galleryTimer;
	var thumbWidth=0;
	var galleryPaths = Array();
	var galleryIndex = 0;
	var slideshowImages = Array();
	var slideshowImagesLoaded=false;
	var maskActive=false;
	
	var overlay = new Overlay(document.body,{
		        id: 'overlay',
		        color: '#fdfcf6',
		        duration: 300,
		        opacity: 0.6,
		        zIndex: 1000,
		        onClick: function() {
		        	stopSlideshow();
		        	$('zoomContainer').hide();
		        	$('main_img').src=jsRoot+'/imageprocess/process.php?wl=672&src='+jsRoot+'/uploads/'+galleryPaths[galleryIndex];
		            this.close();
		        }
		});
	
	$$('.galleryitem').each(function(item, index, array){
		var path = item.getChildren()[0].src.split("/")[item.getChildren()[0].src.split("/").length-1];
		galleryPaths[index]=path;
		item.store('itemIndex', index);
		item.store('preloadedImage', new Image().src=jsRoot+'/imageprocess/process.php?wl=672&src='+jsRoot+'/uploads/'+path);
		item.addEvent('mouseenter', function(){
			galleryIndex=this.retrieve('itemIndex');
			$('main_img').src=this.retrieve('preloadedImage');
		});
	});
	//magnify
	$('main_img').addEvent('click', function(){
		
		overlay.open();
		var coordx = $('galleryselected').getPosition().x;
		var coordy = $('galleryselected').getPosition().y;
		$('zoomImg').src=jsRoot+"/uploads/"+galleryPaths[galleryIndex];
		$('zoomContainer').setPosition({x: coordx, y: coordy})
		controlsUiUpdate();
		$('zoomContainer').show();
		//alert($('main_img').getPosition().x);
		//mask.hide();
	});
	//handler to close overlay
	$('zoomImg').addEvent('click',function(){
		$('zoomContainer').hide();
		overlay.close();	
	});
	//zoomed image controls
	
	//update and set disabled on click
	var controlsUiUpdate = function(){
		if(galleryIndex==0) {
			$('controlsPrevious').addClass('disabled');
		}
		else {
			if($('controlsPrevious').hasClass('disabled')) {
				$('controlsPrevious').removeClass('disabled');
			}
		}
		if(galleryIndex<galleryPaths.length-1) {
			$('controlsNext').removeClass('disabled');
		}
		else {
			$('controlsNext').addClass('disabled');
		}
		$('controlsIndex').set('text', galleryIndex+1);
	}
	
	//init index and length 
	$('controlsCount').set('text', galleryPaths.length);
	controlsUiUpdate();
	
	//go to next image
	$('controlsNext').addEvent('click', function(e){
		e.preventDefault();
		
		if(galleryIndex<(galleryPaths.length-1)) {
			$('zoomImg').src=jsRoot+"/uploads/"+galleryPaths[++galleryIndex];
		}
		controlsUiUpdate();
	});
	
	// go to previous image
	$('controlsPrevious').addEvent('click', function(e){
		e.preventDefault();
		if(galleryIndex>0) {
			$('zoomImg').src=jsRoot+"/uploads/"+galleryPaths[--galleryIndex];
		}
		controlsUiUpdate();
	});
	
	
	// slideShow
	var stopSlideshow = function() {
		$('controlsSlideshow').set('text','Afspil diasshow');
		$('controlsSlideshow').removeClass('slideShowActive');
		$clear(galleryTimer);	
	}
	var startSlideshow = function() {
		//Preloading images
		var paths = new Array();
		for(i=0;i<galleryPaths.length;i++) {
			paths[i]=jsRoot+'/uploads/'+galleryPaths[i];	
		}
		slideshowImages = Asset.images(paths, {
		    onComplete: function(){
		        slideshowImagesLoaded=true;
		    }
		});
		
		$('controlsSlideshow').set('text','Stop diasshow');
		var slideShow = function(){
			if(galleryIndex<(galleryPaths.length-1)) {
				if(slideshowImagesLoaded) {
					//if preload is done, start using the preloaded
					$('zoomImg').src=slideshowImages[++galleryIndex].src;
				}
				else {
					$('zoomImg').src=jsRoot+"/uploads/"+galleryPaths[++galleryIndex];
				}
				
				
			}
			else {
				galleryIndex=0;
				if(slideshowImagesLoaded) {
					//if preload is done, start using the preloaded
					$('zoomImg').src=slideshowImages[galleryIndex].src;
				}
				else {
					$('zoomImg').src=jsRoot+"/uploads/"+galleryPaths[galleryIndex];
				}
			}
			controlsUiUpdate();
		}
		galleryTimer = slideShow.periodical(3000);	
	}
	//add event
	$('controlsSlideshow').addEvent('click', function(e){
		e.preventDefault();
		this.toggleClass('slideShowActive');
		if(this.hasClass('slideShowActive')) {
			startSlideshow();
		}
		else {
			stopSlideshow();	
		}

		
	});
	
	/* Zebra stripes for fact box */
	$$('dd').each(function(item, index){
		if(index%2){
			item.setStyle('background-color', '#ccc');
		}
	});
	$$('dt').each(function(item, index){
		if(index%2){
			item.setStyle('background-color', '#ccc');
		}
	});
	/* end zebra */
	
	/* Gallery eventhandlers */
	//scroll right
	$('gallerynext').addEvent('mousedown', function(){
		var thumbWidth = ($$('.galleryitem').length*110)+10;
		var slide = function(){
			var l = parseInt($('gallerythumbs').getStyle('left'));
			if (l > (482+(thumbWidth * -1))) {
				$('gallerythumbs').setStyle('left', (parseInt($('gallerythumbs').getStyle('left')) - 1) + 'px');
			}
		};
		galleryTimer = slide.periodical(4);	
	});
	$('gallerynext').addEvent('mouseup', function(){
		$clear(galleryTimer);	
	});
	//scroll left
	$('galleryprev').addEvent('mousedown', function(){
		var slide = function(){
			if(parseInt($('gallerythumbs').getStyle('left'))<=0) {
				$('gallerythumbs').setStyle('left', (parseInt($('gallerythumbs').getStyle('left'))+1)+'px');
			}
		};
		galleryTimer = slide.periodical(4);	
	});
	$('galleryprev').addEvent('mouseup', function(){
		$clear(galleryTimer);	
	});
}

//utility
var enableExternalLinks = function() {
	$$('a').each(function(item){
		if(item.getProperty('rel') == 'external') {
			item.addEvent('click', function(){
				window.open(item.get('href'), '_blank');
	    		return false;
			});
		}
	});
}
//text increase, print etc
var textResize = function(o, s){
	o.setStyle('font-size', (s)+'px');
	//o.setStyle('line-height', (s+7)+'px');	
	//Cookie.write('cltdvFontSize', s, {'path':'/'});
};
var initCtrl = function(){
	//if(document.body.get('id')=='front') {return false;} //no txt to increase on front page. return.
	/*var savedFontSize = Cookie.read('cltdvFontSize');
	if(parseInt(savedFontSize)) {
		//Cookie.dispose('cltdvFontSize');
		textResize($('textcontent'), savedFontSize);
	}*/
	/*$('txtincrease').addEvent('click', function(){
		var currentSize = parseInt($('textcontent').getStyle('font-size'));
		if(currentSize < 18) {
			textResize($('textcontent'), ++currentSize);
		}
		return false;	
	});	
	$('txtdecrease').addEvent('click', function(){
		var currentSize = parseInt($('textcontent').getStyle('font-size'));
		if(currentSize > 10) {
			textResize($('textcontent'), --currentSize);
		}
		return false;	
	});*/
	$('btn_print').addEvent('click', function(){
		window.print()
		return false;	
	});
	
 	if($('totop')!=undefined){
		$('totop').addEvent('click', function(){
			scroll(0,0);
			return false;	
		});	
 	}	
};

/* third party */

var Overlay=new Class({Implements:[Options,Events],options:{id:"overlay",color:"#000",duration:500,opacity:0.5,zIndex:5000},initialize:function(a,b){this.setOptions(b);this.container=document.id(a);if(Browser.Engine.trident&&Browser.Engine.version<=6){this.ie6=true;}this.overlay=new Element("div",{id:this.options.id,opacity:0,styles:{position:(this.ie6)?"absolute":"fixed",background:this.options.color,left:0,top:0,"z-index":this.options.zIndex},events:{click:function(){this.fireEvent("click");}.bind(this)}}).inject(this.container);this.tween=new Fx.Tween(this.overlay,{duration:this.options.duration,link:"cancel",property:"opacity",onStart:function(){this.overlay.setStyles({width:"100%",height:this.container.getScrollSize().y});}.bind(this),onComplete:function(){this.fireEvent(this.overlay.get("opacity")==this.options.opacity?"show":"hide");}.bind(this)});window.addEvents({resize:function(){this.resize();}.bind(this),scroll:function(){this.scroll();}.bind(this)});},open:function(){this.fireEvent("open");this.tween.start(this.options.opacity);return this;},close:function(){this.fireEvent("close");this.tween.start(0);return this;},resize:function(){this.fireEvent("resize");this.overlay.setStyle("height",this.container.getScrollSize().y);return this;},scroll:function(){this.fireEvent("scroll");if(this.ie6){this.overlay.setStyle("left",window.getScroll().x);}return this;}});


