/*
 * file:	functions.js
 * autor:	sven.georgi@unister-gmbh.de
 * 
 */

// behavior to save the cookie on window closing
var bodyRules={
	'window':function(element){
		element.onclose=function(){
			cookie.save();
			if ($('engine')) $('engine').value='';
			if ($('category')) $('category').value='';
		},
		element.onresize=function(){
			windowResize();
		}
		
	}
}
Attitude.append(bodyRules);

// behavior to change the background an save value in cookie  user/changebgr.tpl
	var bgrImgRules={
		'#backgrounds a': function(element){
			element.onclick=function(){
				if (element.id !='prevbackground' && element.id!='nextbackground' && element.id!='uploadimg'){
						// Src des Child-Img's auslesen
						var imgSrc=$(element).childNodes[0].src.toString();
						imgSrc = imgSrc.slice(imgSrc.indexOf('?')+1, imgSrc.indexOf(','));
						changeBgrImg(imgSrc);
						cookie.save();
						return false;
				}
			}
		},
		'a#prevbackground': function(element){
			element.onclick=function(){
				var url=$('prevbackground').href + '&ajax=true';
				var parameters='';
				new Loader('bgrloader','',url,parameters);
				return false;
			}
		},
		'a#nextbackground': function(element){
			element.onclick=function(){
				var url=$('nextbackground').href + '&ajax=true';
				var parameters='';
				new Loader('bgrloader','',url,parameters);
				return false;
			}
		}
	}
//Attitude.append(bgrImgRules);

// behaviors of the search form in index/searchform.tpl
var formRules={
/*
	'form#search' : function(form) {
		form.onsubmit = function(){
			var url = this.action + "?ajax=true";
		//	this.action = this.action + "?" + Form.serialize(this);
			$('pagelink').value="";
			var parameters = Form.serialize(this);
			new Loader('loader', '', url, parameters);
			return false;
		}
	}, */
	'form#search2' : function(form) {
		form.onsubmit = function(){
			loadme();
			$('pagelink').value="";
			$('stext').value = $('stext2').value;
			if ($F('stext')!=""){
				var oldhref 		= this.href;
				
				var params  	  	= Form.serialize('search');
				var act 		  	= home_url + "search/?" + params;
				location.href	= act;
				return false;
			}
		}
	}
}

var pagingRules={
	'#pagination a':function(element){
		element.onclick = function(){
			loadme();
			var value			= this.href.substr(this.href.indexOf('pagelink')+9);
			var oldhref 		= this.href;
			$('pagelink').value	= value;
			var params  	  	= Form.serialize('search');
			var act 		  	= home_url + "search/?" + params;
			location.href	= act;
			return false;
		}
	}
}
Attitude.append(pagingRules);
// behaviors of elements in index/menu_search.tpl
var engineRules={
	'#engines a': function(element) {
	
		element.onclick = function(){
			if ($(element).id == 'home') return true;
			for(i=0;i<engines.length;i++){
				var eek = engines[i];
				if ($(engines[i])) $(engines[i]).className="";
			}
			$(element).className="active";
			$('engine').value=this.id;
			
			if ($F('stext')!=""){
				//$('search').action = $('search').action + "&" + Form.serialize('search');
				loadme();
				var params  = Form.serialize('search');
				
				//$('form#search').submit();
				var act = home_url + "search/?" + params;
				//$('search').submit();
				//alert($('search').action);
				document.location = act;
				//$('search').onsubmit();
				//document.search.submit();
			}
			return false;
		}
	},
	'#categories a':function(element){
		element.onclick = function(){
				
			 for (t=0;t<=3;t++){
					 $("category"+t).style.fontWeight="normal";
			 }
		
			$(element).style.fontWeight="bold";
			$('category').value=this.id.substr(this.id.length -1);


			if ($F('stext')!=""){
				loadme();
				var params  = Form.serialize('search');
				var act = home_url + "search/?" + params;
				document.location = act;
			}

			return false;
		}
	},
	'a#advanced':function(element){
		element.onclick = function(){
			if ($('advancedopts').style.display=='block'){
				$('advancedopts').style.display="none"
			}else {
				$('advancedopts').style.display="block"
			}
			return false;
		}
	}
}
//Attitude.append(engineRules);	

var engineRules2={
	'a#advanced2 a':function(element){
		element.onclick = function(){
			if ($('advancedopts2').style.display=='block'){
				$('advancedopts2').style.display="none"
			}else {
				$('advancedopts2').style.display="block"
			}
			return false;
		}
	}
}

// changes the background of the whole body
function changeBgrImg(imgSrc){
	imgSrc = imgSrc.replace(/images/, "images/backgrounds/" + getWindowWidth());
	if (document.body){
		document.body.style.backgroundImage='url('+ home_url + imgSrc + ')';
	}
}

function getWindowWidth(){
	if (document.body){
		width = getPageSize();

		if (width[0]<=800)
			widthNew = 800;
		else if (width[0] <=1024)
			widthNew = 1024;
		else if (width[0] <=1280)
			widthNew = 1280;
		else if (width[0] <=1440)
			widthNew = 1440;
		else if (width[0] <=1680)
			widthNew = 1680;
		else if (width[0] <=1920)
			widthNew = 1920;
		else 
			widthNew = 2560;

		return widthNew;
	}
}
// cookie functions
var cookie={
	save: function(){
		var now = new Date();
		var expiresy = now.setTime(now.getTime() + 2592000000);
		var expires  = now.toGMTString();
		var cookievalue=document.body.style.backgroundImage+";path=/;expires=" + expires;
		document.cookie="bgrImg"+"="+cookievalue;
	},
	load: function(){
		var cookievalue=this.get("bgrImg");
		
		if (cookievalue!="" && cookievalue!="no" &&  cookievalue!=home_url + "images/no")
			{document.body.style.backgroundImage=cookievalue;}
		else
			{document.body.style.backgroundImage="url(" + home_url + "images/backgrounds/" + getWindowWidth() + "/00678_vancouverdusk_1600x1200.jpg)";}
	},
	get: function(Name){
		var search = Name + "="
		var returnvalue = "";
		if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search)
			if (offset != -1) {
				offset += search.length
				end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
				returnvalue=unescape(document.cookie.substring(offset, end))
			}
		}
		return returnvalue;
	}
}

function test(){
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		$('overlay').width  = arrayPageSize[0];
		$('overlay').height = arrayPageSize[1];

		new Effect.SlideDown('overlay', { duration: 0.2, from: 0.0, to: 0.8 });
}

function loadme(){
	$('loadme').style.display = 'block';
}
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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}