$(document).ready(function()
{
	search_init();
	home_section_colours();
	headings_init();
	nav();
	nav_sub();
	nav_related_init();
	nav_section_selector();
	scrollable();
	tables_init();
	shadows_init();
	commercial_data_sheets();
	shop_form_links();
	list_show_hide();
	image_alignment();
	register_form();
	resize_text();
	image_captions();
	lightbox_links();
});


// manage drop downs
function nav ()
{
	$("#nav ul li div").parent().hover(function()
       	{
        	$(this).addClass("dir-on");
	}, function()
       	{
               	$(this).removeClass("dir-on");
	});
}

// remove text from search on click
function search_init ()
{
	$("#search-txt").focus(function()
	{
		$(this).val("");
	});

	$("#search-submit").click(function()
	{
		$("#header form").submit();
	});
}

// loop homepage sections and work out what colour each 
// block should be
function home_section_colours ()
{
	$(".id").each(function()
	{
		if ($(this).text() == "")
		{
			$(this).parent().css("display", "none");
		}

		col = colour_get($(this).text());
		$(this).parent().addClass(col);
		$(this).remove();
	});
}

// append span tags to headings
function headings_init ()
{
	$(".home .section h2").each(function()
	{
		$(this).addClass("clearfix");
		html = '<span class="heading-pad">' + $(this).find("a").html(); + '</span>';
		$(this).find("a").html(html);
		$(this).find("a").append('<span class="shadow-b" /><span class="shadow-r" />');
	});

	$(".section #content #left, .box-with-heading, .nav-sub-lighthouses, .form #content #left").each(function()
	{
		var element;

		if 		($(this).find("h1:first").length > 0) { element = $(this).find("h1:first"); }
		else if ($(this).find("h2:first").length > 0) { element = $(this).find("h2:first"); }
		else if ($(this).find("h3:first").length > 0) { element = $(this).find("h3:first"); }

		if (element)
		{
			element.addClass("clearfix");
			element.addClass("legend");
			html = '<span class="heading-pad">' + element.html(); + '</span>';
			element.html(html);
			element.append('<span class="shadow-b" /><span class="shadow-r" />');
		}
	});	

	$(".holidays h1 a").html("<span>"+$(".holidays h1 a").html()+"</span>");
}


// takes a text identifier and returns correct class name
function colour_get(id)
{
	switch (id)
	{
		case "Home" :
		return "th";
		break;


		case "Mariner Information" :
		return "mariner";
		break;

		case "Venue Hire" :
		return "hire";
		break;

		case "Lighthouses" :
		return "lighthouses";
		break;

		case "News & Information" :
		return "news";
		break;

		case "Commercial Services" :
		return "commercial";
		break;

		case "Supplying Us" :
		return "suppliers";
		break;

		case "Holidays" :
		return "holidays";
		break;

		case "Careers" :
		return "careers";
		break;

		default :
		return "";
		break;
	}
}

// add last class to last li
function nav_sub ()
{
	$("ul.nav-sub li:last, ul.nav-sub li li:last").addClass("last");
}

// add span tags to holiday splash menus
function nav_sub_holidays ()
{
	$("ul.nav-sub li a").each(function()
	{
		$(this).html("<span>"+$(this).html()+"</span>");
	});
}



// add spans around related links items
function nav_related_init ()
{
	$("#right .related li a, #right .nav-sub-extra li a").each(function()
	{
		html = '<span>' + $(this).html() + '</span>';
		$(this).html(html);
	});
}


// set up slideshow, add last class to each 4th item
function nav_section_selector ()
{
	$(".section-selector img").each(function(i)
	{
		label = $(this).attr("title");
		$(this).parent().append("<h2><span>"+label+"</span></h2><span class='point'></span>");
		
		i = i + 1;
		if (i == $(".section-selector img").length)
		{
			$(this).parent().addClass("last");
		}
	});
}


// init scrollabel sections extra coding 
// removes next button when showing last items
function scrollable ()
{
	if ($(".scrollable").length)
	{
		$(".scrollable").scrollable();
		var api 				= $(".scrollable").data("scrollable");
		if (api == null)
		{
			return;
		}
	
		api.getConf().circular 	= false;
		api.getConf().speed		= 500;
		api.getConf().easing	= "swing";
		var num_items_per_slide = 4;

		api.onSeek(function()
		{
			if (api.getIndex() + num_items_per_slide >= api.getSize())
			{
				$(".next").addClass("disabled");
			}
		});
	}
}


// add classes to data tables 
function tables_init ()
{
	tables_convert();

	var rows = $(".data-horizontal tr").length;
	$("#content table.data-horizontal, #content table.data-dotted").each(function()
	{
		$(this).find("tr").each(function(index)
		{	
			$(this).find("td:last, th:last").addClass("last");
			$(this).find("td:first").addClass("first");
	
			if (index == 1 && $(this).find("td").length > 0)
			{
				$(this).addClass("first");
			}
			else if (index == (rows - 1))
			{
				$(this).addClass("last");
			}
		});
	});

	$(".data-vertical tr:last, .gallery-data tr:last, .data-dotted tr:last").addClass("last");
}

// convert normal tables to either vertical or horizontal class
function tables_convert ()
{
	$("#content .convert-table-vertical table").addClass("data-vertical");
	$("#content .convert-table-vertical table tr").each(function()
	{
		$(this).children("td:first").each(function()
		{
			html = $(this).html();
			$(this).parent().prepend("<th>"+html+"</th>");
			$(this).remove();
		});
	});
}

// adds span tags to any element with .shadow class
function shadows_init()
{
	$(".shadow").append('<span class="shadow-b" /><span class="shadow-r" />');
}


// add classes to commercial data sheet table
function commercial_data_sheets()
{
	$("#content .table-data-sheets tr").each(function ()
	{
		$(this).children("td:last").addClass("last");
		$(this).find("td:last a").addClass("btn btn-download");
	});

}

//  turn links into submit buttons on shop items
function shop_form_links ()
{
	$("#content .shop-item form a, #content .shop-item-print form a").click(function()
	{
		$(this).parents("form").submit();
	});
}


// set up show/hide lists
function list_show_hide ()
{
	// hide sub lists
	$(".list-show-hide li ul").css("display", "none");
	// set up onclick events
	$(".list-show-hide li a").click(function()
	{
		var sub_list = $(this).parents("li").find("ul");
		if (sub_list.css("display") == "none")
		{
			sub_list.slideDown();
		}
		else
		{
			sub_list.slideUp();
		}
		return false;
	});
}


function image_alignment ()
{
	$("#content img[align=right]").addClass("float-right pad");
	$("#content img[align=right]").attr("align", "");
	$("#content img[align=left]").addClass("float-left pad");
	$("#content img[align=left]").attr("align", "");
}

// set up register form with preselected get values
function register_form ()
{
	// get querystring as an array split on "&"
	var querystring = location.search.replace( '?', '' ).split( '&' );
	// declare object
	var queryObj = {};
	// loop through each name-value pair and populate object
	for ( var i=0; i<querystring.length; i++ ) {
		// get name and value
		var name = querystring[i].split('=')[0];
		var value = querystring[i].split('=')[1];
		// populate object
		queryObj[name] = value;
	}

	if (queryObj["alerts_vacancies_2776_dbcc7e2e7c5c62573de439574814066f"] == "dbcc7e2e7c5c62573de439574814066f")
	{
		$("#fg-alerts_vacancies_2776_dbcc7e2e7c5c62573de439574814066f").attr("checked", "checked");
	}

	if (queryObj[ "alerts_press_2769_f28f59e4509081ea3c4d862f976b383b"] == "f28f59e4509081ea3c4d862f976b383b")
	{
		$("#fg-alerts_press_2769_f28f59e4509081ea3c4d862f976b383b").attr("checked", "checked");
	}

	if (queryObj[ "alerts_redundant_2809_8e7a0d1589f253e06bd224324b44bce2"] == "8e7a0d1589f253e06bd224324b44bce2")
	{
		$("#fg-alerts_redundant_2809_8e7a0d1589f253e06bd224324b44bce2").attr("checked", "checked");
	}
	
	if (queryObj[ "alerts_dspbb_2851_49f07fb99b0f0f47d2faeb21b86051a4"] == "49f07fb99b0f0f47d2faeb21b86051a4")
	{
		$("#fg-alerts_dspbb_2851_49f07fb99b0f0f47d2faeb21b86051a4").attr("checked", "checked");
	}

	$('input[name*="alerts_mariners_2762"]').each(function()
	{
		if ($(this).val() == queryObj["alerts_mariners_2762_5a422d168aac27b2d6453b8362bb149e"] || $(this).val() == queryObj["alerts_mariners_2762_0c989993bc1eb96c32387c6bd57e5d94"] || $(this).val() == queryObj["alerts_mariners_2762_c2f4e5d9ad20fea9bea3e885e937490f"] || $(this).val() == queryObj["alerts_mariners_2762_56517c55b7cca55c45ee3653bf35c149"] || $(this).val() == queryObj["alerts_mariners_2762_4919baf7ea0845b15c581a2b9731702a"] || $(this).val() == queryObj["alerts_mariners_2762_82bebf3a5d13b391654771f4e02253e6"] || queryObj["alerts_mariners_2762"] == "all")
		{
			$(this).attr("checked", "checked");
		}
	});
	
	
}


// resize text
function resize_text ()
{

	if (getCookie("text") == "larger")
	{
		resize_text_append_css("/css/larger-1.css");
	}
	else if (getCookie("text") == "largest")
	{
		resize_text_append_css("/css/larger-2.css");
	}
	else
	{
		resize_text_append_css(false);
	}


	$("#resizer .normal").click(function()
        {
		setCookie("text", "normal", 1);		
        	resize_text_append_css(false);
	});


	$("#resizer .larger").click(function()
	{
		setCookie("text", "larger", 1);
		resize_text_append_css("/css/larger-1.css");
	});

	$("#resizer .largest").click(function()
        {
		setCookie("text", "largest", 1);
        	resize_text_append_css("/css/larger-2.css");
	});
}


// appends style sheet to page
function resize_text_append_css (path)
{
	$("head link").each(function()
        {
		if($(this).attr("href") == "/css/larger-2.css" || $(this).attr("href") == "/css/larger-1.css")
		{
			$(this).remove();
		}
	});

	if (path != false)
	{
		$("head").append('<link rel="stylesheet" href="'+path+'" type="text/css" />');
	}
}


// cookie functions taken from http://www.w3schools.com/JS/js_cookies.asp
function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

// add caption style to images in content area
function image_captions ()
{
	$("#left img").each(function()
	{

		if ($(this).attr("title").substring(0, 8) == "Caption:")
		{
			var title = $(this).attr("title").substring(8);
			var img_class = $(this).attr("class");
			$(this).attr("title", title);
			$(this).attr("class", "");
			$(this).wrap("<span class='img-caption "+img_class+"'></span>");
			$(this).parent().append("<span>"+title+"</span>");

		}
	});
}

// attach lightbox event to any links
function lightbox_links ()
{
	$('#content a.lightbox').lightBox();	
}



