﻿var firstRun = true;
$(function () {
    $("img").live("contextmenu", function (e) {
        return false;
    });

    $(".service-description .Detail img.arrow").click(function () {
        var des = $(this).parents(".description");
        var content = $(this).parents(".description").find(".content");

        if (des.height() < 770) {
            $(content).animate({
                "height": "700"
            }, "slow");
            $(des).animate({
                "height": "770"
            }, "slow");
            $(this).attr("src", "/Themes/Tanha/Images/arrow-up.png");
        } else {
            $(des).animate({
                "height": "230"
            }, "slow");
            $(content).animate({
                "height": "195"
            }, "slow");
            $(this).attr("src", "/Themes/Tanha/Images/arrow-down.png");
        }

    });

    // initialize top navigation menu
    $("ul#top-menu").superfish({
        delay: 200,
        animation: { "marginTop": "-5px", "opacity": "show" },
        speed: "fast",
        autoArrows: true,
        onBeforeShow: function () { this.css("marginTop", "20px"); }
    }).find('> li > ul').prepend('<span class="top-arrow"></span>');
    $('ul.nav > li > a.sf-with-ul').parent('li').addClass('sf-ul');


    // initialize change google map in lien he
    $("#dia-chi #dia-chi-van-phong").click(function () {
        $("#dia-chi #ban-do").html('<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?msa=0&amp;msid=201869782236590847444.00043accc292c42cc61cc&amp;ie=UTF8&amp;t=m&amp;vpsrc=6&amp;ll=21.057749,105.885458&amp;spn=0.014017,0.018239&amp;z=15&amp;iwloc=0004ae56d014a555943be&amp;output=embed"></iframe>');
    });

    $("#dia-chi #dia-chi-nha-may").click(function () {
        $("#dia-chi #ban-do").html('<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?msa=0&amp;msid=201869782236590847444.00043accc292c42cc61cc&amp;ie=UTF8&amp;t=m&amp;vpsrc=6&amp;ll=20.984521,106.043472&amp;spn=0.112193,0.145912&amp;z=12&amp;output=embed"></iframe>');
    });

    if ($(".bang-categories-nav").length > 0) {

        var bangNav = $(".bang-categories-nav");
        var bangBread = $(".category .ul-bread").css("font-size", "15px");
        var titleclick = $(".page-title");

        bangNav.find(".sub-cat").tabSwitch("create", { width: 960, height: 170 });

        bangNav.find(".child-cat-level1 li").click(function () {
            bangNav.find(".child-cat-level1 li.current").removeClass("current");
            var idx = bangNav.find(".child-cat-level1 li").index(this);
            var catName = $(this).text();
            bangNav.find(".tensanpham").text(catName);

            $(this).addClass("current");
            bangNav.find(".sub-cat").tabSwitch("moveTo", { index: idx });

            // load product list for this category
            var catId = $(this).attr("rel");
            if (!firstRun) {
                $.ajax({
                    url: '/Home/AjaxProductList/' + catId,
                    type: 'POST',
                    success: function (data) {
                        $(".product-list").replaceWith(data);
                        productItemTip();
                        titleclick.text(catName);
                        bangBread.find("li:nth-child(n)").empty();
                        bangBread.find("li:first").html('<a href="/home/index">Trang chủ</a>');
                        bangBread.find("li:nth-child(2)").text(" » " + catName);
                    }
                });
            } else {
                firstRun = false;
            }
        });


        $.ajax({
            url: '/Home/Lienhe',
            type: 'POST',
            success: function (data) {
                aler('ok');
            }
        });
        if (bangNav.find(".child-cat-level1 li.current").length == 0) {
            bangNav.find(".child-cat-level1 li:first").click();
        } else {
            bangNav.find(".child-cat-level1 li.current").click();
        }
    }

    if ($(".image-box img.main-image").length > 0) {
        $(".image-box ul a").click(function (e) {
            e.preventDefault();
            $(".image-box img.main-image").attr("src", $(this).find("img").attr("src"));
            //$(".image-box img.main-image").ImageSwitch({ Type: "FadeIn", NewImage: $(this).find("img").attr("src") });
            $(".image-box a").attr("href", $(this).find("img").attr("src"));
        });
    }

    //Examples of how to assign the ColorBox event to elements
    $(".group1").colorbox({ rel: 'group1' });
    //    $("a[rel='colorbox']").colorbox()

    if ($("#container #page-header .page-title").length > 0 && $("#container #page-header .page-title").css("font-size").replace("px", "") > 0) {
        var fontSize = parseInt($("#container #page-header .page-title").css("font-size").replace("px", ""));
        var headerBox = $("#container #page-header .page-title");
        while (headerBox.outerHeight() > fontSize + 5) {
            fontSize -= 2;
            headerBox.css("font-size", fontSize);
        }
    }
    $('input.contact').focus();

    productItemTip();
});



var productItemTip = function () {

    //Tool tip with product
    $('.product-item').each(function () {
        var distance = 50;
        var time = 250;
        var hideDelay = 300;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);


        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;

                info.css({
                    top: 190,
                    left: 33,
                    padding: 15,
                    width: 300,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function () {
                    beingShown = false;
                    shown = true;
                });
            }

            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        });


    });

}
