$(function(){
	jQuery.expr[':'].Contains = function(a, i, m) {return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;};
	var currentURL = window.location;
	if ($.cookie("font_size")) {
		 $("#font_size").attr("href","http://www.mersey-maritime.co.uk/res/css/"+$.cookie("font_size")+".css");
	}
	if ($.cookie("accessibility")) {
		 $("#accessibility").attr("href","http://www.mersey-maritime.co.uk/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");	
	}
	if ((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<="8")) { } else {
		$(".Phuse_PanelContainingDiv td").each(function(){ $(this).css("width",$(this).parent().parent().parent().attr("width")+"px"); });
	}
	$(".MainMenuItem_Over:visible").parent().parent().addClass("nav_active");
	$(".NavigationMenu td:first").children("div").addClass("nav_first");
	$(".NavigationMenu td:last").children("div").addClass("nav_last");
	$(".NavigationMenu td:last a,#footer_links .left a:last").css("border-right","0px");
	$(".calendar_container *").removeAttr("style");
	$(".calendar_container tr:nth-child(1) td:nth-child(1)").addClass("prev_month");
	$(".calendar_container tr:nth-child(1) td:nth-child(2)").addClass("month_title");
	$(".calendar_container tr:nth-child(1) td:nth-child(3)").addClass("next_month");
	$(".calendar_container tr:nth-child(2) td").addClass("calendar_day_title");
	$(".calendar_container tr:gt(2) td").addClass("calendar_day");
	$(".calendar_othermonth").empty();
	$(".prev_month a,next_month a").text("");
	$(".without_event").removeAttr("href");
	$("#blogItems").html($("#blogFeed").html());
	$(".grey_panel .MMDataCaptureForm_Table").vertTable();
	$("#ticker").each(function(){
		var itemLength = $("#ticker .menu_item").length;for($i=0;$i<=itemLength;$i++){$("#ticker .menu_item:eq("+$i+")").css("top",eval($i*59)-6+"px").css("z-index",$i+1).attr("rel",$i);}
	});
	$(".MMDataCaptureForm_SubmitButton").val("");
	$("#ticker .menu_item:first").addClass("active_item");
	$("#ticker .content:gt(0)").hide();
	$(".testimonial").hide();
    $(".testimonial:eq("+Math.floor(Math.random()*$(".testimonial").length)+")").show();
	$("#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;","|");
		charReplace = charReplace.replace("&gt;","|");
		$(this).html(charReplace);
	});
	$(".event_small .date").each(function(){
		var thisDate = $(this);
		var thisText = thisDate.text();
		var thisMonth = showMonth(thisText.substr(3,2));
		thisDate.text(thisText.substr(0,2));
	});
	$(".event_small,.news_item,.red_button_container,.search_result_item").fullLink();
	function showMonth(date){
		if (date=="01") { return "January"; }
		else if (date=="02") { return "February"; }
		else if (date=="03") { return "March"; }
		else if (date=="04") { return "April"; }
		else if (date=="05") { return "May"; }
		else if (date=="06") { return "June"; }
		else if (date=="07") { return "July"; }
		else if (date=="08") { return "August"; }
		else if (date=="09") { return "September"; }
		else if (date=="10") { return "October"; }
		else if (date=="11") { return "November"; }
		else if (date=="12") { return "December"; }
	}
	$(".generic_left li a").each(function(){
		var thisLink = $(this);
		var thisURL = thisLink.attr("href");
		if (thisURL==currentURL) {
			thisLink.addClass("sub_nav_active");
			thisLink.parent().children("ul").show();
		}
	});
	$(".generic_left li ul li a").each(function(){
		var thisLink = $(this);
		var thisURL = thisLink.attr("href");
		if (thisURL==currentURL) {
			thisLink.removeClass("sub_nav_active");
			thisLink.parent().parent().show();
			thisLink.parent().parent().parent().children("a").addClass("sub_nav_active");
		}
	});
	$("#tideTimes").html($(".tideTimes").html()).fadeIn();
	$(".news_item .filter:Contains('maritime')").parent().show();
	$(".news_container").pagination();
	var supportedHeight = $(".supported").height();
	var sponsoredHeight = $(".sponsored").height();
	var addressHeight = $(".address").height();
	if ((supportedHeight>sponsoredHeight)&&(supportedHeight>addressHeight)) {
	    $(".sponsored,.address").css("height",supportedHeight+"px");
	} else if ((sponsoredHeight>supportedHeight)&&(sponsoredHeight>addressHeight)) {
	    $(".supported,.address").css("height",sponsoredHeight+"px");
	} else {
	    $(".sponsored,.supported").css("height",addressHeight+"px");
	}
	//var countNews = $(".news_item").length;
	//alert(countNews);

	function populateDropdown(){
		var selectCategory = "<option value=\"null\">Select a category...</option>";
		var selectAll = "<option value=\"all\">All categories</option>";
		$("#news_filter_ddl").html("").append(selectCategory+""+selectAll);
		Array.prototype.unique = function () {
			var r = new Array();
			o:for(var i = 0, n = this.length; i < n; i++) {
				for(var x = 0, y = r.length; x < y; x++) {
					if(r[x]==this[i]) {
						continue o;
					}
				}
				r[r.length] = this[i];
			}
			return r;
		}
		var categoryArray = new Array();		
		//for (y=0;y<$(".news_item:visible").length;y++){
		for (y=0;y<130;y++){
			var thisCategory = $(".news_item:visible:eq("+y+")").children(".category").text();
			var thisCategory = thisCategory.toLowerCase()
			var thisCategory = thisCategory.charAt(0).toUpperCase() + thisCategory.slice(1);
			categoryArray[y]="<option value=\""+thisCategory+"\">"+thisCategory+"</option>";
		}
		var unique = categoryArray.unique();
		var sort = unique.sort();
		var string = sort.toString();
		$("#news_filter_ddl").append(string.replace(/,/g,"\n"));
	}
	populateDropdown();
	function showAllNews(){
		$(".news_item").show();
		$(".paging").remove();
		$(".news_item").css("top","0px");
		$(".news_container").pagination();
		populateDropdown();
	}
	function showMaritimeNews(){
		$(".news_item").hide();
		$(".news_item .filter:Contains('maritime')").parent().show();
		$(".paging").remove();
		$(".news_item").css("top","0px");
		$(".news_container").pagination();
		populateDropdown();
	}
	$("#news_filter_ddl").change(function(){
        var selectedValue = $("option:selected",this).val();
		if (selectedValue!="null") {
			$(".news_item").hide();
			$(".paging").remove();
			$(".news_item").css("top","0px");
			if (selectedValue=="all") {
				thisFilter=$(".tab_on").attr("id");
				if (thisFilter=="maritime_news") {
					showMaritimeNews();
				} else {
					showAllNews();
				}
			} else {
				$(".news_item .category:Contains('"+selectedValue+"')").parent().fadeIn();
				$(".news_container").pagination();
			}
		}
    });
	$("#maritime_news").live("click",function(){ showMaritimeNews(); });
	$("#all_news").live("click",function(){ showAllNews(); });
	var ticker=0;
	var newsTicker = setInterval(function(){
		ticker++;
		if (ticker>3) { ticker=0; }
		$("#ticker .content").hide();		
		$("#ticker .active_item").removeClass("active_item");
		$("#ticker .menu_item:eq("+ticker+")").addClass("active_item").parent().parent().children().children(".content").fadeIn(1000);
	},7000);
	$(".newsletter table tr").each(function(){
		$("td:first",this).addClass("label");
		$("td:last",this).addClass("input").children().addClass("textbox");
	});   
    $(".newsletter table .label").each(function(){
		var thisLabel = $(this);
		$("span",thisLabel).remove();
		var thisText = thisLabel.text();
		var newText = thisText.replace(/^\s+|\s+$/g, '');
		thisLabel.text(newText);
	}).wrapInner("<div></div>");
    $(".newsletter table input:last").addClass("SubmitButton").val("");
    $(".newsletter table textarea").parent().addClass("textareaTD");
    $(".newsletter table tr:last").addClass("SubmitButtonTR");
    $(".newsletter table td:last").addClass("SubmitButtonTD");
	if (window.location.hostname!="controlpanel2.phusecms.co.uk") {
		$("#event_details ul").each(function(){
			var thisEvent = $(this);
			var address2=new Array();
			var date = $("li:first:nth-child(1)",thisEvent).html();
			var time = $("li:first:nth-child(2)",thisEvent).html();
			var organiser = $("li:first:nth-child(3)",thisEvent).html();
			var address = $("li:first:nth-child(4)",thisEvent).not("li:first:nth-child(4) ul",thisEvent).html();
			var contact = $("li:first:nth-child(5)",thisEvent).html();
			var content = "<h2 class=\"red_underline\">Event details</h2><div class=\"date\"><strong>Date</strong><br />"+date+"</div><div class=\"time\"><strong>Time</strong><br />"+time+"</div><div class=\"organiser\"><strong>Organiser</strong><br />"+organiser+"</div><div class=\"venue\"><strong>Venue</strong><br />"+address.toString().replace(/,/g,"<br />")+"</div><div class=\"contact\"><strong>Contact</strong><br />"+contact+"</div>";
			$("#event_details").html(content);
		});
	}
    $(".Phuse_Search_Button").attr("value","");
	
	//Interaction
	
	$("#ticker .menu_item").live("click",function(){
		clearInterval(newsTicker);
		$("#ticker .content").hide();		
		$("#ticker .active_item").removeClass("active_item");
		$(this).addClass("active_item").parent().parent().children().children(".content").fadeIn();
	});
	$("input[type='text']").not(".form input[type='text']").each(function(){
		var thisInput = $(this);var origText = thisInput.val();
		thisInput.focus(function(){if(thisInput.val()==origText){thisInput.val("");}
		}).blur(function(){if(thisInput.val()==""){thisInput.val(origText);}});
	});
	$(".mm_form input[type='text']").each(function(){
		var thisInput = $(this);var origText = thisInput.val();
		thisInput.focus(function(){if(thisInput.val()==origText){thisInput.val("");}
		}).blur(function(){if(thisInput.val()==""){thisInput.val(origText);}});
	});
	$("#print").live("click",function(){ window.print(); return false; });
	$("#email").live("click",function(){ window.location="mailto:?body="+window.location; return false; });
	$("#small").live("click",function(){
		$.cookie("font_size",null,{path:"/"});
		$("#font_size").attr("href","http://www.mersey-maritime.co.uk/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","http://www.mersey-maritime.co.uk/res/css/large.css");
		$.cookie("font_size","large",{path:"/"});
	});
	$("#high_contrast").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility").attr("href","http://www.mersey-maritime.co.uk/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").attr("href","");
		$(this).text("High contrast").attr("id","high_contrast");
	});
	$("#text_only").live("click",function(){
		$.cookie("accessibility",null,{path:"/"});
		$("#accessibility").attr("href","http://www.mersey-maritime.co.uk/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").attr("href","");
		$(this).text("Text only").attr("id","text_only");
	});
	$(".with_event").live("click",function(){
		$.cookie("eventDate",$(this).attr("rel"),{path:"/"});
	});
	$(".tab_off").live("click",function () {
		$(".tab_on").removeClass("tab_on").addClass("tab_off");
		$(this).removeClass("tab_off").addClass("tab_on");
	});
	$(".download").fullLink();
	$(".NavigationMenu td,.Phuse_MI_SubTable").live("mouseover",function(){
		$("#ticker .menu_item").css("z-index","0");
	}).live("mouseout",function(){
		$("#ticker").each(function(){
			var itemLength = $("#ticker .menu_item").length;for($i=0;$i<=itemLength;$i++){$("#ticker .menu_item:eq("+$i+")").css("z-index",$i+1);}
		});
	});
	for ($i=0;$i<=$("input").length;$i++) { $("input:eq("+$i+")").attr("tabindex",$i); }
	$("#chkInvoiceAddress").live("click",function(){
		if ($(this).is(":checked")) {
			$(".hide").hide();
			$(".hide input").val("");
		} else {
			$(".hide").show().css("background","#d80332").animate({backgroundColor:"#efeff0"},1000);
		}
	});
	if ($.browser.msie) {
		$('select.wide')
		.bind('focus mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
		.bind('click', function() { $(this).toggleClass('clicked'); })
		.bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
		.bind('blur', function() { $(this).removeClass('expand clicked'); });
	}
    $(".Phuse_Search_Button").click(function(){
        var searchTerm = $(".Phuse_Search_TextQuery").attr("value");
        location.href = "http://www.mersey-maritime.co.uk/searchresults.phuse?q="+searchTerm+"&rpp=10&t=0&sp=4";
    });
    $(".gallery .photo a").lightBox();
});
