var httpHost = window.location.protocol  == "https:" ? "https://sslsites.de/shusta.de/" : window.location.protocol  + "//" + window.location.host + "/";

var maxShop = 10;
$(document).ready(function() {
	$.viewportHeight = function() {
		 return self.innerHeight ||
			jQuery.boxModel && document.documentElement.clientHeight ||
			document.body.clientHeight;
	};
	$.viewportWidth = function() {
		 return self.innerWidth ||
			jQuery.boxModel && document.documentElement.clientWidth ||
			document.body.clientWidth;
	};
//	$('img').ifixpng('/img/pixel.gif');
	$('img.mouseover,input.mouseover').mouseover(function(){
		var bn = $(this).attr("src");
		var fil = bn.replace(/^(.+)([\.]{1}.+?)$/, '$1');
		var ext = bn.replace(/^(.+)([\.]{1}.+?)$/, '$2');
		$(this).attr("src", fil + "_a" + ext);
	});
	$('img.mouseover,input.mouseover').mouseout(function(){
		var bn = $(this).attr("src");
		var fil = bn.replace(/^(.+)([\.]{1}.+?)$/, '$1');
		    fil = fil.replace(/_a$/, '');
		var ext = bn.replace(/^(.+)([\.]{1}.+?)$/, '$2');
		$(this).attr("src", fil + ext);
	});
	$('a.blank').attr("target", "_blank");
	$('.langDE').mouseover(function(){
		$(this).attr("src", "/img/de_act.gif");
	});
	$('.langDE').mouseout(function(){
		$(this).attr("src", "/img/de.gif");
	});
	$('.langEN').mouseover(function(){
		$(this).attr("src", "/img/en_act.gif");
	});
	$('.langEN').mouseout(function(){
		$(this).attr("src", "/img/en.gif");
	});

});


/*
$(window).load(function(){
	$.ajax({
	   type: "POST",
	   url: "/ajax/agentInfo",
	   data: "vw=" + $.viewportWidth() + "&vh=" + $.viewportHeight()
	});
//	init();
});
function init() {
	checkDim();
	window.onresize = checkDim;
}

function checkDim() {
	var curWdt = $.viewportWidth();
	var curHgt = $.viewportHeight();
	var divWdt = $('#splash').width();
	var divHgt = $('#splash').height();

	if (curWdt < divWdt) {
		$('#splash').css('margin-left', '0px');
		$('#splash').css('left', '0px');
	}
	else {
		var ml = Math.round(divWdt / 2) * -1;
		$('#splash').css('margin-left', ml + 'px');
		$('#splash').css('left', '50%');
	}
	/*
	if (curHgt < divHgt) {
		$('#splash').css('margin-top', '0px');
		$('#splash').css('top', '0px');
	}
	else {
		var mt = Math.round(divHgt / 2) * -1;
		$('#splash').css('margin-top', mt + 'px');
		$('#splash').css('top', '50%');
	}
	*//*
}
*/

function replaceSelect(select, replace) {
	if (!$('#' + select).length) {
		return;
	}
	var callback = typeof(replaceSelect.arguments[2]) != "undefined" ? replaceSelect.arguments[2] : null;
	var rel = $('#' + select).attr('rel');
	rel = typeof(rel) == "undefined" ? "" : rel;
	$('#' + select + "Submit").remove();
	$('#' + select).after('<div id="' + replace + '" rel="' + rel + '"><div id="' + replace + 'Header"><div></div></div><div id="' + replace + 'Content"><div></div></div></div>');
	var val = $('#' + select).val();
	var name = $('#' + select).attr('name');
	html = "";
	options = new Array;
	$('#' + select + ">option").each(function(i){
		if ($(this).is(":selected")) {
			html = $(this).html();
		}
		options[i] = new Object;
		options[i]['value'] = $(this).attr('value');
		options[i]['html'] = $(this).html();
		options[i]['title'] = $(this).attr('title');
	});
	$('#' + replace + "Header>div").html(html);
	for (var i = 0; i < options.length; i++) {
		$('#' + replace + "Content>div").append("<div rel='" +  options[i]['value'] + "' title='" + options[i]['title'] + "'>" + options[i]['html'] + "</div>");
	}
	$('#' + replace + 'Content>div>div').mouseover(function(){
		$(this).css('background-color', '#aaaaaa');
		$(this).css('color', '#ffffff');
	});
	$('#' + replace + 'Content>div>div').mouseout(function(){
		$(this).css('background-color', 'transparent');
		$(this).css('color', '');
	});
	$('#' + replace + 'Content>div>div').click(function(){
		if ($(this).attr('title').match(/|/)) {
			var title = $(this).attr('title').split("|");
			for (var i = 0; i < title.length; i++) {
				if (i == 0) {
					$('#' + replace + "Title").html( title[i] );
				}
				else {
					$('#' + replace + "Title" + i).html( title[i] );
				}
			}
		}
		else {
			$('#' + replace + "Title").html( $(this).attr('title') );
		}
		$('#' + select).val( $(this).attr('rel') );
		$('#' + replace + "Header>div").html( $(this).html() );
		$('#' + replace + 'Content').slideUp('fast', function(){
		if (callback != null) {
			callback();
		}
		    $("form").each(function () {
		    	form = this;
		    	$(this).children().each(function(){
		    		if ($(this).attr('id') == replace && $(this).attr('rel') == "submit") {
		    			$(form).submit();
		    		}
		    	})
		    });
		});
	});
	$('#' + replace + 'Header').click(function(){
		var el = $(this).attr('id').replace(/Header$/, 'Content');
		$('#' + el).slideToggle('fast');
	});
	$('#' + replace + "Content").css("position", "absolute");
	$('#' + replace + "Content").css("z-index", "100");
	$('#' + select).remove();
	$('#' + replace).append('<input type="hidden" id="' + select + '" name="' + name + '" value="' + val + '" />');
	$('#' + replace).css('display', 'block');

	if (options.length > 10) {
		$('#' + replace + "Content>div").css("height", "155px");
		$('#' + replace + "Content>div").css("overflow", "auto");
	}

	/* *************** */
	if (!$.support.boxModel) {
		$('#' + replace + 'Content>div').css('border-left', '1px solid #aaaaaa');
		$('#' + replace + 'Content>div').css('border-right', '1px solid #aaaaaa');
		$('#' + replace + 'Content>div').css('border-bottom', '1px solid #aaaaaa');
		$('#' + replace + 'Content>div>div').css('padding', '3px 6px');
		$('#' + replace + 'Content>div>div').css('cursor', 'pointer');
	}
}
function number_format(number) {
	number = Math.round(number * 100) / 100;
	number = String(number);
	number = number.replace(/\./, ',');
	if (number.match(/,[0-9]{1}$/)) {
		number += "0";
	}
	else if (!number.match(/,/)) {
		number += ",00";
	}
	number = number.replace(/([0-9]{1})([0-9]{3})(,[0-9]{2})$/, '$1.$2$3');
	return number;
}
function checkForm(required) {
	var error = new Array();
	if (typeof(required['email']) != "undefined") {
		for (var i = 0; i < required['email'].length; i++) {
			var value = $('#' + required['email'][i]).val();
			if (!value.match(/^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,4}$/g)) {
				error[ error.length ] = required['email'][i];
			}
		}
	}
	if (typeof(required['text']) != "undefined") {
		for (var i = 0; i < required['text'].length; i++) {
			if (typeof($('#' + required['text'][i]).val()) == "undefined" || $('#' + required['text'][i]).val().length == 0) {
				error[ error.length ] = required['text'][i];
			}
		}
	}
	if (typeof(required['date']) != "undefined") {
		for (var i = 0; i < required['date'].length; i++) {
			var dateDay = $('#' + required['date'][i] + "Day").val();
			var dateMonth = $('#' + required['date'][i] + "Month").val();
			var dateYear = $('#' + required['date'][i] + "Year").val();

			if (!checkdate(dateDay, dateMonth, dateYear)) {
				error[ error.length ] = required['date'][i] + "Day";
				error[ error.length ] = required['date'][i] + "Month";
				error[ error.length ] = required['date'][i] + "Year";
			}
		}
	}
	if (typeof(required['equal']) != "undefined") {
		for (var i = 0; i < required['equal'].length; i+=2) {
			if ($('#' + required['equal'][i]).val() !=  $('#' + required['equal'][i+1]).val()) {
				error[ error.length ] = required['equal'][i];
				error[ error.length ] = required['equal'][i+1];
			}
		}
	}

	return error;
}
function checkdate(day, month, year) {
	day = day.replace(/^[0]{1,11}/, '');
	month = month.replace(/^[0]{1,11}/, '');

	day = parseInt(day);
	month = parseInt(month);
	year = parseInt(year);

	if (day == '' || isNaN(day) || day < 1 || day > 31) {
		return false;
	}
	if (month == '' || isNaN(month) || month < 1 || month > 12) {
		return false;
	}
	if (year == '' || isNaN(year) || year < 1) {
		return false;
	}

	leap = false;
	if (year % 4 == 0) {
		leap = true;
	}
	if (year % 100 == 0) {
		leap = false;
	}
	if (year % 400 == 0) {
		leap = true;
	}
	switch (month) {
		case 4:
		case 6:
		case 9:
		case 11:
			if (day > 30) {
				return false;
			}
			break;
		case 2:
			if (leap && day > 29) {
				return false;
			}
			if (!leap && day > 28) {
				return false;
			}
			break;
	}
	return true;
}
function checkMethods(country) {
	$('#shipping_method option').each(function(){
		var id = $(this).attr('value');
		if (typeof(shippingAllowed[country]) != "undefined" && id != "") {
			if ($.in_array(id, shippingAllowed[country]) != -1) {
				$(this).show();
			}
			else {
				if ($(this).is(":selected")) {
					$('#shipping_method').val('');
				}
				$(this).hide();
			}
		}
	});
	$('#paymenttype option').each(function(){
		var id = $(this).attr('value');
		if (typeof(paymentAllowed[country]) != "undefined" && id != "") {
			if ($.in_array(id, paymentAllowed[country]) != -1) {
				$(this).show();
			}
			else {
				if ($(this).is(":selected")) {
					$('#paymenttype').val('');
				}
				$(this).hide();
			}
		}
	});
}
/*
function in_array(needle, haystack) {
	for (i in haystack) {
		if (haystack[i] == needle) {
			return true;
		}
	}
	return false;
}
*/
function calculateTotal() {
	alert(2);
	var shipping = $('#shipping_method').val();
	var payment = $('#paymenttype').val();
	if (shipping == '' || payment == '' || typeof(shippingCost[shipping]) == "undefined" || typeof(paymentAgio[payment]) == "undefined") {
		$('#shippingcost').html('');
		$('#total').html('');
		return;
	}
	var ship = shippingCost[shipping] + paymentAgio[payment];
	var total = subtotal + ship;
	$('#shippingcost').html(number_format(ship) + " €");
	$('#total').html(number_format(total) + " €");
}
function recalc() {
	var ship = $('#shippingMethod').val();
	var pay = $('#paymentMethod').val();

	var currentShipPrice = shipping[shipSelected]['discount_at'] != 0 && subtotal >= shipping[shipSelected]['discount_at'] ? shipping[shipSelected]['price_discount'] : shipping[shipSelected]['price'];
	var currentPayPrice = payment[paySelected]['price'];

	var currentTax = currentShipPrice * shipping[shipSelected]['tax'] / (1 + shipping[shipSelected]['tax']);
	var currentTaxId = shipping[shipSelected]['tax'] * 1000;
	$('#tax' + currentTaxId).html(number_format(parseFloat($('#tax' + currentTaxId).html().replace(/,/, ".")) - currentTax));

	var currentPayTax = currentPayPrice * payment[paySelected]['tax'] / (1 + payment[paySelected]['tax']);
	var currentPayTaxId = payment[paySelected]['tax'] * 1000;
	if (currentPayTaxId != 0) {
		$('#tax' + currentPayTaxId).html(number_format(parseFloat($('#tax' + currentPayTaxId).html().replace(/,/, ".")) - currentPayTax));
	}

	var newShipPrice = shipping[ship]['discount_at'] != 0 && subtotal >= shipping[ship]['discount_at'] ? shipping[ship]['price_discount'] : shipping[ship]['price'];
	var newPayPrice = payment[pay]['price'];

	var newTax = newShipPrice * shipping[ship]['tax'] / (1 + shipping[ship]['tax']);
	var newTaxId = shipping[ship]['tax'] * 1000;
	$('#tax' + newTaxId).html(number_format(parseFloat($('#tax' + newTaxId).html().replace(/,/, ".")) + newTax));

	var newPayTax = newPayPrice * payment[pay]['tax'] / (1 + payment[pay]['tax']);
	var newPayTaxId = payment[pay]['tax'] * 1000;
	if (newPayTaxId != 0) {
		$('#tax' + newPayTaxId).html(number_format(parseFloat($('#tax' + newPayTaxId).html().replace(/,/, ".")) + newPayTax));
	}

	var newTotal = subtotal + newShipPrice + newPayPrice;
	$('#shippingcost').html(number_format(newShipPrice));
	$('#paymentcost').html(number_format(newPayPrice));
	$('#total').html(number_format(newTotal));

	shipSelected = ship;
	shippingPrice = shipping[ship]['price'];
	shippingDiscount = shipping[ship]['price_discount'];
	shippingDiscountAt = shipping[ship]['discount_at'];
	shippingTax = shipping[ship]['tax'];

	paySelected = pay;
	paymentPrice = shipping[pay]['price'];

	$('.taxRow').each(function(){
		var id = $(this).attr('id').replace(/Row/, '');
		if (parseFloat($('#' + id).html().replace(/,/, ".")) == 0) {
			$(this).hide();
		}
		else {
			$(this).show();
		}
	});

}
function shopTooltip() {
	tooltipOver = true;
	var image = typeof(shopTooltip.arguments[0]) != "undefined" ? shopTooltip.arguments[0] : "";
	var brand = typeof(shopTooltip.arguments[1]) != "undefined" ? shopTooltip.arguments[1] : "";
	var shoe  = typeof(shopTooltip.arguments[2]) != "undefined" ? shopTooltip.arguments[2] : "";
	var model = typeof(shopTooltip.arguments[3]) != "undefined" ? shopTooltip.arguments[3] : "";

	if (image == "") {
		image = "/img/shop/empty_big.gif";
	}

	if ((image == "" || brand == "" || shoe == "" || model == "")) {
		if (!tooltipOver) {
		//	$('#shoptooltip').fadeOut('fast');
		}
		return;
	}

	$('#shoptooltipimg').attr('src', httpHost + image);
	$('#shoptooltipbrand').html(brand);
	$('#shoptooltipname').html(shoe);
	$('#shoptooltipmodel').html(model);
	//$('#shoptooltip').fadeIn('fast');
        $('#shoptooltip').show();
}
function changeImg(img, el, det) {
	$('.gallery').removeClass('gallerySelected');
	$('#detailImg').attr('src', httpHost + img);
	$('#detailImgLink').attr('href', httpHost + det);
	$('#' + el).addClass('gallerySelected');
	if (typeof(showZoom) == "function") {
		showZoom();
	}
}



var tooltipOver = false;
$(document).mousemove(function(e){
	if ($('#shoptooltip').is(":visible")) {
		if (!tooltipOver) {
			//$('#shoptooltip').fadeOut('fast');
                        $('#shoptooltip').hide();
			return;
		}
		var width = $('#shoptooltip').outerWidth(true) + 25;
		var height = $('#shoptooltip').outerHeight(true) + 25;
		var left = e.pageX;
		var top  = e.pageY;

                var mouseX = left;
                var mouseY = top;


		if (top + height > $.viewportHeight()) {
			top = $.viewportHeight() - height;
		}
		if (left + width> $.viewportWidth()) {
			left = $.viewportWidth() - width;
		}

		left += 20;
		top += 20;

                if (mouseX >= left && mouseX <= left + width && mouseY >= top && mouseY <= top + height ) {
                    left = mouseX - width - 20;
                }
                if (mouseX >= left && mouseX <= left + width && mouseY >= top && mouseY <= top + height ) {
                    $('#shoptooltip').hide();
                }

		$('#shoptooltip').css('left', left + "px");
		$('#shoptooltip').css('top', top + "px");
	}

});

/*
try {
	$(function(){
		$.Lightbox.construct({
			fixedNavigation: true,
			show_helper_text:	false,
			download_link: false,
			files: {
				images: {
					loading: 'img/loading.gif',
					next: 'img/next.gif',
					prev: 'img/back.gif'
				}
			},
			text: {
				about: {
					text: ''
				},
				closeInfo: language['lbcloseInfo'],
				close: language['lbclose']
			}
		});
	});
}
catch (e) { }

*/
