$(document).ready(function () {
    //remove outline from links 
    $('a').click(function () {
        this.blur();
    });

    /*
    When the mouse enters #officeSupplies, it shows the 
    flyout. When the mouse leaves #officesupplies, it 
    checks if it left to enter the flyout, and if it did, it
    stays. if it didn't then it hides.
    if it leaves the flyout, it hides. 
    */
    $('.genLink').hover(function () {
        $('.menuSelect').addClass("menuSelectHighlight");
    }, function () {
        $('.menuSelect').removeClass("menuSelectHighlight");
    });

    //Office SUpplies
    $('#officeSupplies').hoverIntent(function () {
        $('#flyOfficeSupplies').show();
        //
        $('#flyCleaning').hide();
        $('#flyTechnology').hide();
        $('#flyFurniture').hide();
        $('#flyToner').hide();
    }, function () {
        $('#flyOfficeSupplies').hover(function () {
            $('#officeSupplies').addClass('hover');
            $('#flyOfficeSupplies').show();
       }, function () {
            $('#flyOfficeSupplies').hide();
            $('#officeSupplies').removeClass('hover');
        });
        $('#officeSupplies').mouseleave(function () {
            $('#flyOfficeSupplies').hide();
        });
    });

    $('#flyOfficeSupplies').mouseenter(function () {
        $('#officeSupplies').addClass('hover');
    }).mouseleave(function () {
        $('#officeSupplies').removeClass('hover');
    });


    //Cleaning/Breakroom
    $('#cleaning').hoverIntent(function () {
        $('#flyCleaning').show();
        //
        $('#flyOfficeSupplies').hide();
        $('#flyTechnology').hide();
        $('#flyFurniture').hide();
        $('#flyToner').hide();
    }, function () {
        $('#flyCleaning').hover(function () {
            $('#cleaning').addClass('hover');
            $('#flyCleaning').show();
        }, function () {
            $('#flyCleaning').hide();
            $('#cleaning').removeClass('hover');
        });
        $('#cleaning').mouseleave(function () {
            $('#flyCleaning').hide();
        });
    });
    $('#flyCleaning').mouseenter(function () {
        $('#cleaning').addClass('hover');
    }).mouseleave(function () {
        $('#cleaning').removeClass('hover');
    });


    //Technology
    $('#technology').hoverIntent(function () {
        $('#flyTechnology').show();
        //
        $('#flyOfficeSupplies').hide();
        $('#flyCleaning').hide();
        $('#flyFurniture').hide();
        $('#flyToner').hide();
    }, function () {
        $('#flyTechnology').hover(function () {
            $('#technology').addClass('hover');
            $('#flyTechnology').show();
        }, function () {
            $('#flyTechnology').hide();
            $('#technology').removeClass('hover');
        });
        $('#technology').mouseleave(function () {
            $('#flyTechnology').hide();
        });
    });
    $('#flyTechnology').mouseenter(function () {
        $('#technology').addClass('hover');
    }).mouseleave(function () {
        $('#technology').removeClass('hover');
    });


    //Furniture
    $('#furniture').hoverIntent(function () {
        $('#flyFurniture').show();
        //
        $('#flyOfficeSupplies').hide();
        $('#flyCleaning').hide();
        $('#flyTechnology').hide();
        $('#flyToner').hide();
    }, function () {
        $('#flyFurniture').hover(function () {
            $('#furniture').addClass('hover');
            $('#flyFurniture').show();
        }, function () {
            $('#flyFurniture').hide();
            $('#furniture').removeClass('hover');
        });
        $('#furniture').mouseleave(function () {
            $('#flyFurniture').hide();
        });
    });
    $('#flyFurniture').mouseenter(function () {
        $('#furniture').addClass('hover');
    }).mouseleave(function () {
        $('#furniture').removeClass('hover');
    });


    //Ink
    $('#toner').hoverIntent(function () {
        $('#flyToner').show();
        //
        $('#flyOfficeSupplies').hide();
        $('#flyCleaning').hide();
        $('#flyTechnology').hide();
        $('#flyFurniture').hide();
    }, function () {
        $('#flyToner').hover(function () {
            $('#toner').addClass('hover');
            $('#flyToner').show();
        }, function () {
            $('#flyToner').hide();
            $('#toner').removeClass('hover');
        });
        $('#toner').mouseleave(function () {
            $('#flyToner').hide();
        });
    });
    $('#flyToner').mouseenter(function () {
        $('#toner').addClass('hover');
    }).mouseleave(function () {
        $('#toner').removeClass('hover');
    });

    
    //Shopping Cart Toggle
    $('.showCart').click(function (e) {
        e.preventDefault();
        $('#flyCart').slideToggle('fast');
        $('.cartToggle').toggleClass('cartToggleOff');
    });

    $('#cartContents').click(function () {
        $('#flyCart').slideToggle('fast');
        $('.cartToggle').toggleClass('cartToggleOff');
    });


    /* *** QUICK LINK HOVERS***  */
    $('#browseProducts').mouseenter(function () {
        $('#browseAllProducts').addClass('bgOn');
    }).mouseleave(function () {
        $('#browseAllProducts').removeClass('bgOn');
    });

    //
    $('#myAccount').mouseenter(function () {
        $('#tMyAccount').addClass('bgOn');
    }).mouseleave(function () {
        $('#tMyAccount').removeClass('bgOn');
    });

    //
    $('#myList').mouseenter(function () {
        $('#tMyList').addClass('bgOn');
    }).mouseleave(function () {
        $('#tMyList').removeClass('bgOn');
    });
    //
    $('#specialsSurveys').mouseenter(function () {
        $('#tSpecials').addClass('bgOn');
    }).mouseleave(function () {
        $('#tSpecials').removeClass('bgOn');
    });
    $('#myBookmarks').mouseenter(function () {
        $('#tBookmarks').addClass('bgOn');
    }).mouseleave(function () {
        $('#tBookmarks').removeClass('bgOn');
    });

    /* ***************** */


    $('.login').click(function (e) {
        e.preventDefault();
        $('#flyLogin').slideToggle('fast');
    });

    $('#txtSearch').example('Search for...', {
        className: 'not_example'
    });

    $('.txtQty').example('Qty', {
        className: 'not_example'
    });

    $('#shopContents .comment').example('Comment: ex. boss’s supplies, for tech support, etc...', {
        className: 'not_example'
    });


    /* Toggle Quick Links and Personal Assistant */
    $('#quickLinks h2').click(function () {
        $('.toggleQl').slideToggle();
        $('#quickLinks .arrowToggle').toggleClass('arrowOn');
    });


    $('#pa h2').click(function () {
        $('.togglePa').slideToggle();
        $('#pa .arrowToggle').toggleClass('arrowOn');
    });


    $(function () {
        $('input').keydown(function (e) {
            if (e.keyCode == 13) {
                $(this).parents('form').submit();
                return false;
            }
        });
    });

    /* removing curosr pull to search box
    if (typeof (tCursor) == 'undefined') {
    //get cursor
    $('#txtSearch').focus();
    } else {
    //do nothingtarg
    }
    */
});

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}


