jQuery(document).ready(function(){

    jQuery(".header_container .menu ul").each(function(){
    
    
        menu_num = jQuery(this).children("li").size() - 1;
        jQuery(this).children("li:eq(" + menu_num + ")").css("borderRight", "none");
        
    })
    
    jQuery(".bottom_icons_right div").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    })
    
    jQuery(".bottom_icons_left div").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    })
    
    jQuery(".logo").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    })
    
    jQuery(".header_container .social div").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    })
    
    jQuery(".secondary_menu .container div").mouseover(function(){
    
        jQuery(this).children("a").css("color", "#ffffff");
        
    });//////////mouseover on secondary menu
    jQuery(".secondary_menu .container div").mouseout(function(){
    
        if (!jQuery(this).hasClass("active")) 
            jQuery(this).children("a").css("color", "#000000");
        
    });//////////mouseout on secondary menu
    jQuery(".secondary_menu .container div").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    });//////////click on secondary menu
    //swfobject.embedSWF("http://www.mjflooring.com/sales/ad-carbinetry.swf", "main_adv_video", "279", "183", "8.0.0", "expressInstall.swf");
    
    
    var flashvars = {};
    flashvars.file = siteurl + "/wp-content/themes/mjflooring/flash/mj_video.flv";
    flashvars.image = siteurl + "/wp-content/themes/mjflooring/flash/mj-flooring-commercial.jpg";
    
    
    flashvars.autostart = "false";
    flashvars.controlbar = "none";
    flashvars.repeat = "always";
    flashvars.quality = "true";
    var params = {};
    params.wmode = "transparent";
    
    
    var attributes = {};
    attributes.id = "player";
    attributes.name = "player";
    
    swfobject.embedSWF(siteurl + "/wp-content/themes/mjflooring/flash/player-licensed.swf", "main_video", "279", "230", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    
    jQuery("main_video").css("position", "absolute");
    
    
    
    /////////////////////////////
    ///moving manufacturer logos
    /////////////////////////////
    
    var move = 0;
    
    jQuery(".flooring_partners .flooring_partners_container").each(move_partner_logos);/////////looping each image
    function move_partner_logos(){
    
        speed = 150;
        
        jQuery(this).animate({
            marginLeft: '-=1'
        }, 10, "linear", function(){
            jQuery(".flooring_partners .flooring_partners_container").each(move_partner_logos);
        });
        move -= 1;
        
        if (move % speed == 0) {
        
            a = jQuery(".flooring_partners_container a:eq(" + (-1 * (move / speed) - 1) + ")").clone();
            number = jQuery(".flooring_partners_container a").size() - 1;
            jQuery(".flooring_partners_container a:eq(" + number + ")").after(a);
            
        }
        
    }///////////partner logo moving method
    /////////////////////////////
    ///moving manufacturer logos
    /////////////////////////////
    
    
    
    ////////////////////
    ////arrow clicking
    ////////////////////
    
    jQuery(".content .flooring_content_mid .flooring_arrows .text_content").slideUp(0);
    
    jQuery(".content .flooring_content_mid .flooring_arrows .arrow").each(function(){
    
        jQuery(this).click(function(){
        
        
        
            if (jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(0)").is(":visible")) {
                jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(0)").slideUp(1000);
            }
            else {
                jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(0)").slideDown(1000);
                
            }
            
            if (jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(1)").is(":visible")) {
                jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(1)").slideUp(1000);
            }
            else {
                jQuery(".content .flooring_content_mid .flooring_arrows .text_content:eq(1)").slideDown(1000);
                
            }
            
        });//////arrow click on main page
    });/////////////looping arrow events
    ////////////////////
    ////arrow clicking
    ////////////////////
    
    
    ////////////////////
    ////tab clicking
    ////////////////////
    
    jQuery(".carousel .carousel_tabs .tab").click(tab_clicked);
    
    function tab_clicked(){
    
        if (jQuery(this).hasClass("red_tab")) {
            jQuery(".mouse_over").css("display", "none");
            jQuery(this).children("span").css("color", "#000000");
        }
        
        
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img").hide();
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .caption").fadeOut(0);
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .loading").fadeOut(0);
        
        
        
        current_tab = jQuery(this).index();
        
        
        
        if (siteurl + "/" == current_url || current_url == "http://www.mjflooring.com/alternate-home/") 
            current_tab--;
        
        if (jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img").size() == 0) 
            load_tab_images(current_tab);
        
        jQuery(".navigation_left").unbind('click');
        jQuery(".navigation_right").unbind('click');
        
        jQuery(".navigation_left").bind('click', navigation_left);
        jQuery(".navigation_right").bind('click', navigation_right);
        
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .caption").fadeIn(0);
        
        
        
        if (jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img").size() == 0) 
            jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .loading").fadeIn(0);
        
        jQuery(".product_sub .container").slideUp(0);
        jQuery(".product_sub .container:eq(" + current_tab + ")").slideDown(0);
        
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img:eq(0)").fadeIn(1000);
        fade_num = 0;
        
        if (!jQuery(this).hasClass("active_tab")) {
        
            jQuery(".carousel .carousel_tabs .tab").each(function(){
            
                jQuery(this).removeClass("active_tab");
                jQuery(this).removeClass("active");
                
                if (jQuery(this).hasClass("red_tab")) 
                    jQuery(this).children("span").css("color", "#da6767");
                
                jQuery(this).children("div").each(function(){
                
                    jQuery(this).remove();
                    
                });
                
                
                jQuery(this).children("span").css("background", "none");
                jQuery(this).children("span").css("border-top-color", "#eeeeee");
                jQuery(this).css("color", "#b7b7b7");
                
            });
            
            jQuery(this).children("span").before('<div class="tab_before"></div>');
            jQuery(this).children("span").after('<div class="tab_after"></div>');
            jQuery(this).children("span").css("background-color", "#ffffff");
            jQuery(this).children("span").css("border-top-color", "#e1e1e1");
            jQuery(this).css("color", "#000000");
            
            jQuery(this).addClass("active_tab");
            if (jQuery(this).hasClass("red_tab")) 
                jQuery(this).children("span").css("color", "#000000");
            
        }
        
    }
    
    ////////////////////
    ////tab clicking
    ///////////////////
    
    
    
    
    
    
    
    
    
    ////////////////////////////
    ////product image fading
    ////////////////////////////
    var fade_num = 0;
    var current_tab = 0;
    var first_load = true;
    
    jQuery(".carousel_mid .img_holder img").hide();
    jQuery(".caption").hide();
    jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img:eq(0)").fadeIn(1000);
    //jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .caption").fadeIn(1000);
    
    setInterval(function(){
    
        //jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .caption").css("opacity", "70");
        //jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .caption").css("display", "block");
        
        if (first_load) {
            current_tab = 0;
            first_load = false;
        }
        
        next_fade = fade_num + 1;
        
        if (next_fade > jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img").size() - 1) 
            next_fade = 0;
        
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img:eq(" + fade_num + ")").fadeOut(1000);
        jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") img:eq(" + next_fade + ")").fadeIn(1000);
        
        if (next_fade == 0) 
            fade_num = -1;
        
        fade_num++;
        
        
        
    }, 3000);
    
    ////////////////////////////
    ////product image fading
    ////////////////////////////
    
    
    
    
    ////////////////////////////
    ////product subpage gallery fading
    ////////////////////////////
    var fade_num_the_gal = 0;
    var the_gal_stopped = false;
    jQuery(".the_gallery img").hide();
    jQuery(".the_gallery img:eq(0)").fadeIn(1000);
    jQuery(".the_gallery .caption").hide();
    jQuery(".the_gallery .caption:eq(0)").fadeIn(1000);
    
    setInterval(function(){
    
        if (!the_gal_stopped) {
        
            next_fade_the_gal = fade_num_the_gal + 1;
            
            if (next_fade_the_gal > jQuery(".the_gallery img").size() - 1) 
                next_fade_the_gal = 0;
            
            jQuery(".the_gallery img:eq(" + fade_num_the_gal + ")").fadeOut(1000);
            jQuery(".the_gallery img:eq(" + next_fade_the_gal + ")").fadeIn(1000);
            jQuery(".the_gallery .caption:eq(" + fade_num_the_gal + ")").fadeOut(1000);
            jQuery(".the_gallery .caption:eq(" + next_fade_the_gal + ")").fadeIn(1000);
            
            if (next_fade_the_gal == 0) 
                fade_num_the_gal = -1;
            
            fade_num_the_gal++;
            
            jQuery(".the_gallery .navi .dot").removeClass("active");
            jQuery(".the_gallery .navi .dot:eq(" + fade_num_the_gal + ")").addClass("active");
            
        }
        
        
        
    }, 3000);
    
    jQuery(".the_gallery .navi .dot").click(function(){
    
        a = jQuery(this).index();
        
        if (a == fade_num_the_gal) {
        
            jQuery(".the_gallery img:eq(" + fade_num_the_gal + ")").fadeIn(1000);
            jQuery(".the_gallery .navi .dot").removeClass("active");
            jQuery(".the_gallery .navi .dot:eq(" + fade_num_the_gal + ")").addClass("active");
            
        }
        else {
        
            jQuery(".the_gallery img:eq(" + fade_num_the_gal + ")").fadeOut(1000);
            jQuery(".the_gallery img:eq(" + a + ")").fadeIn(1000);
            jQuery(".the_gallery .caption:eq(" + fade_num_the_gal + ")").fadeOut(1000);
            jQuery(".the_gallery .caption:eq(" + a + ")").fadeIn(1000);
            fade_num_the_gal = a;
            jQuery(".the_gallery .navi .dot").removeClass("active");
            jQuery(".the_gallery .navi .dot:eq(" + fade_num_the_gal + ")").addClass("active");
            
        }
        the_gal_stopped = true;
        
    });
    
    ////////////////////////////
    ////product subpage gallery fading
    ////////////////////////////
    
    
    
    var contact_urls = Array();
    
    
    
    jQuery(".pointer a").each(function(){
    
        contact_urls.push(jQuery(this).attr("href"));
        
    });
    
    
    jQuery(".phone_contact div").click(function(){
    
        if (jQuery(this).index() < 3) {
            document.location.href = contact_urls[jQuery(this).index() - 1];
        }
        
    })
    
    
    
    
    
    
    
    
    
    
    jQuery(".w_content ul li ul").slideUp(0);
    
    
    jQuery(".w_content span[id=" + current_post + "]").parent("li").parent("ul").slideDown(0);
    jQuery(".w_content span[id=" + current_post + "]").parent("li").children("ul").slideDown(0);
    
    
    try {
        if (submit_quote) 
            jQuery(document).scrollTo(jQuery("#quote_form").position().top + 360, 600);
    } 
    catch (err) {
    
    }
    
    
    
    
    
    //////////////////////////
    ///////carousel
    //////////////////////////
    jQuery(".product_sub .container").slideUp(0);
    
    jQuery(".product_sub .container:eq(" + current_tab + ")").slideDown(0);
    
    for (current_tab = 0; current_tab < jQuery(".product_sub .container").size(); current_tab++) {
    
        var original_size = jQuery(".product_sub .container:eq(" + current_tab + ")").children("div").size() - 1;
        
        for (i = 0; i <= original_size * 6; i++) {
        
            a = i % (original_size + 1);
            temp = jQuery(".product_sub .container:eq(" + current_tab + ")").children("div:eq(" + a.toString() + ")").clone();
            temp.appendTo(jQuery(".product_sub .container:eq(" + current_tab + ")"));
            
        }
        
        a = original_size * 3 * 127;
        
        jQuery(".product_sub .container:eq(" + current_tab + ")").animate({
            marginLeft: "-=" + a.toString()
        }, 0, "linear");
        
    }
    
    
    jQuery(".navigation_left").bind('click', navigation_left);
    jQuery(".navigation_right").bind('click', navigation_right);
    
    
    function navigation_right(){
    
        jQuery(".product_sub .container:eq(" + current_tab + ")").animate({
            marginLeft: "+=127"
        }, 1000, "linear", function(){
        
        
        });
        
    }////////////navigation_right method
    function navigation_left(){
    
    
        jQuery(".product_sub .container:eq(" + current_tab + ")").animate({
            marginLeft: "-=127"
        }, 1000, "linear", function(){
        
        
        
        });
        
        
    }/////////////navigation_left method
    //////////////////////////
    ///////carousel
    //////////////////////////
    
    
    
    jQuery(".icons div").click(function(){
    
        document.location.href = jQuery(this).children("a").attr("href");
        
    });
    
    
    
    jQuery(".bottom_links div").click(function(){
    
        document.location.href = jQuery(this).children("p").children("a").attr("href");
        
    });
    
    
    
    
    jQuery(".content_tab").hide();
    jQuery(".content_tab:eq(0)").show();
    var current_content_tab = 0;
    
    jQuery(".tab").click(function(){
        b = 0;
        if (jQuery(this).hasClass("red_tab")) 
            b = 1;
        a = jQuery(this).index() - b;
        jQuery(".content_tab:eq(" + a + ")").fadeIn();
        jQuery(".content_tab:eq(" + current_content_tab + ")").hide();
        current_content_tab = a;
        
        
    });
    
    
    /*jQuery(".social div").mouseover(function (){
     if(jQuery(this).attr("class") != "like" && jQuery(this).attr("class") != "share" && jQuery(this).attr("class") != "bbb_top"){
     jQuery(this).animate({marginTop: '-13px'}); alert(jQuery(this.attr("class")));
     }
     
     if(jQuery(this).attr("class") == "bbb_top")
     jQuery(this).animate({marginTop: '-23px'});
     });
     jQuery(".social div").mouseout(function (){
     if(jQuery(this).attr("class") != "like" && jQuery(this).attr("class") != "share")
     jQuery(this).animate({marginTop: '0px'});
     });
     */
    jQuery(".social .live_chat_header").mouseover(function(){
    
    
        jQuery(this).animate({
            marginTop: '-43px'
        });
        alert(jQuery(this.attr("class")));
        
        
    });
    
    jQuery(".social .live_chat_header").mouseout(function(){
    
    
        jQuery(this).animate({
            marginTop: '-1px'
        });
        
        
    });
    
    jQuery(".w_content form textarea").maxLength(60);
    
    
    jQuery(".article .faq h3").click(function(){
    
        if (jQuery(this).parent(".faq").children("p").is(":visible")) {
        
            jQuery(".article p").slideUp(1000);
            jQuery(".article ul").slideUp(1000);
            
        }
        else {
        
            jQuery(".article p").slideUp(1000);
            jQuery(".article ul").slideUp(1000);
            
            jQuery(this).siblings("p").slideDown(1000);
            jQuery(this).siblings("ul").slideDown(1000);
            
        }
        
    });
    
    
    
    jQuery(".icons div").mouseover(function(){
    
    
        jQuery(this).animate({
            marginTop: '-=10'
        }, 200, "linear", function(){
            jQuery(this).animate({
                marginTop: '+=10'
            }, 200, "linear", function(){
            });
        });
        
        
    });
    
    
    jQuery(".footer a").mouseover(function(){
        jQuery(this).css("color", "#ffffff");
    });
    
    jQuery(".footer a").mouseout(function(){
        jQuery(this).css("color", "#bdb194");
    });
    
    
    jQuery(".red_tab").mouseover(function(){
    
        if (!jQuery(this).hasClass("active") && !jQuery(this).hasClass("active_tab")) {
            p = jQuery(this).position();
            
            jQuery(".mouse_over").css("left", p.left);
            new_top = p.top + 1;
            jQuery(".mouse_over").css("top", new_top);
            jQuery(".mouse_over").css("display", "inline");
            new_width = jQuery(this).width() + 20;
            jQuery(".mouse_over").css("width", new_width);
            new_height = jQuery(this).height() + 3;
            jQuery(".mouse_over").css("height", new_height);
            jQuery(this).children("span").css("color", "#ffffff");
        }
    });
    
    jQuery(".red_tab").mouseout(function(){
    
        if (!jQuery(this).hasClass("active") && !jQuery(this).hasClass("active_tab")) {
            p = jQuery(this).position();
            jQuery(".mouse_over").css("left", p.left);
            new_top = p.top + 1;
            jQuery(".mouse_over").css("top", new_top);
            jQuery(".mouse_over").css("display", "none");
            new_width = jQuery(this).width() + 20;
            jQuery(".mouse_over").css("width", new_width);
            new_height = jQuery(this).height() + 3;
            jQuery(".mouse_over").css("height", new_height);
            jQuery(this).children("span").css("color", "#da6767");
        }
        
    });
    
    
});///////////document.ready
jQuery.fn.maxLength = function(max){
    this.each(function(){
        //Get the type of the matched element
        var type = this.tagName.toLowerCase();
        //If the type property exists, save it in lower case
        var inputType = this.type ? this.type.toLowerCase() : null;
        //Check if is a input type=text OR type=password
        if (type == "input" && inputType == "text" || inputType == "password") {
            //Apply the standard maxLength
            this.maxLength = max;
        }
        //Check if the element is a textarea
        else 
            if (type == "textarea") {
                //Add the key press event
                this.onkeypress = function(e){
                    //Get the event object (for IE)
                    var ob = e || event;
                    //Get the code of key pressed
                    var keyCode = ob.keyCode;
                    //Check if it has a selected text
                    var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
                    //return false if can't write more
                    return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
                };
                //Add the key up event
                this.onkeyup = function(){
                    //If the keypress fail and allow write more text that required, this event will remove it
                    if (this.value.length > max) {
                        this.value = this.value.substring(0, max);
                    }
                };
            }
    });
    
    
    jQuery(".sub_gal").attr("style", "display: none;");
    jQuery(".sub_gal:eq(0)").attr("style", "display: block;");
    
    jQuery(".gallery_tab .page_num").click(function(){
    
        a = jQuery(this).text() - 1;
        jQuery(".sub_gal:eq(" + a + ")").attr("style", "display: block;");
        a_last = jQuery(".gallery_tab .active").text() - 1;
        jQuery(".sub_gal:eq(" + a_last + ")").attr("style", "display: none;");
        jQuery(".gallery_tab .active").removeClass("active");
        jQuery(this).addClass("active");
        
    });
    
};

function load_tab_images(current_tab){

    page_id = jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") input").attr("value");
    
    jQuery.ajax({
        url: ajax_url,
        type: 'POST',
        async: true,
        data: ({
            action: "return_images",
            page_id: page_id
        }),
        dataType: 'text',
        timeout: 5000,
        error: function(){
        
        
        
        },
        success: function(respond){
        
        
        
            try {
            
                jQuery(".carousel_mid .img_holder:eq(" + current_tab + ") .loading").hide();
                respondObject = eval(respond);
                
                for (i = 0; i < respondObject.length; i++) {
                
                
                
                    jQuery(".carousel_mid .img_holder:eq(" + current_tab + ")").html(respondObject[i] + jQuery(".carousel_mid .img_holder:eq(" + current_tab + ")").html());
                    
                    
                }
                //jQuery(".carousel .carousel_tabs .tab:eq(" + current_tab + ")").trigger("click");
                
                
                
            } 
            catch (err) {
            
                //alert(err);
            
            }
            
        }///////respond method
    });/////////////ajax call
    return true;
    
}////////load_tab_images method

