(function($)
{
    $(function()
    {
        // Active menu element (for IE 6,7)
        function fix_menu_item_active_width()
        {
            if ($(this).parent().width() != $("#menu_items").width())
                $(this).width($(this).parent().width());
            else
                setTimeout(arguments.callee(), 100);
        }
        $("#menu_items .menu_item .active").each(fix_menu_item_active_width);
        
        // Menu right part width (for IE 7)
        var reservation_eshop_width = 0;
        $("#menu .reservation_eshop .menu_item, #menu .reservation_eshop .menu_item2, #menu .reservation_eshop menu_split").each(function()
        {
            reservation_eshop_width += $(this).width();
        });
        $("#menu .reservation_eshop").width(reservation_eshop_width);
        
        // Language chooser
        var lang_hover = false;
        var lang_height = $("#lang_inner").height();
        $("#lang .list .lang_item, #lang .trikd a").hover(function()
        {
            lang_hover = true;
            $("#lang .list div.dn").data('inactive_lang', true).removeClass('dn');
            var height = 0;
            $("#lang .list div").each(function() { height += $(this).height(); });
            $("#lang .list").height(height);
            $("#lang_inner").height(height);
        }, function()
        {
            lang_hover = false;
            $(this).oneTime(1000, function() {
                if (lang_hover == false) {
                    $("#lang_inner .list div").each(function()
                    {
                        if ($(this).data('inactive_lang'))
                            $(this).addClass('dn');
                    });
                    
                    $("#lang .list").height(lang_height);
                    $("#lang_inner").height(lang_height);
                }
            });
        });
        
        // Search
        $("#search .search_input a").click(function()
        {
            $("#search form").submit();
            return false;
        });
        
        
        // Homepage stuff
        $("#menu_bg").width($("#menu").width());
        $("#menu_bg").height($("#menu").height());
        $("#menu_bg2").width($("#menu").width());
        $("#menu_bg2").height(1);
        
        $("#menu_bg").fadeTo(0, 0.3);
        
        // Styled radios & checkbox'es
        $.extend(
        {
            styledRadios : function()
            {
                $("input[type='radio']").each(function()
                {
                    if ($(" + img", this).length == 0)
                    {
                        var img_service_active_off  = 'images/service_active_radio_off.gif';
                        var img_service_active_on   = 'images/service_active_radio_on.gif';
                        var img_off                 = 'images/radio_off.gif';
                        var img_on                  = 'images/radio_on.gif';
                        var img_gray_off            = 'images/gray_radio_off.gif';
                        var img_gray_on             = 'images/gray_radio_on.gif';
                        var img_gray2_off            = 'images/gray2_radio_off.gif';
                        var img_gray2_on             = 'images/gray2_radio_on.gif';
                        
                        function radio_set_on(radio, img)
                        {
                            if ($(radio).parent().hasClass('service'))
                                img.attr('src', img_service_active_on);
                            else if ($(radio).parent().hasClass('time_radio'))
                                img.attr('src', img_gray_on);
                            else if ($(radio).parent().hasClass('payment_type_left'))
                                img.attr('src', img_gray2_on);
                            else
                                img.attr('src', img_on);
                        }
                        function radio_set_off(radio, img)
                        {
                            if ($(radio).parent().hasClass('service'))
                                img.attr('src', img_gray_off);
                            else if ($(radio).parent().hasClass('time_radio'))
                                img.attr('src', img_gray_off);
                            else if ($(radio).parent().hasClass('payment_type_left'))
                                img.attr('src', img_gray2_off);
                            else
                                img.attr('src', img_off);
                        }
                        
                        var radio = this;
                        $(this).hide();
                        
                        
                        var img = $("<img />");
                        var radio_name = $(this).attr('name');
                        img.css('cursor', 'pointer'); 
                        img.css('margin-right', '3px');
                        
                        if ($(this).is(":checked"))
                            radio_set_on(this, img);
                        else
                            radio_set_off(this, img);
                        
                        function reset_all_radios()
                        {
                            $("input[type='radio'][name='"+radio_name+"']").each(function()
                            {
                                this.checked = false;
                                var img = $(this).data('styled_img');
                                radio_set_off(this, img);
                            });
                        }
                        
                        function update_radio()
                        {
                            if (radio.checked)
                                radio_set_on(radio, img);
                            else
                                radio_set_on(radio, img);
                        }
                        
                        img.click(function()
                        {
                            if ($(radio).attr('disabled') == false) {
                                radio.checked = true;
                                $(radio).change();
                            }
                            return false;
                        });
                        
                        $(radio).change(function()
                        {
                            var checked = this.checked;
                            reset_all_radios();
                            this.checked = checked;
                            update_radio();
                            return this.checked;
                        });
                        
                        $(this).after(img);
                        $(this).data('styled_img', img);
                    }
                });
            },
            
            styledCheckboxes : function()
            {
                $("input[type='checkbox']").each(function()
                {
                    if ($(" + img", this).length == 0)
                    {
                        var img_off                 = 'images/checkbox_off.gif';
                        var img_on                  = 'images/checkbox_on.gif';
                        
                        function radio_set_on(radio, img)
                        {
                            img.attr('src', img_on);
                        }
                        function radio_set_off(radio, img)
                        {
                            img.attr('src', img_off);
                        }
                        
                        var radio = this;
                        $(this).hide();
                        
                        
                        var img = $("<img />");
                        var radio_name = $(this).attr('name');
                        img.css('cursor', 'pointer'); 
                        img.css('margin-right', '3px');
                        
                        if ($(this).is(":checked"))
                            radio_set_on(this, img);
                        else
                            radio_set_off(this, img);
                        
                        function update_radio()
                        {
                            if (radio.checked)
                                radio_set_on(radio, img);
                            else
                                radio_set_off(radio, img);
                            return false;
                        }
                        
                        img.click(function()
                        {
                            if ($(radio).attr('disabled') == false) {
                                if (radio.checked)
                                    radio.checked = false;
                                else
                                    radio.checked = true;
                                update_radio();
                                $(radio).change();
                            }
                            return false;
                        });
                        
                        $(radio).change(function()
                        {
                            update_radio();
                            return this.checked;
                        });
                        
                        $(this).after(img);
                        $(this).data('styled_img', img);
                    }
                });
            }
        });
        $.styledRadios();
        $.styledCheckboxes();
        // End Styled radios & checkbox'es
        
        // Styled drop-down's
        $("input.dropdown").focus(function()
        {
            $(this).next().next().slideDown("fast");
        });
        $("input.dropdown").blur(function()
        {
           $(this).next().next().slideUp("fast");
        });
        $("ul.dropdown li a").click(function(){return false;});
        $("ul.dropdown li a").mousedown(function()
        {
            $("input.dropdown", $(this).parent().parent().parent()).val($(this).html());
            $("input[name='"+$(this).parent().parent().attr('name')+"']", $(this).parent().parent().parent())
                .val($(this).parent().attr('name'));
        });
        
        
        /****** E-SHOP ******/
        $.extend(true, {
            lifeenergy : {
                eshop : {
                    categories : []
                }
            }
        });
        for (var i = 0; i < $.lifeenergy.eshop.categories.length; i++) {
            (function()
            {
                var cat = $.lifeenergy.eshop.categories[i];
                
                if (cat.active) {
                    $("#eshop_submenu .item_" + cat.id).addClass("item_active");
                    $("#eshop_submenu .sub_items_" + cat.id).show();
                }
                
                $("#eshop_submenu .item_" + cat.id).each(function()
                {
                    var item = this;
                    
                    $("a", this).click(function()
                    {
                        if ($(item).hasClass("item_active") == false)
                        {
                            $("#eshop_submenu .item").removeClass("item_active");
                            $(item).addClass("item_active");
                            $("#eshop_submenu .sub_items").hide('fast');
                            $("#eshop_submenu .sub_items_" + cat.id).show('fast');
                        }
                        
                        return false;
                    });
                });
            })();
        }
        
        function update_cart_info(json)
        {
            $("#cart_info_total_amount").html(json.cart_total_amount);
            $("#cart_total_price").html(json.cart_total_price);
            
            if (json.cart_total_amount > 0)
                $("#cart_info_cnt").show();
            else
                $("#cart_info_cnt").hide();
        }
        
        var cart_info_new_product_time;
        
        $.lifeenergy.eshop.add_to_cart = function(product_id)
        {
            if (product_id != undefined && product_id > 0)
                $.post($.lifeenergy.site_base_href + 'ajax.php',
                    { action: 'add_to_cart', 'product_id': product_id }, function(json)
                {
                    if (json.success) {
                        update_cart_info(json);
                        
                        $("#cart_info_new_product")
                            .html($.lifeenergy.lang.product_added_to_cart +
                                ' : <strong>' + json.product.data.name + '</strong>')
                            .show('fast');
                            clearTimeout(cart_info_new_product_time);
                            cart_info_new_product_time = setTimeout(function()
                            {
                                $("#cart_info_new_product").hide('slow');
                                clearTimeout(cart_info_new_product_time);
                            }, 5000);
                    }
                }, 'json');
            
            return retFalse();
        }
        
        function roundPrice(price)
        {
            return Math.ceil(price);
        }
        
        function check_amounts_and_recalculate_cart_prices()
        {
            var cart_total_price = 0;
            
            $("#cart_content .cart_content_table tr.cart_content_table_product_tr").each(function()
            {
                var product = this;
                var price_single = $("input[name='price_single']", product).val();
                var amount = $(".cart_amount input", product).val();
                var product_id = parseInt($(".cart_amount input", product).attr('name').replace('amount_', ''), 10);
                if ($.lifeenergy.eshop.product_amounts[product_id] != undefined &&
                    amount > $.lifeenergy.eshop.product_amounts[product_id])
                {
                    amount = $.lifeenergy.eshop.product_amounts[product_id];
                    $(".cart_amount input", product).val(amount);
                }
                var total_price = roundPrice(price_single * amount);
                if (total_price > 0) {
                    $(".price_total", product).html(total_price);
                    cart_total_price += total_price;
                } else
                    $(".price_total", product).html(0);
            });
            
            $("#cart_content .cart_summary .cart_total_price").html(cart_total_price);
            if (cart_total_price == 0) {
                $("#cart_content .cart_summary .price_to_pay").html(0);
                $("#cart_content .cart_summary .cart_proceed").hide();
            } else {
                var price_to_pay = cart_total_price + $.lifeenergy.eshop.delivery_price;
                $("#cart_content .cart_summary .price_to_pay").html(price_to_pay);
                $("#cart_content .cart_summary .cart_proceed").show();
            }
        }
        $("#cart_content .cart_content_table tr.cart_content_table_product_tr .cart_amount input").change(check_amounts_and_recalculate_cart_prices);
        check_amounts_and_recalculate_cart_prices();
        
        $.lifeenergy.eshop.remove_from_cart = function(product_id)
        {
            if (product_id != undefined && product_id > 0)
                $.post($.lifeenergy.site_base_href + 'ajax.php',
                    { action: 'remove_from_cart', 'product_id': product_id }, function(json)
                {
                    if (json.success) {
                        $("#cart_content .cart_content_table tr.cart_content_table_product_tr:has(a.cart_remove_product_" + product_id + ")").remove();
                        $("#cart_content .cart_content_table").show(); // Apzaidziam atsiradusius borderius
                        check_amounts_and_recalculate_cart_prices();
                    }
                }, 'json');
            
            return retFalse();
        }
        
        function check_need_bill()
        {
            if ($(".payment_block input[type='checkbox'][name='need_bill']").is(":checked"))
                $(".payment_block tr.bill_field").show();
            else
                $(".payment_block tr.bill_field").hide();
        }
        $(".payment_block input[type='checkbox'][name='need_bill']").change(check_need_bill);
        check_need_bill();
    });
})(jQuery);

function dump(arr,return_text,level,objs_dumped) {
    if (return_text == undefined) return_text = false;
    var dumped_text = "";
    if(!level) level = 0;
    if (objs_dumped == undefined) objs_dumped = [];
    
    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";
    
    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
        for(var item in arr) {
            try {
                var value = arr[item];
                if(typeof(value) == 'object') { //If it is an array,
                    dumped_text += level_padding + "'" + item + "' ...\n";
                    // Recursion protection
                    var already_dumped = false;
                    for (var k = 0; k < objs_dumped.length; k++)
                        if (value == objs_dumped[k]) {
                            already_dumped = true;
                            dumped_text += 'recursion! (already dumped somewhere above)\n';
                        }
                    if (already_dumped == false) {
                        objs_dumped[objs_dumped.length] = value;
                        dumped_text += dump(value,true,level+1,objs_dumped);
                    }
                } else {
                    dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
                }
            } catch (err) {}
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    
    if (return_text)
        return dumped_text;
    else
        alert(dumped_text);
}

// Return false, specially for windows vista ie 7
function retFalse()
{
	try
	{
		// VISTA IE 7
		window.event.returnValue = false;
	} catch (error)
	{
		// ALL OTHER (MOZILLA, OPERA, SAFARI, IE 6 & 7 ON XP)
		return false;
	}
}