$JQ = jQuery.noConflict();

var oldLayer = false;
var oldLayer2 = "tabContent01";
var firstrun = 0;
$JQ(document).ready(function(){

	var defaultValue = $JQ("#contentSearchSiteTerm").attr("value");
	
	//detect if in preview mode and apply correct parameter
	var preview = $JQ("#preview").val();
	var previewQuestion = "";
	if(preview.length>0){
		preview = "&preview=true";
		previewQuestion = "?preview=true";
		
	};

	//google
	
	//var pageTracker = _gat._getTracker("UA-2434963-1");
		//alert($JQ.browser.msie);
	//$JQ(".content.hp").append('<div class="ie6placeholder"></div>');
	$JQ(".content.hp").append('<div class="slideButtonLeft outer disabledLeft"><strong><a href="#"><span>Scroll left</span></a></strong></div><div class="tooltipLeft"></div>');
	$JQ(".content.hp").append('<div class="slideButtonRight outer"><strong><a href="#"><span>Scroll right</span></a></strong></div><div class="tooltipRight"></div>');
	//$JQ(".slideButtonLeft.outer").hover(function(){if($JQ(".slideButtonLeft").attr("class").indexOf("disabledLeft")==-1)$JQ(".tooltipLeft").fadeIn(100);},function(){$JQ(".tooltipLeft").fadeOut(100);})
	$JQ(".slideButtonLeft.outer").hover(function(){if($JQ(".slideButtonLeft").attr("class").indexOf("disabledLeft")==-1)$JQ(".tooltipLeft").css('display','block');},function(){$JQ(".tooltipLeft").css('display','none');})
	//$JQ(".slideButtonRight.outer").hover(function(){if($JQ(".slideButtonRight").attr("class").indexOf("disabledRight")==-1)$JQ(".tooltipRight").fadeIn(100);},function(){$JQ(".tooltipRight").fadeOut(100);})
	$JQ(".slideButtonRight.outer").hover(function(){if($JQ(".slideButtonRight").attr("class").indexOf("disabledRight")==-1)$JQ(".tooltipRight").css('display','block');},function(){$JQ(".tooltipRight").css('display','none');})
	
	$JQ(".content.comments").append('<div class="slideButtonLeft inner disabledLeft"><strong><a href="#"><span>Scroll left</span></a></strong></div><div class="tooltipLeft"></div>');
	$JQ(".content.comments").append('<div class="slideButtonRight inner"><strong><a href="#"><span>Scroll right</span></a></strong></div><div class="tooltipRight"></div>');
	//$JQ(".slideButtonLeft.inner").hover(function(){if($JQ(".slideButtonLeft").attr("class").indexOf("disabledLeft")==-1)$JQ(".tooltipLeft").fadeIn(100);},function(){$JQ(".tooltipLeft").fadeOut(100);})
	$JQ(".slideButtonLeft.inner").hover(function(){if($JQ(".slideButtonLeft").attr("class").indexOf("disabledLeft")==-1)$JQ(".tooltipLeft").css('display','block');},function(){$JQ(".tooltipLeft").css('display','none');})
	//$JQ(".slideButtonRight.inner").hover(function(){if($JQ(".slideButtonRight").attr("class").indexOf("disabledRight")==-1)$JQ(".tooltipRight").fadeIn(100);},function(){$JQ(".tooltipRight").fadeOut(100);})
	$JQ(".slideButtonRight.inner").hover(function(){if($JQ(".slideButtonRight").attr("class").indexOf("disabledRight")==-1)$JQ(".tooltipRight").css('display','block');},function(){$JQ(".tooltipRight").css('display','none');})
	
	$JQ("#genericText .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
	$JQ("#page").val(getAnchorValues("page",window.location.hash));
	
	var searchStr = (getAnchorValues("s",window.location.hash));
	var tagStr = (getAnchorValues("t",window.location.hash));

	

/*======= active cat =======*/	
	$JQ("#footer_menu > *").removeClass('active');
	//add current active menu
	var active_menu = $JQ('#menu'+$JQ("#category").val());
	active_menu.addClass('active');

/*======= tag cloud ========*/
	$JQ(".tab-link").click(function(){
		tagSearch($JQ(this).html());
	});
	
/*======= search box =======*/
	$JQ('#contentSearchSiteTerm').focus(function(){
		if(this.value==defaultValue) this.value = "";
	})
	$JQ('#contentSearchSiteTerm').blur(function(){
		if(this.value=="") this.value = defaultValue;
	})
	$JQ("#contentSearchSiteSubmit").click(function(){
		search();
	});
/*======= overlayers (top bunch) =======*/
	$JQ(".overButton").click(function(){
		
		idNo = $JQ(this).attr("id");
		idNo = idNo.substring(idNo.length-2,idNo.length);
		
		//send google analytics usage info
		if (idNo =='01'){
			pageTracker._trackPageview('/newsletter/open');
		}
		else if (idNo=='02') {
			detectLinks($JQ('#overLayer02 ul  a'),false)
			pageTracker._trackPageview('/rss/open');
		}
		else if (idNo=='03') {
			pageTracker._trackPageview('/explore/open');
		}
		else if (idNo=='04') {
			pageTracker._trackPageview('/archive/open');
		}
		$JQ(".overButtonOn").attr({"class":"overButton"});
		$JQ("#overButton"+idNo).attr({"class":"overButtonOn"});
		
		if($JQ("#overLayer"+idNo).css("display")=="none"){
			if(oldLayer!=false){
				//$JQ(oldLayer).fadeOut(100, function(){
				//	$JQ("#overLayer"+idNo).fadeIn(250);
				//	oldLayer = $JQ("#overLayer"+idNo);
				//});
				$JQ(oldLayer).css('display','none');
				$JQ("#overLayer"+idNo).css('display','block');
				oldLayer = $JQ("#overLayer"+idNo);
			}else{
				//$JQ("#overLayer"+idNo).fadeIn(250);
				$JQ("#overLayer"+idNo).css('display','block');
				oldLayer = $JQ("#overLayer"+idNo);
			}
		} else {
			//$JQ("#overLayer"+idNo).fadeOut(250);
			$JQ("#overLayer"+idNo).css('display','none');
			$JQ(".overButtonOn").attr({"class":"overButton"});
			oldLayer = false;
		}
		
		return false;
	})

	
	
	
/*======= explore overlayer (tabs) =======*/
	$JQ("#overLayer03 .menu A").click(function(){
		idNo = $JQ(this).attr("id");
		idNo = idNo.substring(idNo.length-2,idNo.length);
		$JQ("#overLayer03 .menu LI").removeClass();
		$JQ($JQ("#overLayer03 #tab"+idNo).parent()[0]).attr({"class":"active"});
		
		
		
		if($JQ("#overLayer03 .tabContent"+idNo).css("display")=="none"){
			
			if (idNo =='01'){
				pageTracker._trackPageview('/tags/open');
			}
			if (idNo =='02'){
				
				pageTracker._trackPageview('/hottest/open');
			}
			if (idNo =='03'){
				
				pageTracker._trackPageview('/mostcommented/open');
			}
			if(oldLayer2!=false){
				//$JQ("#overLayer03 ."+oldLayer2).fadeOut(100, function(){
				//	$JQ("#overLayer03 .tabContent"+idNo).fadeIn(250);
				//	oldLayer2 = "tabContent"+idNo;
				//});
				$JQ("#overLayer03 ."+oldLayer2).css('display','none');
				$JQ("#overLayer03 .tabContent"+idNo).css('display','block');
				oldLayer2 = "tabContent"+idNo;
			}else{
				//$JQ("#overLayer03 .tabContent"+idNo).fadeIn(250);
				$JQ("#overLayer03 .tabContent"+idNo).css('display','block');
				oldLayer2 = "tabContent"+idNo;
			}
		} else {
			//$JQ("#overLayer03 .tabContent"+idNo).fadeOut(250);
			$JQ("#overLayer03 .tabContent"+idNo).css('display','none');
			oldLayer2 = false;
		}
		return false;
		$JQ(".x").click(function(){
			parrentArr = $JQ(this).parent("DIV");
			//$JQ(parrentArr[0]).fadeOut(250);
			$JQ(parrentArr[0]).css('display','none');
			$JQ(".overButtonOn").attr({"class":"overButton"});
			return false;
		});
	});
	
	$JQ("#teaserPane3").load(teaserArray[2], {random: Math.floor(Math.random()*999999)}, function(){initTeasers();/*initLayer("#teaserPane1")*/});
	$JQ("#overLayer02").load(template_directory+"/services/rssfeedXHTML.php"+previewQuestion, {random: Math.floor(Math.random()*999999)}, function(){
		$JQ("#overLayer02.overLayer").prepend("<a href=\"#\" class=\"x\"><span>X</span></a>");
	
		
		$JQ(".x").click(function(){
			parrentArr = $JQ(this).parent("DIV");
			//$JQ(parrentArr[0]).fadeOut(250);
			$JQ(parrentArr[0]).css('display','none');
			$JQ(".overButtonOn").attr({"class":"overButton"});
			return false;
		})
		
	});
	
	
	
	
	$JQ("#overLayer03 .tabContent02").load(template_directory+"/services/hottestXHTML.php"+previewQuestion, {random: Math.floor(Math.random()*999999)});
	$JQ("#overLayer03 .tabContent03").load(template_directory+"/services/mostcommentsXHTML.php"+previewQuestion, {random: Math.floor(Math.random()*999999)});
	//build close btns
	$JQ(".overLayer").prepend("<a href=\"#\" class=\"x\"><span>X</span></a>");
	
	$JQ(".x").click(function(){
	
		parrentArr = $JQ(this).parent("DIV");
		//$JQ(parrentArr[0]).fadeOut(250);
		$JQ(parrentArr[0]).css('display','none');
		$JQ(".overButtonOn").attr({"class":"overButton"});
		return false;
	})
	$JQ("#calendar").load(template_directory+"/services/archiveXHTML.php"+previewQuestion, {random: Math.floor(Math.random()*1000001)}, function(){
		/*======= archive overlayer (calendar) =======*/
		 $JQ("#calendar TD").hover(function(){
			
			 childrenArr = $JQ($JQ(this).children()).children();
			 if(childrenArr.length>0){
			 	$JQ(childrenArr[0]).hide();
			 }
			 if(childrenArr.length>1){
			 	$JQ(childrenArr[1]).show();
			 }
		 },function(){
			 childrenArr = $JQ($JQ(this).children()).children();
			 if(childrenArr.length>0){
			 	$JQ(childrenArr[0]).show();
			 }
			 if(childrenArr.length>1){
			 	$JQ(childrenArr[1]).hide();
			 }
		 })	
		 $JQ("#calendar").css({"left":parseInt(-(curCalendarYear-1)*275)+"px"})
		$JQ(".calendarLeft").click(function(){
			if(curCalendarYear>0){
				curCalendarYear--;
				$JQ("#calendar").animate({ left: parseInt($JQ("#calendar").css("left"))+275+"px" });
			}
			return false;
		});
		$JQ(".calendarRight").click(function(){
			if(curCalendarYear<$JQ("#calendar TABLE").length){
				curCalendarYear++;
				$JQ("#calendar").animate({ left: parseInt($JQ("#calendar").css("left"))-275+"px" });
			}
			return false;
		});
		$JQ("#calendar TD a").click(function(){
			var href = $JQ(this).attr("href");
			var year = getAnchorValues("year",href);
			$JQ("#page").val(1);
			$JQ("#mode").val("grid");
			//clear any active menu items
			$JQ("#footer_menu > *").removeClass('active');
			var month = getAnchorValues("month",href);
			if($JQ("#mode").val()=="grid"){
				
				filterSection("","","",year,month,"");
				window.location = siteURL+"/#&year="+year+"&month="+month+"&page=1";
			}
			else{
				window.location = siteURL+"/#&year="+year+"&month="+month+"&page=1";
			}
			return false;
			
			
		});
	});
	$JQ("#search_term").val(cleanSearch(searchStr,'add'));
	$JQ("#tag").val(tagStr);
	$JQ("#category").val(getAnchorValues("category",window.location.hash));
	

	filterSection($JQ("#category").val(),$JQ("#page").val(),cleanSearch($JQ("#search_term").val(),'add'),"","", $JQ('#tag').val());
	
	//hpInit(); // home page initialization
	//spInit(); // article page initialization
	
})
/*======= set up article close button =======*/
//if there is an active search term make search closing return user to search results
function setArticleCloseButtons(){
	var search_term  = cleanSearch($JQ('#search_term').val(),'add');
	var link = $JQ("a.x2");
	var category = getAnchorValues("category",window.location.hash);
	var page = getAnchorValues("page",window.location.hash);
	
	
	if (search_term) {
		
		
		$JQ('#contentSearchSiteTerm').attr("value",cleanSearch(search_term,'strip'));
		link.attr({href:""});
		
		link.click(function(){
				search(search_term);			
				return false;
			});
		return false;
	}
	var pars = '';
	if (category) {
		pars ='&category='+category;
	}
	if (page){
		pars +='&page='+page;
	}
	if ($JQ("#type").val()=="preview"){
	link.attr({href:"/?preview=true#"+pars});	
	}
	else
	{
		link.attr({href:"/#"+pars});	
	}
}

/*======== detect external emails and track to google analytics ======= */
				
function detectLinks (link,checkHost) {
	//if there is a link to process
	
	if (link ){
	var href = link.attr('href');
	if (href == undefined)
	{
		return false;
	}
	if (checkHost==true) {
		
	var host = "http://"+$JQ('#host').val()+'/';
	if (href.search(host)==-1&& href.search('http://')!=-1) {
		//make sure it only has 1 click
		link.unbind('click');
		//set click
		link.click(function() {
			var trimHref = this.href.replace('http://','');
			var outgoing = '/outgoing/'+trimHref;
			
			outgoing = escape(outgoing);
			
			pageTracker._trackPageview(outgoing);
	
		
			
		});
	}
	}
	else {
		if (href.search('http://')!=-1) {
			
			//make sure it only has 1 click
			link.unbind('click');
			//set click
			link.click(function() {
				var trimHref = this.href.replace('http://','');
				var outgoing = '/outgoing/'+trimHref;
				
				outgoing = escape(outgoing);
				
				pageTracker._trackPageview(outgoing);
				
				
				
			});
	
	}
	}
	}
	//end if
	
}
/*======= slide buttons - generic =======*/
function updateTooltips(curItem, itemCount){
	showRandomLogo();
	$JQ(".tooltipLeft").html(parseInt(curItem-1)+" / "+itemCount);
	$JQ(".tooltipRight").html(parseInt(curItem+1)+" / "+itemCount);
	if(parseInt(curItem-1)==0) $JQ(".tooltipLeft").hide();
	if(parseInt(curItem+1)>itemCount) $JQ(".tooltipRight").hide();

}

/*======= slide buttons - generic =======*/
function showRandomLogo() {
	if(typeof document.body.style.maxHeight === "undefined") {
	} else {
		var nLogo = Math.floor(Math.random()*logos.length);
		$JQ('.headerLeft a').html(logos[nLogo]);
		$JQ('.headerLeft a').css('display', 'inline');
		$JQ('p.themed .themedImage').html(themedImages[nLogo]);
	}
}

/*======= checkUrlupdate - stops returning nothing to location.hash and reloading the page ======*/
function checkUrlUpdate (curPane){
		params = getAnchorParams(curPane);
		if(params!=''){
			window.location.hash = getAnchorParams(curPane);
			}
}


/*======= slide buttons - teaser slides on homepage =======*/
function hpInit(){
	//detect if we are displaying an article preview
	var type = $JQ('#type').val();
	if(teaserArray.length>0 && type!="preview") {
		
		//set variables
		var slideCount = teaserArray.length;
		var slides = $JQ("#post_count").val();
		//var postNumber = $JQ("#post_number").val();		
		var requestedPage = getAnchorValues("page",window.location.hash);
		var curPane = 3;
		var visiblePane = 1;
		var nudge1 = 0; var nudge2 = 0; var nudge3 = 0;
		var preloadPane = false;
		var inProgress;
		var load_end;
		
		
		//start the array at 0 unless otherwise specified
		if(requestedPage.length==0){curItem = 0;
		requestedPage=1;}
	
		else{curItem = findMyRequestedPage(requestedPage);}
		
		
		//set the arrows to say x/y
		updateTooltips(curItem+1,slideCount);
		
		//if we only have 1 item we do not need the right arrow.
		if(slideCount<=1) {$JQ(".slideButtonRight").addClass("disabledRight");}
		
		//make sure there is not a handler on this already
		$JQ(".slideButtonRight.outer A").unbind('click');
		//right nav click function
		$JQ(".slideButtonRight.outer A").click(function(){
			//if we are already sliding dont do this
		
			if(inProgress==true){
				return;
			}
			else {
				
				//set some values
				slideCount = teaserArray.length;
				inProgress = true;
				load_end = false;
				//$JQ(".overLayer").fadeOut(250);
				$JQ(".overLayer").css('display','none');
				$JQ(".overButtonOn").attr({"class":"overButton"});
				preloadPane = false;
				curPane++;
				curItem++;
				//if we are at the last pane the 1st pane will shift over to be the next
				if(curPane>3){curPane=1;}
				
				//if we are at the end of the item list disable the right button otherwise have it shown
				if(curItem+1>=slideCount){$JQ(".slideButtonRight").addClass("disabledRight");}
				else{$JQ(".slideButtonRight").removeClass("disabledRight");}
				//if we are not the first item enable the left arrow
				if(curItem>0){$JQ(".slideButtonLeft").removeClass("disabledLeft");}

				//update the arrows for this event (x/y)
				updateTooltips(curItem+1,slideCount);
				
				//update url
				
				$JQ('#page').val(parseInt(curItem)+1);
				checkUrlUpdate (curPane);
				//we need to preload the next pane and make sure its to the right of the current one
				if(curPane==1){
					//set the pane to the left
					$JQ("#teaserPane2").css({"left":"1740px"});
					//get ready to load it.
					if(curItem<slideCount-1){
						$JQ("#teaserPane2").empty();
						preloadPane = "#teaserPane2";
					}
					nudge1 = 0;
					nudge2 = 870;
					nudge3 = -870;
				}else if(curPane==2){
					$JQ("#teaserPane3").css({"left":"1740px"});
					if(curItem>1 && curItem<slideCount-1){
						$JQ("#teaserPane3").empty();
						preloadPane = "#teaserPane3";
					}
					nudge1 = -870;
					nudge2 = 0;
					nudge3 = 870;
				}else{
					$JQ("#teaserPane1").css({"left":"1740px"});
					if(curItem>1 && curItem<slideCount-1){
						$JQ("#teaserPane1").empty();
						preloadPane = "#teaserPane1";
					}
					nudge1 = 870;
					nudge2 = -870;
					nudge3 = 0;
				}	
				
				//set each animation callback var
				var nudge1_comp = false;
				var nudge2_comp = false;
				var nudge3_comp = false;
				
				//do the animation
				$JQ("#teaserPane1").animate({ left: nudge1 },function(){
					nudge1_comp = true;
				});
			    $JQ("#teaserPane2").animate({ left: nudge2 },function(){
					nudge2_comp = true;
				});
			    $JQ("#teaserPane3").animate({ left: nudge3 }, function(){
			    	//once the animation is complete make sure the next one is loaded ready to slide
			    	nudge3_comp = true;
					if(preloadPane && teaserArray[curItem+1]){
						
						$JQ(preloadPane).load(teaserArray[curItem+1]+"&divid="+preloadPane.replace("#",""), {random: Math.floor(Math.random()*999999)}, function(){
							initTeasers();
							initLayer(preloadPane);$JQ(preloadPane+" .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
							load_end = true;
							
							if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true) && (load_end == true)) {
								inProgress = false;
								getGridView();
						  
						    }
						});
					} else if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true))
						
					{
						inProgress = false;
						
					}
				});
			    
			}
			
			//update the anchor so that we can bookmark this article
//			window.location.hash = getAnchorParams(curPane);
		
	var urlString = String(window.location);

	 var googleUrl = setGoogle(urlString);
	 
	
			return false;
		})
		//make sure we do not have a click function already binded
		$JQ(".slideButtonLeft.outer A").unbind('click');
		//left nav click function
		$JQ(".slideButtonLeft.outer A").click(function(){
			if(inProgress==true){
				return;
			} else {
				slideCount = teaserArray.length;
				inProgress = true;
				load_end = false;
				//$JQ(".overLayer").fadeOut(250);
				$JQ(".overLayer").css('display','none');
				$JQ(".overButtonOn").attr({"class":"overButton"});
				var currPage='';
				curItem--;
				curPane--;
				if(curPane<1){curPane=3;}
				preloadPane = false;
				
				//check to see if buttons need to be displayed;
				if(curItem<slideCount-1){$JQ(".slideButtonRight").removeClass("disabledRight");}
				if(curItem<1){$JQ(".slideButtonLeft").addClass("disabledLeft");}
				else{$JQ(".slideButtonLeft").removeClass("disabledLeft");}
				if(curItem<0){curItem=0;}
				updateTooltips(curItem+1,slideCount);
				//update url
				if (curItem==0||curItem==''){
					currPage = 1;
				}
				else {
					currPage =parseInt(curItem)+1;
				}
				
				$JQ('#page').val(currPage);
				checkUrlUpdate (curPane);
				var preloadNumber;
				//sort out where the panes shoud be
				if(curPane==1){
					//set the pane to the left
					$JQ("#teaserPane3").css({"left":"-870px"})
					//get ready to load it.
					if(curItem>0){
						$JQ("#teaserPane3").empty();
						preloadPane = "#teaserPane3";
					}
					nudge1 = 0;
					nudge2 = 870;
					nudge3 = -870;
				}else if(curPane==2){
					$JQ("#teaserPane1").css({"left":"-870px"})
					if(curItem>0 && curItem<slideCount-1){
						$JQ("#teaserPane1").empty();
						preloadPane = "#teaserPane1";
					}
					nudge1 = -870;
					nudge2 = 0;
					nudge3 = 870;
				}else{
					$JQ("#teaserPane2").css({"left":"-870px"})
					if(curItem>0 && curItem<slideCount-1){
						$JQ("#teaserPane2").empty();
						preloadPane = "#teaserPane2";
					}
					nudge1 = 870;
					nudge2 = -870;
					nudge3 = 0;
				}	
				 
				//set each animation callback var
				nudge1_comp = false;
				nudge2_comp = false;
				nudge3_comp = false;
				//do animation
				message = "article = "+curItem+", pane = "+curPane+" ("+nudge1+", "+nudge2+", "+nudge3+")<br>"//curLeft+" "+$JQ("#articlePane1").css("left");
				$JQ("#teaserPane1").animate({ left: nudge1 },function(){
					nudge1_comp = true;
				});
			    $JQ("#teaserPane2").animate({ left: nudge2 },function(){
					nudge2_comp = true;
				});
			    $JQ("#teaserPane3").animate({ left: nudge3 }, function(){
					//alert(curItem+" a "+preloadPane+" && "+teaserArray[curItem-2]);
					//once the animation is complete make sure the next one is loaded ready to slide
			    	nudge3_comp = true;
			    	if(preloadPane && teaserArray[curItem-1]){
			    		//$JQ("#temp")[0].innerHTML += "do "+preloadPane+" loading: "+teasereArray[curItem-2]+"<br>";

						$JQ(preloadPane).load(teaserArray[curItem-1]+"&divid="+preloadPane.replace("#",""), {random: Math.floor(Math.random()*999999)}, function(){
							initTeasers();
							initLayer(preloadPane);
							load_end = true;
							if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true) && (load_end == true)) {
								inProgress = false;
								getGridView();
						  
						    }
						});
					} else if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true))
						
					{
						inProgress = false;
						
					}
				});
			   
			}
			var urlString = String(window.location);

			 var googleUrl = setGoogle(urlString);
			
			
			return false;
		})
		
		//get a list of all our pane items
		var items = new Array();
		items = getTeaserPaneArray(curItem);
		
		var found = false;
		
		for(i=0;i<items.length;i++){
			id = i+1;
			if(curItem==items[i]){
				curPane =id;
			}
		}
		var params ='';
		
		var nLogo = "";
		
		if(teaserArray[items[0]]!=undefined || teaserArray[items[0]]!=null){
			$JQ("#teaserPane1").load(teaserArray[items[0]]+"&divid=teaserPane1&themed="+nLogo+"&r="+Math.floor(Math.random()*999999), function(){
				initLayer("#teaserPane1");
				initTeasers();getGridView()
				
				if(curPane==1){
					checkUrlUpdate (curPane);
				return false;}
			});
		}
		if(teaserArray[items[1]]!=undefined || teaserArray[items[1]]!=null){
			$JQ("#teaserPane2").load(teaserArray[items[1]]+"&divid=teaserPane2&themed="+nLogo+"&r="+Math.floor(Math.random()*999999), function(){
				initLayer("#teaserPane2");
				initTeasers();getGridView()
				
			
				if(curPane==2){
					checkUrlUpdate (curPane);
					
				}
				return false;
			});
		}
		if(teaserArray[items[2]]!=undefined || teaserArray[items[2]]!=null){
			$JQ("#teaserPane3").load(teaserArray[items[2]]+"&divid=teaserPane3&themed="+nLogo+"&r="+Math.floor(Math.random()*999999), function(){
				initLayer("#teaserPane3");
				initTeasers();getGridView()
				if(curPane==3){	
					checkUrlUpdate (curPane);
					
				}
				return false;
			});
		}
		
		var panel1Left;
		var panel2Left;
		var panel3Left;
		if(curPane==1){
			panel1Left = 0;
			panel2Left = 870;
			panel3Left = -870;	
		}
		else if(curPane==2){
			panel1Left = -870;
			panel2Left = 0;
			panel3Left = 870;
		}
		else{
			panel1Left = 870;
			panel2Left = -870;
			panel3Left = 0;
		}
		
		$JQ("#teaserPane1").css({"left":""+panel1Left+"px"});
		$JQ("#teaserPane2").css({"left":""+panel2Left+"px"});
		$JQ("#teaserPane3").css({"left":""+panel3Left+"px"});
		
		if(curItem<=0){$JQ(".slideButtonLeft").addClass("disabledLeft");}
		else{$JQ(".slideButtonLeft").removeClass("disabledLeft");}
		
		if(curItem>=teaserArray.length-1){$JQ(".slideButtonRight").addClass("disabledRight");}
		else{$JQ(".slideButtonRight").removeClass("disabledRight");}
		
		 
		
	}
	
	var urlString = String(window.location);

	 var googleUrl = setGoogle(urlString);


	
	$JQ("#category").val(getAnchorValues("category",window.location.hash));
	if($JQ("#category").val().length>0){
		var category = $JQ("#category").val();
		$JQ("#footer_menu > *").removeClass('active');
		//add current active menu
		var active_menu = $JQ('#menu'+category);
		active_menu.addClass('active');
	}
	
	
}
/**
 * cleans the url for google tracking
 * @param url
 * @return
 */
function setGoogle(url) {
	 
	 var category = $JQ('#category').val();
	 var catName = '';
	 if (category) {
		 $JQ.get(template_directory+'/services/getCategoryName.php?category='+category,function(data) {
		 catName = data;
		 url = url.replace(category,data);
		 var trimString = url.replace('#','');
		 var trimStringa = trimString.replace('&single=true','');
		 
		 var trimString2 = trimStringa.replace(/&/g,'/');
		 var trimString3 = trimString2.replace(/=/g,'/');
		 var trimString1 = trimString3.replace('/s/','/search/?s=');
		 var remove = "http://"+$JQ('#host').val()+'/';
		 var cleanedUrl = trimString1.replace(remove,'');
		 		 
		 pageTracker._trackPageview(cleanedUrl);
		 return;
		 });
		 }
	 else {
	 var trimString = url.replace('#','');
	 var trimStringa = trimString.replace('&single=true','');
	 var trimStringb = trimStringa.replace('&t=','/tag/');
	 
	 var trimString2 = trimStringb.replace(/&/g,'/');
	 var trimString3 = trimString2.replace(/=/g,'/');
	 var trimString1 = trimString3.replace('/s/','/search/?s=');
	 var remove = "http://"+$JQ('#host').val()+'/';
	 var cleanedUrl = trimString1.replace(remove,'');
	 
	
	 pageTracker._trackPageview(cleanedUrl);
	 return;
	 }
}



/**
 * Given a key as its parameter it will try and find the value of that key in the anchor. It will
 * return its value or nothing
 * @param key
 * @return
 */
 

function getGridView() {
	//grid view settings
		var textActive = $JQ("#gat").attr('class');
		
		if (textActive == "active"){
		$JQ(".article").unbind('hover');
		artiFlip = true; 
		childrenArr = $JQ(".article .articleTeaserOff");
		$JQ(childrenArr).slideDown(500);
		$JQ("#gat").attr({"class":"active"});
		$JQ("#gai").removeClass();
		
		}
	 
	 
 }
function getAnchorValues(key,location){
	var data = location.split("&");
	
	for(i=0;i<data.length;i++){
		var term = data[i].replace("#","").replace("/","").split("=");
		
		if(term.length>1){
			
			if(term[0]==key){
				
				return term[1];
				
			}
		}
	}
	return "";
}

function findMyRequestedArticle(requestedArticle){
	for(i=0;i<articleArray.length;i++){
		if(articleArray[i].indexOf("post_id="+requestedArticle)!="-1"){
			return i;
		}
	}
	return 0;
}
function findMyRequestedPage(requestedPage){
	for(i=0;i<teaserArray.length;i++){
		if(teaserArray[i].indexOf("page="+requestedPage)!="-1"){
			return i;
		}
	}
	return 0;
}
/*======= retreive article url ==========*/
function getArtcileUrl (curPane) {
	//alert(curPane);
	//check to see if we are in preview mode
	var preview = $JQ("#preview").val();
	if(preview.length>0){preview = "&preview=true";}
	var article = $JQ("#articlePane"+curPane+"post_id").val();
	$JQ.get(template_directory+"/services/getArticleLink.php?postid="+article+preview,function(data){
			
			var urlString = data;
			var remove = "http://"+$JQ('#host').val();
			var articleUrl = urlString.replace(remove,'');
			var googleUrl = articleUrl;
			pageTracker._trackPageview(googleUrl);
			$JQ('#article_url').val(googleUrl);//set hidden form field
		return;
		});
	
}

/*======= slide buttons - articles on sub pages =======*/
function spInit(){

	//detect if we are displaying an article preview
	var type = $JQ('#type').val();
	
	if(articleArray.length>0 && type!="preview") {
		
		//set variables
		var slideCount = articleArray.length;
		var slides = $JQ("#post_count").val();
		//var postNumber = $JQ("#post_number").val();		
		var requestedArticle = getAnchorValues("article",window.location.hash);
		var preview_id = $JQ("#preview_id").val();
		if(preview_id.length>0){requestedArticle = preview_id;}
		var curPane = 3;
		var visiblePane = 1;
		var nudge1 = 0; var nudge2 = 0; var nudge3 = 0;
		var preloadPane = false;
		var inProgress;
		var load_end;
		
		//start the array at 0 unless otherwise specified
		if(requestedArticle.length=0){curItem = 0;}
		else{curItem = findMyRequestedArticle(requestedArticle);}

		//set the arrows to say x/y
		updateTooltips(curItem+1,slideCount);
	
		//if we only have 1 item we do not need the right arrow.
		if(slideCount<=1) {$JQ(".slideButtonRight").addClass("disabledRight");}
		
		//make sure there is not a handler on this already
		$JQ(".slideButtonRight.inner A").unbind('click');
		//right nav click function
		$JQ(".slideButtonRight.inner A").click(function(){
			//if we are already sliding dont do this
			if(inProgress==true){
				return;
			}
			else {
				//set some values
				slideCount = articleArray.length;
				inProgress = true;
				load_end = false;
				//$JQ(".overLayer").fadeOut(250);
				$JQ(".overLayer").css('display','none');
				$JQ(".overButtonOn").attr({"class":"overButton"});
				preloadPane = false;
				curPane++;
				curItem++;
				//if we are at the last pane the 1st pane will shift over to be the next
				if(curPane>3){curPane=1;}
				
				//if we are at the end of the item list disable the right button otherwise have it shown
				if(curItem+1>=slideCount){$JQ(".slideButtonRight").addClass("disabledRight");}
				else{$JQ(".slideButtonRight").removeClass("disabledRight");}
				//if we are not the first item enable the left arrow
				if(curItem>0){$JQ(".slideButtonLeft").removeClass("disabledLeft");}

				//update the arrows for this event (x/y)
				updateTooltips(curItem+1,slideCount);
				window.location.hash = getAnchorParams(curPane);
				//we need to preload the next pane and make sure its to the right of the current one
				
				if(curPane==1){
					//set the pane to the left
					$JQ("#articlePane2").css({"left":"1740px"});
					//get ready to load it.
					if(curItem<slideCount-1){
						$JQ("#articlePane2").empty();
						preloadPane = "#articlePane2";
					}
					nudge1 = 0;
					nudge2 = 870;
					nudge3 = -870;
				}else if(curPane==2){
					$JQ("#articlePane3").css({"left":"1740px"});
					if(curItem>1 && curItem<slideCount-1){
						$JQ("#articlePane3").empty();
						preloadPane = "#articlePane3";
					}
					nudge1 = -870;
					nudge2 = 0;
					nudge3 = 870;
				}else{
					$JQ("#articlePane1").css({"left":"1740px"});
					if(curItem>1 && curItem<slideCount-1){
						$JQ("#articlePane1").empty();
						preloadPane = "#articlePane1";
					}
					nudge1 = 870;
					nudge2 = -870;
					nudge3 = 0;
				}	
				
				//set each animation callback var
				var nudge1_comp = false;
				var nudge2_comp = false;
				var nudge3_comp = false;
				
				//do the animation
				$JQ('.articleImg embed').css('display','none');
				$JQ("#articlePane1").animate({ left: nudge1 },function(){
					nudge1_comp = true;
					$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
				});
			    $JQ("#articlePane2").animate({ left: nudge2 },function(){
					$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
					nudge2_comp = true;
				});
			    $JQ("#articlePane3").animate({ left: nudge3 }, function(){
			    	$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
			    	//once the animation is complete make sure the next one is loaded ready to slide
			    	nudge3_comp = true;
					if(preloadPane && articleArray[curItem+1]){
						
						$JQ(preloadPane).load(articleArray[curItem+1]+"&divid="+preloadPane.replace("#",""), {random: Math.floor(Math.random()*999999)}, function(){
							initLayer(preloadPane);$JQ(preloadPane+" .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
							load_end = true;
							detectLinks ($JQ('div.articleBody a'),true);
							if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true) && (load_end == true)) {
								inProgress = false;
								
								
						    }
						});
					} else if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true))
						
					{
						inProgress = false;
						
					}
				});
			    
			}
			//update the anchor so that we can bookmark this article
			getArtcileUrl (curPane);
			window.location.hash = getAnchorParams(curPane);
		
			return false;
		})
		//make sure we do not have a click function already binded
		$JQ(".slideButtonLeft.inner A").unbind('click');
		//left nav click function
		$JQ(".slideButtonLeft.inner A").click(function(){
			if(inProgress==true){
				return;
			} else {
				slideCount = articleArray.length;
				inProgress = true;
				load_end = false;
				//$JQ(".overLayer").fadeOut(250);
				$JQ(".overLayer").css('display','none');
				$JQ(".overButtonOn").attr({"class":"overButton"});
				
				curItem--;
				curPane--;
				if(curPane<1){curPane=3;}
				preloadPane = false;
				
				//check to see if buttons need to be displayed;
				if(curItem<slideCount-1){$JQ(".slideButtonRight").removeClass("disabledRight");}
				if(curItem<1){$JQ(".slideButtonLeft").addClass("disabledLeft");}
				else{$JQ(".slideButtonLeft").removeClass("disabledLeft");}
				if(curItem<0){curItem=0;}
				updateTooltips(curItem+1,slideCount);
				window.location.hash = getAnchorParams(curPane);
				var preloadNumber;
				//sort out where the panes shoud be
				if(curPane==1){
					//set the pane to the left
					$JQ("#articlePane3").css({"left":"-870px"})
					//get ready to load it.
					if(curItem>0){
						$JQ("#articlePane3").empty();
						preloadPane = "#articlePane3";
					}
					nudge1 = 0;
					nudge2 = 870;
					nudge3 = -870;
				}else if(curPane==2){
					$JQ("#articlePane1").css({"left":"-870px"})
					if(curItem>0 && curItem<slideCount-1){
						$JQ("#articlePane1").empty();
						preloadPane = "#articlePane1";
					}
					nudge1 = -870;
					nudge2 = 0;
					nudge3 = 870;
				}else{
					$JQ("#articlePane2").css({"left":"-870px"})
					if(curItem>0 && curItem<slideCount-1){
						$JQ("#articlePane2").empty();
						preloadPane = "#articlePane2";
					}
					nudge1 = 870;
					nudge2 = -870;
					nudge3 = 0;
				}	
				 
				//set each animation callback var
				nudge1_comp = false;
				nudge2_comp = false;
				nudge3_comp = false;
				//do animation
				message = "article = "+curItem+", pane = "+curPane+" ("+nudge1+", "+nudge2+", "+nudge3+")<br>"//curLeft+" "+$JQ("#articlePane1").css("left");
				$JQ('.articleImg embed').css('display','none');
				$JQ("#articlePane1").animate({ left: nudge1 },function(){
					$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
					nudge1_comp = true;
				});
			    $JQ("#articlePane2").animate({ left: nudge2 },function(){
					$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
					nudge2_comp = true;
				});
			    $JQ("#articlePane3").animate({ left: nudge3 }, function(){
			    	$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
					//alert(curItem+" a "+preloadPane+" && "+articleArray[curItem-2]);
					//once the animation is complete make sure the next one is loaded ready to slide
			    	nudge3_comp = true;
			    	if(preloadPane && articleArray[curItem-1]){
			    		//$JQ("#temp")[0].innerHTML += "do "+preloadPane+" loading: "+articleArray[curItem-2]+"<br>";

						$JQ(preloadPane).load(articleArray[curItem-1]+"&divid="+preloadPane.replace("#",""), {random: Math.floor(Math.random()*999999)}, function(){
						//$JQ(preloadPane).css({"background":"url('images/articles/article"+parseInt(-1)+"b.jpg') -7px -7px no-repeat"});
							initLayer(preloadPane);$JQ(preloadPane+" .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
							load_end = true;detectLinks ($JQ('div.articleBody a'),true);
							if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true) && (load_end == true)) {
								inProgress = false;
								
						  
						    }
						});
					} else if ((nudge1_comp == true) && (nudge2_comp == true) && (nudge3_comp == true))
						
					{
						inProgress = false;
						
					}
				});
			   
			}
			getArtcileUrl (curPane);
			return false;
		})
		
		//get a list of all our pane items
		var items = new Array();
		items = getPaneArray(curItem);
		
		var found = false;
		
		for(i=0;i<items.length;i++){
			id = i+1;
			if(curItem==items[i]){
				curPane =id;
			}
		}
		
		if(articleArray[items[0]]!=undefined || articleArray[items[0]]!=null){
			$JQ("#articlePane1").load(articleArray[items[0]]+"&divid=articlePane1&r="+Math.floor(Math.random()*999999), function(){
				$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
				initLayer("#articlePane1");
				$JQ("#articlePane1 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				detectLinks ($JQ('div.articleBody a'),true);
				if(curPane==1){
					window.location.hash = getAnchorParams(curPane);
						if (firstrun === 0) {
						
					//	getArtcileUrl (curPane);
						firstrun = 1;
						}
				return false;}
				
			});
		}
		if(articleArray[items[1]]!=undefined || articleArray[items[1]]!=null){
			$JQ("#articlePane2").load(articleArray[items[1]]+"&divid=articlePane2&r="+Math.floor(Math.random()*999999), function(){
				$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
				initLayer("#articlePane2");
				$JQ("#articlePane2 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				detectLinks ($JQ('div.articleBody a'),true);
			
				if(curPane==2){
					if (firstrun === 0) {
						
						getArtcileUrl (curPane);
						firstrun = 1;
						}
					window.location.hash = getAnchorParams(curPane);}
				return false;
			});
		}
		if(articleArray[items[2]]!=undefined || articleArray[items[2]]!=null){
			$JQ("#articlePane3").load(articleArray[items[2]]+"&divid=articlePane3&r="+Math.floor(Math.random()*999999), function(){
				$JQ('#articlePane'+curPane+' .articleImg embed').css('display','inline');
				initLayer("#articlePane3");
				$JQ("#articlePane3 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				detectLinks ($JQ('div.articleBody a'),true);
				if(curPane==3){
						if (firstrun === 0) {
						
						getArtcileUrl (curPane);
						firstrun = 1;
						}
					window.location.hash = getAnchorParams(curPane);}
				return false;
			});
		}
		
		
		var panel1Left;
		var panel2Left;
		var panel3Left;
		if(curPane==1){
			panel1Left = 0;
			panel2Left = 870;
			panel3Left = -870;	
		}
		else if(curPane==2){
			panel1Left = -870;
			panel2Left = 0;
			panel3Left = 870;
		}
		else{
			panel1Left = 870;
			panel2Left = -870;
			panel3Left = 0;
		}
		
		$JQ("#articlePane1").css({"left":""+panel1Left+"px"});
		$JQ("#articlePane2").css({"left":""+panel2Left+"px"});
		$JQ("#articlePane3").css({"left":""+panel3Left+"px"});
		
		if(curItem<=0){$JQ(".slideButtonLeft").addClass("disabledLeft");}
		else{$JQ(".slideButtonLeft").removeClass("disabledLeft");}
		
		if(curItem>=articleArray.length-1){$JQ(".slideButtonRight").addClass("disabledRight");}
		else{$JQ(".slideButtonRight").removeClass("disabledRight");}
		
		 
		
	}
	
	$JQ("#category").val(getAnchorValues("category",window.location.hash));
	if($JQ("#category").val().length>0){
		var category = $JQ("#category").val();
		$JQ("#footer_menu > *").removeClass('active');
		//add current active menu
		var active_menu = $JQ('#menu'+category);
		active_menu.addClass('active');
	}
}

function getAnchorParams(curPane){
	var pars = "";
	
	if ($JQ('#mode').val()!='grid'){

		var article = $JQ("#articlePane"+curPane+"post_id").val();
		
		if (article == undefined) {
			return ;
		}
		else if((article.length>0) && (article !=undefined)){
			var pars = "&article="+article;
		}
	}
	
	var post_count = $JQ('#post_count').val();//added to prevent a refresh on a single page loading all articles	
	if (post_count==1){
		pars+="&single=true";
	}
	var category = $JQ("#category").val();
	if(category.length>0){
		pars += "&category="+category;
		
	}
	var year = $JQ("#year").val();
	if(year.length>0){
		pars += "&year="+year;
	}
	var month = $JQ("#month").val();
	if(month.length>0){
		pars += "&month="+month;
	}
	
	var page = $JQ("#page").val();
	if(page.length>0){
		pars += "&page="+page;
		
	}
	var tag = $JQ("#tag").val();
	if(tag.length>0){
		pars += "&t="+tag;
	}
	var search = $JQ("#search_term").val();
	if(search.length>0){
		pars += "&s="+cleanSearch(search,'add');
	}

	return pars;
	
}

function getPaneArray(curItem){
	var items = new Array();
	if(curItem>0){items.push(curItem-1);}
	items.push(curItem);
	if(curItem+1<articleArray.length){items.push(curItem+1);}
	
	
	//now we pad it if we could not generate everything we wanted
	i=curItem+2;
	
	while(items.length<3&&i<articleArray.length){
		items.push(i);
		i++;
	}
	i=curItem-2;
	while(items.length<3&&i>=0){
		temp = new Array();
		temp.push(i);
		temp = temp.concat(items);
		items=temp;
		i--;
	}	
	return items;
}
function getTeaserPaneArray(curItem){
	
	var items = new Array();
	if(curItem>0){items.push(curItem-1);}
	items.push(curItem);
	if(curItem+1<teaserArray.length){items.push(curItem+1);}
	
	
	//now we pad it if we could not generate everything we wanted
	i=curItem+2;
	
	while(items.length<3&&i<teaserArray.length){
		items.push(i);
		i++;
	}
	i=curItem-2;
	while(items.length<3&&i>=0){
		temp = new Array();
		temp.push(i);
		temp = temp.concat(items);
		items=temp;
		i--;
	}	
	return items;
}

/*======= article flip + grid swap =======*/
function initTeasers(){

	$JQ(".readMore").unbind('mouseenter');
	$JQ(".readMore").bind('mouseenter', function() {
		var readMoreLink = $JQ(this);
		readMoreLink.css('text-decoration','none');
	});

	$JQ(".readMore").unbind('mouseleave');
	$JQ(".readMore").bind('mouseleave', function() {
		var readMoreLink = $JQ(this);
		readMoreLink.css('text-decoration','underline');
	});

	var artiFlip = true;
	var textActive=$JQ("#gat").attr('class');
	if (textActive=='active') {
		artiFlip = false;
	}
	$JQ(".article").hover(function(){
		if(artiFlip){
			childrenArr = $JQ(this).children();
			$JQ(childrenArr[1]).slideDown(150);
		}
	},function(){
		if(artiFlip){
			childrenArr = $JQ(this).children();
			$JQ(childrenArr[1]).slideUp(500);
		}
	})
	$JQ("#gai").click(function(){ 
		artiFlip = true; 
		childrenArr = $JQ(".article .articleTeaserOff");
		$JQ(childrenArr).slideUp(150);
		$JQ("#gai").attr({"class":"active"});
		$JQ("#gat").removeClass();
		return false;
	})
	
	$JQ("#gat").click(function(){ 
		artiFlip = false; 
		childrenArr = $JQ(".article .articleTeaserOff");
		$JQ(childrenArr).slideDown(500);
		$JQ("#gat").attr({"class":"active"});
		$JQ("#gai").removeClass();
		return false;
	})
	
}
/*======= article loading + layer initiation =======*/
function initLayer(paneId){
	
	var oldLayer = false;
	$JQ(paneId+" .overButton").click(function(){
		
		if($JQ(this).parent().prev().prev().html()){
			tabNo = 3;
			theLayer = $JQ(this).parent().parent().parent().next().next().next();
			pageTracker._trackPageview($JQ('#article_url').val()+'sharethis/open');
			detectLinks($JQ('.overLayer07 ul.menu li a'),false);
		} else if($JQ(this).parent().prev().html()){
			tabNo = 2;
			theLayer = $JQ(this).parent().parent().parent().next().next();
			pageTracker._trackPageview($JQ('#article_url').val()+'viewcomments/open');
		} else {
			tabNo = 1;
			theLayer = $JQ(this).parent().parent().parent().next();
			pageTracker._trackPageview($JQ('#article_url').val()+'writecomment/open');
		}
	
		
		$JQ(".overButtonOn").attr({"class":"overButton"});
		$JQ(this).attr({"class":"overButtonOn"});
		if($JQ(theLayer).css("display")=="none"){
			if(oldLayer!=false){
				
				/*$JQ(oldLayer).fadeOut(100, function(){
					$JQ(theLayer).fadeIn(250, function(){
						
						if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane1").css("left")=="0px"){
						 $JQ("#articlePane1 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
						}
						if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane2").css("left")=="0px"){
						 $JQ("#articlePane2 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
						}
						if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane3").css("left")=="0px"){
						 $JQ("#articlePane3 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
						}
					});
					oldLayer = theLayer;
				});*/
				$JQ(oldLayer).css('display','none');
				$JQ(theLayer).css('display','block');
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane1").css("left")=="0px"){
				 $JQ("#articlePane1 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane2").css("left")=="0px"){
				 $JQ("#articlePane2 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane3").css("left")=="0px"){
				 $JQ("#articlePane3 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				oldLayer = theLayer;
				

			}else{
				/*$JQ(theLayer).fadeIn(250, function(){
				
					if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane1").css("left")=="0px"){
					 $JQ("#articlePane1 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
					}
					if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane2").css("left")=="0px"){
					 $JQ("#articlePane2 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
					}
					if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane3").css("left")=="0px"){
					 $JQ("#articlePane3 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
					}
				});*/
				$JQ(theLayer).css('display','block');
			
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane1").css("left")=="0px"){
				 $JQ("#articlePane1 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane2").css("left")=="0px"){
				 $JQ("#articlePane2 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				if($JQ(theLayer).css("display")!="none"&& $JQ(theLayer).attr("class")=="overLayer overLayer06"&& $JQ("#articlePane3").css("left")=="0px"){
				 $JQ("#articlePane3 .scroll").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
				}
				
				oldLayer = theLayer;
			}
		} else {
			//$JQ(theLayer).fadeOut(250);
			$JQ(theLayer).css('display','none');
			$JQ(".overButtonOn").attr({"class":"overButton"});
			oldLayer = false;
		}
		return false;
	})
	if ($JQ('#type').val()!="preview"){
	$JQ(paneId+" .overLayer").prepend("<a href=\"#\" class=\"x\"><span>X</span></a>");
	}
	$JQ(paneId+" .x").click(function(){
		parrentArr = $JQ(this).parent("DIV");
		//$JQ(parrentArr[0]).fadeOut(250);
		$JQ(parrentArr[0]).css('display','none');
		$JQ(".overButtonOn").attr({"class":"overButton"});
		return false;
	})
	
	
	setArticleCloseButtons();
	
	
	
}
/*======= article loading + layer initiation =======*/
function shiftYear(increment){
	alert(increment)
}

/*======= Handles doing the whole search thing =====*/
function search(searchTerm){
	//get our search term
	$JQ('#page').val('1');
	if(searchTerm==undefined){
		searchTerm = cleanSearch($JQ("#contentSearchSiteTerm").val(),'add');
	}
	if(searchTerm.length==0 || searchTerm=="Search site:"){
		alert("Please insert a search term.");
		return false;
	}
	//check to see if we are in preview mode
	var preview = $JQ("#preview").val();
	if(preview.length>0){preview = "?preview=true";}
	
	//if we are here we are displaying one article at a time	
	if($JQ("#mode").val()=="grid"){
		filterSection("","",searchTerm,"","","");
		window.location = siteURL+"/"+preview+"#&s="+cleanSearch(searchTerm,'add');
	}
	else{
		
		window.location = siteURL+"/"+preview+"#&s="+cleanSearch(searchTerm,'add');
		
	}
		
	return false;
}

function tagSearch(tag) {

	var preview = $JQ("#preview").val();
	if(preview.length>0){preview = "?preview=true";}

	if($JQ("#mode").val()=="grid"){
		$JQ('#page').val('1');
		filterSection("","","","","", tag);
		window.location = siteURL+"/"+preview+"#&t="+tag;
	}
	else{
		window.location = siteURL+"/"+preview+"#&t="+tag;
	}
	return false;
}

function filterSection(category_id,page,searchTerm,year,month,tag){
	var pars = "";
	
	if(category_id.length>0){
		pars = "&category="+category_id;
		$JQ("#category").val(category_id);
	}
	else{
		$JQ("#category").val("");
	}
	if(page.length>0){
		pars += "&page="+page;
	}
	else{
		
		pars += "&page=1";	
	}
	if(searchTerm.length>0){
		$JQ("#search_term").val(cleanSearch(searchTerm,'strip'));
		pars += "&s="+cleanSearch(searchTerm,'add');
	}
	else{
		$JQ("#search_term").val("");
	}
	if(tag) {
		$JQ("#tag").val(tag);
		pars += '&t=' + tag;
	}
	else{
		$JQ("#tag").val("");
	}
	if (!category_id) {
	
			if(year.length==0){
				year = getAnchorValues("year",window.location.hash);
			}
			if(month.length==0){
				month = getAnchorValues("month",window.location.hash);
			}
			
			//check to see if we are going to filter by date
			if(year.length>0){
				
				$JQ("#year").val(year);
				pars += "&year="+year;
			}
			else{				
				$JQ("#year").val();
			}
			if(month.length>0){
				$JQ("#month").val(month);
				pars += "&month="+month;
			}	
			else{
				$JQ("#month").val("");
			}
		
	}
	var preview = $JQ("#preview").val();
	if(preview.length>0){pars += "&preview=true";}
	
	
	var single = getAnchorValues("single",window.location.hash);
	if(single.length>0&&single=="true"&&$JQ("#mode").val()!="grid"){
		$JQ("#post_count").val(1);
		loadArticleArray();
	}
	else{
	$JQ.get(template_directory+"/services/getPostCount.php?"+pars, function(data){
		$JQ("#post_count").val(data);
		
		//depending on what mode we are call the relevent function
		if($JQ("#mode").val()=="grid"){ 
			loadTeaserArray();
		
		}
		else{
			
			loadArticleArray();
		}
	});
	}
	return false;
}

function loadTeaserArray(){
	var service = template_directory+"/services/homeXHTML.php";
	var page = $JQ("#page").val();
	var pages = Math.ceil($JQ("#post_count").val()/9);
	var category = getAnchorValues("category",window.location.hash);
	teaserArray = new Array();
	var pars = "";
	if(category.length>0){
		pars+="?category="+category;
	}
	
	//check preview mode
	var preview = $JQ("#preview").val();
	if(preview.length>0){pars += "&preview=true";}
	
	searchTerm = cleanSearch($JQ("#search_term").val(),'add');	
	year = $JQ("#year").val();
	month = $JQ("#month").val();
	tag = $JQ('#tag').val();
	if(page>1){
		
		i=page;
		$JQ.post(template_directory+"/services/getPageList.php"+pars,function(data){
			
			var ids = data.split(",");
			for(var i=0	;i<=ids.length;i++){
				pars="?page="+ids[i];
				if(tag.length>0){
					pars += "&t="+tag;
				}
				if(category.length>0){
					pars+="&category="+category;
				}
				teaserArray.push(service+pars);
			
			}
			hpInit();});
	}
	else {
		
		//handle null search results
		if (pages==0 && searchTerm.length>0)
		{	
			pars="?page=0&s=null";
			teaserArray.push(service+pars);
		}
		//continue as normal
		else {
		i=1;
		
	
	for(i;i<=pages;i++){
		
		pars="?page="+i;
		if(category.length>0){
			pars+="&category="+category;
		}
		if(searchTerm.length>0){
			pars += "&s="+cleanSearch(searchTerm,'add');
		}
		if(tag.length>0){
			pars += "&t="+tag;
		}
		if(year.length>0){
			pars += "&year="+year;
		}
		if(month.length>0){
			pars += "&month="+month;
		}	
		
		//check preview mode
		var preview = $JQ("#preview").val();
		if(preview.length>0){pars += "&preview=true";}
		
		teaserArray.push(service+pars);
		}
		}
		//end else
	hpInit();
	}

	
}


function loadArticleArray(){
	
	var service = template_directory+"/services/articleXHTML.php";
	var posts = $JQ("#post_count").val();
	var category = $JQ("#category").val();
	var search_term = $JQ("#search_term").val();
	var year = $JQ("#year").val();
	var month = $JQ("#month").val();
	var pars="?";
	
	if(category.length>0){
		pars+="&category="+category;
	}
	if(search_term.length>0){
		pars+="&s="+cleanSearch(search_term,'add');
	}
	if(year.length>0){
		pars+="&year="+year;
	}
	if(month.length>0){
		pars+="&month="+month;
	}
	
	//check preview mode
	var preview = $JQ("#preview").val();
	if(preview.length>0){
		preview += "&preview=true";
		pars += "&preview=true";
	}
	
	var r ="&r="+Math.floor(Math.random()*999999);
	articleArray = new Array();
	
	var single = getAnchorValues("single",window.location.hash);
	var article = getAnchorValues("article",window.location.hash);
	
	if(single.length>0 && single=="true" && article.length>0){		
		articleArray.push(service+"?post_id="+article+preview+r);
		spInit();
	
	}
	else{
	//get a list of all the post id's
	

	$JQ.post(template_directory+"/services/getIDList.php"+pars,function(data){
		var ids = data.split(",");
		for(var i=0;i<ids.length;i++){
			pars="?post_id="+ids[i];
			if(category.length>0){
				pars+="&category="+category;
			}
			articleArray.push(service+pars+preview+r);
		}
		
		spInit();	
	});
	}
	
}

function processCategory(category){

	//check preview mode
	var preview = $JQ("#preview").val();
	if(preview.length>0){pars += "?preview=true";}

	if($JQ("#mode").val()=="grid"){
		$JQ('#year').val('');
		$JQ('#month').val('');
		$JQ('#page').val('1');
		$JQ('#overLayer04').css('display','none');
		$JQ('#overButton04').removeClass('overButtonOn');
		
		filterSection(category,"","","","","");
	//$JQ('#category').value = category;//set hidden form field
	//clear any active menu items
	$JQ("#footer_menu > *").removeClass('active');
	//add current active menu
	var active_menu = $JQ('#menu'+category);
	active_menu.addClass('active');
		window.location = siteURL+"/"+preview+"#&category="+category;
	}
	else{
		window.location = siteURL+"/"+preview+"#&category="+category;
	}

}

/**
 * Clean spaces from search terms
 * 
 */

function cleanSearch (searchTerm,flag) {
	var clean;
	if (flag=='add') {
	clean = searchTerm.replace(/ /g,'+');
	
	return clean;
	}
	if (flag=='strip') {
		clean = searchTerm.replace(/\+/g,' ');
		
		return clean;
	}
	
}

function textSize(dir) {
	
	var currentSize = $JQ("body").attr("class").replace(/size/, '');
	
	if (dir == 'up') {
		if (currentSize < 5) {			
			currentSize++;
			$JQ("body").attr("class", "size"+currentSize);
			$JQ.ajax({
				url: template_directory+"/services/setTextSize.php", 
				data: {textsize: "up"},
				async: true,
				type: "GET",
				dataType: "html",
				cache: false});
			$JQ("#articlePane1 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
			$JQ("#articlePane2 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
			$JQ("#articlePane3 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
		}
	}

	if (dir =='down') { 
		if (currentSize > 1) {			
			currentSize--;
			$JQ("body").attr("class", "size"+currentSize);
			$JQ.ajax({
				url: template_directory+"/services/setTextSize.php", 
				data: {textsize: "down"},
				async: true,
				type: "GET",
				dataType: "html",
				cache: false});
			$JQ("#articlePane1 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
			$JQ("#articlePane2 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
			$JQ("#articlePane3 .scroll-pane").jScrollPane({"showArrows":"true","arrowSize":"13","scrollbarWidth":"6","scrollbarMargin":"20"});
		}
	}
	
	return false;
}