// jQuery.noConflict();

var jkmegamenu={

effectduration: 300, //duration of animation, in milliseconds
delaytimer: 200, //delay after mouseout before menu should be hidden, in milliseconds

//No need to edit beyond here
megamenulabels: [],
megamenus: [], //array to contain each block menu instances
zIndexVal: 1000, //starting z-index value for drop down menu
$shimobj: null,

addshim:function($){
	$(document.body).append('<IFRAME id="outlineiframeshim" src="'+(location.protocol=="https:"? 'blank.htm' : 'about:blank')+'" style="display:none; left:0; top:0; z-index:999; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>')
	this.$shimobj=$("#outlineiframeshim")
},

alignmenu:function($, e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $anchor=megamenu.$anchorobj
	var $menu=megamenu.$menuobj
	var menuleft=($(window).width()-(megamenu.offsetx-$(document).scrollLeft())>megamenu.actualwidth)? megamenu.offsetx : megamenu.offsetx-megamenu.actualwidth+megamenu.anchorwidth //get x coord of menu
	//var menutop=($(window).height()-(megamenu.offsety-$(document).scrollTop()+megamenu.anchorheight)>megamenu.actualheight)? megamenu.offsety+megamenu.anchorheight : megamenu.offsety-megamenu.actualheight
	var menutop=megamenu.offsety+megamenu.anchorheight  //get y coord of menu
	$menu.css({left:menuleft+"px", top:menutop+"px"})
	this.$shimobj.css({width:megamenu.actualwidth+"px", height:megamenu.actualheight+"px", left:menuleft+"px", top:menutop+"px", display:"block"})
},

showmenu:function(e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $menu=megamenu.$menuobj
	var $menuinner=megamenu.$menuinner
	if ($menu.css("display")=="none"){
		this.alignmenu(jQuery, e, megamenu_pos)
		$menu.css("z-index", ++this.zIndexVal)
		$menu.show(this.effectduration, function(){
			$menuinner.css('visibility', 'visible')
		})
	}
	else if ($menu.css("display")=="block" && e.type=="click"){ //if menu is hidden and this is a "click" event (versus "mouseout")
		this.hidemenu(e, megamenu_pos)
	}
	return false
},

hidemenu:function(e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $menu=megamenu.$menuobj
	var $menuinner=megamenu.$menuinner
	$menuinner.css('visibility', 'hidden')
	this.$shimobj.css({display:"none", left:0, top:0})
	$menu.hide(this.effectduration)
},

definemenu:function(anchorid, menuid, revealtype){
	this.megamenulabels.push([anchorid, menuid, revealtype])
},

render:function($){
	for (var i=0, labels=this.megamenulabels[i]; i<this.megamenulabels.length; i++, labels=this.megamenulabels[i]){
		if ($('#'+labels[0]).length!=1 || $('#'+labels[1]).length!=1) //if one of the two elements are NOT defined, exist
			return
		this.megamenus.push({$anchorobj:$("#"+labels[0]), $menuobj:$("#"+labels[1]), $menuinner:$("#"+labels[1]).children('ul:first-child'), revealtype:labels[2], hidetimer:null})
		var megamenu=this.megamenus[i]	
		megamenu.$anchorobj.add(megamenu.$menuobj).attr("_megamenupos", i+"pos") //remember index of this drop down menu
		megamenu.actualwidth=megamenu.$menuobj.outerWidth()
		megamenu.actualheight=megamenu.$menuobj.outerHeight()
		megamenu.offsetx=megamenu.$anchorobj.offset().left
		megamenu.offsety=megamenu.$anchorobj.offset().top
		megamenu.anchorwidth=megamenu.$anchorobj.outerWidth()
		megamenu.anchorheight=megamenu.$anchorobj.outerHeight()
		$(document.body).append(megamenu.$menuobj) //move drop down menu to end of document
		megamenu.$menuobj.css("z-index", ++this.zIndexVal).hide()
		megamenu.$menuinner.css("visibility", "hidden")
		megamenu.$anchorobj.bind(megamenu.revealtype=="click"? "click" : "mouseenter", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
			return jkmegamenu.showmenu(e, parseInt(this.getAttribute("_megamenupos")))
		})
		megamenu.$anchorobj.bind("mouseleave", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			if (e.relatedTarget!=menuinfo.$menuobj.get(0) && $(e.relatedTarget).parents("#"+menuinfo.$menuobj.get(0).id).length==0){ //check that mouse hasn't moved into menu object
				menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
					jkmegamenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_megamenupos")))
				}, jkmegamenu.delaytimer)
			}
		})
		megamenu.$menuobj.bind("mouseenter", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
		})
		megamenu.$menuobj.bind("click mouseleave", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
				jkmegamenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_megamenupos")))
			}, jkmegamenu.delaytimer)
		})
	} //end for loop
	if(/Safari/i.test(navigator.userAgent)){ //if Safari
		$(window).bind("resize load", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]
				var $anchorisimg=(megamenu.$anchorobj.children().length==1 && megamenu.$anchorobj.children().eq(0).is('img'))? megamenu.$anchorobj.children().eq(0) : null
				if ($anchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
					megamenu.offsetx=$anchorisimg.offset().left
					megamenu.offsety=$anchorisimg.offset().top
					megamenu.anchorwidth=$anchorisimg.width()
					megamenu.anchorheight=$anchorisimg.height()
				}
			}
		})
	}
	else{
		$(window).bind("resize", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]	
				megamenu.offsetx=megamenu.$anchorobj.offset().left
				megamenu.offsety=megamenu.$anchorobj.offset().top
			}
		})
	}
	jkmegamenu.addshim($)
}

}

jQuery.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
 
	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.
 
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
 
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);
 
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( jStyleDiv.html() );
	objDoc.write( "</head>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
 
	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}

$(document).ready(function() {
	$("#top_bar").append("<div style=\"float: right; padding-top: 0px;\"><a href=\"#\" id=\"printScreen\">Print</a><a href=\"#\" id=\"decreaseFont\" style=\"font-size: 10px;\">A-</a><a href=\"#\" id=\"normalFont\" style=\"font-size: 12px;\">A</a><a href=\"#\" id=\"increaseFont\" style=\"font-size: 14px;\">A+</a></div>");
	
	$("#container").append("<div style=\"float: right; text-align: right; clear: left; width: 400px; padding: 20px 10px 0;\"><a href=\"#wrapper\" style=\"color: #000; font-weight: bold; text-transform: lowercase;\">Jump to top</a></div>");;
	
	// industries menu
	var industriesmenu = '<div id="industriesmenu" class="megamenu">'+
		'<ul>'+
			'<li><a href="index.php?page=apparel_footwear">Apparel &amp; Footwear</a></li>'+
			'<li><a href="index.php?page=consumer_products">Consumer Products</a></li>'+
			'<li><a href="index.php?page=distribution">Distribution</a></li>'+
			'<li><a href="index.php?page=financial_services">Financial Services</a></li>'+
			'<li><a href="index.php?page=electronics">Electronics</a></li>'+
			'<li><a href="index.php?page=general_manufacturing">General Manufacturing</a></li>'+
			'<li><a href="index.php?page=imports">Imports</a></li>'+
			'<li><a href="index.php?page=industrial_products">Industrial Products</a></li>'+
		'</ul>';
	industriesmenu += '</div>';
	$("body").append(industriesmenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("industrieslink", "industriesmenu", "mouseover");</script>');
	
	// end industries menu
	
	// products menu
	var productsmenu = '<div id="productsmenu" class="megamenu">'+
			'<ul>'+
				'<li><a href="index.php?page=xperia_executiv_solutions">Xperia Executiv Solutions</a>'+
					'<ul>'+
						'<li style="clear: left; float: left;"><a href="index.php?page=comprehensiv#comprehensiv" class="scroll">Comprehensiv&trade;</a>'+
							'<ul>'+
								'<li><a href="index.php?page=collaborativ#collaborativ">Collaborativ&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=collaborativ_procurement_production#collaborativ_procurement">Procurement &amp; Production</a></li>'+
										'<li><a href="index.php?page=collaborativ_warehouse_inventory#collaborativ_warehouse">Warehouse &amp; Inventory</a></li>'+
										'<li><a href="index.php?page=collaborativ_demand_crm#collaborativ_demand">Demand &amp; CRM</a></li>'+
									'</ul>'+
								'</li>'+
								'<li><a href="index.php?page=productiv#productiv">Productiv&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=productiv_procurement_production#productiv_procurement">Procurement &amp; Production</a></li>'+
										'<li><a href="index.php?page=productiv_warehouse#productiv_warehouse">Warehouse</a></li>'+
										'<li><a href="index.php?page=productiv_inventory#productiv_inventory">Inventory</a></li>'+
									'</ul>'+
								'</li>'+
								'<li><a href="index.php?page=supportiv#supportiv">Supportiv&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=supportiv_CRM#supportiv_crm">CRM</a></li>'+
										'<li><a href="index.php?page=supportiv_inventory_warehouse#supportiv_inventory">Inventory &amp; Warehouse</a></li>'+
										'<li><a href="index.php?page=supportiv_ecommerce#supportiv_ecommerce">E-Commerce</a></li>'+
									'</ul>'+
								'</li>'+
								'<li><a href="index.php?page=accountiv#accountiv">Accountiv&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=accountiv_budgets#accountiv_budgets">Budgets</a></li>'+
										'<li><a href="index.php?page=accountiv_spreadsheets#accountiv_spreadsheets">Spreadsheets</a></li>'+
										'<li><a href="index.php?page=accountiv_financials#accountiv_financials">Financials</a></li>'+
									'</ul>'+
								'</li>'+
							'</ul>'+
						'</li>'+
						'<li style="float: left;"><a href="index.php?page=connectiv#connectiv">Connectiv&trade;</a>'+
							'<ul>'+
								'<li><a href="index.php?page=adaptiv#adaptiv">Adaptiv&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=adaptiv_ibm_iSeries#adaptiv_ibmiseries">IBM iSeries</a></li>'+
										'<li><a href="index.php?page=adaptiv_application_server#adaptiv_applicationserver">Application &amp; Server</a></li>'+
										'<li><a href="index.php?page=adaptiv_edi#adaptiv_edi">EDI</a></li>'+
									'</ul>'+
								'</li>'+
								'<li><a href="index.php?page=decisiv#decisiv">Decisiv&trade;</a>'+
									'<ul>'+
										'<li><a href="index.php?page=decisiv_dashboards#decisiv_dashboards">Dash Boards</a></li>'+
										'<li><a href="index.php?page=decisiv_label_generation#decisiv_labelgeneration">Label Generation</a></li>'+
										'<li><a href="index.php?page=decisiv_business_intelligence#decisiv_businessintelligence">Business Intelligence</a></li>'+
									'</ul>'+
								'</li>'+
							'</ul>'+
						'</li>'+
					'</ul>'+
				'</li>'+
			'</ul>'+
		'</div>';
	$("body").append(productsmenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("productslink", "productsmenu", "mouseover");</script>');
	// end products menu
	
	// services menu
	var servicesmenu = '<div id="servicesmenu" class="megamenu">'+
		'<ul>'+
			'<li><a href="index.php?page=services#consulting">Consulting</a></li>'+
			'<li><a href="index.php?page=services#education">Education</a></li>'+
			'<li><a href="index.php?page=services#implementation">Implementation</a></li>'+
			'<li><a href="index.php?page=services#methodologies">Methodologies</a></li>'+
			'<li><a href="index.php?page=services#upgrades">Upgrades</a></li>'+
			'<li><a href="index.php?page=services#solution_development">Solution Development</a></li>'+
			'<li><a href="index.php?page=services#support">Support</a></li>'+
		'</ul>'+
	'</div>';
	
	$("body").append(servicesmenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("serviceslink", "servicesmenu", "mouseover");</script>');
	// end services menu
	
	// solutions menu
	var solutionsmenu = '<div id="solutionsmenu" class="megamenu">'+
		'<ul style="clear: left; float: left;">'+
			'<li><a href="index.php?page=decisiv_business_intelligence#decisiv_businessintelligence">Business Intelligence</a></li>'+
			'<li><a href="index.php?page=supportiv_CRM#supportiv_crm">Customer Relationship Mangement</a></li>'+
			'<li><a href="index.php?page=decisiv_dashboards#decisiv_dashboards">Dashboards</a></li>'+
			'<li><a href="index.php?page=collaborativ_demand_crm#collaborativ_demand">Demand Management</a></li>'+
			'<li><a href="index.php?page=supportiv_ecommerce#supportiv_ecommerce">E-Commerce</a></li>'+
			'<li><a href="index.php?page=adaptiv_edi#adaptiv_edi">EDI</a></li>'+
			'<li><a href="index.php?page=comprehensiv#comprehensiv">ERP - Comprehensiv&trade;</a></li>'+
			'<li><a href="index.php?page=accountiv_financials#accountiv_financials">Financial Management</a></li>'+
		'</ul>'+
		'<ul style="float: left;">'+
			'<li><a href="index.php?page=adaptiv_ibm_iSeries#adaptiv_ibmiseries">IBM iSeries</a></li>'+
			'<li><a href="index.php?page=productiv_inventory#productiv_inventory">Inventory Management</a></li>'+
			'<li><a href="index.php?page=collaborativ_demand_crm#collaborativ_demand">Order Entry Processing</a></li>'+
			'<li><a href="index.php?page=collaborativ_procurement_production#collaborativ_procurement">Procurement Management</a></li>'+
			'<li><a href="index.php?page=collaborativ_procurement_production#collaborativ_production">Production Management</a></li>'+
			'<li><a href="index.php?page=collaborativ#collaborativ">Supply Chain Management</a></li>'+
			'<li><a href="index.php?page=adaptiv#adpativ">Technology Platforms</a></li>'+
			'<li><a href="index.php?page=productiv_warehouse#productiv_warehouse">Warehouse Management</a></li>'+
		'</ul>'+
	'</div>';
	$("body").append(solutionsmenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("solutionslink", "solutionsmenu", "mouseover");</script>');
	// end solutions menu
	
	// partnerships menu
	var partnershipsmenu = '<div id="partnershipsmenu" class="megamenu">'+
		'<ul>'+
			'<li><a href="index.php?page=partnerships_ibm">IBM</a></li>'+
			'<li><a href="index.php?page=partnerships_partner_community">Partner Community</a></li>'+
			'<li><a href="index.php?page=partnerships_program_overview">Partner Program Overview</a></li>'+
			'<li><a href="index.php?page=partnerships_support_partners">Technical Partners</a></li>'+
			'<li><a href="index.php?page=partnerships_resellers">Resellers</a></li>'+
		'</ul>'+
	'</div>';
	
	$("body").append(partnershipsmenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("partnershipslink", "partnershipsmenu", "mouseover");</script>');
	// end partnerships menu
	
	// xperia menu
	var xperiamenu = '<div id="xperiamenu" class="megamenu">'+
	'<ul>'+
		'<li><a href="index.php?page=about_xperia">About Xperia</a></li>'+
		'<li><a href="index.php?page=announcements">Announcements</a></li>'+
		//'<li><a href="index.php?page=careers">Careers</a></li>'+
		'<li><a href="index.php?page=case_studies">Case Studies</a></li>'+
		'<li><a href="index.php?page=methodologies">Methodologies</a></li>'+
		'<li><a href="index.php?page=announcements">News</a></li>'+
		'<li><a href="index.php?page=success_stories">Success Stories</a></li>'+
		'<li><a href="index.php?page=white_papers">White Papers</a></li>'+
	'</ul>'+
	'</div>';
	$("body").append(xperiamenu);
	$("head").append('<script type="text/javascript">jkmegamenu.definemenu("xperialink", "xperiamenu", "mouseover");</script>');
	// end xperia menu
	
	$("#printScreen").click(function() {
		$("#container").print();
		return(false);
	});
	$("#increaseFont").click(function() {
		var curSize = parseInt($("#container").css('font-size'));
		curSize++;
		if(curSize < 15) {
			$("#container").css({fontSize: (curSize)+'px'});
		}
		return(false);
	});
	$("#normalFont").click(function() {
		var curSize = parseInt($("#container").css('font-size'));
		if(curSize != 12) {
			$("#container").css({fontSize: '12px'});
		}
		return(false);
	});
	$("#decreaseFont").click(function() {
		var curSize = parseInt($("#container").css('font-size'));
		curSize--;
		if(curSize > 9) {
			$("#container").css({fontSize: (curSize)+'px'});
		}
		return(false);
	});
});
