var bShowTeaser		= false;
var bShowBox		= false;
var iBoxInactiveCnt	= 0;
var mouseX			= null;
var mouseY			= null;

function boxAutoSwitch(e) {
	if(!bShowBox ) {
		iBoxInactiveCnt = 0;
		return;
	}
	var offset = $('#angebotsbox').offset();
	if(mouseX > offset.left && mouseX < offset.left+845 && mouseY>offset.top && mouseY<offset.top+400) {
		iBoxInactiveCnt = 0;
		return;
	}
	iBoxInactiveCnt ++;
	
	if(iBoxInactiveCnt > 30) {
		iBoxInactiveCnt = 0;
		// get active id
		for(var i = 1; i<=3; i++) {
			if($('#offer'+i).hasClass('offerBoxActive')) {
				var id = i;
				break;
			}
		}
		// skip to next id
		id = id%3+1;
		// activate item
		for(var i = 1; i<=3; i++) {
			if(i!=id) {
				$('#offer'+i).removeClass('offerBoxActive');
				$('#bigOffer'+i).removeClass('bigOfferActive');
			}
			else {
				$('#offer'+i).addClass('offerBoxActive');
				$('#bigOffer'+i).addClass('bigOfferActive');
			}
		}
		
	}
}

function showTeaser() {
	bShowTeaser = true;
	$('#angebotsteaser').animate({'left':'-10px'}, 600);
}

function hideTeaser() {
	bShowTeaser = false;
	$('#angebotsteaser').animate({'left':'-55px'},300);
}

function showBox() {
	// teaser ausblenden
	hideTeaser();
	// hintergrund überblenden
	$('#overlay').fadeIn(600,function(){
		// box zeitverzögert einblenden
		$('#angebotsbox').animate({'left': Math.max(($(window).width()-845)/2, 10)+'px'}, 800, function(){
			bShowBox = true;
		});
	});
	// box aufbauen
	$('#angebotsbox').css('left','-855px')
					.css('top', Math.max(($(window).height()-380)/2, 10)+'px')
					.html('<div id="closeBox"></div>'
						+'<div id="box1"></div>'
						+'<div id="box2">'
						+'<div id="offers">'
							+'<div id="offer1" class="offerBoxInactive offerBoxActive"></div>'
							+'<div id="offer2" class="offerBoxInactive"></div>'
							+'<div id="offer3" class="offerBoxInactive"></div>'
							+'<div id="offerBack"><a href="javascript:hideBox()">weiter zur Webseite &raquo;</a></div>'
						+'</div>'
						+'<div id="bigOffers">'
							+'<div id="bigOffer1" class="bigOfferInactive bigOfferActive"></div>'
							+'<div id="bigOffer2" class="bigOfferInactive"></div>'
							+'<div id="bigOffer3" class="bigOfferInactive">'
								+'<div id="btnAngebote"></div>'
							+'</div>'
						+'</div>'
						+'</div>'
						+'<div id="box3"></div>')

	
	// animationen initialisieren
	$('#closeBox').hover(
		//over
		function(){
			$('#closeBox').css('background-position','0 -34px')
		},
		//out
		function(){
			$('#closeBox').css('background-position','0 0')
		})
	.click(
		function(){
			hideBox();
		});
	$('.offerBoxInactive').hover(
		//over
		function(){
			$(this).css('background-position','0 0')
		},
		//out
		function(){
			$(this).css('background-position','0 -100px')
		})
	.click(
		function(){
			var id=parseInt($(this).attr('id').substr(5));
			for(var i = 1; i<=3; i++) {
				if(i!=id) {
					$('#offer'+i).removeClass('offerBoxActive');
					$('#bigOffer'+i).removeClass('bigOfferActive');
				}
				else {
					$('#offer'+i).addClass('offerBoxActive');
					$('#bigOffer'+i).addClass('bigOfferActive');
				}
			}
		});
	$('#btnAngebote').hover(
		//over
		function(){
			$(this).css('background-position','0 -35px')
		},
		//out
		function(){
			$(this).css('background-position','0 0')
		})
	.click(function(){
		document.location.href = "/german/hotel/angebote/weihnachten.html";
	});
}
function hideBox() {
	bShowBox = false;
	// box ausblenden
	$('#angebotsbox').animate({'left': '-850px'},600,function(){
						$(this).html('');
						showTeaser();
					});	
	// hintergrund ausblenden
	$('#overlay').fadeOut();
}

$(document).ready(function(){
	// inits:
	$('body').prepend('	<div id="overlay"></div>'
				+ '	<div id="angebotsteaser"></div>'
				+ '	<div id="angebotsbox"></div>');
	//if(!$.cookie('box-shown')) {
	//	$.cookie('box-shown',1, {path:'/'});
	//	showBox();
	//}
	//else {
		showTeaser();
	//}
	$().mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	}); 

	$('#angebotsteaser').hover(
		//over
		function(){
			if(bShowTeaser == true) {
				$('#angebotsteaser').animate({'left':'0px'},200);
			}
		},
		//out
		function(){
			if(bShowTeaser == true) {
				$('#angebotsteaser').animate({'left':'-10px'},200);
			}
		})
	.click(function(){
		showBox();
	});
	window.setInterval(boxAutoSwitch, 200);
});
