//Menu
$(document).ready(function() {	
	//Code goes here
	function megaHoverOver()
	{
		$(this).find(".PE_main_subnav").stop().fadeTo(10, 0.95).show(); //Find sub and fade it in
		
		(function($) 
		{
			//Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() { //for each ul...
					rowWidth += $(this).width(); //Add each ul's width together
				});
			};
		})(jQuery); 

		if ( $(this).find(".row").length > 0 ) 
		{ //If row exists...

			var biggestRow = 0;	

			$(this).find(".row").each(function() {	//for each row...
				$(this).calcSubWidth(); //Call function to calculate width of all ul's
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});

			$(this).find(".PE_main_subnav").css({'width' :biggestRow}); //Set width
			$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
			
		} 
		else 
		{ //If row does not exist...

			$(this).calcSubWidth();  //Call function to calculate width of all ul's
			$(this).find(".PE_main_subnav").css({'width' : rowWidth}); //Set Width
		}
	}
	//On Hover Out
	function megaHoverOut()
	{
	  //$(this).find(".toplink").css({'background' :'url(../pe_graphics/menu_02.png) no-repeat','display':'inline'});
	  $(this).find(".PE_main_subnav").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		  $(this).hide();  //after fading, hide it
	  });
	  
	}
	
	//Set custom configurations
	var config = {
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 100, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 250, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("ul#PE_main_nav li .PE_main_subnav").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	//$("ul#PE_main_nav li").hoverIntent(config); //Trigger Hover intent 
	$("ul#PE_main_nav li").hover(function()
			{
				//$(this).find(".PE_main_subnav").stop().fadeTo(10, 0.95).show(); //Find sub and fade it in
				$(this).find(".PE_main_subnav").stop().css({'opacity':'.95'}).show(); //Find sub and fade it in
				(function($) 
				{
					//Function to calculate total width of all ul's
					jQuery.fn.calcSubWidth = function() {
						rowWidth = 0;
						//Calculate row
						$(this).find("ul").each(function() { //for each ul...
							rowWidth += $(this).width(); //Add each ul's width together
						});
					};
				})(jQuery); 

					if ( $(this).find(".row").length > 0 ) 
					{ //If row exists...

						var biggestRow = 0;	

						$(this).find(".row").each(function() {	//for each row...
							$(this).calcSubWidth(); //Call function to calculate width of all ul's
							//Find biggest row
							if(rowWidth > biggestRow) {
								biggestRow = rowWidth;
							}
						});

						$(this).find(".PE_main_subnav").css({'width' :biggestRow}); //Set width
						$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
						
					} 
					else 
					{ //If row does not exist...

						$(this).calcSubWidth();  //Call function to calculate width of all ul's
						$(this).find(".PE_main_subnav").css({'width' : rowWidth}); //Set Width
					}
				},
				function()
				{
					 //$(this).find(".toplink").css({'background' :'url(../pe_graphics/menu_02.png) no-repeat','display':'inline'});
					  $(this).find(".PE_main_subnav").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
						  $(this).hide();  //after fading, hide it
					  });
				}
			);
	
	function getAbsolutePath() {
		var loc = window.location;
		var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/')-1);
		return pathName;
		//loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
	}
	
	var url = window.location.href.split("/");
	var count = 0;
	
	if(url[url.length-1]=='index.php' || url[url.length-1]=='')
	{ count = 9; }
	else
	{ count = 4; }
	
	//use the JQuery get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
	//$.get("http://www.protocol-education.com/Portal/Jobs/RSS.aspx?DoSearch=LatestJobs", function(d)
	$.get("proxy.php?url=http://www.protocol-education.com/Portal/Jobs/RSS.aspx?DoSearch=LatestJobs", function(d) {
			//find each 'item' in the file and parse it
			var html = "";
			var x = 0;
			$(d).find('item').each(function() {
				//name the current found item this for this particular loop run
				var $item = $(this);
				// grab the post title
				var title = $item.find('title').text();
				// grab the post's URL
				var link = $item.find('link').text();
				link = link.replace("portal","Portal");
				// next, the description
				var description = $item.find('description').text();
				//shorten description 
				
				//don't forget the pubdate
				var pubDate = $item.find('pubDate').text();
	 
				// now create a var 'html' to store the markup we're using to output the feed to the browser window
				html += "<div class=\"latestjob\"><p class=\"description\"><a style=\"color:#666\" href=\"" + link + "\" target=\"_blank\" style=\"text-decoration:none;\"><strong>" + title + "<\/strong><\/a>";
				//html +=  "</br>" + description + "</br>";
				html += "<br /><a href=\"" + link + "\" target=\"_blank\"><strong>View Job<\/strong><\/a></p><\/div>";
				if(x >= count)
					return false;
				else
					html += "<div class=\"PE_dottedline\"><\/div>";
					x++;
				//put that feed content on the screen!
			});
			$("#latestjobsleft").empty();
			$('#latestjobsleft').append($(html));  
		}); /**/
		
		
	
		
	$(window).resize(function(){
		var newPos = $('#PE_left').position();
		$('.gsc-wrapper').css('left', newPos.left);
	});
});
