jQuery(document).ready(function() {
    $("a#single_image").fancybox();

    $("a.inline").fancybox({
        'hideOnContentClick': true
    });

    $("a.group").fancybox({
        'transitionIn'    :    'elastic',
        'transitionOut'   :    'elastic',
        'speedIn'         :    600,
        'speedOut'        :    200,
        'overlayShow'     :    false
    });
});

var pos = 0;
jQuery(document).ready(function () {
    window.setInterval("moveImage()",60);
});

function moveImage() {
    var item = jQuery("#header");
//    alert(item.css('backgroundPosition'));
    pos--;
    if (pos < -1040)
        pos = 0;
    item.css({backgroundPosition: pos});
}

jQuery(document).ready(function () {
    if (jQuery('#nav')[0]) {
        jQuery('#nav > li').hover(
            function () {
                if (jQuery(this).children("ul").length > 0) {
                    jQuery(this).addClass('over');
                    var ul = jQuery(this).children("ul")[0];
                    var height = jQuery(ul).height();
                    jQuery(ul).css('display', 'block');
                }
            },
            function () {
                if (jQuery(this).children("ul").length > 0) {
                    var ul = jQuery(this).children("ul")[0];
                    var height = jQuery(ul).height();
                    var li = this;
                    jQuery(ul).css('display', 'none');
                    jQuery(li).removeClass('over');
                    /*jQuery(ul).slideToggle(100, function () {
                        jQuery(li).removeClass('over');
                    });*/
                }
        });
    }
});

function replyTo(id) {

    var form = $("form[name=comments_f]");
    $('html, body').animate({
        scrollTop: form.offset().top},500);
    form.children("input")[0].focus();
    form.children("input[name=reply_to]").val(id);

}

function showDI() {
    var input = $("input[name=ss_q]");
    if ($(input).val() == df_ss_v)
        $(input).val("");

    $(".dis_holder").slideToggle();
}

function hideDI() {
    var input = $("input[name=ss_q]");
    if ($(input).val().length == 0)
        $(input).val(df_ss_v);

    $(".dis_holder").slideToggle();
}

jQuery(document).ready(function () {
    if ($("#dis_list")[0]) {
        if ($("#dis_list").height() < parseInt($("#dis_list").css("maxHeight"))) {
            $("#dis_list").css("overflowY", "none");
        }
        $("#dis_list a.last").hover(
            function () {
            $(".dis_bottom").addClass("bottom_hover");
            },
            function () {
            $(".dis_bottom").removeClass("bottom_hover");
            });

        $("input[name=ss_q]").keyup(function () {
            $("#dis_list").load("index.php?__ajax=AjaxDIList", {q : this.value}, function () {
                if ($("#dis_list").height() < parseInt($("#dis_list").css("maxHeight"))) {
                    $("#dis_list").css("overflow-y", "hidden");
                }
                else
                    $("#dis_list").css("overflow-y", "scroll");
            });
        });

        $("form[name=ss_form]").submit(function () {
            if ($("input[name=ss_q]").val() == df_ss_v || ($("input[name=ss_q]").val().length == 0))
                return false;
            return true;
        });
    }
});

function ssSubmit(name) {
    $("input[name=ss_q]").val(name);
    $("form[name=ss_form]").submit();
}

function SelectPerson(who, tab) {
    if (who == 'f') {
        $("#person_j").css("display", "none");
        $("#person_f").css("display", "block");
        $("#person_type").val('f');
        $($($(".person")[0]).children("span")[0]).removeClass("second_layout2").addClass("second_layout1");
    }
    else {
        $("#person_f").css("display", "none");
        $("#person_j").css("display", "block");
        $("#person_type").val('j');
        $($($(".person")[0]).children("span")[0]).removeClass("second_layout1").addClass("second_layout2");
    }

    $(".person").removeClass("selected");
    $(tab).addClass("selected");
}

function GetCities(county, elem_id) {
    var name = '';
    switch (elem_id) {
        case 'cities_j' : name = 'city_j';
                        break;
        case 'cities'   :
        default         : name = 'city';
    }

    $('#'+elem_id).load("index.php?__ajax=AjaxCities", {county_id : county, s_name : name});
}

jQuery(document).ready(function () {
    if ($('#order_form')[0]) {
        $("#order_form input[type=text]").focus(function () {
            var id = $(this).attr('name') + '_label';
            if ($('#'+id)) {
                $('#'+id).fadeIn('fast');
            }
        });
        $("#order_form input[type=text]").blur(function () {
            var id = $(this).attr('name') + '_label';
            if ($('#'+id)) {
                $('#'+id).fadeOut('fast');
            }
        });
    }
});

jQuery(document).ready(function () {
    if ($("#quantities").get(0)) {
        $("#quantities input[type=text]").focus(function () {
            this.select();
        });

        $("#quantities input[name=bb]").keyup(function () {
            var subtotal = 0;
            if (parseFloat($(this).val()) == $(this).val() && $(this).val() > 0)
                subtotal = $(this).val() * 75.99;

            var number = new Number(subtotal);
            $("#bb_total").html(subtotal.toFixed(2));

            SumItUp();
        });
        $("#quantities input[name=sb]").keyup(function () {
            var subtotal = 0;
            if (parseFloat($(this).val()) == $(this).val() && $(this).val() > 0)
                subtotal = $(this).val() * 44.99;

            var number = new Number(subtotal);
            $("#sb_total").html(subtotal.toFixed(2));

            SumItUp();
        });
        $("#quantities input[name=pb]").keyup(function () {
            var subtotal = 0;
            if (parseFloat($(this).val()) == $(this).val() && $(this).val() > 0)
                subtotal = $(this).val() * 66.99;

            var number = new Number(subtotal);
            $("#pb_total").html(subtotal.toFixed(2));

            SumItUp();
        });
    }
});

function SumItUp() {
    var total = parseFloat($("#bb_total").html()) + parseFloat($("#sb_total").html());
    var nrTotal = new Number(total);
    if (nrTotal.toFixed(2) != parseFloat($("#total_price").html())) {
        $("#total_price").html(nrTotal.toFixed(2));
    }
}

jQuery(document).ready(function () {
   if ($(".btn_cancel")[0]) {
        var cancel = new $('<p>You are about to cancel your order and leave this page.</p>');
        cancel.dialog({
                    autoOpen: false,
                    buttons: {
                            "No, return": function() { $(this).dialog("close");},
                            "Yes, leave": function() { document.location.href = $(".btn_cancel")[0]; }
                            },
                    title: 'Are you sure you wish to cancel?',
                    modal: true
                    });
        $(".btn_cancel").click(function () {
             cancel.dialog("open");
             return false;
        });
   }
});

/*jQuery(document).ready(function () {
    if ($('#delivery_addr')[0] && $('#da_holder')[0]) {
        $('#delivery_addr').change(function () {
            ShowHideDA();
        });
    }
});*/

function ShowHideDA() {
        if ($('#delivery_addr')[0].checked)
            $('#da_holder').css({display: 'none'});
        else
            $('#da_holder').css({display: 'block'});
}

var currentSlide = 0;
var slideTimer = 0;
jQuery(document).ready(function (){
    if ($("#slideshow")[0]) {
        slideTimer = window.setTimeout(function () {
             nextSlide();
         }, 3000);

        $('#arrow_left').click(function () {
            previousSlide();
        });

        $('#arrow_right').click(function () {
            nextSlide();
        });

        $('#slide_btns .btn').click(function () {
            var id = $(this).attr('id').substr(2);
            window.clearTimeout(slideTimer);
            slideTimer = 0;
            goToSlide(id);
            $(this).children(".tooltip").slideUp('fast');
        });

        $('#slide_btns .btn').hover(
            function () {
                $(this).children(".tooltip").slideDown('fast');
            },
            function () {
                $(this).children(".tooltip").slideUp('fast');
            });
    }
});

function nextSlide() {
    window.clearTimeout(slideTimer);
    slideTimer = 0;
    goToSlide(currentSlide+1);
}

function previousSlide() {
    window.clearTimeout(slideTimer);
    slideTimer = 0;
    goToSlide(currentSlide-1);
}

function goToSlide(slideNr) {

    slideNr = parseInt(slideNr);

    var totalDivs = $("#slideshow").children("div").length;
    var div = $("#slideshow").children("div")[currentSlide];

    if (slideNr >= totalDivs)
        slideNr = 0;
    else if (slideNr < 0)
        slideNr = totalDivs - 1;

    currentSlide = slideNr;

    $(div).fadeOut(1000);
    var newDiv = $("#slideshow").children("div")[slideNr];
    $(newDiv).fadeIn(1000);

    var wait = parseInt($(newDiv).attr('wait')) + 1000;
    if (wait == 1000)
        wait = 11000;

    slideTimer = window.setTimeout(function () {
            goToSlide(currentSlide+1);
        }, wait);
}

jQuery(document).ready(function() {
    if ($("#coloniada")[0]) {
        $("#coloniada_btn").hover(
            function () {
                $("#coloniada").fadeIn('fast');
            },
            function () {
                $("#coloniada").fadeOut('fast');
            });
    }
});
