// JavaScript Document
var $j = jQuery.noConflict();

function callback(hash) {
	if (hash != "") {
		contentLoad(hash + ".php");
	}
	else {
		contentLoad("index.php");
	}
}

$j(document).ready(function() {
//init variables and methods;
urlLoad = "";
$j(".linkdescription").hide();
Shadowbox.init({skipSetup: true});
previd="";
$j.history.init(callback);

$j('#flashnav').flash({
    src: 'flashnav.swf',
	width: 1090,
	height: 56,
	wmode: 'transparent'
});

$j('#flashquote').flash({
    src: 'quoteheading.swf',
	width: 469,
	height: 188,
	wmode: 'transparent'
});

$j("#slideshow").jqFancyTransitions({
   effect: 'wave',
   width: 447, // width of panel
	height: 291, // height of panel
	strips: 20, // number of strips
	delay: 4000, // delay between images in ms
	stripDelay: 50, // delay beetwen strips in ms
	titleOpacity: 0.7, // opacity of title
	titleSpeed: 1000 // speed of title appereance in ms
});

$j("body").supersleight({shim: "images/transparent.gif"});

// Ajax content links
$j('a[rel*="ajaxlink"]').livequery('click', function(){
        $j.history.load(this.href.replace(/^.*#/, ''));
        return false;
    });

	$j('a[rel*="shadowbox[Grouped]"]').livequery('click', function(){
	        $j('.ft-title').hide();
	        return false;
	    });
	
$j('#sb-nav-close').livequery('click', function(){
        $j('.ft-title').show();
	        return false;
	    });

// Links accordion
$j(".linktitle").click(function(){
			   		id = '#' + $j(this).attr('id');
					if (previd=="") {
						$j(id + 'description').slideDown('fast');
						$j(id + 'togglebutton').attr('src','images/collapse.png')
					}
					if (id != previd && previd != "") {
						$j(previd + 'togglebutton').attr('src','images/expand.png');
						$j(previd + 'description').slideUp('fast', function() {
							$j(id + 'description').slideDown('fast');
						});
							$j(id + 'togglebutton').attr('src','images/collapse.png');
					}
					if (id == previd) {
						$j(id + 'description').slideUp('fast');
						$j(id + 'togglebutton').attr('src','images/expand.png');
						previd="";
						return false;
					}						
					previd = id;					
               });

});

function flashLinks(navlink) {
	$j.history.load(navlink.replace(/^.*#/, ''));
	return false;
}

function contentLoad(urlLoad) {
	
	toload = urlLoad + ' #content';
	$j("#content").load(toload, function() {
	
	// Global
	$j("#content").show();
	$j("#content").supersleight({shim: "images/transparent.gif"});
	Shadowbox.clearCache();
	Shadowbox.setup();
	
	//Index
	if (urlLoad=="index.php") {
		$j("#twitter").getTwitter({
			userName: tAccount,
			numTweets: 3,
			loaderText: "",
			slideIn: false,
			showHeading: false,
			showProfileLink: false
		});
	}
	
	// Showreel
	if (urlLoad=="showreel.php"){
		$j('#showreelflash').flash({
	        src: 'showreel.swf',
			width: 614,
			height: 380
	    });
	}
	
	// Gallery
	if (urlLoad=="gallery.php") {
		$j(".opacify").fadeTo(1, 0.6);
		  				$j(".opacify").hover(
		    			function () {
		      			$j(this).fadeTo("fast", 1);
		    			},
		    			function () {
		      			$j(this).fadeTo("normal", 0.6);
		    			});
	}
	
	// Showreel
	if (urlLoad=="contact.php"){
		contactSubmit();
	}
	
    });
	return false;
}

// Contact form
function contactSubmit() {
	$j(".button#submit_btn").click(function() {
		
		var name = $j("input#name").val();
	    var email = $j("input#email").val();
		var phone = $j("input#phone").val();
		var eDate = $j("input#eDate").val();
		var question = $j("textarea#question").val();
		
		var emailtest = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var phonetest = /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/;
		
		$j("input").css("border-color","#559191");
		$j("textarea").css("border-color","#559191");
		
		preverror=false;
		
		if (name == "") {
			$j(".error").slideUp('fast', function() {
				$j("input#name").css("border-color","#CC0000");
				$j('#name_error').html("Please leave a name.");
			  	$j('#name_error').slideDown();
			});
			return false;
		}
		if (email == "") {
			$j(".error").slideUp('fast', function() {
				$j("input#email").css("border-color","#CC0000");			
				$j('#email_error').html("Please leave an e-mail address");
				$j('#email_error').slideDown();
			});
			return false;
		}
		if (emailtest.test(email) == false) {
			$j(".error").slideUp('fast', function() {
				$j("input#email").css("border-color","#CC0000");
				$j('#email_error').html("Please provide a valid e-mail address");
				$j('#email_error').slideDown();
			});
			return false;
		}
		if (phone == "") {
			$j(".error").slideUp('fast', function() {
				$j("input#phone").css("border-color","#CC0000");
				$j('#phone_error').html("Please leave a phone number");
				$j('#phone_error').slideDown();
			});
			return false;
		}
		if (phonetest.test(phone) == false) {
			$j(".error").slideUp('fast', function() {
				$j("input#phone").css("border-color","#CC0000");
				$j('#phone_error').html("Please provide a valid phone number");
				$j('#phone_error').slideDown();
			});
			return false;
		}
		if (eDate == "") {
			$j(".error").slideUp('fast', function() {
				$j("input#eDate").css("border-color","#CC0000");
				$j('#eDate_error').html("Please leave an approximate date");
				$j('#eDate_error').slideDown();
			});
			return false;
		}
		if (question == "") {
			$j(".error").slideUp('fast', function() {
				$j("textarea#question").css("border-color","#CC0000");
				$j('#question_error').html("Please provide an enquiry");
				$j('#question_error').slideDown();
			});
			return false;
		}
		
		var contactString = $j("#contactform").serialize();
		$j.post("processContact.php", contactString, function(){
			$j(".contactform").html("<div id=\"confirmation\">Thank you for submitting, we will contact you very soon.</div>");
			$j('.error').hide();
		});
 		return false;
	});	
}
