(function($)
{
    $(function()
    {
        // Prerequisites
        $.import_js('scripts/dump.js');
        $.import_js('scripts/jquery.ui.draggable.js');
        $.import_js('scripts/jquery.enlarge_photo.1.0.js');
        $.import_js('scripts/jquery.center.1.0.js');
        // $.import_js('scripts/bevel.1.21.js');
        // $.import_js('scripts/jquery.corner.2.0.2.js');
        
        
        // Firefox .btn style fix
        if ($.browser.mozilla)
            $(".btn input[type='submit'], .btn input[type='button']").css('padding-bottom', '3px');
        
        
        // Common function for pop-up submenu items
        $.fn.popup_submenu_item = function(active_selector)
        {
            $(this).each(function()
            {
                $(this).hover(function()
                {
                    $($(this).selector).removeClass(active_selector);
                    $(this).addClass(active_selector);
                }, function()
                {
                    $(this).removeClass(active_selector);
                });
                
                var href = $("a", this).attr("href");
                
                $(this).click(function()
                {
                    document.location=href;
                    return false;
                });
            });
        }
        
        
        /*
         * Left menu
         */
        function hover_handler(items_sel, sel)
        {
            var is_hovered = false;
            
            function hover_on()
            {
                $(".sub", items_sel).hide();
                $(".sub", sel).show();
                is_hovered = true;
            }
            function hover_off()
            {
                is_hovered = false;
                function delay_hover_off()
                {
                    if (is_hovered == false)
                        $(".sub", sel).hide();
                }
                var time = setTimeout(delay_hover_off, 200);
            }
            
            $(sel).hover(hover_on, hover_off);
        }
        for (var i = 0; i < $("#home_left_menu .item_cnt").length; i++)
            hover_handler($("#home_left_menu .item_cnt"), $("#home_left_menu .item_cnt:eq("+i+")"));
        
        $("#home_left_menu .sub .sub_item").popup_submenu_item('sub_item_active');
        $("#home_left_menu .sub_down .sub_down_item").popup_submenu_item('sub_down_item_active');
        
        // Top submenu
        for (var i = 0; i < $("#home_top_menu_items .item_cnt").length; i++)
            hover_handler($("#home_top_menu_items .item_cnt"), $("#home_top_menu_items .item_cnt:eq("+i+")"));
        $("#home_top_menu_items .item_cnt .sub_cnt .sub_item").popup_submenu_item('sub_item_active');
        
        
        /*
         * Subscribe box
         */
        $(".subscribe_box .cnt").each(function()
        {
            var cnt = $(this);
            
            $("form", cnt).submit(function()
            {
                var data = { fn: 'subscribe', email: $("input[name='email']", this).val() };
                $.post('ajax', data, function(json)
                {
                    if (json.success)
                        $(".box_content", cnt).html(json.message);
                    else
                        $(".box_content", cnt).html('<span class="error">' + json.message + '</span>');
                }, 'json');
                
                return false;
            });
        });
        
        /*
         * Home page
         */
        if ($("#home_flash").length > 0) {
            // $(document).ready(function(){
                // $("#home_flash > .item .bg_photo").corner();
            // });
            
            var time;
            var current_item = 0;
            var item_show_for_ms = 5000;
            
            function next_image()
            {
                var next_item = current_item + 1;
                if (next_item >= $("#home_flash > .item").length)
                    next_item = 0;
                set_image(next_item);
                
                time = setTimeout(next_image, item_show_for_ms);
            }
            function set_image(index)
            {
                if (current_item != index) {
                    current_item = index;
                    
                    $("#home_flash > .item").fadeOut();
                    $("#home_flash .selectors > .item img").attr('src', 'images/home_photo_inactive.png');
                    $("#home_flash .selectors > .item .num").removeClass('num_active');
                    
                    $("#home_flash > .item:eq("+index+")").fadeIn();
                    $("#home_flash .selectors > .item:eq("+index+") img").attr('src', 'images/home_photo_active.png');
                    $("#home_flash .selectors > .item:eq("+index+") .num").addClass('num_active');
                }
            }
            for (var i = 0; i < $("#home_flash > .selectors > .item").length; i++)
                (function()
                {
                    var index = i;
                    $("#home_flash .selectors > .item:eq("+index+")").click(function()
                    {
                        clearTimeout(time);
                        set_image(index);
                    });
                })();
                
            
            time = setTimeout(next_image, item_show_for_ms);
        }
        
        /*
         * Inner - product page
         */
        if ($(".product_info").length > 0) {
            $(".product_info_right .product_photo").enlarge_photo({
                full_photo_path: $.zoom.product.photo,
                click_selectors: [
                    $(".product_info_right .product_photo"),
                    $(".product_info_right .zoom_photo")],
                center_enlarged_photo: true,
                close_everywhere: true
            });
            
            // Product comments
            var comments_paging_page = 0;
            function comments_paging_set_page(page)
            {
                comments_paging_page = page;
                $(".product_comments .item").hide();
                $(".product_comments .item:eq("+comments_paging_page+")").show();
                
                $(".product_comments .paging .current .active").html(comments_paging_page + 1);
                if (comments_paging_page == 0)
                    $(".product_comments .paging .btn_left img").attr('src', 'images/page_prev_inactive_btn.gif');
                else
                    $(".product_comments .paging .btn_left img").attr('src', 'images/page_prev_btn.gif');
                if (comments_paging_page == $(".product_comments .item").length - 1)
                    $(".product_comments .paging .btn_right img").attr('src', 'images/page_next_inactive_btn.gif');
                else
                    $(".product_comments .paging .btn_right img").attr('src', 'images/page_next_btn.gif');
            }
            $(".product_comments .paging .btn_left").click(function() {
                if (comments_paging_page > 0)
                    comments_paging_set_page(comments_paging_page - 1);
            });
            $(".product_comments .paging .btn_right").click(function() {
                if (comments_paging_page < $(".product_comments .item").length - 1)
                    comments_paging_set_page(comments_paging_page + 1);
            });
            
            // Write comment
            $(".write_comment_btn").click(function() {
                $(".write_comment_btn").hide();
                $(".similar_products").hide();
                $(".write_comment").show();
            });
            
            // Send to friend
            $("#send_to_friend").center({ force: true });
            $("#send_to_friend .close").click(function ()
            {
                $("#send_to_friend").hide();
            });
            $(".product_info .recommend_to_friend").click(function()
            {
                $("#send_to_friend").show();
            });
            $("#send_to_friend_done").center({ force: true });
            $("#send_to_friend_done .btn input").click(function()
            {
                $("#send_to_friend_done").hide();
            });
        }
    });
    
    
    
})(jQuery);