PINT.vs = { 
menus : [{'title':'Monitors ', 'href':'/products/desktop-monitors/', items: [{'title':'Application', 'href':'#', items: [{'title':'Home &amp; Office', 'href':'/home-office/desktop-monitors/'},{'title':'Entertainment', 'href':'/entertainment/desktop-monitors/'},{'title':'Business', 'href':'/business/desktop-monitors/'},{'title':'Government/Education', 'href':'/government/desktop-monitors/'}]},{'title':'Technology', 'href':'#', items: [{'title':'LCD', 'href':'http://www.viewsonic.com/products/desktop-monitors/search-results.php?'},{'title':'LED Backlit', 'href':'/products/desktop-monitors/lcd/led-lcd/'},{'title':'3D', 'href':'/products/desktop-monitors/v3d-series/'}]},{'title':'Series', 'href':'#', items: [{'title':'Graphic Series', 'href':'/products/desktop-monitors/lcd/graphic-series/'},{'title':'LCD &amp; LED', 'href':'/products/desktop-monitors/lcd/led-lcd/'},{'title':'Pro Series', 'href':'/products/desktop-monitors/lcd/pro-series/'},{'title':'Value Series', 'href':'/products/desktop-monitors/lcd/value-series/'},{'title':'X Series', 'href':'/products/desktop-monitors/lcd/x-series/'},{'title':'3D Series', 'href':'/products/desktop-monitors/v3d-series/'}]},{'title':'View All Models', 'href':'/products/desktop-monitors/view-all-models/'},{'title':'Monitor Videos', 'href':'/products/desktop-monitors/monitor-videos/'},{'title':'LED TCO Calculator', 'href':'/products/desktop-monitors/led-tco-calculator/'}]},{'title':'TVs', 'href':'/products/tvs/', items: [{'title':'Home &amp; Office', 'href':'/home-office/lcd-tv/'},{'title':'Home Theater', 'href':'/entertainment/lcd-tv/home-theater/'},{'title':'Gaming', 'href':'/entertainment/lcd-tv/gaming/'},{'title':'Any Room', 'href':'/entertainment/lcd-tv/other-room/'},{'title':'View All Models', 'href':'/products/tvs/view-all-models/'},{'title':'HDTV Videos', 'href':'/products/tvs/hdtv-videos/'},{'title':'LED TVs TCO Calculator', 'href':'/products/tvs/ledtv-tco-calculator/'}]},{'title':'Projectors', 'href':'/products/projectors/', items: [{'title':'Application', 'href':'#', items: [{'title':'Home &amp; Office', 'href':'/home-office/projectors/'},{'title':'Entertainment', 'href':'/entertainment/projectors/'},{'title':'Business', 'href':'/business/projectors/'},{'title':'Government', 'href':'/government/projectors/'},{'title':'Education', 'href':'/education/projectors/'}]},{'title':'Technology', 'href':'#', items: [{'title':'General Purpose', 'href':'/products/projectors/technology/general-purpose/'},{'title':'Wide Format', 'href':'/products/projectors/technology/wide-format/'},{'title':'Large Venue', 'href':'/products/projectors/technology/large-venue/'},{'title':'Networkable', 'href':'/products/projectors/technology/networkable/'},{'title':'Short Throw', 'href':'/products/projectors/technology/short-throw/'},{'title':'Ultra Portable', 'href':'/products/projectors/technology/ultra-portable/'},{'title':'Interactive', 'href':'/products/projectors/technology/interactive/'},{'title':'Full HD 1080p', 'href':'/products/projectors/technology/full-hd-1080p/'},{'title':'High Brightness', 'href':'/products/projectors/technology/high-brightness/'},{'title':'LCD Projectors', 'href':'/products/projectors/technology/lcd-projectors/'},{'title':'LED Projectors', 'href':'/products/projectors/technology/led-projectors/'}]},{'title':'Accessories', 'href':'/products/projectors/accessories/'},{'title':'Calculators', 'href':'#', items: [{'title':'DLP TCO Calculator', 'href':'/products/projectors/dlp-tco-calculator/'},{'title':'Distance Calculator', 'href':'/products/projectors/distance-calculator/'}]},{'title':'View All Products', 'href':'/products/projectors/view-all-projectors/'},{'title':'Projector Videos', 'href':'/products/projectors/projector-videos/'}]},{'title':'Digital Signage', 'href':'/products/digital-signage/', items: [{'title':'Products', 'href':'#', items: [{'title':'Commercial Displays', 'href':'/products/digital-signage/commercial-displays/'},{'title':'ePosters', 'href':'/products/digital-signage/eposters/'},{'title':'Media Players', 'href':'/products/digital-signage/network-media-players/'},{'title':'Digital Signage Software', 'href':'/products/digital-signage/digital-signage-software/'}]},{'title':'Solutions', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/', items: [{'title':'Hospitality &amp; Leisure', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/hospitality-transportation/'},{'title':'Transportation', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/hospitality-transportation/'},{'title':'Education', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/education/'},{'title':'Retail &amp; POS', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/retail-e-poster/'},{'title':'Corporate Communications', 'href':'http://www.viewsonic.com/products/digital-signage/applications-solutions/corporate-communication/'}]},{'title':'View All Models', 'href':'/products/digital-signage/view-all-products/'},{'title':'Digital Signage Videos', 'href':'/products/digital-signage/digital-signage-videos/'}]},{'title':'Photo Frames', 'href':'/products/digital-photo-frames/', items: [{'title':'View All Models', 'href':'/products/digital-photo-frames/view-all-models/'},{'title':'Photo Frame Videos', 'href':'/products/digital-photo-frames/photo-frame-videos/'}]},{'title':'Digital Media Devices', 'href':'http://www.viewsonic.com/products/digital-media-devices/', items: [{'title':'Cameras &amp; Devices', 'href':'/products/digital-media-devices/cameras-camcorders/'},{'title':'Media Players', 'href':'/products/digital-media-devices/media-players/'},{'title':'View All Products', 'href':'/products/digital-media-devices/view-all-products/'},{'title':'Device Videos', 'href':'/products/digital-media-devices/device-videos/'}]},{'title':'Computers &amp; Tablets', 'href':'/products/computing/', items: [{'title':'All-In-One PCs', 'href':'/products/computing/all-in-ones/'},{'title':'PC Minis', 'href':'/products/computing/pc-mini/'},{'title':'Tablets &amp; e-Readers', 'href':'/products/tablets/view-all-models/'},{'title':'View All Products', 'href':'/products/computing/view-all-models/'},{'title':'Computer Videos', 'href':'/products/computing/computer-videos/'}]},{'title':'Solutions', 'href':'http://www.viewsonic.com/multiclient', items: [{'title':'MultiClient Solution', 'href':'http://www.viewsonic.com/multiclient'}]}],
_createMenu : function (items, level, genealogy) {
	// Find the menu element
	var div = document.getElementById('menu_'+level);
	
	// Begin the links inside our menu
	var menuId = 'submenu_'+genealogy.join('_');
	var html = '<div id="'+menuId+'" class="scroll"><ul>';
	for (var i=0; i<items.length; i++) {
		genealogy[level] = (i+1);
		var id = genealogy.join('_');
		// If there are children inside the current list item
		var isParent = (items[i].items && items[i].items.length) ? true : false;
		html = html + '<li';
		if (i==items.length-1) html = html + ' class="last"';
		html = html + '><a href="'+items[i].href+'"';
		if (isParent) html = html + ' class="parent"';
		html = html + '" id="link_'+id+'">'+items[i].title+'</a>';
	}
	html = html + '</ul></div>';
	div.innerHTML = html;

	// Make the last div scrollable and then hide it (quirkiness of flexcroll)
	if (level == 2 && items.length > 6) { 
		// Creates the scroller and hides it (it has to be shown at first to get calculations right)
		div.style.display = 'block'; 
		CSBfleXcroll(menuId);
		div.style.display = 'none'; 
	}
	else if (level == 3 && items.length > 6) { 
		// Creates the scroller and hides it (it has to be shown at first to get calculations right)
		div.style.display = 'block'; 
		CSBfleXcroll(menuId);
		div.style.display = 'none'; 
	}
	// Add listeners to the A tags
	if (level < 3) YAHOO.util.Event.on(div.getElementsByTagName('a'), 'mouseover', PINT.vs._showNextMenu);
},
_prepareMenu : function(e) {
	var isLast = false;

	// Get the menu containing the link that was moused over.
	var menu = this.parentNode.parentNode.parentNode.parentNode;
	
	// Remove any previous selections
	YAHOO.util.Dom.removeClass(menu.getElementsByTagName('a'), 'selected');
	
	// Build out the genealogy of the current menu
	var genealogy = menu.id.split('_');
		genealogy = genealogy.slice(1,4);
		
	// Find the current menu level
	var level = menu.className.split('_')[1];
	
	// Find the level of the link (when added into the genealogy, we can figure out the menu to be shown)
	var nextLevel =  this.className.split(' ')[0].split('_')[1];
	if (genealogy[0] == 0) genealogy[0] = nextLevel;
	else if (genealogy[1] == 0) genealogy[1] = nextLevel;
	else if (genealogy[2] == 0) genealogy[2] = nextLevel;
	else isLast = true;
	
	
	if (!isLast) {
		// Select the link if it is a parent
		if (YAHOO.util.Dom.hasClass(this, 'parent')) YAHOO.util.Dom.addClass(this, 'selected');
		
		var genealogyId = 'menu_' + genealogy.join('_');
		
		// Hide previous divs by checking to see if they are a higher level than our current level AND making sure the next divToShow is not already showing
		var checkLevel = function(el) {
			return (el.className.split(' ')[0].split('_')[1] > level && el.id != genealogyId);
		}
		var divsToHide = YAHOO.util.Dom.getElementsBy(checkLevel, 'div', 'menu');
		YAHOO.util.Dom.setStyle(divsToHide, 'display', 'none');
		

		// Check to see if the next menu exists and that it's not currently showing
		var divToShow = YAHOO.util.Dom.get(genealogyId);
		
		// If it does, we show it
		if (divToShow && divToShow.style.display != 'block') PINT.vs._showMenu(divToShow, level);
	}
},
_showNextMenu : function() {
	var isLast = false;

	// Get the menu containing the link that was moused over.
	var menu = this.parentNode.parentNode.parentNode.parentNode;
	
	// Remove any previous selections
	YAHOO.util.Dom.removeClass(menu.getElementsByTagName('a'), 'selected');
	
	// Build out the genealogy of the current link
	var genealogy = this.id.split('_');
		genealogy = genealogy.slice(1,5);
	
	// Figure out the next level
	var nextLevel = 0;
	if (genealogy[1] == 0) {
		nextLevel = 1;
	} else if (genealogy[2] == 0) {
		nextLevel = 2;
	} else if (genealogy[3] == 0) {
		nextLevel = 3;
	}
	
	var items = [];
	if (nextLevel >= 1) items = PINT.vs.menus[(genealogy[0]-1)].items;
	if (nextLevel >= 2 ) items = items[[(genealogy[1]-1)]].items;
	if (nextLevel >= 3) items = items[[(genealogy[2]-1)]].items;
	if (items && items.length) {
		PINT.vs._createMenu(items, nextLevel, genealogy);
		PINT.vs._showMenu(YAHOO.util.Dom.get('menu_'+nextLevel), nextLevel);
	} else {
		nextLevel--;
	}

	// Select the link if it is a parent
	if (YAHOO.util.Dom.hasClass(this, 'parent')) YAHOO.util.Dom.addClass(this, 'selected');
	// Hide divs from last to first
	for (var i=4; i>nextLevel; i--) {
		YAHOO.util.Dom.setStyle('menu_'+i, 'display', 'none');
	}

	
},
_showMenu : function(menu, level) {
	// by/from value default
	var position = [0,0];
	
	// Hardcoded by/from values
	if (level == 1) position = [162, 10];
	else if (level == 2) position = [182, 172];
	else if (level == 3) position = [182, 354];
	
	var slide = new YAHOO.util.Anim(menu, { 
	    left: { by : position[0], from : position[1] }  
	}, .25, YAHOO.util.Easing.easeOut); 
	slide.onStart.subscribe(function() { menu.style.left = position[1] + 'px'; menu.style.display = 'block'; });
	slide.animate();
	
	// Remove any pre-selections
	YAHOO.util.Dom.removeClass(menu.getElementsByTagName('a'), 'selected');
},
init : function() {
	PINT.vs.menuContainer = YAHOO.util.Dom.get('menu');	
    PINT.vs._createMenu(PINT.vs.menus, 0, [0,0,0,0]);
	
	PINT.vs.menu = new PINT.MenuCreator({
		delay:1000, 
        menus:[
			{ id: 'menu', trigger: 'productsMenuLink' },
			{ id: 'menu_shop', trigger: 'shopMenuLink' },
			{ id: 'menu_support', trigger: 'supportMenuLink' },
			{ id: 'menu_company', trigger: 'companyMenuLink' }
		],
        show:function(trigger,menu) {
			if (trigger.parentNode.className.substr(trigger.parentNode.className.length-6) !== '_hover') { 
                    trigger.parentNode.className += '_hover';
            }
			YAHOO.util.Dom.setStyle(menu, 'display', 'block');
			if (menu.id == 'menu') YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getChildren(menu)[0], 'display', 'block');
			YAHOO.util.Dom.removeClass(menu.getElementsByTagName('a'), 'selected');
        },
        hide:function(trigger,menu) {
            if (trigger.parentNode.className.substr(trigger.parentNode.className.length-6) === '_hover') { 
                trigger.parentNode.className = trigger.parentNode.className.substr(0, trigger.parentNode.className.length-6);
            }
            if (menu.id == 'menu') YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getChildren(menu), 'display', 'none');
           	YAHOO.util.Dom.setStyle(menu, 'display', 'none');
        }
	});

	// Removes the IE6 flicker during animation
	if (PINT.util.browser.bIe6) try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
}
};

// This function creates a show/hide relationship between a trigger and menu object.
PINT.MenuCreator = function(obj) {
	for (var oMenu in obj.menus) {
		(function(index, total) {
			var trigger = (obj.menus[index].trigger) ? YAHOO.util.Dom.get(obj.menus[index].trigger) : YAHOO.util.Dom.get(obj.menus[index]);
			var menu = (obj.menus[index].id) ? YAHOO.util.Dom.get(obj.menus[index].id) : YAHOO.util.Dom.get(obj.menus[index]).lastChild;
			var timeout;
			
			if (!trigger || !menu || !obj.show || !obj.hide) return;

			function enterLink(e) {
				clearTimeout(timeout);
				clearMenus();
				obj.show(trigger,menu);
				YAHOO.util.Event.stopEvent(e);
				YAHOO.util.Event.on(document.body, 'mouseout', exitField);
			}

			function exitLink(e) {
				if (!e) var e = window.event;
				if (e.stopPropagation) e.stopPropagation();
				else e.cancelBubble = true;
				var relTarg = e.relatedTarget || e.toElement;
				if (relTarg && relTarg != menu && relTarg.parentNode != menu && relTarg.parentNode.parentNode != menu) {
					timeout = setTimeout(function() {obj.hide(trigger, menu);},obj.delay);
                    YAHOO.util.Event.removeListener(document.body, 'mouseout', exitField);
					return false;
				}
				YAHOO.util.Event.stopEvent(e);
			}
			
			function enterField(e) {
				clearTimeout(timeout);
			}

			function exitField(e) {
				if (!e) var e = window.event;
				var tg = (window.event) ? e.srcElement : e.target;
				if (e.stopPropagation) e.stopPropagation();
				else e.cancelBubble = true;
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				while (reltg&& reltg != tg && reltg.nodeName != 'BODY' && reltg.nodeName != 'HTML') {
					if (reltg == this) return;
					reltg = reltg.parentNode
				}
				if (reltg == tg) return;
				
				YAHOO.util.Event.removeListener(document.body, 'mouseout', exitField);
				YAHOO.util.Event.stopEvent(e);
				timeout = setTimeout(function() {obj.hide(trigger, menu);},obj.delay);
			}
			
			
			function clearMenus() {
				for (var i=0; i<total; i++) {
					obj.hide((obj.menus[i].nodeName != 'LI') ? YAHOO.util.Dom.get(obj.menus[i].trigger) : YAHOO.util.Dom.get(obj.menus[i]), (obj.menus[i].nodeName != 'LI') ? YAHOO.util.Dom.get(obj.menus[i].id) : YAHOO.util.Dom.get(obj.menus[i]).lastChild);
				}
			}
			YAHOO.util.Event.on(trigger, 'mouseover', enterLink);
			YAHOO.util.Event.on(trigger, 'mouseout', exitLink);
			YAHOO.util.Event.on(menu, 'mouseover', enterField);
			YAHOO.util.Event.on(menu, 'mouseout', exitField);
		}(oMenu, obj.menus.length));
	}
};
if (!PINT.isEditing) YAHOO.util.Event.onAvailable('menu', PINT.vs.init);
