$(function(){
	var currentURL = window.location;
	var baseURL = "http://www.bhlink.org";

	//Body Load
	if ((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<="8")) { } else {
		$(".Phuse_PanelContainingDiv td").each(function(){ $(this).css("width",$(this).parent().parent().parent().attr("width")+"px"); });
	}
	if ($.cookie("font_size")) {
		 $("#font_size").attr("href",baseURL+"/res/css/"+$.cookie("font_size")+".css");
	}
	if ($.cookie("accessibility")) {
		 $("#accessibility_style").attr("href",baseURL+"/res/css/"+$.cookie("accessibility")+".css");
	}
	if ($.cookie("accessibility")=="high_contrast") {
		$("#high_contrast").text("Normal contrast").attr("id","normal_contrast");	
	} else if ($.cookie("accessibility")=="text_only") {
		$("#text_only").text("Reset graphics").attr("id","reset_graphics");	
	}
	$("h2,h3,h4,h5,h6").prev("a").hide();
	$("#footer_images img:first").css("border-left","0px");
	$(".MMDataCaptureForm_Table").vertTable();
	$("#breadcrumb").each(function(){
        var charReplace = $(this).html();
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        $(this).html(charReplace);
    });
	$(".calendar").parent().parent().parent().parent().removeAttr("style").wrap("<div class=\"calendar_container\"></div>");
	$(".calendar_container *").removeAttr("style");
	$(".calendar_container table tr").each(function(){
		$("td:first",this).css("border-left","0px");
		$("td:last",this).css("border-right","0px");
	});
	$(".event_item_small .date").each(function(){
		var thisDate = $(this);
		var thisText = thisDate.text();
		thisDate.text(thisText.substr(0,5));
	});
	$(".events_item_large .date").each(function(){
		var thisDate = $(this);
		var thisText = thisDate.text();
		var thisMonth = showMonth(thisText.substr(3,2));
		thisDate.html("<span>"+thisMonth+"</span>"+thisText.substr(0,2));
	});
	$(".news_container,.events_container").pagination();
	$(".newsletter_container").pagination({perPage:7});
	$("#step02,#step03,#step04").hide();
	if ($(".collapsible_content").length>0) {
		$(".collapsible_header").nextUntil(".collapsible_header").hide();
	}
	$(".youtube a").each(function(){
		var thisLink = $(this).attr("href");
		var thisCode = thisLink.match("[\\?&]v=([^&#]*)");
		var youTube = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'+thisCode[1]+'&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+thisCode[1]+'&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
		$(this).replaceWith(youTube);
	});
	$(".news_item_archive:lt(5)").hide();
	$(".news_archive").pagination({"speed":500});
	
	//User Interaction
	$(".collapsible_header").live("click",function(){
		$(this).nextUntil(".collapsible_header").toggle();
	});
	$("a[href^='http']").not("[href*='bhlink.org']").attr("target","_blank");
	$("a[href$='.pdf']").attr("target","_blank");
	$("#social_media ul li a").live("click",function(){ return false; });
    $("#delicious").live("click",function(){ location.href="http://stumbleupon.com/submit?url="+currentURL; });
    $("#digg").live("click",function(){ location.href="http://digg.com/submit?url="+currentURL; });
    $("#facebook").live("click",function(){ location.href="http://facebook.com/sharer.php?u="+currentURL; });
    $("#netvibes").live("click",function(){ location.href="http://netvibes.com/share?url="+currentURL; });
    $("#reddit").live("click",function(){ location.href="http://reddit.com/submit?url="+currentURL; });
    $("#stumble").live("click",function(){ location.href="http://stumbleupon.com/submit?url="+currentURL; });
	$("#twitter").live("click",function(){ location.href="http://twitter.com/home?status=Currently reading "+currentURL; });
	$("#email").attr("href","").live("click",function(){ location.href="mailto:?subject=Brighton LINk&body="+currentURL;return false; });
	$("#small").live("click",function(){
		$.cookie("font_size",null,{path:"/"});
		$("#font_size").attr("href",baseURL+"/res/css/small.css");
		$.cookie("font_size","small",{path:"/"});
	});
	$("#medium").live("click",function(){
		$.cookie("font_size",null,{path:"/"});
		$("#font_size").attr("href","");
	});
	$("#large").live("click",function(){
		$.cookie("font_size",null,{path:"/"});
		$("#font_size").attr("href",baseURL+"/res/css/large.css");
		$.cookie("font_size","large",{path:"/"});
	});
	$("#high_contrast").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility_style").attr("href",baseURL+"/res/css/high_contrast.css");
		$.cookie("accessibility","high_contrast",{path:"/"});
		$(this).text("Normal Contrast").attr("id","normal_contrast");
		$("#text_only").text("Text Only").attr("id","text_only");
	});
	$("#normal_contrast").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility_style").attr("href","");
		$(this).text("High Contrast").attr("id","high_contrast");
	});
	$("#text_only").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility_style").attr("href",baseURL+"/res/css/text_only.css");
		$.cookie("accessibility","text_only",{path:"/"});
		$(this).text("Reset Graphics").attr("id","reset_graphics");
		$("#high_contrast").text("High Contrast").attr("id","high_contrast");
	});
	$("#reset_graphics").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility_style").attr("href","");
		$(this).text("Text Only").attr("id","text_only");
	});
	$("a[href*='submit-an-issue']").addClass("iframe").fancybox({"frameWidth":600,"frameHeight":550,"hideOnContentClick":false,"overlayOpacity":0.6});
	$(".go_to_step01").live("click",function(){ $(".get_involved_form").hide();$("#step01").fadeIn(); });
	$(".go_to_step02").live("click",function(){ $(".get_involved_form").hide();$("#step02").fadeIn(); });
	$(".go_to_step03").live("click",function(){ $(".get_involved_form").hide();$("#step03").fadeIn(); });
	$(".go_to_step04").live("click",function(){ $(".get_involved_form").hide();$("#step04").fadeIn(); });
});

function favourites(){
	var currentURL = window.location;
	var title = "Brighton LINk";
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		window.external.AddFavorite(currentURL,title);
	} else if (navigator.appName == "Netscape") {
		window.sidebar.addPanel(title,currentURL,"");
	} else {
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
	}
}

function fancybox(){
	$(function(){
		$(".iframe").click();
	});
}

function showMonth(date){
	if (date=="01") { return "Jan"; }
	else if (date=="02") { return "Feb"; }
	else if (date=="03") { return "Mar"; }
	else if (date=="04") { return "Apr"; }
	else if (date=="05") { return "May"; }
	else if (date=="06") { return "Jun"; }
	else if (date=="07") { return "Jul"; }
	else if (date=="08") { return "Aug"; }
	else if (date=="09") { return "Sep"; }
	else if (date=="10") { return "Oct"; }
	else if (date=="11") { return "Nov"; }
	else if (date=="12") { return "Dec"; }
}
