// UDMv4.5 //
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//                                                               //
//  ULTIMATE DROP DOWN MENU Version 4.5 by Brothercake          //
//  http://www.udm4.com/                                         //
//                                                               //
//  This script may not be used or distributed without license   //
//                                                               //
///////////////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////////////
// POPUP MENU EXPANSION FUNCTIONS                                //
///////////////////////////////////////////////////////////////////

// Get browser type and version
var mozillabrowser;
var iebrowser;
var operabrowser;
var versionsnummer;
function sniffBrowser() {
	var ua = navigator.userAgent.toLowerCase();
	mozillabrowser = (ua.indexOf("gecko/") != -1);
	iebrowser = (ua.indexOf("msie") > -1 && ua.indexOf("opera") == -1);
	operabrowser = (ua.indexOf("opera") > -1);
	var av = navigator.appVersion;
	var version = av.substring(0, av.indexOf(" ")) *1;
	var version2 = av.substring(0, av.indexOf(".")) *1;
	if (mozillabrowser || operabrowser) {
		versionsnummer = version2;
	} else if (iebrowser){
		if (version2 == 4 && ua.indexOf("msie 5") == -1 && ua.indexOf("msie 6") == -1 && ua.indexOf("msie 7") == -1) {
			versionsnummer = 4;
		} else if (version2 == 4 && ua.indexOf("msie 5") > -1) {
			versionsnummer = 5;
		} else if (version2 == 4 && ua.indexOf("msie 6") > -1) {
			versionsnummer = 6;
		} else if (version2 == 4 && ua.indexOf("msie 7") > -1) {
			versionsnummer = 7;
		}
	}
}

// Open menu with given ID
function openMenu(menuID,linkObj,menuType)  {
	// If the menu code is ready
	if(um.ready) {
		// Get browser type and version
		sniffBrowser();
		
		// Find co-ordinates of link object
		var coords = {
			'x' : um.getRealPosition(linkObj,'x'),
			'y' : um.getRealPosition(linkObj,'y')
		};

		// If the menu should be dropdown style
		if(menuType == 'dropdown') {
			// The relative positioning is browser type depedent
			if(mozillabrowser) {
				var xCorrection = -26;
				var yCorrection = -26;			
			} else {
			  if (versionsnummer < 7) {
				if (versionsnummer < 6) {
				   // Due to CSS issues with IE < 6 a special positioning
				   // is required for theses IE versions
				   var xCorrection = -45;
				   var yCorrection = 113;
				} else {
				  if (versionsnummer == 6) {
				    var xCorrection = -37;
				    var yCorrection = 149;
				  } else {
					var xCorrection = 280;
					var yCorrection = 150;
				  }
				}
				// Since this HTML layout is centered in the viewport,
				// for IE a viewport size dependent correction is required:
				// unlike firefox, is doesn't take into account the centered
				// position of the whole layout when measuring the coordinates
				// of the linkObj. IE always assumes the the page align is left.
				var viewPortSize = um.getWindowDimensions();
				var xViewPortSizeCorrection = (viewPortSize.x - 1024) / 2;
				if (viewPortSize.x < 1024) {
				  xViewPortSizeCorrection = 0;
				}
				xCorrection -= xViewPortSizeCorrection;
			  } else {
				// IE 7 or newer does not have the error described above any more.
				var xCorrection = -24;
				var yCorrection = -26;
			  }
			}
			
			// Increase y-position to place it below the link
			coords.x += (linkObj.offsetHeight + xCorrection);
			coords.y -= yCorrection;  
		}
			
		// If the menu should be flyout style
		if(menuType == 'flyout') {
			// The relative positioning is browser type depedent
			if(mozillabrowser) {
				var xCorrection = 0;
				var yCorrection = 0;
			} else {
			    if (versionsnummer < 7) {
                  var xCorrection = -18;
				  var yCorrection = 175;
				  // Since this HTML layout is centered in the viewport,
				  // for IE a viewport size dependent correction is required:
				  // unlike firefox, is doesn't take into account the centered
				  // position of the whole layout when measuring the coordinates
				  // of the linkObj. IE always assumes the the page align is left.
				  var viewPortSize = um.getWindowDimensions();
				  var xViewPortSizeCorrection = (viewPortSize.x - 1024) / 2;
				  if (viewPortSize.x < 1024) {
				    xViewPortSizeCorrection = 0;
				  }
				  xCorrection -= xViewPortSizeCorrection;
				} else {
				  // IE 7 or newer does not have the error described above any more.
				  var xCorrection = 0;
				  var yCorrection = 0;					
				}
			}
			// alert("Offset: " + linkObj.offsetWidth + " -- viewPortSize: " + viewPortSize.x + " -- xCorrection: " + xCorrection + " -- xViewPortSizeCorrection: " + xViewPortSizeCorrection);		
			coords.x += (linkObj.offsetWidth + xCorrection);
			coords.y -= yCorrection;
		}
			
		// Activate menu at returned co-ordinates
		um.activateMenu(menuID, coords.x + 'px', coords.y + 'px');
	}
}

	
// Close menu with given ID
function closeMenu(menuID) {
	// If the menu code is ready
	if(um.ready) {
		// Deactivate menu with given id
		um.deactivateMenu(menuID);
	}
}
	
// Close all menus
function closeMenus() {
	 // If the menu code is ready
	 if(um.ready) {
	   	// Deactivate all menus
		um.closeAllMenus();
	 }
}