$(document).ready(function() {

    /* This is basic - uses default settings */

    $("a#single_image").fancybox();

    /* Using custom settings */

    $("a#inline").fancybox({
        'hideOnContentClick': true
    });

    /* Apply fancybox to multiple items */

    $("a.group").fancybox({
        'transitionIn'    :    'elastic',
        'transitionOut'   :    'elastic',
        'speedIn'         :    600,
        'speedOut'        :    200,
        'overlayShow'     :    false
    });

});

jQuery(document).ready(function () {
	var content = jQuery("#content");
	var height = jQuery(window).height() - 154 - 157 - parseInt(content.css('paddingBottom'));
	if (content.height() < height)
		content.height(height);
});

jQuery(document).ready(function () {
    $(".btt").click(function () {
        $("html").scrollTop(0);
    });
});

function reply_to(id) {
    var form = $("form[name=comments]");
    $('html, body').animate({
        scrollTop: form.offset().top},500);
    form.find("input[name=reply_to]").val(id);
    form.find("input")[0].focus();
}

function GetCities(county, elem_id, set_tax) {
    var name = '';
    switch (elem_id) {
        case 'da_city' : name = 'city_id';
                        break;
        case 'pj_city' : name = 'city_id';
                        break;
        case 'pf_city' :
        default        : name = 'city_id';
    }

    $('#'+elem_id).load("index.php?__ajax=AjaxCities", {county_id : county, s_name : name, set_tax : set_tax}, function() {
        $("#"+elem_id+" select")[0].focus();
        SetTaxForBv($("#"+elem_id+" select").val());
    });
}

function ChangePerson(type) {

    if (type == 'pf') {
        $("#p_select_holder div").removeClass("selected");
        $($("#p_select_holder div")[0]).addClass("selected");
        $("#pf").css("display", "block");
        $("#pj").css("display", "none");
        $("input[name='person']").val('f');
    }
    else {
        $("#p_select_holder div").removeClass("selected");
        $($("#p_select_holder div")[1]).addClass("selected");
        $("#pf").css("display", "none");
        $("#pj").css("display", "block");
        $("input[name='person']").val('j');
    }
}

function LoginCheckout() {
    var mail = $($('#check_log input[name="l_email"]')[0]).val();
    var pass = $($('#check_log input[name="l_pass"]')[0]).val();

    var error = "";
    if (mail.length == 0 || pass.length == 0) {
        $("#check_log div.errors").css({display: 'block'});
    }
    else {
        $("#loader").css({display: "block"});
        $.get("index.php?__ajax=AjaxCheckoutLogin", { "email": mail, "pass": pass },
                function(data){
                    $("#user_details").html(data.output);
                    if (data.logged > 0)
                        $(".get_the_points span").html(data.points_text);
                    if (data.discount > 0) {
                        $(".p_discount_holder").removeClass("hidden");
                        var replace = $(".p_discount_holder .pds");
                        $(replace[0]).html(data.discount);
                        $(replace[1]).html(data.points);
                    }
                }, "json");
    }

    return false;
}

jQuery(document).ready(function() {
    $("div[rel='stock_desc']").hover(function () {
        var div = $("#stock_" + (this.id.substring(1)));
        if (parseInt(div.css('top')) == 0) {
            var new_pos = {top: 0, left: 0};
            new_pos.top = -div.height();
            new_pos.left = -(div.innerWidth() - $(this).innerWidth())/2;
            div.css({top: new_pos.top+"px", left: new_pos.left+"px"});
        }
        div.css({display: "block"});
    }, function () {
        $("#stock_" + (this.id.substring(1))).css({display: "none"});
    });

    var timerId = 0;
    $("a[name='atc_list']").click(function () {
        var pid = this.id.substr(1);
        var div = $("#atc"+ pid);
        var loading = $(div.find(".loading")[0]);
        var btn_holder = $(this).parent().parent().parent();
        div.css({
            top: "auto",
            bottom: function () {
                return (parseInt(btn_holder.css('bottom')) + btn_holder.innerHeight()) +"px";
            },
            left: function () {
                return (div.parent().innerWidth() - div.innerWidth())/2 + "px";
            }
        });
        div.slideDown();
        loading.width(loading.parent().width());
        loading.height(loading.parent().height());
        $("#cart_left").load("index.php?__ajax=AjaxCartAdd", {pid : pid, qty : 1}, function() {
            $(this).fadeOut('fast').fadeIn('fast');
            loading.css({display: "none"});
            timerId = window.setTimeout(function () {
                if (div.css("display") == "block")
                    div.slideUp();
                window.clearTimeout(timerId);
            }, 3000);
        });

        $("a.the_close").click(function () {
            $(this).parent().parent().slideUp();
            window.clearTimeout(timerId);
        });

        return false;
    });
});

var t = 0;
jQuery(document).ready(function () {
    if ($("#slideshow")[0]) {
        $("#slide_btns a").click(function () {
            t = window.clearInterval(t);
            GoToSlide($(this).index());
            t = window.setInterval(function () {
                GoToSlide($("#slideshow .active").index()+1);
            }, 10000);
        });

        t = window.setInterval(function () {
            GoToSlide($("#slideshow .active").index()+1);
        }, 10000);
    }
});

function GoToSlide(i) {
    var total = $("#slideshow .slide").length;
    var selected = $("#slideshow .active").index();
    if (i == selected)
        return ;

    if (i == total)
        i = 0;
    $("#slideshow .slide").css({zIndex: 1});
    $($("#slideshow .slide")[i]).css({zIndex: 99}).slideDown(500, function () {
        $($("#slideshow .slide")[selected]).removeClass("active").css({display: "none"});
        $(this).addClass("active");
    });
    $($("#slide_btns").css({backgroundPosition: -(i*80) +"px"}));
}

$(document).ready(function () {
    if ($(".hp_plist")[0]) {
        $(".hp_plist").width(function () {
            var total = $(this).children().length - 1;
            return total * $($(this).children()[0]).outerWidth(true);
        });
    }
});

function ScrollLeft(id) {
    var left = parseInt($("#"+id).css('left'));
    if (left >= 0)
        return null;
    else
        $("#"+id).animate({left: '+=752'}, 'swing');
}
function ScrollRight(id) {
    var left = parseInt($("#"+id).css('left'));
    if (left <= -($("#"+id).width()) + 752)
        return null;
    else
        $("#"+id).animate({left: '-=752'}, 'swing');
}

jQuery(document).ready(function () {
    if ($("#give_rating")[0]) {
        $("#give_rating span").css({cursor: "pointer"});
        var index = 0;
        $("#give_rating span").hover(function () {
                index = $(this).index();
                for (var i = 0; i < 5; i++)
                    if (i <= index)
                        $($("#give_rating span")[i]).removeClass('negative');
                    else
                        $($("#give_rating span")[i]).addClass('negative');
            },
            function () {
                index = $("#product_rating").val();
                for (var i = 0; i < 5; i++)
                    if (i < index)
                        $($("#give_rating span")[i]).removeClass('negative');
                    else
                        $($("#give_rating span")[i]).addClass('negative');
            });

        $("#give_rating span").click(function () {
            var rating = $(this).index() + 1;
            $("#product_rating").val(rating);
        });
    }
});

jQuery(document).ready(function () {
    if ($("#sorter")[0]) {
        $("#sorter").change(function () {
            document.location.href = this.value;
        });
    }
});

jQuery(document).ready(function() {
    if ($(".pl_filters")[0]) {
        if ($(".pl_left").height() < $(".pl_filters").height())
            $(".pl_left").height($(".pl_filters").height());
    }
});

function ChangeTab(id, btn) {
    if (id != $("#tab_contents .pp_desc_item").filter(":visible").attr("id")) {
        $("#tab_contents .pp_desc_item").hide();
        $("#"+id).show();
        $("#tabs a").removeClass("active");
        $(btn).addClass("active");
    }
}
