function  SetupLogin(){
	$j("#login_box").hide();
	$j("a.togglelogin").click(function(event){
		if  ($j("#login_box").is(':hidden')) {
			$j(this).html("cancel");
			$j("#login_box").show("normal");
			document.forms['catseczoneform269'].elements['SZUsername'].focus();
		} else{
			$j(this).html("log in &raquo;");
			$j("#login_box").hide("normal");
			document.forms['catseczoneform269'].elements['SZUsername'].blur();
		}
		// Stop the link click from doing its normal thing
		return false;
	});
}

function MembersNav(){
	if  (loggedin == 1){
		$j('#navregister').hide();
		$j('#guestlogin').hide();
		$j('#guestloginbutt').hide();
		$j('#membersname').show();
		$j('#memberlogin').show();
	}else{
		$j('#navregister').show();
		$j('#guestlogin').show();
		$j('#guestloginbutt').show();
		$j('#membersname').hide();
		$j('#memberlogin').hide();
	}
}


function LatestIssue(){
	var n = $j("#latestliterature .literaturelistitem");

	if (n.size() > 0){

		$j(n).each(function(i){
			var loc = $j(this).find("a").attr("href");
			//var img2 = $j(this).find("img");
			var img2 = $j(this).find(".litimg").text();
			var src2 = "/utilities/showthumbnail.aspx?USM=1&R=1&W=200&H=250&Img=/Images/issues/lg/" + img2 + ".jpg";
			//var src2 = $j(img2).attr("src");
			$j(this).find(".litimg").empty();
			$j(this).find(".litimg").prepend("<img src='" + src2 + "' />");
			
			$j("img", this).wrap("<a href='" + loc + "'></a>");
			//$j(img2).attr("src", "/utilities/showthumbnail.aspx?USM=1&R=1&W=200&H=250&Img=/Images/issues/lg/" + src2);
			//$j(img2).wrap("<a href='" + loc + "'></a>");
		});
	}
}

function LatestIssueHomePage(){
	var n = $j(".latestlithome #latestliterature .literaturelistitem");

	if (n.size() > 0){

		$j(n).each(function(i){
			var loc = $j(this).find("a").attr("href");
			var img2 = $j(this).find(".litimg").text();
			//var img2 = $j(this).find("img");
			//var src2 = $j(img2).attr("src");
			var src2 = "/utilities/showthumbnail.aspx?USM=1&R=1&W=500&H=600&Img=/Images/issues/lg/" + img2 + ".jpg";
			
			//$j(img2).attr("src", "/utilities/showthumbnail.aspx?USM=1&R=1&W=500&H=600&Img=/Images/issues/lg/" + src2);
			$j(this).find(".litimg").empty();
			$j(this).find(".litimg").prepend("<img src='" + src2 + "' />");
			
			$j("img", this).wrap("<a href='" + loc + "'></a>");
		});
	}
}

function isPost(){

	var str=window.location.href;
	console.log(str.search(/_bpost/i)> 0);
	
	if( str.search(/_bpost/i)> 0 ){
		console.log("post view");
		return true;
	}else{
		console.log("not post view");
		return false;
	}

}


function ShowComments(){
	var str=window.location.href;
	if ($j(".commentblock").size() > 0){
		
		if( str.search(/_bpost/i)> 0 || str.search(/PostID=/i)> 0 ){
			$j(".commentblock").show();
			if (loggedin == 1){
				$j(".commentform").show();
				$j(".commentformlogin").hide();
			}else{
				$j(".commentform").hide();
				$j(".commentformlogin").show();
			}
		}else{
			$j(".commentblock").hide();
		}
	}
}

function BlogBreadCrumbs(){
	var t = "";
	var linktext = "";
	if($j(".blogbreadcrumb").size()>0){
		t = $j(".blogbreadcrumb").text();
		linktext = t.replace(" ","_");
		$j(".blogbreadcrumb").wrap("<a href='/_blog/" + linktext + "'></a>");
	}
}

function features_initCallback(carousel){
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {carousel.stopAuto();}, function() {carousel.startAuto();});
}

function setupHomePageFeaturesRotation(){

	if($j("#features").size()>0){
	
		$j(".feature").each(function(i){
			$j(this).wrap("<li></li>");
			//switch web app url (wrapping the image) with the actual feature URL
			var url = $j(".featureurl", this).attr("href");
			var imgHref = $j(".featureimg a", this).get(0);
			$j(imgHref).attr("href", url);
			
		});
		
		$j('#features').jcarousel({
			auto: 5,
			scroll: 1,
			wrap: 'both',
			initCallback: features_initCallback
		});
	}
}

function displayHomePageCompetitions(){
	
	if($j(".competitionslist").size()>0){
		
		var cl = $j(".competitionslist").get(0);
		$j(cl).css({ "margin-top":"0"});
		
		if($j(".compimage").size()>0){
			
			$j(".competition").each(function(i){
				var compImage = $j(".compimage", this ).get(0);
				var title = $j("h3", this ).text();
				$j(this).empty();
				$j(this).append(compImage);
				var img = $j("img", this ).get(0);
				var src = $j(img).attr("src");
				$j(img).attr("src", "/utilities/showthumbnail.aspx?USM=1&R=1&W=100&H=100&Img=" + src);
				$j(img).attr("title", title);
				$j(img).attr("alt", title);
				$j(this).wrap("<li></li>");
			});
		
			$j("#homepageCompetitions").jcarousel({
				auto: 0,
				scroll: 1,
				wrap: 'none'
			});
		}else{
			//alert("no comps");
		}
	}
}
	
	
	
function displayHomePageIssues(){
	
	if($j(".issueslist").size()>0){
		
		var il = $j(".issueslist").get(0);
		$j(il).css({ "margin-top":"0"});
		
		$j("#homepageIssues .literaturelistitem").each(function(i){
			var loc = $j(this).find("a").attr("href");
			var img2 = $j(this).find(".litimg").text();
			var src2 = "/utilities/showthumbnail.aspx?USM=1&R=1&W=100&H=100&Img=/Images/issues/lg/" + img2 + ".jpg";
			$j(this).find(".litimg").empty();
			$j(this).find(".litimg").prepend("<img src='" + src2 + "' />");
			$j("img", this).wrap("<a href='" + loc + "'></a>");
		});
		
		$j("#homepageIssues").jcarousel({
			auto: 0,
			scroll: 1,
			wrap: 'none'
		});
		
	}else{
		//alert("no issues to display");
	}
	
}

function displayHomePageIssuesGC(){
	
	if($j(".issueslist").size()>0){
		
		var il = $j(".issueslist").get(1);
		$j(il).css({ "margin-top":"0"});
		
		$j("#homepageIssuesGC .literaturelistitem").each(function(i){
			var loc = $j(this).find("a").attr("href");
			var img2 = $j(this).find(".litimg").text();
			var src2 = "/utilities/showthumbnail.aspx?USM=1&R=1&W=100&H=100&Img=/Images/issues/lg/" + img2 + ".jpg";
			$j(this).find(".litimg").empty();
			$j(this).find(".litimg").prepend("<img src='" + src2 + "' />");
			$j("img", this).wrap("<a href='" + loc + "'></a>");
		});
		
		$j("#homepageIssuesGC").jcarousel({
			auto: 0,
			scroll: 1,
			wrap: 'none'
		});
		
	//}else{
		//alert("no issues to display");
	}
	
}

function homePageCheckThisOut(){
	if($j(".homecheckthisout").size()>0){
		
		$j(".homecheckthisout").each(function(i){
				
				$j(".author", this).remove();
				$j(".date", this).remove();
											  
		});
	}
	
	
	if($j(".homewhatsnews").size()>0){
		
		$j(".homewhatsnews").each(function(i){
				
				$j(".author", this).remove();
				$j(".date", this).remove();
											  
		});
	}
	
	
}


function homePageWhatsNews(){
	
	
	
}


$j(document).ready(function(){
	 MembersNav();
	 SetupLogin();
	 // if(!$j.browser.msie){
	 $j('.rounded').corners("10px");
	 //rounded corners on home page causing too many problems with the Carousel
	 //$j('.homebox').corners("10px");
	 //$j('.homeboxblue').corners("10px");
	  //}
	 homePageCheckThisOut();
	 setupHomePageFeaturesRotation();
	 displayHomePageCompetitions();
	 displayHomePageIssues();
         displayHomePageIssuesGC();
	 LatestIssue();
	 LatestIssueHomePage();
	 ShowComments();
	 BlogBreadCrumbs();
	 
});