/**
 * @author engelke
 */
jQuery(function() {
	jQuery('ul.sf-menu').superfish({
		autoArrows : false, // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance
		dropShadows : false,
		speed : 1,
		disableHI : true
	});
});
$(document).ready(function() {
	$('.submenu, #verticalMenu li.hasSub').mouseenter(function() {
		$('#subnavi').show();
	});
	
});
function showFitClock() {
	$('#fits1').hide();
	var bottom1 = '0';
	var bottom2 = '0';
	if($('#mainClock').data('gender') == 'f') {
		bottom1 = '25px';
	} else {
		bottom2 = '25px';
	}
	$('#mainClock').animate({
		'width' : '350px',
		'bottom' : '0',
		'margin-top' : bottom1,
		'left' : '100px'
	}, 400);
	$('#fitClock').animate({
		'width' : '350px',
		'bottom' : '0',
		'margin-top' : bottom2,
		'right' : '100px'
	}, 400);
	$('#info3').show();
	$('#info4').show();
	$('.closeFitClock').show();
	$('#counter').hide();
}

function hideFitClock() {
	$('#info3').hide();
	$('#info4').hide();
	$('.closeFitClock').hide();
	$('#mainClock').animate({
		'width' : '960px',
		'bottom' : '0',
		'left' : '0px',
		'margin' : 0
	}, 400);
	$('#fitClock').animate({
		'width' : '0px',
		'bottom' : '0',
		'right' : '0px',
		'margin' : 0
	}, 400, function() {
		$('#fits1').show();
	});
	$('#counter').hide();
}

$(document).ready(function() {
	
	/*
	 * Reset der Suche
	 */
	if($('div#ArtikelSuche').length) {
			$('a.resetForm').click(function(){
				$(this).parent().find('input:checked').prop("checked",false);
			});
	}
	
	
	
	if($('div.vAlignCenter').length) {
			var pHei = $('div#main').innerHeight();
			var tHei = $('div.vAlignCenter').outerHeight();
			var H1Hei = $('.content_top h1').outerHeight();
	
			var top = tHei / 2 -H1Hei;
			if(top > 0) {
				$('div.vAlignCenter').css('margin-top', top);
			}
	}

	
	// initialize scrollable
	$(".artile_list_wrap .scrollable img").ready(function() {
		if($('#activeElem').length) {
			var actPnr = $('#activeElem').val();

			var position = $(".artile_list_wrap .scrollable .items").find('#' + actPnr).index();
			var subposition = position % 4;
			var position = position - subposition;
			var api = $(".artile_list_wrap .scrollable").data("scrollable").focus();
			api.move(position);
		}
	});
	var api = $(".artile_list_wrap .scrollable").scrollable({
		next : '.undefined',
		prev : '.undefined'
	}).data("scrollable");

	$("#zoom1").miniZoomPan({
		sW : 960,
		sH : 500,
		lW : $('#zoom1 img').attr('width'),
		lH : $('#zoom1 img').attr('height')
	});

	$(".artile_list_wrap .next").click(function() {
		api.move(4);
	});
	$(".artile_list_wrap .prev").click(function() {
		api.move(-4);
	});

	$("#fits1 a").live('click', function() {
		showFitClock();
		return false;
	});

	$('a.closeFitClock').live('click', function() {
		hideFitClock();
		return false;
	});

	$(".artile_list_wrap .scrollable .items a").click(function() {
		$(".artile_list_wrap .scrollable .items a").each(function() {
			$(this).removeClass('active');
		});
		var thisIndex = $(".artile_list_wrap .scrollable .items a").index(this);
		// see if same thumb is being clicked
		if($(this).hasClass("active")) {
			return;
		}

		// calclulate large image's URL based on the thumbnail URL (flickr specific)

		$("#mainView > div").stop().fadeTo("medium", 0.0);
		var zoomurl = $(this).parent().data('zoomurl');
		//$.ajax('index.php?eID=lia_artile_as')
		$.ajax({
			url : $(this).parent().data('url'),
			cache : false,
			dataType : 'json',
			success : function(json) {
				$('#mainClock').attr('style', '');
				$('#fitClock').css({
					'width' : 0
				}); 
				var infoHeader = function(art) { ;return art.pnr ? (art.gender == 'm' ? "FÜR IHN –" : "FÜR SIE –") + '<br/>Kollektion: ' + art.pageTitle.toUpperCase() : null };
				$('#info1').html(infoHeader(json) + '<br/>' + json.description1 + '<br/>' + json.description2 + '<br/>' + json.description3 + '<br/>' + json.description4 + '<br/>' + json.description5);

				$('#info2').html( json.articleNumber + '<br/>&euro; ' + json.price + ' ');
				$('#info3').hide().html(infoHeader(json.fit) + '<br/>' + json.fit.description1 + '<br/>' + json.fit.description2 + '<br/>' + json.fit.description3 + '<br/>' + json.fit.description4 + '<br/>' + json.fit.description5);
				$('#info4').hide().html( json.fit.articleNumber+ '<br/>&euro; ' + json.fit.price + ' ');
				$('#mainClock').attr('data-gender', json.gender);
				$('#counter .number').text(thisIndex + 1);
				if(json.fits1.length) {
					$('#fits1').html('Passende Partneruhr<br/><a href="#" data-pnr="' + json.fits1 + '" class="imgwrap"><img alt="' + json.fits1 + '" src="' + json.fit.img + '" height="100"></a> ');
				} else {
					$('#fits1').text('');
				}
				var url = $('img', this).attr("src");

				var wrap = $("#mainClock");

				var img = new Image();

				img.onload = function() {

					// make wrapper fully visible
					wrap.fadeTo("fast", 1);
					$("#mainView > div").fadeTo("medium", 1.0);
					wrap.find("a img").unwrap();
					wrap.find("img").attr("src", json.img).wrap('<a href="' + zoomurl + '" class="zoomImage" data-pnr="' + json.pnr + '" />');

				};

				img.src = json.img;

				var wrap2 = $("#fitClock");
				wrap2.html('<img src="clear.gif">');
				if(json.fit.img) {
					var img2 = new Image();
					img2.onload = function() {
						wrap2.find("img").attr("src", json.fit.img);
					};
					img2.src = json.fit.img;
				}
				// activate item
				$(".items img").removeClass("active");
			}
		});
		$(this).addClass("active");

		return false;
	})
	if($("#mainView").hasClass('listAll')) {
		$(".artile_list_wrap .scrollable .items a").filter(":first").addClass("active").click();
	}
	if($(".artile_list_wrap .scrollable .items a").find('.active"')) {
		var ind = $(".artile_list_wrap .scrollable .items .item").has("a.active").index();
		$('#counter .number').text(ind+1);
	}

	/*
	*  Startseiten Slider
	*
	*/
	/*setTimeout(function(){
	$(".tx_liaslideras .scrollable .items a").filter('.active').next().css('border','5px solid red').click();
	}, 1000);
	*/
	// Poll every 100ms until some_condition is true

	function moveItem() {
		var actElem = $(".tx_liaslideras .scrollable .items a").filter('.active');
		if($(".tx_liaslideras .scrollable .items a:last").hasClass('active')) {
			$(".tx_liaslideras .scrollable .items a:first").click();
		} else {
			actElem.next().click();
		}

	}

	//var intervalSlider = setInterval(moveItem,5000);
	//var intervalSlider = setInterval(moveItem,5000);
	var intervalSlider = null;
	$('.tx_liaslideras .StartStopMotion').click(function() {
		if($(this).hasClass('gestoppt')) {
			$('.tx_liaslideras .StartStopMotion').removeClass('gestoppt');
			intervalSlider = setInterval(moveItem, 5000);
		} else {
			$('.tx_liaslideras .StartStopMotion').addClass('gestoppt');
			clearInterval(intervalSlider);
		}
		return false;
	});

	$(".tx_liaslideras .scrollable .items a").click(function() {
		clearInterval(intervalSlider);
		intervalSlider = setInterval(moveItem, 5000);
		$('.tx_liaslideras .StartStopMotion').removeClass('gestoppt');

		if($(this).hasClass("active")) {
			return false;
		}

		var url = $('div', this).data("img");
		var href = $(this).attr("href");
		var wrap = $("#sliderMainPic");
		wrap.fadeTo("slow", 0, function() {
			var img = new Image();
			// call this function after it's loaded
			img.onload = function() {
				// make wrapper fully visible
				wrap.html('<a><img src="clear.gif"></a>');
				wrap.fadeTo("slow", 1);
				// change the image
				wrap.find("img").attr("src", url);
				wrap.find("a").attr('href', href);
			};
			img.src = url;
		});
		// activate item
		$(".items a").removeClass("active");
		$(this).addClass("active");

		return false;
		// when page loads simulate a "click" on the first image
	}).filter(":first").click();

});

