Event.observe(window,'unload',function(){
	if(typeof GUnload=='function'){
		GUnload();
	}
});
document.observe('dom:loaded',function(){
	loadExtensions();
});
function loadExtensions(){
	if(typeof MOGI_sortableList=='function'){
		MOGI_sortableList();
	}
	if(typeof MOGI_dragableServiceBox=='function'){
		MOGI_dragableServiceBox();
	}
	if(typeof MOGI_googlemap=='function'){
		MOGI_googlemap();
	}
	if(typeof MOGI_workbox=='function'){
		MOGI_workbox();
	}
	if(typeof MOGI_wastebin=='function'){
		MOGI_wastebin();
	}
	if(typeof initSlideChannels=='function'){
		initSlideChannels();
	}
	if(typeof dojo=='object'){
		dojo.addOnLoad( function (){setRecipeDefaultValues();});
	}
	_uacct = false;
	if(window.location.hostname=='www.dinnersearch.at'){
		_uacct='UA-241256-1';
	}
	if(window.location.hostname=='suche.dinnersearch.at'){
		_uacct='UA-66992-4';
	}
	if(window.location.hostname=='www.dinnersearch.de'){
		_uacct='UA-66992-6';
	}
	if(window.location.hostname=='mein-restaurant.at'){
		_uacct='UA-66992-1';
	}
	if(window.location.hostname=='www.mein-restaurant.at'){
		_uacct='UA-241256-3';
	}
	if(window.location.hostname=='www.dinnersearch.com'){
		_uacct='UA-241256-2';
	}
	if(_uacct!=false){ //new google api 2009
		try {
			var pageTracker = _gat._getTracker(_uacct);
			pageTracker._trackPageview();
		} catch(err) {}
	}
	if($('adserver_linktippswebconomy') && $('adserver_linktippsgoogle')){
		if($('adserver_linktippswebconomy')){
			AdSlides.push($('adserver_linktippswebconomy'));
		}
		if($('adserver_linktippsgoogle')){
			AdSlides.push($('adserver_linktippsgoogle'));
		}
		Event.observe($('slidingads'),'mouseout',
			function(event){ 
				AdSliding=1;
			}
		);
		Event.observe($('slidingads'),'mouseover',
			function(event){ 
				AdSliding=0;
			}
		);
		if($('adserver_linktippswebconomy_tab')){
			Event.observe($('adserver_linktippswebconomy_tab'),'mouseout',
				function(event){ 
					AdSliding=1;
				}
			);
		}
		if($('adserver_linktippsgoogle_tab')){
			Event.observe($('adserver_linktippsgoogle_tab'),'mouseout',
				function(event){ 
					AdSliding=1;
				}
			);
		}
		if($('adserver_linktippswebconomy_tab')){
			Event.observe($('adserver_linktippswebconomy_tab'),'mouseover',
				function(event){ 
					i=1;
					AdSliding=1;
					SlideAdvert(0);
					AdSliding=0;
				}
			);
		}
		if($('adserver_linktippsgoogle_tab')){
			Event.observe($('adserver_linktippsgoogle_tab'),'mouseover',
				function(event){ 
					i=0;
					AdSliding=1;
					SlideAdvert(0);
					AdSliding=0;
				}
			);
		}
		if($('addusercomment')){
			Event.observe($('addusercomment'),'click',
				function(event){ 
					$('commentformular').show();
				}
			);
		}
		setInterval("SlideAdvert(1)",15000);
	}
	var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  
  setActiveStyleSheet(title);
}

var AdSlides = new Array();
var i=0;
var AdSliding=1;
function SlideAdvert(Duration) {
	if(AdSliding==1){
		if(i==AdSlides.length){i=0;}
		Effect.Fade(AdSlides[i], { duration:Duration, from:1.0, to:0.0,queue:'front'});
		var innerI=i+1;
		if(innerI==AdSlides.length){innerI=0;}
		Effect.Appear(AdSlides[innerI], { duration:Duration, from:0.0, to:1.0 ,queue:'end'});
		i=i+1;
	}
}
function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}
function setRecipeDefaultValues(){ //reinitialize recipe ingredients
	$$('.selectvalues').each(function(item){
			dijit.byId(str_replace('_default','',item.id)).setAttribute('value',item.value);
		}
	)
}


function setActiveStyleSheet(title) {
  var i, a, main;
  var newStylesheetIsSet=false;
  var preferedStylesheet=getPreferredStyleSheet();
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title){
				a.disabled = false;
				createCookie("style", title, 365);
				newStylesheetIsSet=true;
			}
    }
  }
  if(newStylesheetIsSet==false && preferedStylesheet!=null){
	  setActiveStyleSheet(preferedStylesheet);	  
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") &&  a.getAttribute('title').indexOf('style')!=-1 && !a.disabled){
    	return a.getAttribute("title");
    }
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function resetBackground(listidentifier){
	currentClass='bglight';
	$(listidentifier).childElements().each(function(element){ 
		element.removeClassName('bglight');
		element.removeClassName('bgdark');
		if(currentClass=='bglight'){
			currentClass='bgdark';
		}
		else{
			currentClass='bglight';
		}
		element.addClassName(currentClass);
		});
}
function setRating(number,ulidentifier){
	$(ulidentifier).removeClassName('stars_1');
	$(ulidentifier).removeClassName('stars_2');
	$(ulidentifier).removeClassName('stars_3');
	$(ulidentifier).removeClassName('stars_4');
	$(ulidentifier).removeClassName('stars_5');
	$(ulidentifier).removeClassName('stars_6');
	$(ulidentifier).addClassName('stars_'+number);
	$('rating-stars').value=number;
	return false;
}
function saveRating(){
	if($('rating-content').value==""){
		alert('Geben Sie bitte einen Kommentar ein, weshalb Sie diese Berwertung abegeben!')
	}
	else{
		var url=location.pathname;
		var urlarray=url.split('/');
		requestUrl='/json/setguestbook/id/'+urlarray[4]+'/type/'+urlarray[1];
		var updateNavigation = new Ajax.Request(
			window.location.protocol+'//'+window.location.hostname+requestUrl,
			{
				method: 'post',
				parameters: "rating="+$('rating-stars').value+"&content="+encodeURIComponent($('rating-content').value)+"&head="+encodeURIComponent($('rating-head').value),
				onSuccess:function(transport){
					var responseArray =  transport.responseText.evalJSON(true);
					//$('guestbooklist').insert('<b>laskf</b>',{position:before});
					
					var li = new Element('li');
					var div_head= new Element('div');
					div_head.addClassName('guestbookhead');
					div_head.update(responseArray.head);
					var div_headline = new Element('div');
					div_headline.addClassName('guestbookentry_headerline');
					div_headline.update(responseArray.created+' - '+responseArray.name);
					
					var div_stars = new Element('div');
					div_stars.addClassName('displaystars stars_'+responseArray.rating);
					//div_stars.update(responseArray.created+' - '+responseArray.name);
					div_headline.insert(div_stars);
					
					var div_content = new Element('div');
					div_content.update(responseArray.content.replace(/\n/g,"<br>"));
					
					li.insert(div_head);
					li.insert(div_headline);
					li.insert(div_content);
					$('guestbooklist').insert({'top':li});
					resetBackground('guestbooklist');
					$('rating-content').value='';
					$('rating-head').value='';
					setRating(0,'guestbookrating');
				}
			}
		);
	}
	return false;
}
