$(document).ready(function() {
	
	// google analytics
	//var pageTracker = _gat._getTracker("UA-XXXXXXX");
	//pageTracker._initData();
	//pageTracker._trackPageview();

	$('a[rel="external"]').click( function() {
        window.open($(this).attr('href') );
        return false;
    });
	
	if($('body').is('.home,.p-models')) {
		$(function() {
		    $.nyroModalSettings({
		      processHandler: function(settings) {
		        var url = settings.url;
		        if (url && url.indexOf('http://www.youtube.com/watch?v=') == 0) {
		          $.nyroModalSettings({
		            type: 'swf',
		            height: 355,
		            width: 425,
		            url: url.replace(new RegExp("watch\\?v=", "i"), 'v/')
		          });
		        }
		      }
		    });

			function preloadImg(image) {
				var img = new Image();
				img.src = image;
			}      
		    preloadImg('images/ajaxLoader.gif');
		    preloadImg('images/prev.gif');
		    preloadImg('images/next.gif');
		  });
	}
	if($('body').is('.home')) {	
		$('.flash ul').innerfade({
			speed: 'slow', 
			timeout: 2000, 
			type: 'sequence',
			containerheight: '530px'
		});
	}
	if($('body').is('.p-contact') || $('body').is('.p-list')) {
		$("#contact").validate();
	}
	if($('body').is('.p-contact')) {
		$("#brisbane").css("display","none");
		$("#brisbane h3").before('<div class="map"><iframe width="400" height="400" frameborder="0" src="http://maps.google.com.au/?q=Brisbane,+Rivergate+Place,+Murarrie,+QLD&amp;ie=UTF8&amp;sll=-25.335448,135.745076&amp;sspn=49.42191,61.171875&amp;z=16&amp;iwloc=near&amp;addr&amp;output=embed"></iframe></div>');
		$("#gc h3").before('<div class="map"><iframe width="400" height="400" frameborder="0" src="http://maps.google.com.au/?q=76-84+Waterway+Drive,+Coomera,+QLD,+4209&amp;ie=UTF8&amp;sll=-27.862108,153.338521&amp;spn=0.00664,0.00912&amp;z=16&amp;iwloc=near&amp;addr&amp;output=embed"></iframe></div>');
		$("#goldcoast").click(function(){
			if ($("#goldcoast").is(":checked")) {
				$("#brisbane").hide();
				$("#gc").show();
			} else {	   
				$("#gc").hide();
				$("#brisbane").show();
			}
		});
		$("#bris").click(function(){
			if ($("#bris").is(":checked")) {
				$("#gc").hide();
				$("#brisbane").show();
			} else {	   
				$("#brisbane").hide();
				$("#gc").show();
			}
		});
	}
	if($('body').is('.p-owner-care')) {
		$("#owner").validate();
	}
	if($('body').is('.p-for-sale') && $('body').is('.p-detail')) {
		$("#sale").validate();
	}
	if($('body').is('.p-pricing')) {
		$("#trade-form").validate();
		$("#sales-form").validate();
		$("#trade-form").css("display","none");
		$("#trade").click(function(){
			if ($("#trade").is(":checked")) {
				$("#sales-form").hide();
				$("#trade-form").show();
			} else {	   
				$("#trade-form").hide();
				$("#sales-form").show();
			}
		});
		$("#sales").click(function(){
			if ($("#sales").is(":checked")) {
				$("#trade-form").hide();
				$("#sales-form").show();
			} else {	   
				$("#sales-form").hide();
				$("#trade-form").show();
			}
		});
	}
	if($('body').is('.p-detail')) {

		$(".fancy").fancybox({'overlayOpacity':0.8,'overlayColor':'#000'});
	}

	if($('body').is('.p-boats')) {
		$(".fancy li:not(.paginate) a").fancybox({'overlayOpacity':0.8,'overlayColor':'#000'});
	}
	if($('body').is('.p-news')) {
		$(".img-r a").fancybox({'overlayOpacity':0.8,'overlayColor':'#000'});
	}
	if($('body').is('.p-anchorages')) {
		$(".fancy").fancybox({'overlayOpacity':0.8,'overlayColor':'#000'});				
	}
	
	if($('body').is('.p-boats') || $('body').is('.p-anchorages')) {
		$("h1 + .btn a").fancybox({
			'scrolling'		: 'no',
			'titleShow'		: false
		});

	}
	
});

function remove_newlines(inputString) {
	rExp = /\r\n|\r/g;
	result = inputString.replace(rExp, " ");
	return result;
}
function trim_spaces(inputString) {
	if (typeof inputString != "string") {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { 
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { 
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
	}
	return retValue; 
}
function remove_newlines_and_trim_spaces(inputString) {
	if (typeof inputString != "string") {
		rExp = /\r\n|\r/g;
		result = inputString.replace(rExp, " ");
		return result;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { 
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { 
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
	}
	rExp = /\r\n|\r/g;
	result = retValue.replace(rExp, " ");
	return result; 
}
function check_email(inputString)
{
	var filter=/^([\w-]+([\.\w-'])*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(inputString)) {
		return false;
	}
	return true;
}
function check_digit(inputString) {
	isPrice = /^\d+\.{0,1}\d*$/;
	return isPrice.test(inputString);
}


function check_currency(inputString) {
	isCurrency = /^\$\d+\.{0,1}\d*$/;
	return isCurrency.test(inputString);
}
function check_word_only(inputString) {
	isAlphaNumeric = /^[A-Za-z0-9\ \-'\’\‘\/\.]*$/;
	return isAlphaNumeric.test(inputString);
}

function highlight(field, is_error) {
	style_name = "no_error";
	if (parseInt(is_error)==1) {
		style_name = "error";
	}
	field.setAttribute((document.all ? "className" : "class"),style_name); 
	return true;
}
function validate_form() {
			 	var error_count = 0;
			 	var error_questions = "";
			 	var set_focus_field_id = "";
			 
						if(document.getElementById("question_2")) {
							highlight(document.getElementById("question_2"), "0");
						}
			if(document.getElementById("question_2").value.length == 0) {
				highlight(document.getElementById("question_2"),"1");
				error_questions += "Answer can not be empty for First Name\n"; 
				temp = document.getElementById("question_2").name;
				if (set_focus_field_id.length == 0) {
					set_focus_field_id = "question_2";	
				}
				error_count++;
			}
		
				temp = document.getElementById("question_2").name;
				if(eval("check_word_only(document.main_form."+temp+".value)") == false) {
					error_questions += "Invalid word for First Name\n";
					if (set_focus_field_id.length == 0) {
						set_focus_field_id = "question_2";	
					}
					highlight(document.getElementById("question_2"),"1");
					error_count++;
				}
			
			temp = document.getElementById("question_2").name;
			temp2 = eval("remove_newlines_and_trim_spaces(document.main_form."+temp+".value)");
			temp3 = eval("document.main_form."+temp);
			temp3.value = temp2;
		 
						if(document.getElementById("question_1")) {
							highlight(document.getElementById("question_1"), "0");
						}
			if(document.getElementById("question_1").value.length == 0) {
				highlight(document.getElementById("question_1"),"1");
				error_questions += "Answer can not be empty for Email Address\n"; 
				temp = document.getElementById("question_1").name;
				if (set_focus_field_id.length == 0) {
					set_focus_field_id = "question_1";	
				}
				error_count++;
			}
		
				temp = document.getElementById("question_1").name;
				if(document.getElementById("question_1").value.length>0  && eval("check_email(document.main_form."+temp+".value)") == false) {
					error_questions += "Invalid email for Email Address\n"; 
					if (set_focus_field_id.length == 0) {
						set_focus_field_id = "question_1";	
					}
					highlight(document.getElementById("question_1"),"1");
					error_count++;
				}
			
			temp = document.getElementById("question_1").name;
			temp2 = eval("remove_newlines_and_trim_spaces(document.main_form."+temp+".value)");
			temp3 = eval("document.main_form."+temp);
			temp3.value = temp2;
		
	if (error_count > 0) {
		if (set_focus_field_id.length>0 && document.getElementById(set_focus_field_id)) {
			eval(document.getElementById(set_focus_field_id).focus());
		}
		alert ("Please fill the form correctly\n\n"+error_questions);
		return false;
	}
		document.main_form.submit();
}
