var productlist = new Array();

var timeout = new Array();

var zoomid = 0;

function addProduct(id, amount) {
	productlist.push(new Array(id, amount));
}

$(document).ready(function() {
	$("a").focus(function() {
		$(this).blur();
	});
	$("input[type='submit']").focus(function() {
		$(this).blur();
	});
	$("input[type='reset']").focus(function() {
		$(this).blur();
	});
	$("input[type='button']").focus(function() {
		$(this).blur();
	});
	$("input[type='radio'], input[type='checkbox']").each(function() {
		if ($(this).attr('id') && $(this).css('display') != 'none') {
			$(this).before(niceRadio(this));
		}
	});
	$(".product_thumb").each(function() {
		if ($(this).hasClass("inactive")) $(this).fadeTo(1, 0.5);
	});
	initZoomWin();
	initProducts(productlist);
	initButtons();
});

function bigImage(thumb, n, id) {
	if (id && timeout[id.toString()]) clearTimeout(timeout[id.toString()]);
	var dest = $('#image_' + n + '_' + id);
	var img1 = $('#image_1_' + id);
	var img2 = $('#image_2_' + id);
	var img3 = $('#image_3_' + id);
	var thumbs = new Array($('#thumb_1_' + id), $('#thumb_2_' + id), $('#thumb_3_' + id));
	if (n != 1) img1.fadeOut(300);
	if (n != 2) img2.fadeOut(300);
	if (n != 3) img3.fadeOut(300);
	for (var i=0; i<thumbs.length; i++) {
		if (!thumb || thumbs[i].get(0) != thumb.get(0)) thumbs[i].fadeTo(50, 0.5);
	}
	if (thumb) {
		thumb.fadeTo(50, 1);
	} else {
		thumbs[0].fadeTo(50, 1);
	}
	dest.fadeIn(300);
}

function resetTimer(id) {
	if (id && timeout[id.toString()]) clearTimeout(timeout[id.toString()]);
}

function resetBigImage(n, id) {
	if (id && timeout[id.toString()]) clearTimeout(timeout[id.toString()]);
	timeout[id.toString()] = setTimeout("bigImage(0, " + n + ", " + id + ")", 1000);
}

function initZoomWin() {
	$('#zoomwin').click(function() {
		return false;
	});
}

function initZoomOut() {
	$('html').click(function() {
		$('#zoomwin').fadeOut(400);
		$('#zoomloader').hide();
	});
	$('#zoomimg').click(function() {
		$('#zoomwin').fadeOut(400);
		$('#zoomloader').hide();
	});
}

function zoomImage(n, id) {
	$('#zoomwin').hide();
	$('#brandwin').hide();
	$('#zoom_prev').hide();
	$('#zoom_next').hide();

	$('html').unbind();
	$('#zoomimg').unbind();

	zoomid = id;

	if (n) {
		var dest = $('#image_' + n + '_' + id);
	} else {
		var img1 = $('#image_1_' + id);
		var img2 = $('#image_2_' + id);
		var img3 = $('#image_3_' + id);
		var dest = (img1.css("display") != "none")? img1 : ((img2.css("display") != "none")? img2 : img3);
		n = (img1.css("display") != "none")? 1 : ((img2.css("display") != "none")? 2 : 3);
	}
	
	if (n > 1) {
		$('#zoom_prev').show();
		$('#zoom_prev').attr('rel', n);
	}
	if (n < 3) {
		$('#zoom_next').show();
		$('#zoom_next').attr('rel', n);
	}
	
	$('#imgid').html(n);
		
	var src = dest.attr("src");
	src = src.substr(0, src.indexOf("&"));
	
	var img = new Image();
	
	var pos = dest.parent().parent().parent().position();
	var w = dest.parent().parent().parent().parent().parent().width();

	var loader = $('#zoomloader');
	loader.unbind();
	loader.css('top', (pos.top + Math.max(200, parseInt($('#zoomwin').height() / 2, 10))) + "px");
	loader.fadeIn(100);
	
	img.onload = function() {
		$('#zoomimg').attr('src', src);
		var win = $('#zoomwin').get(0);
		
		var winwidth = img.width + 20;
				
		win.style.top = (pos.top + 100) + "px";
		win.style.left = pos.left + Math.round(w / 2 - winwidth / 2) - 8 + "px";
		$('#zoomwin').fadeIn(400);
		loader.fadeOut(100);
	}
	
	img.src = src;
	
	$('html, body').animate( { scrollTop:pos.top }, 500, "swing", function() {
		initZoomOut();
	});
	
	return false;
}

function switchImage(obj, d) {
	var current = parseInt($(obj).attr("rel"));
	if (!current) current = 0;
	var n = current + d;
	n = Math.max(1, n);
	n = Math.min(3, n);
	zoomImage(n, zoomid);
}

function brandInfos(obj, prodid, type, id, lang) {
	$('#zoomwin').hide();
	$('#brandwin').hide();
	
	$.ajax({
		type: "POST",
		url: "edition/scripts/brandinfo.php",
		data: "type=" + type + "&id=" + id + "&lang=" + lang,
		success: function(msg){
			showBrandInfos(obj, prodid, msg);
		}
	});
	return false;
}

function showBrandInfos(obj, prodid, msg) {
	msg = msg.split("<|>");
	var src = 'edition/scripts/file.php?src=' + unescape(msg[2]) + '&w=178&h=212&c=1';
	var name = msg[0];
	var description = msg[1];
	
	var img = new Image();
	img.src = src;
	
	var product = $('#productframe_' + prodid);
	var pos = product.position();
	var pos2 = $(obj).position();
	
	
	$('#brandname').html(name);
	$('#branddescr').html(description);
			
	var win = $('#brandwin').get(0);
	win.style.top = (pos.top + pos2.top - $('#brandwin').height() + 180) + "px";
	
	$('#brandwin').fadeIn(400);
	$('html').mousedown(function() {
		$('#brandwin').fadeOut(400);
	});
	
	//$('html, body').animate( { scrollTop:pos.top - 100 }, 500 );
}

function showAlertwin(obj, prodid, min_amt) {
	var product = $('#productframe_' + prodid);
	var pos = product.position();
	var pos2 = $(obj).position();
	
	var win = $('#alertwin').get(0);
	win.style.top = (pos.top + pos2.top - $('#alertwin').height() + 186) + "px";
	
	$('#alert_amt').html(min_amt);
	
	$('#alertwin').fadeIn(400);
	$('html').mousedown(function() {
		$('#alertwin').fadeOut(400);
	});
	
	//$('html, body').animate( { scrollTop:pos.top - 100 }, 500 );
	
	$('#alertbut').click(function() {
		var input = $('#amount_' + prodid);
		input.val(min_amt);
		$(obj).parent().children().removeClass('radio_disabled');
		changeRadio(obj, true);
		return false;
	});
}

function initProducts(list) {
	updateProduct(list[0][0], list[0][1], true, 0);
}

function initNextProduct(idx) {
	idx ++;
	if (productlist.length > idx) {
		updateProduct(productlist[idx][0], productlist[idx][1], true, idx);
	}
}

function updateProduct(product_id, amt, init, idx) {
	amt = parseInt(amt, 10);
	if (!amt) {
		amt = parseInt($("#amount_" + product_id).val(), 10);
	}
	$.ajax({
		type: "POST",
		url: "edition/scripts/checkavail.php",
		data: "id=" + product_id + "&amt=" + amt,
		success: function(msg){
			refreshProduct(msg);
			if (init) {
				initNextProduct(idx);
			}
		}
	});
}

function refreshProduct(msg) {
	var parts = msg.split("&");
	var params = new Array();
	for (var i=0; i<parts.length; i++) {
		var val = parts[i].split("=")[1];
		if (val == parseInt(val, 10).toString()) val = parseInt(val, 10);
		params[parts[i].split("=")[0]] = val;
	}
	var product = params["product"];
	var amt = params["amt"];
	if (amt == '0') {
		amt = 0;
	} else if (amt) {
		updateProduct(product, amt);
	} else {
		var amt = parseInt($("#amount_" + product).val(), 10);
		if (!amt) amt = 0;
	}
	$("#amount_" + product).val(amt);
	var price = params["price"];
	if (price) {
		$("#price_" + product).html(price);
	} else {
		price = 0;
	}
	
	$("td.price").removeClass('active');
	
	$("#price_branding_" + product).html('0,00');
	$("#price_personalization_" + product).html('0,00');
	$("#price_accessories_" + product).html('0,00');
	
	var fullprice = floatVal(price);
	
	var checked = false;
			
	for (var i in params) {
		var name = i.toString();
		if (name.indexOf("_") > 0) {		
			var radio = $("#extra_" + product + "_" + name);
			var radiobut = $("#radiobut_extra_" + product + "_" + name);
			var info = $("#info_" + product + "_" + name);
			var label = $("#label_" + product + "_" + name);
			if (params[i]) {
				radio.attr("disabled", false);
				radiobut.toggleClass("radio_disabled", false);
				info.toggleClass("disabled", false);
				label.toggleClass("disabled", false);
			} else {
				radio.attr("disabled", 'disabled');
				radio.attr("checked", false);
				radiobut.toggleClass("radio_disabled", true);
				radiobut.toggleClass("ractive", false);
				changeRadio(radiobut.get(0));
				info.toggleClass("disabled", true);
				label.toggleClass("disabled", true);
			}
			if (radio.attr("checked")) {
				var span = $("#price_" + name.split("_")[0] + "_" + product);
				span.parent('td').addClass('active');
				checked = true;
				span.html(params["price_" + name]);
				if (params["price_" + name]) fullprice += floatVal(params["price_" + name]);
			}
		}
	}
	if (checked) $('#fullprice_' + product).parent().addClass('active');
	var fullamt = fullprice * amt;
	fullamt = Math.round(fullamt * 100) / 100;
	$("#fullprice_" + product).html(currency(fullamt));
}

function floatVal(str) {
	if (!str) return '0';
	str = str.split(",").join(".");
	return parseFloat(str);
}

function currency(val) {
	var str = val.toString();
	var parts = str.split(".");
	var bc = parts[0];
	if (parts.length > 1) {
		var ac = parts[1].substr(0, 2);
		while (ac.length < 2) {
			ac += "0";
		}
	} else {
		var ac = '00';
	}
	if (bc.length > 3) {
		bc = bc.substr(0, bc.length - 3) + "." + bc.substr(bc.length - 3);
	}
	return bc + "," + ac;
}