/******************************* splash screen ********************************/
$(function () {
    var images = {
	    0 : { src : 'samsonite-b-lite.jpg', link : 'samsonite/blite/' },
	    1 : { src : 'samsonite-velocita.jpg', link : 'samsonite/velocita/' },
	       };

    function changeImage(i) {
	$("div.splash a").attr("href", images[i].link);
	$("div.splash img").fadeOut("normal", function () {
	    $(this).attr("src", "skins/default/images/splash/" + images[i].src).fadeIn("normal");
    	});
    };

    var i = 0;
    window.setInterval(function () {
	if (typeof images[i] == "undefined") i = 0;
	changeImage(i);
	i++;
    }, 5000);
});

/****************************** product ordering ******************************/
$(function (){
    $("#orderby").change(function () {
	$.get("products?plugin=eshop&action=filter&order=" + $(this).val(), function (data, textStatus) {
	    // refresh
	    window.location.href = window.location.href;
	});
    });
});

/******************************* product photos *******************************/
$(function () {
    var product = $("#product-url").text();
    if (typeof product == "undefined") return;

    var color = parseInt($.parseQuery().color);
    if (isNaN(color)) color = 0;

    $("#photos img").each(function (i) {
	var id = parseInt($(this).attr("id").substr(("photo-").length));
    });

    $.getJSON("index?plugin=eshop&action=colors&product=" + product + "&color=" + color, function (data, textStatus) {
	if (textStatus != "success") return;

	function createPhoto(id, link) {
	    $("#photo-" + id).css("cursor", "pointer").bind("mouseenter click", function () {
		$("#photo").attr({
		    src   : "index?plugin=eshop&action=photo&name=" + link.photo_url,
		    alt   : link.photo_alt,
		    title : link.photo_description
		});
		photoPopup(link);
	    });
	}

	function photoPopup(link) {
	    var parent = parseInt(link.id_photo_link);
	    if (!isNaN(parent) && parent > 0) {
		$("#photo").css("cursor", "pointer").unbind("click").click(function () {
		    popup("index.php?plugin=eshop&action=photopopup&id=" + parent + "&url=" + product, 710, 640);
		});
	    }
	}

	function findPhoto(id, data) {
	    for (var i in data) {
		if (data[i].id_product_photo == id) {
		    return i;
		}
	    }
	    return false;
	}

	var link;
	for (var i in data) {
	    if (data[i].position != "preview") continue;
	    if (data[i].id_photo_link == null) continue;

	    link = findPhoto(data[i].id_photo_link, data);
	    if (link) createPhoto(data[i].id_product_photo, data[link]);
	}
    });
});

/****************************** dirty fixes *********************************/
/*$(function () {
    $("div.item:nth-child(2n+2) div.pad > div.info").each(function (i) {
	var prev = $(this).parents("div.item").prev("div.item").find("div.info");
        var next = $(this).parents("div.item").next("div.item").find("div.info");

        var newHeight = Math.max(prev.height(), $(this).height(), next.height());

        if (newHeight >= 75) {
            prev.height(newHeight);
            $(this).height(newHeight);
            next.height(newHeight);
        }
    });
});*/

