var produkte = [];
var curIDX;
var curTop = 0;
function showProdukt(idx, anker){

	curTop = anker ? $(anker).position().top : curTop;
	window.scrollTo(0,0);
	//alert(curTop);
	if(idx < 0){
		idx = produkte.length - 1;
	}else if(idx >= produkte.length){
		idx = 0;
	}
	curIDX = idx;
	var	w = $(window).width() > $(document).width() ? $(window).width() : $(document).width(),
			h = $(window).height() > $(document).height() ? $(window).height() : $(document).height(),
			ww = $(window).width(),
			wh = $(window).height(),
			showcase, vor, zurueck, thisclose;

	if($('#showcase').length){
		showcase = $('#showcase');
		vor = $('#vor');
		zurueck = $('#zurueck');
		thisclose = $('#close');
	}else{
		thisclose = $('<img src="pix/close.png" id="close"/>').click(
				function(){
					$('#showcase').remove();
					$('#overlay').remove();
					$(this).remove();
		});
		vor = $('<img src="pix/vor.png" id="vor"/>').click(function(){showProdukt(curIDX-1);});
		zurueck = $('<img src="pix/zurueck.png" id="zurueck"/>').click(function(){showProdukt(curIDX+1);});
		showcase = $('<div id="showcase"></div>');
		$('body').append(showcase);
		$('body').append(
								$('<div id="overlay"/>').css({width: w, height: h}).append(
																																				vor
																																		).append(
																																				zurueck
																																		)
																						).append(
																																			thisclose
																																		);
	}
	if (jQuery.browser.msie) {
	  if(parseInt(jQuery.browser.version) < 7){
	  	showcase.empty().append(produkte[idx]).show();
	  }else{
	  	showcase.empty().width('auto').height('auto').append(produkte[idx]).show();
	  }
	}else{
		showcase.empty().width('auto').height('auto').append(produkte[idx]).show();
	}

	setTimeout(function(){
		var wS = showcase.width(),
				hS = showcase.height();


		showcase.css( {left: (ww - wS) / 2 , width: wS+'px', height:hS+'px' });
		vor.css({left: (ww - wS) / 2 - 60});
		zurueck.css({left: (ww - wS) / 2 + wS + 40});
		thisclose.css({left: (ww - wS) / 2 + wS - 11});
		if(anker){
			showcase.css({top: 60});
			vor.css({top: 220});
			zurueck.css({top: 220});
			thisclose.css({top: 75 });
		}

	},500);

}

$(document).ready(
	function(){
		$('.produkt').each(
			function(idx, ele){
				_self = this;
				this.id = 'produkt' + idx;
				var anker = $('<a id="link_'+this.id+'" href="#">' + $('.headline_produkt', this).html() + '</a>'),
						func = function(e){
							e.preventDefault();
							showProdukt(idx, _self);
							return false;
						};
				$('#subNavigation').append(anker);
				$('img', this).bind('click', func);
				anker.bind('click', func);
				produkte.push($('<img src="'+$('img',ele)[0].src+'"/><div class="text"><p style="font-weight:bold">'+$('.headline_produkt', ele).html()+' '+$('.short_produkt', ele).html()+'</p><p>'+( $('.long_produkt',ele).html()  )+'</p><p><a href="?article_id=8">Eine Anfrage zum Produkt schicken.</a></p></div>'));
			}
		);
		if( $('.kontaktformular label.formerror').length ){
			$('.kontaktformular label.formerror+input').each(
				function(idx){
					if(idx == 0){
						$('#'+$(this).attribute('for'))[0].focus();
					}
				}
			);
		}
});
