// JavaScript Document
function createCookie(name,value,days) {if(days){var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires="; expires="+date.toGMTString();}else var expires="";document.cookie=name+"="+value+expires+"; path=/";}
function readCookie(name) {var theCookie=""+document.cookie;var ind=theCookie.indexOf(name);if(ind==-1 || name==""){return "";}var ind1=theCookie.indexOf(';',ind);if(ind1==-1){ind1=theCookie.length;}return unescape(theCookie.substring(ind+name.length+1,ind1));}
function eraseCookie(name) {createCookie(name,"",-1);}
var isMenuActive = false;

$(document).ready( function() {
	// Menu Functions
	$('#siteNavigation li').hover(
		function() { 
			_this = $(this);
			_this.children('a:first').addClass('hover');
			if (isMenuActive) {
				fnMenuShow(_this);
			}
			else {	
				setTimeout( function() { fnMenuShow(_this); }, 200);
			}
		},
		function() { 
			_this = $(this);
			_this.children('a:first').removeClass('hover');
			fnMenuHide(_this);
		}
	);
	$('.floodMenu').append('<a href="#" class="lnkClose">close</a>');
	$('.floodMenu a.lnkClose').live('click', function(e) {
		e.preventDefault();
		_this = $(this).parents('li:first');
		_this.children('a:first').removeClass('hover');
		fnMenuHide(_this);		
	});

	// Admin Menu
	$('#siteAdminMenu h2').click( function(e) {
		$('#siteAdminMenu ul').hide();
		if ($(this).is('.active')) {
			$('#siteAdminMenu').removeClass('active');			
			$('#siteAdminMenu .active').removeClass('active');
			eraseCookie("ActiveAdminMenu");
		}
		else {
			$('#siteAdminMenu').addClass('active');
			$('#siteAdminMenu .active').removeClass('active');
			$(this).addClass('active');
			$(this).next('ul').show();
			createCookie("ActiveAdminMenu",$(this).attr('data-Menu'),5);
		}
	});
	if (readCookie("ActiveAdminMenu")) {
		$('h2[data-Menu="' + readCookie("ActiveAdminMenu") + '"]').click();
	}
	
	// Article List Pagination 
	// if there is more then 1 paged data
	if ($('.articlePage').size() > 1) {
		$('.articlePage .article2Third').removeClass('article2Third');
		
		// This checks to see if an articleNo is passed as a Hash
		_thisHash = window.location.hash
		_pageNo = 1; // start on page one unless...
		if (_thisHash.length) {
			// make sure the selected artcle is in the page list
			if ($(_thisHash).parents('.articlePage:first').size() == 1) {
				// goto the page the article is on
				_pageNo = $(_thisHash).parents('.articlePage:first').attr('data-pageNo');			
			}
		}
		fnFormatArticleListPage(_pageNo);
		
		if ($(_thisHash).is(':visible')) {
			// scroll to the artcile in question
			$(document).scrollTop($(_thisHash).offset().top);
		}
	}
	$('.lnkViewPage').live('click', function(e) {
		e.preventDefault();
		//_topPos = $(this).parents('.articleList').offset().top; // Capture the scrollto position before we nuke the pagelist
		fnFormatArticleListPage($(this).attr('data-pageNo'));
		//$(document).scrollTop(_topPos);
	});	
	function filterPath(string) {
		return string.replace(/^\//,'').replace(/(index|default).[a-zA-Z]{3,4}$/,'').replace(/\/$/,'');    
	}
	var locationPath = filterPath(location.pathname);
	var scrollElem = scrollableElement('html', 'body');
	if ($(".categoryTOC").size() > 0) {
			$('a[href*=#]').each(function() {
		
			var thisPath = filterPath(this.pathname) || locationPath;
			if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
				//var target = $('[name="' + $(this).href;
				var tagetName = $(this).attr("href").replace("#","");
				var target = $('[name="' + tagetName + '"]');
				if (target) {
					var targetOffset = target.offset().top;
					$(this).click(function(event) {
						event.preventDefault();
					
						$(scrollElem).animate({scrollTop:targetOffset}, 400, function() { 
							//location.hash = tagetName;
						});
						return false;
					});
	  		}
			}
		});
	}
	// use the first element that is "scrollable"   
	function scrollableElement(els) {
			for (var i = 0, argLength = arguments.length; i <argLength; i++) {
				var el = arguments[i],$scrollElement = $(el);
				if ($scrollElement.scrollTop()> 0) { 
					return el;
				} else {
					  $scrollElement.scrollTop(1);
						var isScrollable = $scrollElement.scrollTop()> 0;
						$scrollElement.scrollTop(0);
						if (isScrollable) {
							return el;
						}
				}
			}
		return [];
	}
}); // Close document.ready()

function fnMenuShow(_this) {
	if (_this.children('a.hover').size() > 0) {
		if (isMenuActive) {
			$('div.floodMenu').stop(true,true);
			_this.children('div.floodMenu').show(); 
		}
		else {
			isMenuActive = true;
			$('#blackOut').remove();
			$('#siteContentContainer').append('<div id="blackOut"></div>');
			$('#blackOut').fadeTo(0,0);
			$('#blackOut').fadeTo(250,0.5);
			_this.children('div.floodMenu').slideDown('fast'); 
		}
	}
	// if a user is heavily speratic with the menu, it could hang.  
	// so after half a second, see if we are still on menu, if not: Nuke
	setTimeout( function() {
		if (!isMenuActive) { 
			$('#blackOut').remove();
			$('div.floodMenu').hide();
			isMenuActive = false; 
		}
	}, 500);
}
function fnMenuHide(_this) {
	_this.children('div.floodMenu').slideUp('fast');
	setTimeout( function () { 
			if (_this.parents('ul').find('a.hover').size() == 0) {
				$('#blackOut').fadeTo(250,0,function() { $(this).remove();});
				isMenuActive = false; 
			}
		}, 100);
}
function fnFormatArticleListPage(_pageNo) {
	//alert(_pageNo);
	//$('.articlePage').hide();

	$('.articlePage').each( function() {
		
		if ($(this).attr("data-pageNo") <= _pageNo) {
			if (_pageNo == 1) {
				$(this).show();
			} else {
				$(this).fadeIn("slow");
			}
		} else {
			$(this).hide();
		}
	});
		
	// if there is an Ad, then we need to display is on the page
/*	if ($('.articlePage .productAdvert').size() >= 1) {
		_productAd = $('.articlePage .productAdvert');
		_lastArticle = $('.articlePage[data-pageNo="' + _pageNo + '"] .article:last');
		_lastArticle.addClass('article2Third').append("<p class='clearRight'></p>");		
		if ($('.articlePage[data-pageNo="' + _pageNo + '"] .article').size() > 1) {
			_nextToLastArticle = _lastArticle.prev();
			_nextToLastArticle.addClass('article2Third').before(_productAd.clone(true,true));
		} else {
			_lastArticle.before(_productAd.clone(true,true));
		}
		_productAd.remove();
	}*/

	// Display the Pagination Links under the Artcle List
	var nextPage = parseInt(_pageNo) + 1;
	if (_pageNo < $('.articlePage').size()) {
		$('.articlePagelist').html(" <a href='#' class='lnkViewPage' data-pageNo='" + nextPage + "'>Show More Articles</a> ");
	} else {
		$('.articlePagelist').html("");
	}
	
	/*$('.articlePagelist').html("Page " + _pageNo + " of " + $('.articlePage').size());
	if (_pageNo == 1) {
		$('.articlePagelist').append(" &lt; prev ");
	} else {
		$('.articlePagelist').append(" <a href='#' class='lnkViewPage' data-pageNo='" + (eval(_pageNo) - 1) + "'>&lt; prev</a> ");
	}
	for (i=1;i <= $('.articlePage').size();i++) {
		if (i == _pageNo){
			$('.articlePagelist').append(" " + i + " ");
		}
		else {
			$('.articlePagelist').append(" <a href='#' class='lnkViewPage' data-pageNo='" + i + "'>" + i + "</a> ");
		}
	}
	if (_pageNo == $('.articlePage').size()) {
		$('.articlePagelist').append(" next &gt; ");
	} else {
		$('.articlePagelist').append(" <a href='#' class='lnkViewPage' data-pageNo='" + (eval(_pageNo) + 1) + "'>next &gt;</a> ");
	}	*/
	
}

