$(document).ready(function(){
	var map = null;
	
	/////////////////////////////
	// SETTINGS FOR GOOGLE MAP //
	/////////////////////////////
	
	var map = new GMap2(document.getElementById('googleMap'));
	var fltLong = 50.82685336103801;
	var fltLat = -0.13773679733276367;
	var oLoc = "";
	var intZoomLevel = 15;
	var markers = [];
	var i = 0;
	var point = 0;
	var marker = "";
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	var lastLoc = "";
	
	map.enableContinuousZoom();
	map.checkResize();
	
	// CREATE CUSTOM ICONS : START
	baseIcon.image = "/_images/contact/marker.png";
	baseIcon.shadow = "/_images/contact/shadow.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	// CREATE CUSTOM ICONS : STOP
	
	// ADD MAP CONTROLS
	map.addControl(new GSmallMapControl());
	
	oLoc = new GLatLng(fltLong,fltLat);
	lastLoc = oLoc; // REMEMBER THIS LOCATION FOR WHEN WINDOW IS RESIZED
	map.setCenter(oLoc, intZoomLevel);
	
	// ADD MAP POINTS : START
	point = new GLatLng(fltLong,fltLat);
	marker = new GMarker(point,baseIcon);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function(){
		window.open('http://local.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=Spook+Studio+Ltd&g=Unit+2,The+Auction+Rooms,28+Gloucester+Street,Brighton,BN1+4EW,UK&ie=UTF8&hq=Spook+Studio+Ltd&hnear=Unit+2,The+Auction+Rooms,28+Gloucester+Street,Brighton,BN1+4EW,UK&ll=50.82685336103801,-0.13773679733276367&spn=0.010613,0.027874&z=16&iwloc=A');
	});
	// ADD MAP POINTS : STOP
	
/**************************************************************************************************************/
	
	/////////////////////////////.///////////////////
	// JQUERY AJAXSUBMIT SETTINGS FOR CONTACT FORM //
	/////////////////////////////.///////////////////
	var optionsContact = { 
//		beforeSubmit:	showRequest,
		target:			'#output1',				// target element(s) to be updated with server response 
		success:		responseContact,	// post-submit callback 
		dataType:		'json',					// 'xml', 'script', or 'json' (expected server response type) 
		url:			'/_ajax/contact.cfm',	// override for form's 'action' attribute 
		clearForm:	true						// clear all form fields after successful submit 
		
		// other available options: 
		//type:			type			// 'get' or 'post', override for form's 'method' attribute 
		//resetForm:	true			// reset the form after successful submit 

		// $.ajax options can be used here too, for example: 
		//timeout:   3000 
	};
	
/**************************************************************************************************************/

	/////////////////////////////.//////////////////
	// SET UP JQUERY VALIDATION FOR  CONTACT FORM //
	/////////////////////////////.//////////////////
	
	$("#commentForm").validate({
		submitHandler : function(form) {
			$("#commentForm").ajaxSubmit(optionsContact);
		},
		rules : {
			name	: "required",
			email	: {
				required	: true,
				email		: true
			},
			comment	: "required"
		},
		messages : {
			name	: "Sorry, what's your name",
			email	: {
				required	: "To get back to you we'll need your address",
				email		: "Sorry but this email address doesn't look right"
			},
			comment	: "What would you like to say?"
		}
	});
	
	$('#showForm').click(showForm);
	
/**************************************************************************************************************/

	/////////////////////////////////////////////////
	// CONTROL BACKGROUND IMAGE DISPLAY FOR FIELDS //
	/////////////////////////////////////////////////
	
	// EVENTS FOR DISPLAYING FIELD NAMES ON FORM
	$('input, textarea').keyup(function(){
		if ( $(this).val().length >= 1){	// Check string length of field value
			$(this).removeClass('bg');		//if its true then do this
		}
		else {
			$(this).addClass('bg');			//if its false then do this
		}
	});
	
	// WHEN THE BROWSER IS REFRESHED CHECK WHETHER FORMS FIELDS ARE ALREADY POPULATED
	$('input, textarea').each(function() {
		if ($(this).attr('type') !== 'submit') {
			if ($(this).val().length > 0) $(this).removeClass('bg');
		}
	});
	
/**************************************************************************************************************/

	/////////////////////////////
	// GET LATEST TWITTER POST //
	/////////////////////////////
	
	getLatestTweet('spookstudio');
});
//////////////////////////////////
// OTHER GENERIC PAGE FUNCITONS //
//////////////////////////////////

function responseContact() {
	$('#thankyou').fadeIn('slow');
	$('#contactForm').hide();
}

function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

function showForm() {
	$('#thankyou').hide();
	$('#contactForm').fadeIn('slow',function() {
		goToByScroll('contactForm')
	});
}