// include separated js files
function includeJS(url) { document.writeln("<script type=\"text/javascript\" src=\"" + url + "\"></script>"); }

/* conditional inclusion of prototype.js, if url matches */
if (/\/fashionshows\/powersearch/i.test(location.href) 
	|| /\/stylefile/i.test(location.href)
	|| /\/sartorialist/i.test(location.href)
	|| /\/beauty\/beautycounter/i.test(location.href)
	|| /\/lookbooks/i.test(location.href)
	|| /\/mylookbooks/i.test(location.href)
	|| /\/peopleparties\/celebritysearch/i.test(location.href)
	|| /\/peopleparties\/modelsearch/i.test(location.href)
	|| /\/beauty\/beautysearch/i.test(location.href)
    || /\/slideshow\//i.test(location.href)
) {
	
	/******************** UNMODIFIED PROTOTYPE.JS VERSIONS ***************************************************************/
//	includeJS("prototype.js");
	
	// uncompressed file used for developemnt purposes: 124K
	//includeJS("/scripts/clearcase/prototype/prototype-1.6.0.2.js");
	
	// uncompressed file used for developemnt purposes: 94K
	//includeJS("/scripts/clearcase/prototype/prototype-1.5.1.2.js");
	
	

	
}

if (!/^(\w+:\/\/)?[^\/]+\/?$/.test(location.href)) /* if not homepage */ { 
	/* BB 6/19/2008 - jquery_noconflict.js has been added immediately after jQuery is sourced, and does nothing but call jQuery.noConflict().  All jQuery code must use jQuery() notation instead of $() */
	includeJS("/scripts/jquery.js"); /* needed for marketing module and quicklinks which are global modules */
	includeJS("/scripts/jquery_noconflict.js"); /* needed for marketing module and quicklinks which are global modules */

	includeJS("/scripts/latestModule.js");
}

/* conditional exclusion from slide show windows rally DE86 */
if (!/\/slideshow\//.test(location.href)) {
    includeJS("/scripts/exitpop.js");
}

/* conditional inclusion of yahoo libs, if url matches */

if (/\/user\/preferences/i.test(location.href) 
	|| /\/user\/registration/i.test(location.href) 
	|| /\/community\/lookbooks/i.test(location.href)
	|| /\/community\/mylookbooks/i.test(location.href)
	|| /\/stylefile/i.test(location.href)
	|| /\/beautycounter/i.test(location.href)
	|| /\/vogue\/scene/i.test(location.href)
	|| /\/fashionshows\/sartorialist/i.test(location.href)
)
{
	includeJS("/scripts/yui/build/yahoo-dom-event/yahoo-dom-event.js");
	includeJS("/scripts/yui/build/container/container_core-min.js"); 
}








function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing
    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;











var Handler = {};

// In DOM-compliant browsers, our functions are trivial wrappers around
// addEventListener() and removeEventListener().
if (document.addEventListener) {
    Handler.add = function(element, eventType, handler) {
        element.addEventListener(eventType, handler, false);
    };

    Handler.remove = function(element, eventType, handler) {
        element.removeEventListener(eventType, handler, false);
    };
}
// In IE 5 and later, we use attachEvent() and detachEvent(), with a number of
// hacks to make them compatible with addEventListener and removeEventListener.
else if (document.attachEvent) {
    Handler.add = function(element, eventType, handler) {
        // Don't allow duplicate handler registrations
        // _find() is a private utility function defined below.
        if (Handler._find(element, eventType, handler) != -1) return;
        
        // To invoke the handler function as a method of the
        // element, we've got to define this nested function and register
        // it instead of the handler function itself.
        var wrappedHandler = function(e) {
            if (!e) e = window.event;

            // Create a synthetic event object with partial compatibility
            // with DOM events.
            var event = {
                _event: e,    // In case we really want the IE event object
                type: e.type,           // Event type
                target: e.srcElement,   // Where the event happened
                currentTarget: element, // Where we're handling it
                relatedTarget: e.fromElement?e.fromElement:e.toElement,
                eventPhase: (e.srcElement==element)?2:3,

                // Mouse coordinates
                clientX: e.clientX, clientY: e.clientY,
                screenX: e.screenX, screenY: e.screenY,
                
                // Key state
                altKey: e.altKey, ctrlKey: e.ctrlKey,
                shiftKey: e.shiftKey, charCode: e.keyCode,

                // Event management functions
                stopPropagation: function() {this._event.cancelBubble = true;},
                preventDefault: function() {this._event.returnValue = false;}
            }

            // Invoke the handler function as a method of the element, passing
            // the synthetic event object as its single argument.
            // Use Function.call() if defined; otherwise do a hack
            if (Function.prototype.call) 
                handler.call(element, event);
            else {
                // If we don't have Function.call, fake it like this
                element._currentHandler = handler;
                element._currentHandler(event);
                element._currentHandler = null;
            }
        };

        // Now register that nested function as our event handler.
        element.attachEvent("on" + eventType, wrappedHandler);
        
        // Now we must do some record keeping to associate the user-supplied
        // handler function and the nested function that invokes it.

        // We have to do this so that we can deregister the handler with the
        // remove() method and also deregister it automatically on page unload.

        // Store all info about this handler into an object
        var h = {
            element: element,
            eventType: eventType,
            handler: handler,
            wrappedHandler: wrappedHandler
        };

        // Figure out what document this handler is part of.
        // If the element has no "document" property, it is not
        // a window or a document element, so it must be the document
        // object itself.
        var d = element.document || element;
        // Now get the window associated with that document
        var w = d.parentWindow;

        // We have to associate this handler with the window,
        // so we can remove it when the window is unloaded
        var id = Handler._uid();  // Generate a unique property name
        if (!w._allHandlers) w._allHandlers = {};  // Create object if needed
        w._allHandlers[id] = h; // Store the handler info in this object

        // And associate the id of the handler info with this element as well
        if (!element._handlers) element._handlers = [];
        element._handlers.push(id);

        // If there is not an onunload handler associated with the window,
        // register one now.
        if (!w._onunloadHandlerRegistered) {
            w._onunloadHandlerRegistered = true;
            w.attachEvent("onunload", Handler._removeAllHandlers);
        }
    };

    Handler.remove = function(element, eventType, handler) {
        // Find this handler in the element._handlers[] array.
        var i = Handler._find(element, eventType, handler);
        if (i == -1) return;  // If the handler was not registered, do nothing

        // Get the window of this element
        var d = element.document || element;
        var w = d.parentWindow;

        // Look up the unique id of this handler
        var handlerId = element._handlers[i];
        // And use that to look up the handler info
        var h = w._allHandlers[handlerId];
        // Using that info, we can detach the handler from the element
        element.detachEvent("on" + eventType, h.wrappedHandler);
        // Remove one element from the element._handlers array
        element._handlers.splice(i, 1);
        // And delete the handler info from the per-window _allHandlers object
        delete w._allHandlers[handlerId];
    };

    // A utility function to find a handler in the element._handlers array
    // Returns an array index or -1 if no matching handler is found
    Handler._find = function(element, eventType, handler) {
        var handlers = element._handlers;
        if (!handlers) return -1;  // if no handlers registered, nothing found

        // Get the window of this element
        var d = element.document || element;
        var w = d.parentWindow;

        // Loop through the handlers associated with this element, looking
        // for one with the right type and function.
        // We loop backward because the most recently registered handler
        // is most likely to be the first removed one.
        for(var i = handlers.length-1; i >= 0; i--) {
            var handlerId = handlers[i];        // get handler id
            var h = w._allHandlers[handlerId];  // get handler info
            // If handler info matches type and handler function, we found it.
            if (h.eventType == eventType && h.handler == handler) 
                return i;
        }
        return -1;  // No match found
    };

    Handler._removeAllHandlers = function() {
        // This function is registered as the onunload handler with 
        // attachEvent.  This means that the this keyword refers to the
        // window in which the event occurred.
        var w = this;

        // Iterate through all registered handlers
        for(id in w._allHandlers) {
            // Get handler info for this handler id
            var h = w._allHandlers[id]; 
            // Use the info to detach the handler
            h.element.detachEvent("on" + h.eventType, h.wrappedHandler);
            // Delete the handler info from the window
            delete w._allHandlers[id];
        }
    }

    // Private utility to generate unique handler ids
    Handler._counter = 0;
    Handler._uid = function() { return "h" + Handler._counter++; };
}


function wrapEventHandler(object, eventHandler) {
	return function() { object[eventHandler].apply(object, arguments); }
}



/**
 * Browser Detection by QuirksMode.org (http://www.quirksmode.org/js/detect.html)
 * 
 * Browser name: BrowserDetect.browser
 * Browser version: BrowserDetect.version
 * OS name: BrowserDetect.OS
 */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();







runOnLoad(addMainNavEvents);

//runOnLoad(addSearchEvents);

function addMainNavEvents()
{

var mainNavEl;
var navLinks = [];
var mainNavLinks = [];
var navMenus = [];

if (document.getElementById("main_nav"))
    {
        mainNavEl = document.getElementById("main_nav");
        
        navLinks = mainNavEl.getElementsByTagName('a');
        
        for (var i=0; i < navLinks.length; i++) 
        {            
            if ( navLinks[i].parentNode.nodeName == "DIV") {
                mainNavLinks.push(navLinks[i]);    
            }
        }
        // add mouseover and mouseout events to main nav links
        for (var j = 0; j < mainNavLinks.length; ++j) {
   
            Handler.add(mainNavLinks[j], "mouseover", function(e) {
                mainNavMenus("dd_" + this.id, true);
            });
            
            Handler.add(mainNavLinks[j], "mouseout", function(e) {
                mainNavMenus("dd_" + this.id, false);
            });
        }  
        
        navMenus = mainNavEl.getElementsByTagName('ul');
        
        // add mouseover and mouseout events to main nav dropdowns
        for (var k = 0; k < navMenus.length; ++k) {
           
            Handler.add(navMenus[k], "mouseover", function(e) {
                mainNavMenus(this.id, true);
            });
            
            Handler.add(navMenus[k], "mouseout", function(e) {
                mainNavMenus(this.id, false);
            });
        }  
    }
}

function mainNavMenus(id, display)
{

var navID;
var navEl;
var ulEl;

var bodyId = document.body.id;

navID = id.replace(/dd_/,"");

if (document.getElementById(id))
    {
        ulEl = document.getElementById(id);
    }

if (display)
    {      
        if (document.getElementById(navID))
        {
            navEl = document.getElementById(navID);
            navEl.style.backgroundColor = "#0e1113";
            navEl.style.color = "#fff";
        }
        
        if (ulEl && bodyId != 'trendsshopping_candy_index')
            ulEl.style.visibility = "visible";

    }
    
else
    {
        if (document.getElementById(navID))
        {
            navEl = document.getElementById(navID);
            navEl.style.backgroundColor = "transparent";
            navEl.style.color = "#000";
        }
        
        if (ulEl && bodyId != 'trendsshopping_candy_index')
            ulEl.style.visibility = "hidden";
    
    }
}

