Event.observe(window, 'load', init, false);

function init() {
	banner_init();
	link_init();
}

function banner_init() {
	banner = $$('.banner');
	counter = 0;
	if (banner.length>1)
		new PeriodicalExecuter(banner_rotate, 8);
}

function banner_rotate() {
	new Effect.Fade(banner[counter], {queue: {position:'parallel', scope: 'menu', limit: 2}});
	if (banner[counter]==banner.last())
		counter = 0;
	else
		counter = counter+1;
	new Effect.Appear(banner[counter], {duration:2, queue: {position:'parallel', scope: 'menu', limit: 2}});
}

function link_init() {
	if (!document.getElementsByTagName){ return; }
	anchors = document.getElementsByTagName('a');

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){

		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute('rel'));
		//Menu
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('gromenu'))){
			//anchor.onmouseover = function () {menu_starttimer(this); return false;}
			//anchor.onmouseout = function () {menu_stoptimer(); return false;}
			anchor.onclick = function () {menu_blind(this); return false;}
		}
		//Tab
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('grotab'))){
			anchor.onclick = function () {tab_change(this); return false;}
		}
		//BookingTab
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('grobooking'))){
			anchor.onclick = function () {booking_sendid(this); tab_change(this); return false;}
		}
		//Offerscale
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('groscale'))){
			offer_init(anchor);
			anchor.onclick = function () {offer_scale(this); return false;}
		}
	}
}

function offer_init(thislink) {
	ancestor = thislink.up(0);
	sibling = thislink.previous(0);
	initheight = Element.getHeight(ancestor);
	innerheight = Element.getHeight(sibling);
	//if (initheight > innerheight)
	//	thislink.hide();
}

function offer_scale(thislink) {
	ancestor = thislink.up(0);
	sibling = thislink.previous(0);
	outerheight = Element.getHeight(ancestor);
	innerheight = Element.getHeight(sibling)+20;
	
	if (thislink.innerHTML == '[einblenden]') {
		percent = innerheight / outerheight * 100;
		thislink.update('[ausblenden]');
	} else {
		percent = (initheight - 4) / (innerheight) * 100;
		thislink.update('[einblenden]');
	}
	new Effect.Scale(ancestor, percent, { duration: 0.5, scaleX: false, scaleContent: false } );
	thislink.blur();
}

function tab_change(thistab) {
	tabcontentArray = $$('div.'+thistab.getAttribute('rel'));
	tabArray = [];
	
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == thistab.getAttribute('rel'))) {
			tabArray.push(anchor);
		}
	}
	
	for (var i=0; i<tabArray.length; i++){
		thisclassname = String(tabArray[i].classNames());
		thisclassname = thisclassname.sub('_active','');
		if (tabArray[i]==thistab) {
			tabcontentArray[i].show();
			tabArray[i].className = thisclassname+'_active';
			relAttribute = String(tabArray[i].getAttribute('rel'));
			if (relAttribute.toLowerCase().match('offer') && (i+1)!=tabArray.length && tabArray[i+1].up().style.display == 'none') {
				booking_rotate('right');
			} else if (relAttribute.toLowerCase().match('offer') && i!=0 && tabArray[i-1].up().style.display == 'none') {
				booking_rotate('left');
			}
		} else {
			tabcontentArray[i].hide();
			tabArray[i].className = thisclassname;
		}
	}
	thistab.blur();
}

function menu_blind(thislink) {
	var olditem = false;
	var newitem = true;
	var thismenuitem = $(thislink.getAttribute('rel').sub('gromenu_',''));
	var menuitems = $$('ul.mainitem');
	menuitems.each(function(item) {
		if (item.style.display!='none') {
			if (item==thismenuitem)
				newitem = false;
			olditem = item;
		}
	})
	if (olditem)
		new Effect.BlindUp(olditem, {duration:0.4, queue: {position:'parallel', scope: 'blindmenu', limit: 2}, onComplete: olditem.previous(0).removeClassName('maincurrent')});
	if (newitem)
		new Effect.BlindDown(thismenuitem, {duration:0.4, queue: {position:'parallel', scope: 'blindmenu', limit: 2}, onComplete: thismenuitem.previous(0).addClassName('maincurrent')});
}

function toggle_step(thislink, id) {
	thislink.blur();
	if ($(id).style.display == 'none') {
		//$(id).show();
		new Effect.BlindDown(id, {duration:0.5, queue: {position:'end', scope: 'blindicon', limit: 2}, afterFinish: function (obj) {
			thislink.addClassName('minusicon');
			thislink.removeClassName('plusicon');
			}
		});
	} else {
		//$(id).hide();
		new Effect.BlindUp(id, {duration:0.5, queue: {position:'end', scope: 'blindicon', limit: 2}, afterFinish: function (obj) {
			thislink.addClassName('plusicon');
			thislink.removeClassName('minusicon');
			}
		});
	}
}

var clickedhelp = new Array();
var nonextaction = false;

function toggle_help(thislink, id, click, nextaction) {
	thislink.blur();
	if (nonextaction && !click) {
		nonextaction = false;
	} else {
		inarray = clickedhelp.indexOf(id);
		if (click) {
			if (inarray==-1) {
				thislink.addClassName('questioniconactive');
				clickedhelp.push(id);
			} else {
				thislink.removeClassName('questioniconactive');
				clickedhelp.splice(inarray,1);
				if (!nextaction)
					nonextaction = true;
			}
			inarray = clickedhelp.indexOf(id);
		}
		if ($(id).style.display == 'none') {
			contentheight = $(id).getHeight();
			ancestors = thislink.ancestors();
			for (i=0;i<ancestors.length;i++) {
				if(ancestors[i].hasClassName('articlebox')) {
					elementoffset = Position.positionedOffset(ancestors[i]);
					topheight = elementoffset[1]-contentheight;
				}
			}
			$(id).setStyle({'top': topheight+'px'});
			$(id).show();
		} else if (inarray==-1) {
			$(id).hide();
		}
	}
}

function booking_rotate(direction) {
	tabArray = [];
	
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == 'grobooking_offer')) {
			tabArray.push(anchor);
		}
	}
	tabcount = tabArray.length;
	firsttohide = false;
	firsttoshow = false;
	areshownelements = false;
	for (var i=0; i<tabcount; i++){
		if (direction == 'right')
			counter = i;
		else
			counter = tabcount-i-1;
			
		lielement = tabArray[counter].up();
		
		if (lielement.style.display != 'none')
			areshownelements = true;
		
		if (lielement.style.display != 'none' && !firsttohide) {
			firsttohide = true;
			lielement.hide();
		} else if (lielement.style.display == 'none' && !firsttoshow && areshownelements) {
			firsttoshow = true;
			lielement.show();
			$('lefticon').show();
			$('righticon').show();
			if (counter==0)
				$('lefticon').hide();
			if ((counter+1)==tabcount)
				$('righticon').hide();
		}
	}
}

function booking_quickquickcheck(url, thisform, show_button) {
	ajaxparams = Form.serialize(thisform);
	$('quicksubmit').blur();
	new Ajax.Request(url, {asynchronous:true, evalScripts:false, parameters: ajaxparams, onLoading: quickquickload(), onComplete:function(response, json) {
		$('ajaxloader').hide();
		for (var property in json) {
  		switch (property) {
    	case 'free':
				errors = $$('td.error');
  			errors.each(function(s) { s.removeClassName('error'); });
    		if (json['free']=='free') {
    			$('quickperiodisfree').show(); $('quickperiodisnotfree').hide();
    			if (!show_button) {
  					$('quickquickcheck').onsubmit = function (e) { return true };
  					$('quicksubmit').value = 'anfragen';
  					$('quicksubmit').removeClassName('checkbutton');
  					$('quicksubmit').addClassName('anfragebutton');
  					new Insertion.Before('quicksubmit', '<input type="hidden" name="quickquick" value="true" />');
  				} else {
  				  Form.Element.enable('bookinglsubmit');
  				}
				} else if (json['free']=='notfree') {
					$('quickperiodisfree').hide(); $('quickperiodisnotfree').show();
					if (show_button) {
					  Form.Element.disable('bookinglsubmit');
					}
				} else {
					$('quickperiodisfree').hide(); $('quickperiodisnotfree').hide();
				}
    		break;
    	case 'errors':
				for (var error in json['errors']) {
    			$('quick'+error).addClassName('error');
    		}
    		break;
  		default:
    		$(property).update(json[property]);
    		break;
    	}
		}
	}});
}
function quickquickload() {
	$('ajaxloader').show();
	$('quickperiodisfree').hide();
	$('quickperiodisnotfree').hide();
}

function booking_quickcheck(url, thisform) {
  ajaxparams = Form.serialize(thisform);
  new Ajax.Request(url, {asynchronous:true, evalScripts:false, parameters: ajaxparams, onLoading: $('ajaxloader').show(), onComplete:function(response, json) {
		$('ajaxloader').hide();
		if (response.responseText != '')
			$('secondpart').update(response.responseText);
		link_init();
		//Json
		for (var property in json) {
  		switch (property) {
    	case 'free':
    		removeerrors();
    		overlays = document.getElementsByClassName('overlay');
    		if (json['free']=='free') {
    			$('periodisfree').show(); $('periodisnotfree').hide(); $('bookingsummary').show();
					overlays.each(function(s) { s.hide(); });
				} else if (json['free']=='notfree') {
					$('periodisfree').hide(); $('periodisnotfree').show(); $('bookingsummary').hide();
					overlays.each(function(s) { s.show(); });
				} else {
					$('periodisfree').hide(); $('periodisnotfree').hide(); $('bookingsummary').hide();
					overlays.each(function(s) { s.show(); });
				}
    		break;
    	case 'errors':
    		removeerrors();
				errorlistpoints = '';
    		for (var error in json['errors']) {
    			$('label'+error).addClassName('error');
    			errorlistpoints += '<li>'+json['errors'][error]+'</li>';
    		}
    		errorlists = $$('ul.error');
    		errorlists[0].show();
    		errorlists[0].update(errorlistpoints);
    		break;
    	case 'summary':
    		for (var summaryproperty in json['summary']) {
    			if ($('summary'+summaryproperty) != null)
    				$('summary'+summaryproperty).update(json['summary'][summaryproperty]);
    		}
    		break;
  		default:
    		$(property).update(json[property]);
    		break;
    	}
		}
	}});
}
function coupon_update(key,value) {
	ajaxparams = 'key='+key+'&value='+value;
	new Ajax.Request(updateurl, {asynchronous:true, evalScripts:false, parameters: ajaxparams, onComplete:function(request, json) {
		for (var property in json) {
			if ($('summary'+property) != null)
  				$('summary'+property).update(json[property]);
  		}
	}});
}
function choose_coupon(link, offerid) {
  $('couponinfos').immediateDescendants().each(function(s) { $(s).hide(); });
  $('couponimages').immediateDescendants().each(function(s) { $(s).removeClassName('selectedcouponimage'); });
  $(link).addClassName('selectedcouponimage');
  $('couponinfo_'+offerid).show();
  $('offerid').value = offerid;
  if (link.title=='Essen und Trinken') {
    $('price_label').show(); $('price_input').show();
  } else {
    $('price_label').hide(); $('price_input').hide();
  }
}
function booking_sendid(thislink, board) {
	if (board) {
		ajaxparams = 'send=board';
	} else {
		relAttribute = String(thislink.getAttribute('rel'));
		if (relAttribute.toLowerCase().match('offer')) {
			ajaxparams = 'send=offer';
		} else {
			ajaxparams = 'send=room';
		}
	}
	ajaxparams += '&id='+thislink.id;
	new Ajax.Request(updateurl, {asynchronous:true, evalScripts:false, parameters: ajaxparams, onComplete:function(request, json) {
		for (var property in json) {
			switch (property) {
    	case 'norooms':
    		if (json[property]) {
    			$('thirdstepbox').hide();
    		} else {
    			if ($('thirdstepbox').style.display == 'none') {
					$('room1description').show();
					$('room2description').hide();
					$('1room').className = 'yellow_active';
					$('2room').className = 'yellow';
				}				
				$('thirdstepbox').show();
    		}
    		break;
    	default:
  			$('summary'+property).update(json[property]);
  			break;
  		}
  	}
	}});
}
function booking_finish(url, thisform) {
	ajaxparams = Form.serialize(thisform);
  new Ajax.Request(url, {asynchronous:true, evalScripts:false, parameters: ajaxparams, onComplete:function(response, json) {
		if (json==null) {
			$('bottomright').update(response.responseText);
		} else {
			//Json
			for (var property in json) {
	  		switch (property) {
	    	case 'errors':
	    		removeerrors();
					errorlistpoints = '';
	    		for (var error in json['errors']) {
	    			$('label'+error).addClassName('error');
	    			errorlistpoints += '<li>'+json['errors'][error]+'</li>';
	    		}
	    		errorlists = $$('ul.error');
	    		errorlists[1].update(errorlistpoints);
	    		break;
	  		default:
	    		$(property).update(json[property]);
	    		break;
	    	}
			}
		}
	}});
}
function removeerrors() {
	errors = $$('td.error');
  errors.each(function(s) { s.removeClassName('error'); });
  errorlists = $$('ul.error');
  errorlists[0].update();
  errorlists[0].hide();
}
