var references = [],
	nextHandler = {},
	referenceIMG = [],
	curRef = 0,
	curTeaser = 0,
	teaser = [];

function openReferenceWindow(idx){
	if(idx >= references.length){
		idx = 0;
	}
	referenceIMG = $('img.refPopUp'+references[idx].id);
	curRef = 0;

	$('#referenceWindow').show();
	$('#nextButton').unbind('click');
	if(referenceIMG.length > 1){
		$('#nextButton').css({visibility:'visible'});
		$('#nextButton').click(nextReference);
	}else{
		$('#nextButton').css({visibility:'hidden'});
	}
	$('#inhalt .referenzen li').addClass('background').removeClass('foreground');
	references[idx].link.removeClass('background').addClass('foreground');
	nextReference();
}

function nextReference(){
	if(curRef >= referenceIMG.length){
		curRef = 0;
	}
	$('#referenceImage')[0].src = referenceIMG[curRef].src;
	$('#referenceDescription')[0].innerHTML = $(referenceIMG[curRef]).attr('alt');
	curRef++;
}
function closeReferenceWindow(){
	$('#referenceWindow').hide();
	$('#inhalt .referenzen li').removeClass('background').removeClass('foreground');
}
function loopTeaser(){
	$('#teaserFrame li').hide();
	for(var i = 0; i < 4; i++){
		if(curTeaser >= teaser.length){
			curTeaser = 0;
		}
		teaser[curTeaser].show();
		curTeaser++;

	}
	$('#teaserFrame').fadeIn(2000, function(){
		setTimeout(function(){
			$('#teaserFrame').fadeOut(1000, loopTeaser);
		},2500);
	});
	/* var h = teaser[curTeaser].height();
	teaser[curTeaser].animate({marginTop:-h},h*40, function(){
			teaser[curTeaser].remove();

			teaser[curTeaser].css({marginTop:0});
			teaser[curTeaser].height(h);
			$('#linkeBox ul.referenzen').append(teaser[curTeaser]);
			curTeaser++;
			loopTeaser();
		});
		*/
}


$(document).ready(function(){
	var html = window.location.href.split('/').pop().split('.').shift();
	if(html == 'index' || html == ''){
		$('#kopf').append('<div id="index-decor"></div>');
	}else{
		$('#start').removeClass('aktiv').addClass('inaktiv');
		if(html == 'impressum'){
			$('#variabler-decor').css({display:'block',marginTop:0, top:'0px', marginLeft:'560px',visibility:'visible'});
		}else if(html == 'kontakt'){
			//$('#variabler-decor').css({display:'block',marginTop:0, top:'60px', marginLeft:'583px',visibility:'visible'});
			$('li#wo').removeClass('inaktiv').addClass('aktiv');
			$('td:contains("zilles\@zilles-design.de")').empty().append('<a href="mailto:zilles@zilles-design.de">zilles@zilles-design.de</a>');
		}else if(html == 'wo'){
			$('td:contains("zilles\@zilles-design.de")').empty().append('<a href="mailto:zilles@zilles-design.de">zilles@zilles-design.de</a>');
		}
	}



	if($('#teaserFrame').length > 0){
		$('#inhalt .referenzen li').css({cursor:'pointer'}).each(function(idx){
			var id = this.id.substr(8);
			references[idx] = {link: $(this).click(function(event){
				openReferenceWindow(idx);
			}), id: id};
			$('#refTease'+id).css({cursor:'pointer'}).click(function(event){
				openReferenceWindow(idx);
			});
		});
		$('#linkeBox .referenzen li').each(function(idx){
			teaser[idx] = $(this);
			teaser[idx].hide();
		});
		//$('#teaserFrame').prepend('<div id="teaserOL"/>');
		loopTeaser();
		$('#closeButton').click(closeReferenceWindow);
		$('#referenceWindow').draggable({handle: $('#referenceDescription'), containment: 'body'});
	}

});
