var SCROLLER_INTERVAL   = 40;
var SCROLLER_SPEED      = 20;

if (typeof console == 'undefined')
{
    console = {}
    console.log = function() {
        }
    console.dir = function() {
        }
}


$(function () {

    logged = false;

    //menu item hover
    $('.menu_item').mouseenter(function(e) {
        $(e.currentTarget).addClass('menu_over');
        var id = $(e.currentTarget).attr('id');
        var length = id.length;
        cid = id.charAt(length - 1);
        show_current_sub(cid);
    })
    if (typeof cid == "undefined") {
        cid = 0;
    }
    $('.menu_item').mouseleave(function(e) {
        if (cid != current_menu) {
            $(e.currentTarget).removeClass('menu_over');
        }
    })

    //menu item call url (first submenu's href as default or home=[index or /])
    $('#menu_item_1,#menu_item_2,#menu_item_3,#menu_item_4,#menu_item_5,#menu_item_6').click(function(e) {
        var id = $(e.currentTarget).attr('id');
        var length = id.length;
        var current_id = id.charAt(length - 1);
        if (current_id != 1 & typeof $('#menu_bottom_item_' + current_id + ' .menu_sub_item_body')[0] != "undefined") {
            window.location.href = $("a", $('#menu_bottom_item_' + current_id).children('.menu_sub_item_body')[0]).attr('href');
        } else if (current_id != 1 & typeof $('#menu_bottom_item_' + current_id + ' .menu_sub_item_body')[0] == "undefined") {
            window.location.href = $('#menu_bottom_item_' + current_id).attr('title');
        } else {
            window.location.href = '/';
        }
    })

    //set current menu's style
    $('#menu_item_' + current_menu).addClass('menu_over');

    //show/hide sub menu items
    function show_current_sub(e) {
        $('#menu_bottom_item_1,#menu_bottom_item_2,#menu_bottom_item_3,#menu_bottom_item_4,#menu_bottom_item_5,#menu_bottom_item_6').hide();
        if (typeof e != "undefined") {
            $('#menu_bottom_item_' + e).show();
        } else {
            $('#menu_bottom_item_' + current_menu).show();
        }
    }

    show_current_sub();

    //sub menu change back to active when leaving menu area
    $('#menu').mouseleave(function(e) {
        show_current_sub();
    })

    //position sub menus

    //count how many menu items the page has
    var menus = $("div[id*='menu_item_']");
    var number_of_menus = menus.length;
    var menus_all_width = 5; //menus_all_width starting 10 to have a left space/margin for the items in the submenu (design cause)
    for (var i = 1; i < number_of_menus + 1; i++) {
        switch (i) {
            case 1:start_offset=3;break;
            case 2:start_offset=65;break;
            case 3:start_offset=170;break;
            case 4:start_offset=313;break;
            case 5:start_offset=436;break;
        }
        start_offset += 1;
        $('#menu_bottom_item_' + i + ':first').css('margin-left', start_offset + 'px');

        //$('#menu_bottom_item_' + i + ':first').css('margin-left', menus_all_width + 'px');
        //var width = $('#menu_item_' + i).outerWidth();
        //menus_all_width += (width);
    }

    //set active sub menu's text color to white
    var subs = $('#menu_bottom_item_' + current_menu + ' .menu_sub_item_body');
    var number_of_subs = subs.length;
    for (var i = 1; i < number_of_subs + 1; i++) {
        if (i == current_sub_menu) {
            $($('#menu_bottom_item_' + i + ' .menu_sub_item_body')[i - 1]).css('color', '#FFF');
            $("a", $('#menu_bottom_item_' + current_menu + ' .menu_sub_item_body')[i - 1]).css('color', '#FFF');
        }
    }
    $('div[id*="menu_bottom_item_"] .menu_sub_item_body a').mouseenter(function(e) {
        $(e.currentTarget).css('color', '#FFF');
        var sub_divs = $(e.currentTarget).parent().parent().children('.menu_sub_item_body');
        for (var i = 0; i < sub_divs.length; i++) {
            if ($(e.currentTarget).parent()[0] == sub_divs[i]) {
                current_sub_id = i + 1;
            }
        }
    })
    $('div[id*="menu_bottom_item_"] .menu_sub_item_body a').mouseleave(function(e) {
        if (cid != 0) { // we have to make sure that the submenu we check below belongs to the active selected menu item
            current_menu_cache = cid;
        } else {
            current_menu_cache = current_menu;
        }
        if ((current_sub_id != current_sub_menu & current_menu_cache == current_menu) | current_menu_cache != current_menu) {
            $(e.currentTarget).css('color', '#000');
        }
    })


    //login submit
    $('#login_button').click(function(e) {
        if (typeof login_name != 'undefined') {
            window.location = '/my-account/';
        } else {
            $("#top_login").submit();
        }
    })

    //search submit
    $('#menu_top .search_area .search_icon img').click(function() {
        $("form#top_search").submit();
    })

    //clear search box
    $('#top_search_input').click(function(e) {
        $(e.currentTarget).attr('value', '');
    })


    //logged in (apply design) and change forms
    function logged_in(username) {
        logged = true;
        $('.login_username').hide();
        $('#top_login .login_username_input').hide();
        $('#top_login .login_password').hide();
        $('#top_login .login_password_input').hide();
        $('.login_status').text('Welcome, ' + username);
        $('#login_button').text('My account');
        $('.logout_signup_button .button_5_body a').text('Log out');
        $('.logout_signup_button .button_5_body a').attr('href', '/log-out/');
        $('.login_status').css('width', '465px');
        $('.login_status').css('text-align', 'right');
        $('.login_status').css('padding-right', '7px');
        $('.login_status').css('color', '#666');
    }

    if (window.login_name) {
        logged_in(login_name);
    }

    //box4 scroll
    function init_vals_box4() {
        get_items = $('.box4_body_body .items .item');
        item_width = $('.box4_body_body .items .item').width();
        item_number = get_items.length;
        whole_width = item_number * item_width;
        vector_pack_items = $('.box4_body_body .items , .box4_body_body .item_texts');
    }

    init_vals_box4();
    $('.box4_body_body .items , .box4_body_body .item_texts').width(whole_width);
    $('.box4_body_body .items , .box4_body_body .item_texts').css('marginLeft', '0px');

    moveRight4 = function() {
        var marginleft = parseInt($('.box4_body_body .items').css('marginLeft'));
        if (marginleft >= (930 - whole_width)) {
            marginleft -= SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    moveLeft4 = function() {
        var marginleft = parseInt($('.box4_body_body .items').css('marginLeft'));
        if (marginleft <= -5) {
            marginleft += SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    $('.box4_left').mousedown(function(e) {
        init_vals_box4();
        moveit = setInterval("moveLeft4()", SCROLLER_INTERVAL);
    })

    $('.box4_left').mouseup(function(e) {
        clearInterval(moveit);
    })

    $('.box4_right').mousedown(function(e) {
        init_vals_box4();
        moveit = setInterval("moveRight4()", SCROLLER_INTERVAL);
    })

    $('.box4_right').mouseup(function(e) {
        clearInterval(moveit);
    })

    //box5 scroll

    function init_vals_box5() {
        get_items = $('.box5_body_body .items .item');
        item_number = get_items.length;
        whole_width = 0;
        for (var j = 0; j < item_number; j++) {
            img_width = $('img', ($($('.box5_body_body .items .item')[j]))).width();
            $($('.box5_body_body .items .item')[j]).width(img_width);
            $($('.item_texts .item')[j]).width(img_width);
            img_width += 20;
            whole_width += img_width;
        }
        vector_pack_items = $('.box5_body_body .items , .box5_body_body .item_texts');
    }

    $(window).load(function() {
        init_vals_box5();
        $('.box5_body_body .items , .box5_body_body .item_texts').width(whole_width);
        $('.box5_body_body .items , .box5_body_body .item_texts').css('marginLeft', '0px');
    });


    moveRight5 = function() {
        var marginleft = parseInt($('.box5_body_body .items').css('marginLeft'));
        if (marginleft >= (910 - whole_width)) {
            marginleft -= SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    moveLeft5 = function() {
        var marginleft = parseInt($('.box5_body_body .items').css('marginLeft'));
        if (marginleft <= -5) {
            marginleft += SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    $('.box5_left').mousedown(function(e) {
        init_vals_box5();
        moveit = setInterval("moveLeft5()", SCROLLER_INTERVAL);
    })

    $('.box5_left').mouseup(function(e) {
        clearInterval(moveit);
    })

    $('.box5_right').mousedown(function(e) {
        init_vals_box5();
        moveit = setInterval("moveRight5()", SCROLLER_INTERVAL);
    })

    $('.box5_right').mouseup(function(e) {
        clearInterval(moveit);
    })

    //box14 scroll
    function init_vals_box14() {
        get_items = $('.box14_body_body .items .item');
        item_width = $('.box14_body_body .items .item').width() + 2;
        item_number = get_items.length;
        whole_width = item_number * item_width;
        vector_pack_items = $('.box14_body_body .items');
    }

    init_vals_box14();
    $('.box14_body_body .items').width(whole_width);
    $('.box14_body_body .items').css('marginLeft', '0px');

    moveRight14 = function() {
        var marginleft = parseInt($('.box14_body_body .items').css('marginLeft'));
        if (marginleft >= (592 - whole_width)) {
            marginleft -= SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    moveLeft14 = function() {
        var marginleft = parseInt($('.box14_body_body .items').css('marginLeft'));
        if (marginleft <= -5) {
            marginleft += SCROLLER_SPEED;
            vector_pack_items.css('marginLeft', marginleft + 'px');
        }
    }
    $('.box14_left').mousedown(function(e) {
        init_vals_box14();
        moveit = setInterval("moveLeft14()", SCROLLER_INTERVAL);
    })

    $('.box14_left').mouseup(function(e) {
        clearInterval(moveit);
    })

    $('.box14_right').mousedown(function(e) {
        init_vals_box14();
        moveit = setInterval("moveRight14()", SCROLLER_INTERVAL);
    })

    $('.box14_right').mouseup(function(e) {
        clearInterval(moveit);
    })

    //stars
    $('.star_left').mouseover(function(e) {
        var id = $(e.currentTarget).attr('title');
        var length = id.length;
        var current_id = id.charAt(length - 1);
        current_id++;
        for (var i = 1; i < current_id; i++) {
            $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).addClass('star_left_on');
            if (i != (current_id - 1)) {
                $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).addClass('star_right_on');
            }
        }
        for (var i = current_id - 1; i < 6; i++) {
            if (i != current_id - 1) {
                $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_left_on');
            }
            $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_right_on');
        }
    })
    $('.star_right').mouseover(function(e) {
        var id = $(e.currentTarget).attr('title');
        var length = id.length;
        var current_id = id.charAt(length - 1);
        current_id++;
        for (var i = 1; i < current_id; i++) {
            $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).addClass('star_left_on');
            $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).addClass('star_right_on');
        }
        for (var i = current_id; i < 6; i++) {
            $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_left_on');
            $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_right_on');
        }
    })

    $('.vote_stars').mouseleave(function(e) {
        for (var i = 0; i < 6; i++) {
            $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_left_on');
            $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_right_on');
        }
    })

    $('.star_left,.star_right').click(function(e) {

        // id could be vectorpack or freevector
        var id = $('input[name="id"]', $(e.currentTarget).parents('div.vote_stars')[0]).val();
        var type = $('input[name="type"]', $(e.currentTarget).parents('div.vote_stars')[0]).val();
        var value = 0;
        for (var i = 0; i < 6; i++) {
            if ($(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).hasClass('star_left_on')) value++;
            if ($(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).hasClass('star_right_on')) value++;
        }
        // used to have a 1..5 range, now it is 1..10
        // so dont' divide.
        // value /= 2;
        $.post('/vote/', {
            content_type:type,
            id:id,
            value:value
        }, function(data, text) {
            //alert("Thank you for your vote");
            location.reload();
        });
    })

    /**
     * TabControl for tags
     * Switches between 3 pages (popular, all, new)
     */
    $('div.fun_tag_selector').click(function(e) {
        var t = $(e.currentTarget).text();
        $('div.fun_tag_selector a').removeClass('active')
        $('div.text_cloud1').hide(200);
        if (t.match(/Popular Tags/i)) {
            $('div.fun_popular_tags').show(300);
        }
        if (t.match(/New Tags/i)) {
            $('div.fun_new_tags').show(300);
        }
        if (t.match(/All Tags/i)) {
            $('div.fun_all_tags').show(300);
        }
        $('a', $(e.currentTarget)).addClass('active');
    })


    $('div.news_tabcontrol div').click(function(e) {
        var t = $(e.currentTarget).text();
        $('div.news_tabcontrol div').addClass('inactive');
        $(e.currentTarget).removeClass('inactive');
        if (t.match(/latest/i)) {
            $('#news_popular').hide();
            $('#news_latest').show();
        }
        if (t.match(/popular/i)) {
            $('#news_latest').hide();
            $('#news_popular').show();
        }
        //var scrolldiv = $('#box1_scrollcontent');
        var scrolldiv = document.getElementById('box1_scrollcontent');
        //alert(scrolldiv);
        scrolldiv.contentScroll("0px", "0px", false);
        scrolldiv.scrollUpdate();
    //CSBfleXcroll('box1_scrollcontent');
    })

    /**
     * Makes a.submit behave like a submit
     * button under all conditions.
     *
     * Submits the first form in content DIV.
     */
    $('a.submit').click(function(e) {
        e.preventDefault();
        var targetForm = $(e.currentTarget).parents("form")[0];
        //alert('submiting'+targetForm);
        targetForm.submit();
    });


    $('.vote_stars').mouseleave(function(e) {
        var original_value = parseInt($(e.currentTarget).attr('title'), 10);
        // remove all
        for (var i = 0; i < 6; i++) {
            $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_left_on');
            $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).removeClass('star_right_on');
        }
        for (var i = 0; i < 6; i++) {
            if (original_value >= i * 2 - 1)
                $(('div[title=star_left_' + i + ']'), $(e.currentTarget).parent()).addClass('star_left_on');
            if (original_value >= i * 2)
                $(('div[title=star_right_' + i + ']'), $(e.currentTarget).parent()).addClass('star_right_on');
        }
    })

    //for auto centering div's !!!EFFECTS ALL .center on page!!!
    function center_divs() {
        all_center = $('.center');
        for (var j = 0; j < all_center.length; j++) {
            var div_center_width = $($('.center')[j]).width();
            var div_center_parent_width = $($('.center')[j]).parent().width();
            $($('.center')[j]).css('marginLeft', ((div_center_parent_width - div_center_width) / 2) + 'px');
        }
    }

    center_divs();

    //box auto size  !!!EFFECTS ALL .box_1 on page!!!
    function autosize_boxes() {
        all_boxes = $('.box_1_body');
        for (var j = 0; j < all_boxes.length; j++) {
            var box_body_width = $($('.box_1_body')[j]).width();
            $($('.box_1_top')[j]).css('width', (box_body_width - 10) + 'px');
            $($('.box_1_bottom')[j]).css('width', (box_body_width - 10) + 'px');
        }
    }

    autosize_boxes();


    // Colorbox initialization: used for vector pack listing
    $("a[rel='colorbox']").colorbox({
        transition:"elastic",
        speed:100
    });
	
    // fullscreen fadout initialization
    //$('#fullscreen_fade').css("width","500px");
    var docheight = $(document).height();
    $('#fullscreen_fade').css("height",docheight);


  

    function closecart(parameters) {
        $('#fullscreen_fade,#popup_cart').css({
            visibility:'hidden'
        });
        $('#popup_cart').addClass('z_hide');
        $('#popup_cart').hide();
    }
    
    $(".close_cart").live('click',closecart);



});

function showcart()
{
    $('#fullscreen_fade,#popup_cart').css({
        visibility:'visible'
    });
    $('#popup_cart').removeClass('z_hide');
    $('#popup_cart').show();
    var position = $(document).width()/2 - $('#popup_cart').width()/2;
    $('#popup_cart').css({
        left:position
    })


    

}

function add_to_cart(id) {
    $('#fullscreen_fade').css({
        visibility:'visible'
    });
    $('#popup_cart_holder').load('/add-to-mini-cart/',{
        item_id:id
    },function(){
        showcart();
        CSBfleXcroll('cart_scrollcontent');
        $('#cart_scrollcontent_vscrollerbase').css('visibility','visible')
        $('#cart_scrollcontent_vscrollerbase').show();
    });
}




