﻿$(document).ready(function () {

    $(".discountLink a").click(function () {
        $("div.checkoutStepBoxDiscount").toggle();
        return false;
    });

    $(".noSpace").each(function () {
        $(this).html(($(this).html()).replace(" ", ""));
    });

    $('#slider').nivoSlider({
        effect: 'fade',
        pauseOnHover: true,
        pauseTime: 4000
    });

    $('div.productImage .imagePreview a').lightBox();
    $('div.productAttributes .imagePreview a').lightBox();    
    $('div.productImage #gallery a').click(function () {
        $('div.productImage .imagePreview img').attr('src', $(this).attr("href"));
        $('div.productImage .imagePreview a').attr('href', $(this).attr("name"));
        return false;
    });

    //facebook like button
    var currentUrl = document.location.href;
    var strFbLike = "<fb:like href='" + currentUrl + "' show_faces='true' width='350' action='recommend' font='arial'></fb:like>";
    $(".fbLike").html(strFbLike);

    //facebook comment box
    var strFbComment = "<div id='fb-root'></div><fb:comments href='" + currentUrl + "' num_posts='100' width='500'></fb:comments>";
    $(".fbComment").html(strFbComment);

    //blog link to product
    // $(".productPage .productBlog textarea").html("<a href='" + currentUrl + "' target='_blank'>Tilda garn från Garnaffären.se</a>");

    $("div.productNav div.item:eq(0)").hover(function () {
        highlightProductButton(0);
        $("div.productNav div.productBox:eq(0)").show();
    }, function () {
        deHighlightProductButton(0)
        $("div.productNav div.productBox:eq(0)").hide();
    });

    $("div.productNav div.productBox:eq(0)").hover(function () {
        highlightProductButton(0);
        $(this).show();
    }, function () {
        deHighlightProductButton(0)
        $(this).hide();
    });

    $("div.productNav div.item:eq(1)").hover(function () {
        highlightProductButton(1);
        $("div.productNav div.productBox:eq(1)").show();
    }, function () {
        deHighlightProductButton(1)
        $("div.productNav div.productBox:eq(1)").hide();
    });

    $("div.productNav div.productBox:eq(1)").hover(function () {
        highlightProductButton(1);
        $(this).show();
    }, function () {
        deHighlightProductButton(1)
        $(this).hide();
    });

    $("div.shoppingCart div.viewCart").hover(function () {
        $(this).addClass("viewCartHover");
    }, function () {
        $(this).removeClass("viewCartHover");
    });

    $("div.shoppingCart div.viewCart").click(function () {
        if ($("div.cartItems").is(":visible")) {
            $("div.cartItems").hide("fast");
            $(this).html("+ Visa kundvagn");
        }
        else {
            $("div.cartItems").show("fast");
            $(this).html("- Dölj kundvagn");
        }
    });

    $(".productBlog a.blogLink").click(function () {
        $("div.productBlog div.productBlogDescription").toggle();
        return false;
    });

    // Load dialog on click
    $('.modal_forgotPsw').click(function (e) {
        $('#modal_forgotPsw').modal();
        return false;
    });

    $('.modal_login').click(function (e) {
        $('#modal_login').modal();
        return false;
    });

    $('.modal_blog').click(function (e) {
        $('#modal_blog').modal();
        return false;
    });

    $('.modal_banner').click(function (e) {
        $('#modal_banner').modal();
        return false;
    });

    $('.modal_recommendation').click(function (e) {
        $('#modal_recommendation').modal();
        return false;
    });

    $('.modal_campain').click(function (e) {
        $('#modal_campain').modal();
        return false;
    });

    $('.modal_cart').click(function (e) {
        $('#modal_cart').modal();
        return false;
    });

    $('.modal_productImage').click(function (e) {
        $('#modal_productImage').modal();
        $('#modal_productImage').parents("#simplemodal-container").addClass("simplemodal-container-productImage");
        return false;
    });

    // textbox
    $(".textbox").focus(function () {
        $(this).css("background-color", "#ffffff");
    }).blur(function () {
        $(this).css("background-color", "#eeeeee");
    });

    $("div.box.newsletter .textbox").focus(function () {
        if ($(this).val() == "Din e-postadress") {
            $(this).val("");
            $(this).addClass("resetableActive");
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val("Din e-postadress");
            $(this).removeClass("resetableActive");
        }
    });

    // side menu highlight  

    var currentUrl = document.location.href;
    $("div.sideNav div a").each(function () {

        if ((currentUrl.indexOf($(this).attr("href"))) != -1) {
            if ($(this).parent("div").hasClass("title")) {
                if ($(this).parent("div").hasClass("titleEaster")) {
                    $(this).css({ "background-image": "url(/images/sideNav_title_hover_easter.png)", "color": "#ffffff" });
                    $(".CatalogTitle").css("background-image", "url(/images/catalogTitle_bkgrd_easter.png)");
                }
                else {
                    $(this).css({ "background-image": "url(/images/siteNav_title_hover.png)", "color": "#ffffff" });
                }
            }
            else {
                $(this).css({ "background-color": "#cc0000", "color": "#ffffff" });
                $(this).parents("li").closest("li.title").css("border", "10px solid red");
                $(this).closest("li.title").css("border", "10px solid red");
            }
        }
    });

    var currentUrl = document.location.href;
    $("div.sideNavB div a").each(function () {

        if ((currentUrl.indexOf($(this).attr("href"))) != -1) {
            if ($(this).parent("div").hasClass("title")) {
                $(this).css({ "background": "#990000 url(/Images/nav_arrow_open_hover.gif) no-repeat", "color": "#ffffff" });
            }
            else {
                $(this).css({ "background-color": "#cc0000", "color": "#ffffff" });
                $(this).parents("li").closest("li.title").css("border", "10px solid red");
                $(this).closest("li.title").css("border", "10px solid red");
            }
        }
    });

    $("div.sideNavB.noSubs div a").each(function () {

        if ((currentUrl.indexOf($(this).attr("href"))) != -1) {
            if ($(this).parent("div").hasClass("title")) {
                $(this).css({ "background": "#990000 url()", "color": "#ffffff" });
            }
            else {
                $(this).css({ "background-color": "#cc0000", "color": "#ffffff" });
                $(this).parents("li").closest("li.title").css("border", "10px solid red");
                $(this).closest("li.title").css("border", "10px solid red");
            }
        }
    });

    // search form - input labels
    $(".searchtextbox").focus(function () {
        if ($(this).val() == "Skriv in produkt här...") {
            $(this).val("");
            $(this).css({ "color": "#000000" });
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val("Skriv in produkt här...");
            $(this).css({ "color": "#999999" });
        }
    });

    /* partner logos hover */

    //facebook
    $(".mgMouseOver").mouseover(function () {
        var src = $(this).attr("src");
        var dotIndex = src.indexOf('.');
        var src1 = src.substr(0, (dotIndex));
        var src2 = src.substr(dotIndex, 4);
        $(this).attr("src", src1 + "_hover" + src2);

    }).mouseout(function () {
        var src = $(this).attr("src");
        var hoverIndex = src.indexOf('_hover');
        var src1 = src.substr(0, (hoverIndex));
        var src2 = src.substr((hoverIndex + 6), 4);
        $(this).attr("src", src1 + src2);
    });

    $("div.featuredProducts div.thumbs div.thumb").click(function () {
        checkFeatProduct($(this).index("div.featuredProducts div.thumbs div.thumb"));
    });



    function highlightProductButton(e) {
        $("div.productNav div.item:eq(" + e + ")").css({ "background-color": "#333", "color": "#fff" });
    }

    function deHighlightProductButton(e) {
        $("div.productNav div.item:eq(" + e + ")").css({ "background-color": "transparent", "color": "#000" });
    }


    /* featured products */
    var currentImg = 0;
    var isOnLoad = true;
    var totalImages = $("div.featuredProducts div.thumbs div.thumb").size();
    var lastInterval = setInterval(function () { rotateImage(nextImg()); }, 10000);
    $("div.featuredProducts div.thumbs div.thumb").removeClass("active");
    $("div.featuredProducts div.thumbs div.thumb:eq(0)").addClass("active");

    $("div.featuredProducts div.thumbs div.thumb:eq(0)").click(function () {
        clickImage(0);
        currentImg = 0;
        clearInterval(lastInterval);
        lastInterval = setInterval(function () { rotateImage(nextImg()); }, 10000);
    });

    $("div.featuredProducts div.thumbs div.thumb:eq(1)").click(function () {
        clickImage(1);
        currentImg = 1;
        clearInterval(lastInterval);
        lastInterval = setInterval(function () { rotateImage(nextImg()); }, 10000);

    });

    $("div.featuredProducts div.thumbs div.thumb:eq(2)").click(function () {
        clickImage(2);
        currentImg = 2;
        clearInterval(lastInterval);
        lastInterval = setInterval(function () { rotateImage(nextImg()); }, 10000);

    });


    function checkFeatProduct(i) {
        $("div.featuredProducts div.bigImages img.active").hide();
        $("div.featuredProducts div.bigImages img.active").removeClass("active");
        $("div.featuredProducts div.bigImages img:eq(" + i + ")").show();
        $("div.featuredProducts div.bigImages img:eq(" + i + ")").addClass("active");

        $("div.featuredProducts div.thumbs div.active").removeClass("active");
        $("div.featuredProducts div.thumbs div.thumb:eq(" + i + ")").addClass("active");

        $("div.featuredProducts div.productInfos div.active").removeClass("active");
        $("div.featuredProducts div.productInfos div.productInfo:eq(" + i + ")").addClass("active");
    }

    function nextImg() {
        if (isOnLoad == true) {
            isOnLoad = false;
            currentImg = currentImg + 1;
            return currentImg;
        } else {

            if ((currentImg + 1) == (totalImages)) {
                currentImg = 0;
                return 0;
            } else {
                currentImg = currentImg + 1;
                return currentImg;
            }
        }
    }

    function rotateImage(nextImg) {
        if (nextImg == 0) {
            $("div.featuredProducts div.bigImages img:eq(" + (totalImages - 1) + ")").hide();
        } else {
            $("div.featuredProducts div.bigImages img:eq(" + (nextImg - 1) + ")").hide();
        }
        $("div.featuredProducts div.bigImages img:eq(" + nextImg + ")").fadeIn(1100);
        $("div.featuredProducts div.thumbs div.thumb").removeClass("active");
        $("div.featuredProducts div.thumbs div.thumb:eq(" + nextImg + ")").addClass("active");

        $("div.featuredProducts div.productInfos div.active").removeClass("active");
        $("div.featuredProducts div.productInfos div.productInfo:eq(" + nextImg + ")").addClass("active");

    }

    function clickImage(whichImg) {
        $("div.featuredProducts div.thumbs div.thumb").removeClass("active");
        $("div.featuredProducts div.thumbs div.thumb:eq(" + whichImg + ")").addClass("active");
        $("div.featuredProducts div.bigImages img").hide();
        $("div.featuredProducts div.bigImages img:eq(" + whichImg + ")").show();
    }

    $('img.cartUpdatedAlert').click(function () {
        $(this).hide();
    });

    /* product alert */
    $(".productBoxTop").append("<a href='" + $(".productBoxTop a:eq(1)").attr("href") + "'><img src='/images/productAlert_top_small.png' alt='' class='productAlert' /></a>");
    $(".saleProduct").append("<a href='" + $(".productBoxTop a:eq(1)").attr("href") + "'><img src='/images/productSale_top_small.png' alt='' class='productAlert' /></a>");
    /*$("div.productImgWrap img").each(function () {
    var imgMargin = ((150) - ($(this).height()));
    imgMargin = imgMargin / 2;
    $(this).css("margin-top", imgMargin + "px");
    });*/

});


function cartUpdated() {
    var e = $('img.cartUpdatedAlert');
    e.fadeIn('slow');
    e.queue(function () {
        setTimeout(function () {
            e.dequeue();
        }, 3000);
    });
    e.fadeOut('fast');
}

this.imagePreview = function () {
    /* CONFIG */

    xOffset = 10;
    yOffset = 30;

    /* END CONFIG */
    $("a.preview").hover(function (e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='" + this.href + "' alt='Laddar bild...' />" + c + "</p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function () {
	    this.title = this.t;
	    $("#preview").remove();
	});
    $("a.preview").mousemove(function (e) {
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });
};

