
//extend jquery object
$.url = function(url)
	{
	//base url from codeigniter
	return $("base").attr("href") + url;
	}

//jquery
$(function()
	{
	//turn on rounded corners
	if( ! jQuery.browser.msie)
		{
		$('.rounded').corners("20px");
		$('#tabs, #tabs h2, #tabs h3').corners("8px top");
		}
	
	//all h1 tags are pngs, so convert
	$("h1, #header h2").each(function()
		{
		h1_alt=$(this).html();
		h1_src = "images/" + h1_alt.toLowerCase().replace(/ /g, "-") + ".jpg";
		$(this).html('<img alt="' + h1_alt + '" src="' + h1_src + '" title="' + h1_alt + '" />');
		});
	
	//reposition h2 (top:-20px;)
	$("#header h2").css({top:-20});
	
	//anti spam
	function doAntiSpam()
		{
		$("#enquiry").html('<a href="' + "mai" + "lto" + ":enq" + "uiry@c" + "ameoser" + "vices.c" + 'o.uk">' + "enq" + "uiry@c" + "ameoser" + "vices.c" + "o.uk<" + "/a>");
		}
	doAntiSpam();
	
	//fade in images on page load
	$("#cameo_main, #header img").hide().fadeIn(2000);

	//listeners for tab hover effect
	$("#tabs h2, #tabs h3").hover(
		function()
			{
			$(this).addClass("over");
			},
		function()
			{
			$(this).removeClass("over");
			});
	
	//click event for tabs, apply anchor url to whole h3 tag
	$("#tabs h3").click(emulateAnchor);
	
	function emulateAnchor()
		{
		//emulate click event of anchor
		$(this).find("a").click();
		};
	
	//ajax urls
	$("a").click(doAjax);
	
	function doAjax()
		{
		
		//get requested url
		var url = $(this).attr("href");
		
		//did it load? not yet.. global var for synchronous ajax
		var loaded=false;
		
		//try to load new page with ajax/ahah
		$.ajax(
			{
			async:false,
			url:url + "/ajax",
			dataType:'text',
			success:function(response)
				{
				loaded=true;//it loaded
				var content = response;//remember new content
				//fade out old content
				$("#main_text").fadeTo(200, 0.1, function()
					{
					//fade in new content
					$(this).html(content).fadeTo(200, 1);
					
					//change tab colours
					txt = $("#tabs h2").html();
					$("#tabs h2").replaceWith("<h3>"+txt+"</h3>");
					$("#tabs h3").hover(function()
						{
						$(this).addClass("over");
						}, function()
						{
						$(this).removeClass("over");
						}).click(emulateAnchor).find("a").click(doAjax);
					$newH2 = $("#tabs").find("a[href=" + url + "]").parent();
					txt = $newH2.html();
					$newH2.replaceWith("<h2>"+txt+"</h2>");
					
					//ajax urls
					$(this).find("a").click(doAjax);
					
					//anti spam email address
					doAntiSpam();
					
					//add corners if non ie browser
					if( ! jQuery.browser.msie)
						{
						$("#tabs h2, #tabs h3").corners("8px top");
						}
					});
				}	
			});
		
		if(loaded)
			{
			//it worked, add anchor name to url for history/bookmarking
			window.location.href="#" + url;
			return false;
			}
		else
			{
			//if it didnt work, just load the page without using AJAX
			return true;
			}
		}
	
	//for h2, we are already there so ignore any clicks
	$("#tabs h2 a").unbind("click").click(function(){return false;});

	//if when page is loaded we find that it should have different content, load it via ajax by emulating click event
	if(window.location.hash.length > 1)
		{
		url  = window.location.hash.replace("#","");//url of anchor to emulate
		$("#tabs").find("a[href=" + url + "]").click();//emulate click
		}
	});
