try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}

$(function() {	
	$.lefNav.init();
	$.templatePath = $('input#templatePath').val();
	$.siteURL = $('input#siteURL').val();
	
	$searchBoxes = $('.search');
	$searchBoxes.focus(function() {
		$(this).val('');							
	});
	
	$('#ajaxloader').ajaxStart(function() {
		$(this).show();								
	}).ajaxStop(function() {
		$(this).hide();
	});
	
});

/**left nav **/
$.lefNav = {
	init: function() {
		var leftnav = $('ul#nav');
		this.subMenus = leftnav.find('li > ul');
		this.initialMenu = leftnav.find('li:eq(0)');
		this.subMenus.hide();
		leftnav.find('li > a').addClass('topLevel');
		leftnav.find('ul > li > a').removeClass('topLevel');
		this.$topLevelItems = $("a.topLevel");
		this.$topLevelItems.each(function(index) {
			var arrowHTML = '<a href="javascript:void(0)" class="arrow">Arrow</a>';
			$(this).parent().append(arrowHTML);
			$(this).click(function(e) {
				var linkContent = $(this).html();
				if(linkContent != "GEGRC Blog") {
					e.preventDefault();
					var alreadyOpen = $(this).parent('li').hasClass('open');
					if(!alreadyOpen){
						$(this).parent('li').addClass('open').siblings('li').removeClass('open').find('ul').hide('slow');		
						$(this).siblings('ul:hidden').show('slow');	
					} else {
						$(this).parent('li').removeClass('open');	
						$(this).siblings('ul').hide('slow');	
					}
				}
			});
		});
		
		var $arrows = leftnav.find('a.arrow');
		
		$arrows.each(function() {
			$(this).click(function(e) {
				var parentContent = $(this).parent('li').html();
				if(parentContent.search('GEGRC Blog') != -1) {
					var $blogNav = $('li.page-item-536 a.topLevel');
					document.location = $blogNav.attr('href');
					return false;
				}
				var alreadyOpen = $(this).parent('li').hasClass('open');
				if(!alreadyOpen){
					$(this).parent('li').addClass('open').siblings('li').removeClass('open').find('ul').hide('slow');		
					$(this).siblings('ul:hidden').show('slow');	
				} else {
					$(this).parent('li').removeClass('open');	
					$(this).siblings('ul').hide('slow');	
				}					   
			});
		});
		
		this.expand();
		
		var $currentItem = $('.current_page_item');
		$currentItem.find('a:eq(0)').addClass('selected');
		
		var $currentParent = $('.current_page_parent');
		$currentParent.find('a:eq(0)').addClass('selected');
		
		var $currentAncestor = $('.current_page_ancestor');
		$currentAncestor.find('a:eq(0)').addClass('selected');
	},
	
	collapse: function() {
		this.subMenus.hide('slow');		
	},
	
	expand: function() {
		this.$topLevelItems.each(function() {
			if( $(this).parent().hasClass('current_page_item') || $(this).parent().hasClass('current_page_parent') || 				$(this).parent().hasClass('current_page_ancestor') ) {
				$(this).parent('li').addClass('open').siblings('li').removeClass('open').find('ul').hide('slow');		
				$(this).siblings('ul:hidden').show('slow');		
			}
		});
	}
}

$.popUp = {
	newPopUp: function(options) {
			var links = options.links;
			var secondaryContentURL = options.secondaryContentURL;
			var profile = options.profile;
			var className = options.className;
			
			links.click(function(e) {
				e.preventDefault();
				var self = $(this);
				var permaLink = $(this).attr('href');
				var $popUpID = $(this).parents('.featuredContent').find('input.popUpID').val();
				var $expandID = $(this).parents('.featuredContent').find('input.expandID').val();
				var title = $(this).parents().find('.title').html();				
				
				
				if($popUpID) {
					var overlay = new $.overlay({width: 560, id : $popUpID, title: $popUpID});
					if(className == 'lab')
						var content = '<div id="overlay-content" class="lab"></div>';
					else
						var content = '<div id="overlay-content"></div>';
					overlay.setContent(content);
					var $ajaxMe = $('#overlay-content');
					var $ajaxURL =  $.templatePath + "/pop-up.php?id=" + $popUpID;
						
					if(!profile) {
						$ajaxMe.load($ajaxURL, function() {					
							//var secondaryContent = $('#secondaryContent');
							//secondaryContent.load(secondaryContentURL, function() {overlay.show();});
							overlay.show();
							$('a.overlay-close').click(function() {
								overlay.hide();										   
							});
							
						});
					} else if(profile) {
						titleFixed = escape(title);
						$ajaxMe.load($ajaxURL, function() {					
						//var secondaryContent = $('#secondaryContent');
						//secondaryContentURL = $.templatePath + "/profile-info.php?id=" + titleFixed;
						//secondaryContent.load(secondaryContentURL, function() {overlay.show();});
						overlay.show();
						$('a.overlay-close').click(function() {
							overlay.hide();										   
						});
					});
					}
				} else if($expandID) {
						var $expandingContent = $(this).parents('.featuredContent').find('.content');
						var $collapseTrigger = $(this).parents('.featuredContent').find('a.more-links')
						var $ajaxURL =  $.templatePath + "/expand.php?id=" + $expandID;
						
						$expandingContent.origHTML = $expandingContent.html();
						$.ajax({
						  url: $ajaxURL,
						  cache: false,
						  success: function(html){
							$expandingContent.hide('slow', function() {
								self.text('read less');
								self.addClass('down-arrow');
								$expandingContent.html(html);
								var $newContent = $expandingContent.find('.content').html();
								$expandingContent.html($newContent);
								$expandingContent.show('slow');
								$collapseTrigger.click(function(e) {
									if(self.hasClass('down-arrow')) {
										$expandingContent.hide('slow', function() {$expandingContent.html($expandingContent.origHTML);$expandingContent.show('slow');self.text('read more');self.removeClass('down-arrow'); });	
									}
								});
							});
						  }
						});	
				}
			});
		}
}

$.pageSizer = {
	init: function(options) {
		this.page = $('#page');
		this.header = $('#header');
		this.mastHead = $('#masthead');
		this.footer = $('#footer');
		this.marquee = $('.landingMarquee');
		this.marqueeBackground = $('.landingMarquee .backgroundImage');
		this.mainColumn = $('.twoColumns');
		this.leftColumn = $('.twoColumns .columnLeft');
		this.rightColumn = $('.twoColumns .columnRight');
		this.flashObject = $('#marqueeFlashContainer');
		this.onResize();
	},
	
	onResize: function() {
		var windowWidth = $(window).width();
		if(windowWidth < 1200) {
			if(windowWidth < 935)
				windowWidth = 935;
			windowWidth = windowWidth - 10;
			this.page.css('width', windowWidth);
			this.mainColumn.css('width', windowWidth);
			this.header.css('width', windowWidth);
			this.footer.css('width', windowWidth);
			this.marquee.css('width', windowWidth);
			this.mastHead.css('width', windowWidth);
			if(this.flashObject)
				this.flashObject.css('width', (windowWidth - 195));
			if(this.marqueeBackground)
				this.marqueeBackground.css('width', windowWidth);
			if(windowWidth < 1150)	
				$.lefNav.collapse();
			else
				$.lefNav.expand();
		}
		else {
			this.page.css('width', 1200);
			this.mainColumn.css('width', 1200);
			this.header.css('width', 1200);
			this.footer.css('width', 1200);
			this.marquee.css('width', 1200);
			this.mastHead.css('width', 1200);
			if(this.flashObject)
				this.flashObject.css('width', (1200 - 195));
			if(this.marqueeBackground)
				this.marqueeBackground.css('width', 1200);
			$.lefNav.expand();
		}
	}	
}

function _RemoveDuplicates(arr)
   {
	//get sorted array as input and returns the same array without duplicates.
	var result=new Array();
	var lastValue="";
	for (var i=0; i<arr.length; i++)
	{
	  var curValue=arr[i];
	  if (curValue != lastValue)
	  {
		 result[result.length] = curValue;
	  }
	  lastValue=curValue;
	}
	return result;
   }

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}