﻿function changeTshirtimage(rowID) {
    var imgObj = $("#tshirt_img_" + rowID);
    var zoomObj = $("#tshirt_zoom_" + rowID);
    var thisObj = $("#tshirt_theme_" + rowID);
    var imgSrc = imgObj.attr("src");
    var imgSplit = imgSrc.split("/");
    var imgName = imgSplit[imgSplit.length - 1];
    var imgUrl = imgSrc.replace(imgName, "") + thisObj.val() + ".jpg";
    imgObj.attr("src", imgUrl);
    zoomObj.attr("href", imgUrl.replace("/products/", "/products/large/"));
    imgObj.after("<div class='loadingimgtshirt'></div>");

    $("#tshirt_img_" + rowID).load(function() {
        setTimeout(function() {
            imgObj.parents("td:first").find(".loadingimgtshirt").remove();
        }, 500);

    });
}
function changeChaiMugimage(rowID) {
    var imgObj = $("#chaimug_img_" + rowID);
    var zoomObj = $("#chaimug_zoom_" + rowID);
    var thisObj = $("#chaimug_theme_" + rowID);
    var imgSrc = imgObj.attr("src");
    var imgSplit = imgSrc.split("/");
    var imgName = imgSplit[imgSplit.length - 1];
    var imgUrl = imgSrc.replace(imgName, "") + thisObj.val() + ".jpg";
    imgObj.attr("src", imgUrl);
    zoomObj.attr("href", imgUrl.replace("/products/", "/products/large/"));
    imgObj.after("<div class='loadingimgchaimug'></div>");
    $("#chaimug_img_" + rowID).load(function() {
        setTimeout(function() {
            imgObj.parents("td:first").find(".loadingimgchaimug").remove();
        }, 500);

    });
}
function changeCoffeMugimage(rowID) {
    var imgObj = $("#coffeemug_img_" + rowID);
    var zoomObj = $("#coffeemug_zoom_" + rowID);
    var thisObj = $("#coffeemug_theme_" + rowID);
    var imgSrc = imgObj.attr("src");
    var imgSplit = imgSrc.split("/");
    var imgName = imgSplit[imgSplit.length - 1];
    var imgUrl = imgSrc.replace(imgName, "") + thisObj.val() + ".jpg";
    imgObj.attr("src", imgUrl);
    zoomObj.attr("href", imgUrl.replace("/products/", "/products/large/"));
    imgObj.after("<div class='loadingimgcoffeemug'></div>");
    $("#coffeemug_img_" + rowID).load(function() {
        setTimeout(function() {
            imgObj.parents("td:first").find(".loadingimgcoffeemug").remove();
        }, 500);

    });
}
function changeBagtagimage(rowID) {
    var imgObj = $("#bagtag_img_" + rowID);
    var zoomObj = $("#bagtag_zoom_" + rowID);
    var thisObj = $("#bagtag_theme_" + rowID);
    var imgSrc = imgObj.attr("src");
    var imgSplit = imgSrc.split("/");
    var imgName = imgSplit[imgSplit.length - 1];
    var imgUrl = imgSrc.replace(imgName, "") + thisObj.val() + ".jpg";
    imgObj.attr("src", imgUrl);
    zoomObj.attr("href", imgUrl.replace("/products/", "/products/large/"));
    imgObj.after("<div class='loadingimgbagtag'></div>");
    if ($('#bagtag_theme_' + rowID + ' option:selected').text() == 'Rectangle') {
        $("#bagtag_rectangle_" + rowID).show();
        $("#bagtag_rounded_" + rowID).hide();
    }
    else {
        $("#bagtag_rectangle_" + rowID).hide();
        $("#bagtag_rounded_" + rowID).show();
    }
    $("#bagtag_img_" + rowID).load(function() {
        setTimeout(function() {
            imgObj.parents("td:first").find(".loadingimgbagtag").remove();
        }, 500);

    });
}

$(document).ready(function() {   
    var _url = document.location.href.toLowerCase();
    var $selectedLI = null;
    var _currentIndex = 0;

    if (_url.indexOf("diwali") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navdiwali');
    }
    if (_url.indexOf("birthday") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navbirthday');
    }
    if (_url.indexOf("wedding") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navwedding');
    }
    if (_url.indexOf("vacation") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navvacation');
    }
    if (_url.indexOf("baby") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navbaby');
    }
    if (_url.indexOf("festivals") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navfestivals');
    }
    if (_url.indexOf("love") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navlove');
    }
    if (_url.indexOf("india") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navindia');
    }
    if (_url.indexOf("ilovemom") > 0 || _url.indexOf("mothersday") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navmothersday');
    }
    if (_url.indexOf("ilovedad") > 0 || _url.indexOf("fathersday") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#navfathersday');
    }
    if (_url.indexOf("rakhi") > 0 || _url.indexOf("worldcup") > 0 || _url.indexOf("occasions") > 0) {
        $selectedLI = $('.occasion-options-gallery ul').find('#wc2010');
    }


    if ($selectedLI != null) {
        $selectedLI.find('.over').show();
    }

    $('.occasion-options-gallery ul li').hover(function() {
        $(this).find('.over').show();
        if ($(this).attr("id") != $selectedLI.attr("id"))
            $selectedLI.find('.over').hide();

    }, function() {
        if ($(this).attr("id") != $selectedLI.attr("id")) {
            $(this).find('.over').hide();
            $selectedLI.find('.over').show();
        }

    });

    $('.occasion-options-gallery ul li a').click(function() {
        return false;
    });
    $('.occasion-options-gallery ul li img').click(function() {
        window.location = $(this).parents('li').find('a').attr('href');
    });

    // currency change raising the currencyChange for reloading the page
    $(CURRENCY).bind('currencyChange', function(args, data) {
        location.reload();
    });
    if ($("#love").length > 0 || $("#mothersday").length > 0 || $("#fathersday").length > 0 || $("#wc2010").length > 0) {
        xOffset = 10;
        yOffset = 30;
        $(".tabWrapper #coffee img, .tabWrapper #chai img, .tabWrapper #poster img, .tabWrapper #travel img,.tabWrapper #tshirts img,.tabWrapper #bagtag img").hover(function(e) {
            this.t = this.title;
            this.title = "";
            var imgUrl = $(this).attr("src").replace("/products/", "/products/large/").replace("/chai/", "/coffee/");
            var c = (this.t != "") ? "<br/>" + this.t : "";
            $("body").append("<p id='screenshot'><img src='" + imgUrl + "' alt='loading...' />" + c + "</p>");
            $("#screenshot").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").css("z-index", "1350").fadeIn("fast");
        }, function() {
            this.title = this.t;
            $("#screenshot").remove();
        });
        $("a.screenshot").mousemove(function(e) {
            $("#screenshot").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px");
        });
    }
    //Exclusively for Wc 2010
    if ($("#wc2010").length > 0) {
        var zoomBoxy = new Boxy($("#ZoomView"), { title: " ", modal: true, width: "600px", height: "300px", fixed: false, unloadOnHide: false, show: false, closeText: "X" });
        var zoomBoxytee = new Boxy($("#ZoomViewtee"), { title: " ", modal: true, width: "110%", height: "110%", fixed: false, unloadOnHide: false, show: false, closeText: "X" });
        $("#coffee .zoom").click(function(e) {
            $("#ZoomViewLoading").css({
                opacity: 0.8
            });
            $("#ZoomViewLoading").show();
            e.preventDefault();
            $("#img_zoomed").attr("src", $(this).attr("href"));
            $("#img_zoomed").load(function() {
                $("#ZoomViewLoading").hide();
            });
            zoomBoxy.center();
            zoomBoxy.show();
        });
        $("#chai .zoom").click(function(e) {
            $("#ZoomViewLoading").css({
                opacity: 0.8
            });
            $("#ZoomViewLoading").show();
            e.preventDefault();
            $("#img_zoomed").attr("src", $(this).attr("href"));
            $("#img_zoomed").load(function() {
                $("#ZoomViewLoading").hide();
            });
            zoomBoxy.center();
            zoomBoxy.show();
        });
        $("#tshirts .zoom").click(function(e) {
            $("#ZoomViewLoadingtee").css({
                opacity: 0.8
            });
            $("#ZoomViewLoadingtee").show();
            e.preventDefault();
            $("#img_zoomedtee").attr("src", $(this).attr("href"));
            $("#img_zoomedtee").load(function() {
                $("#ZoomViewLoadingtee").hide();
            });
            zoomBoxytee.center();
            zoomBoxytee.show();
        });
        $("#bagtag .zoom").click(function(e) {
            $("#ZoomViewLoadingtee").css({
                opacity: 0.8
            });
            $("#ZoomViewLoadingtee").show();
            e.preventDefault();
            $("#img_zoomedtee").attr("src", $(this).attr("href"));
            $('#img_zoomedtee').load(function() {
                $("#ZoomViewLoadingtee").hide();
            });
            zoomBoxytee.center();
            zoomBoxytee.show();
        });
        $('.tabWrapper #coffee .btn-silver').bind("click", function() {
            var themeidval;
            var themename;
            var premiumtypecode;
            premiumtypecode = 524;
            var id = this.id.replace('coffeemug_btn_', '');

            themeidval = "Coffemug" + "-" + $("#coffeemug_theme_" + id).val();
            themename = "Coffee Mug" + "-" + $("#coffeemug_theme_" + id).attr("title") + "-" + $('#coffeemug_theme_' + id + ' option:selected').text();
            var postData = {
                themeid: themeidval,
                Premiumtypecode: premiumtypecode,
                Themename: themename
            };
            console.log(postData);
            $("#loadingajax").css({
                opacity: 0.8,
                height: $("#main-content").height()
            });
            $("#loadingajax").show();
            $.ajax({
                type: "POST",
                url: "WcCart",
                data: postData,
                cache: false,
                dataType: "json",
                success: function(data) {
                    if (data.result == 0) {
                        window.location.pathname = data.Url;
                    }
                }
            });
        });

        $('.tabWrapper #chai .btn-silver').bind("click", function() {
            var themeidval;
            var themename;
            var premiumtypecode;
            premiumtypecode = 525;
            var id = this.id.replace('chaimug_btn_', '');
            themeidval = "Chaimug" + "-" + $("#chaimug_theme_" + id).val();
            themename = "Chai Mug" + "-" + $("#chaimug_theme_" + id).attr("title") + "-" + $('#chaimug_theme_' + id + ' option:selected').text();
            var postData = {
                themeid: themeidval,
                Premiumtypecode: premiumtypecode,
                Themename: themename
            };
            console.log(postData);
            $("#loadingajax").css({
                opacity: 0.8,
                height: $("#main-content").height()
            });
            $("#loadingajax").show();
            $.ajax({
                type: "POST",
                url: "WcCart",
                data: postData,
                cache: false,
                dataType: "json",
                success: function(data) {
                    if (data.result == 0) {
                        window.location.pathname = data.Url;
                    }
                }
            });
        });
        $('.tabWrapper #tshirts .btn-silver').bind("click", function() {
            var themeidval;
            var themename;
            var premiumtypecode;
            var id = this.id.replace('tshirt_btn_', '');
            premiumtypecode = $("#tshirt_size_" + id).val()
            themeidval = "Tshirt" + "-" + $("#tshirt_theme_" + id).val();
            themename = "Tshirt" + "-" + $("#tshirt_theme_" + id).attr("title") + "-" + $('#tshirt_theme_' + id + ' option:selected').text() + "(" + $('#tshirt_size_' + id + ' option:selected').text() + ")";
            var postData = {
                themeid: themeidval,
                Premiumtypecode: premiumtypecode,
                Themename: themename
            };
            console.log(postData);
            $("#loadingajax").css({
                opacity: 0.8,
                height: $("#main-content").height()
            });
            $("#loadingajax").show();
            $.ajax({
                type: "POST",
                url: "WcCart",
                data: postData,
                cache: false,
                dataType: "json",
                success: function(data) {
                    if (data.result == 0) {
                        window.location.pathname = data.Url;
                    }
                }
            });
        });

        $('.tabWrapper  #bagtag .btn-silver').bind("click", function() {
            var themeidval;
            var themename;
            var premiumtypecode;
            var id = this.id.replace('bagtag_btn_', '');
            if ($('#bagtag_theme_' + id + ' option:selected').text() == 'Rectangle') {
                premiumtypecode = 510;
            }
            else {
                premiumtypecode = 511
            }
            themeidval = "Bagtag" + "-" + $("#bagtag_theme_" + id).val();
            themename = "Bag Tag" + "-" + $("#bagtag_theme_" + id).attr("title") + "-" + $('#bagtag_theme_' + id + ' option:selected').text();
            var postData = {
                themeid: themeidval,
                Premiumtypecode: premiumtypecode,
                Themename: themename
            };
            console.log(postData);
            $("#loadingajax").css({
                opacity: 0.8,
                height: $("#main-content").height()
            });
            $("#loadingajax").show();
            $.ajax({
                type: "POST",
                url: "WcCart",
                data: postData,
                cache: false,
                dataType: "json",
                success: function(data) {
                    if (data.result == 0) {
                        window.location.pathname = data.Url;
                    }
                }
            });
        });


    }

});