// JavaScript Document
//Ajax Handling Form


//Ajax autoload banner
$(document).ready(function() {
	$.ajaxSetup({
		cache: true
	});
	//$(".head-banner").load("bannerxchange.php");
	

});
$(window).bind("load",function(){
	var sp = 11000;
	var refreshId = setInterval(function(){
		if(sp>5000) sp = sp - 1000;
		$(".head-banner").load("bannerxchange.php");
		//$(".head-banner").fadeOut("slow", function(){
			//$(this).load("bannerxchange.php").fadeIn("slow");
		//});
	},sp)
});
function frmSubmit(form)
{
	var name = $("input#txtName").val();
	var email = $("input#txtEmail").val();
	var phone = $("input#txtPhone").val();
	var note = $("textarea#txtNote").val();
	var mobile = $("input#txtMobile").val();
	var address = $("input#txtAddress").val();
	var city = $("input#txtCity").val();
	var state = $("input#txtState").val();
	var postcode = $("input#txtPostcode").val();
	var captcha = $("input#captcha-form").val();
	var submitcheck  = $("input#_submit_check").val();
	
	var datastring = 'name='+name+'&email='+email+'&phone='+phone+'&note='+note+
		'&mobile='+mobile+'&address='+address+'&city='+city+'&state='+state+'&postcode='+postcode+
		'&captcha='+captcha+"&submitcheck="+submitcheck;
	
	$.ajax({
		type: "POST",
		url: "send_enquiry.php",
		data: datastring,
		beforeSend: function() {
			$("#frmContact").fadeOut('slow');
			$("#pdf").fadeOut('slow');
			$("div.form-error span").html("PROCESSING...PLEASE WAIT");
			$("div.form-error").fadeIn('slow');
		},
		success: function(html) {
			//handle error
			
			$("div.form-error span").html(html);
			
			$("div.form-error").fadeIn('slow').delay(5000).fadeOut('slow');
			
			$("#captcha").attr("src",'captcha.php?'+Math.random());
			
			$("#frmContact").fadeIn('slow');
			
			$("#pdf").fadeIn('slow');
			
		},
		complete: function() {
			
		},
		error: function(html) {
			alert("Sorry, Unexpected error. Try again later");
		}
	});
}
$(document).ready(function() {
	jQuery.validator.messages.required = "";
	jQuery.validator.messages.email = "";
	jQuery.validator.messages.digits = "";

	$("#frmContact").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field or invalid input. It has been highlighted below'
					: 'You missed ' + errors + ' fields or invalid input. They have been highlighted below';
				$("div.form-error span").html(message);
				$("div.form-error").show();
			} else {
				$("div.form-error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function(form) {
			//$("div.form-error").hide();
			frmSubmit(form);
		},
		debug:false
	});
});
$(document).ready(function(){
	$("#btnReset").click(function(form){
		$("#frmContact")[0].reset();
		return false;
	});
});
$(document).ready(function(){
	$("#btnRefresh").click(function(){
		$("#captcha").attr("src",'captcha.php?'+Math.random());
	});
});

$(document).ready(function(){
	$("#navigation a").click(function(){
		$("#navigation").children().each(function(){
			$(this).find("a").removeClass("active");
		});
		$(this).addClass("active");
		var href = $(this).attr("href");
		changeSize(href);
	});
});
$(document).ready(function(){
	$("#footer a").click(function(){
		var href = $(this).attr("href");
		$("#navigation").children().each(function(){
			$(this).find("a").removeClass("active");
			var curr = $(this).find("a").attr("href");
			if(href==curr)
				$(this).find("a").addClass("active");
		});
		changeSize(href);
	});
});

$(document).ready(function(){
	var href = jQuery.url.attr("anchor");
	href = "#"+href;
	changeSize(href);
});


function changeSize(href) {
	if(href=="#section2"){
		$("#right-content").css("height",$(".section").css("height"));
	}
	else if(href=="#section5" || href=="#section4")
	{
		$("#right-content").css("height","670px");
	}
	else {
		$("#right-content").css("height","380px");
	}
}

function getWidthFromLeft(id) {
	return $(id).position().left + $(id).width() - 10;
	//return $(id).getPosition().x + $(id).getSize().x - 10;
}

function resizebg(){
	
	if($.browser.webkit) {
		$('#main-background').css("width", getWidthFromLeft("#wrapper")+760);
	}
	else {
		$('#main-background').css("width", getWidthFromLeft("#wrapper")+500);
	}
	//$('#main-background').css("height", $(window).height());
	//$("main-background").setStyle("width",get_width_from_left("content-wrapper")).setStyle("height",$(window).getSize().y);
}
function resizeme() {
	//alert("active");
	var x = findPosX(document.getElementById('wrapper'));
	var left = x + 1250;
	/*
	if($.browser.webkit) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height>530)
		{
			left -= 8;	
		}
	}
	if($.browser.mozilla) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height>530)
		{
			//left -= 9;	
		}
	}
	if($.browser.safari) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height>530)
		{
			left += 9;	
		}
	}
	*/
	var hack_div = document.getElementById('main-background');
	hack_div.style.width = left + 'px';
}

function isMyStuffScrolling() {
  var docHeight = $(document).height();
  var scroll    = $(window).height() + $(window).scrollTop();
  return (docHeight == scroll);
}

function resizeme2() {
	var x = findPosX(document.getElementById('wrapper'));
	var left = x + 1250;
	//alert(left);
	/*
	if($.browser.webkit) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			left -= 25;	
		}
	}
	if($.browser.mozilla) {
		
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			//if(left<1650)
			//left -= 9;	
		}
		//alert(getScrollBarWidth());
		//left -= getScrollBarWidth();
	}
	if($.browser.safari) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			left += 25;	
		}
	}
	*/
	var hack_div = document.getElementById('main-background');
	hack_div.style.width = left + 'px';
}
function resizeme3() {
	var x = findPosX(document.getElementById('wrapper'));
  
	var left = x + 1250;
	//alert(left);
	if($.browser.webkit) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			left -= 17;	
		}
	}
	if($.browser.mozilla) {
		
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			if(left<1650)
			left -= 9;	
		}
		//alert(getScrollBarWidth());
		//left -= getScrollBarWidth();
	}
	if($.browser.safari) {
		var height = parseInt($('#wrapper').height());
		//alert(height);
		if(height<530)
		{
			left += 17;	
		}
	}
	var hack_div = document.getElementById('main-background');
	hack_div.style.width = left + 'px';
}
function scrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}
function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
	  while(1) 
	  {
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		  break;
		obj = obj.offsetParent;
	  }
  else if(obj.x)
	  curleft += obj.x;
  return curleft;
}
function getScrollBarWidth () {
  var inner = document.createElement('p');
  inner.style.width = "100%";
  inner.style.height = "200px";

  var outer = document.createElement('div');
  outer.style.position = "absolute";
  outer.style.top = "0px";
  outer.style.left = "0px";
  outer.style.visibility = "hidden";
  outer.style.width = "200px";
  outer.style.height = "150px";
  outer.style.overflow = "hidden";
  outer.appendChild (inner);

  document.body.appendChild (outer);
  var w1 = inner.offsetWidth;
  outer.style.overflow = 'scroll';
  var w2 = inner.offsetWidth;
  if (w1 == w2) w2 = outer.clientWidth;

  document.body.removeChild (outer);

  return (w1 - w2);
};


jQuery(function( $ ){
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	
	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'xy';
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#right-content', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1500
	});
	
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#right-content', // could be a selector or a jQuery object too.
		queue:true,
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
			// The 'this' contains the scrolled element (#content)
		}
	});
});

