var loaded = false;

function validate(data)
{
	document.getElementById('submitcontact').disabled = 'disabled';
	document.getElementById('submitcontact').className = 'large black bilalbutton';
	document.getElementById('cloading').style.display = 'block';
	document.getElementById('error').style.display = 'none';
	// document.getElementById('contactok').style.display = 'none';
	
	document.getElementById('purpose').className = 'contact';
	document.getElementById('name').className = 'contact';
	document.getElementById('email').className = 'contact';
	document.getElementById('message').className = 'contact';
	
	xajax_validate(data);
	
	if (!document.getElementById('Cstatus'))
	{
		Recaptcha.reload();
	}
	
	return false;
}

function CreateRecaptcha()
{
    Recaptcha.create(sid, 'recaptchaID', { theme: 'white', callback: Recaptcha.focus_response_field});
	loaded = true;
}

$(function()
{
	$('#contact').contactfrm();

	//display default values for input fields

	var default_values = new Array();
	$("input.contact").focus(function()
	{
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
		}
		$(this).blur(function() {
			if (this.value == '') {
				this.value = default_values[this.id];
			}
		});
	});
});

$.fn.contactfrm = function() {
	
	$('.contactfrm').toggle(function() {
		$('#overlay').css({display: 'block'});
		$(this).animate({"marginLeft": "-=5px"}, "fast"); 
		$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
		$(this).animate({"marginLeft": "+=713px"}, "slow"); 
		$('#contactForm').animate({"marginLeft": "+=710px"}, "slow");
		
		if (!loaded)
		{
			CreateRecaptcha();
		}
	},
	function() {

		$('#contactForm').animate({"marginLeft": "-=710px"}, "slow");
		$(this).animate({"marginLeft": "-=713px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
		$('#overlay').css({display: 'none'});
	});

};(jQuery);

$(document).ready(function(){   
	imgLoader = new Image();
	imgLoader.src = "/templates/images/working.gif";
});
$('a[href*=#]').click(function() {

   // duration in ms
   var duration=1000;

   // easing values: swing | linear
   var easing='swing';

   // get / set parameters
   var newHash=this.hash;
   var target=$(this.hash).offset().top;
   var oldLocation=window.location.href.replace(window.location.hash, '');
   var newLocation=this;

   // make sure it's the same location      
   if(oldLocation+newHash==newLocation)
   {
      // animate to target and set the hash to the window.location after the animation
      $('html:not(:animated),body:not(:animated)').animate({ scrollTop: target }, duration, easing, function() {

         // add new hash to the browser location
         window.location.href=newLocation;
      });

      // cancel default click action
      return false;
   }
});
function wait_show()
{
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(wait_hide);
			}
		}else{
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(wait_hide);
			}
		}
		
		if(detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");
		}
		
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");
		$('#TB_load').show();
			
	} catch(e) {
	}
}
function showContent(title, content, type)
{
	if (type == "links")
	{
		TB_WIDTH = 800;
	}
	else
	{
		TB_WIDTH =  640;
	}
	
	TB_HEIGHT = 500;
	
	ajaxContentW = TB_WIDTH - 30;
	ajaxContentH = TB_HEIGHT - 45;

	if($("#TB_window").css("display") != "block"){

		$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+title+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a> or Esc Key</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
		
	}else{//this means the window is already up, we are just loading new content via ajax
		$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
		$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
		$("#TB_ajaxContent")[0].scrollTop = 0;
		$("#TB_ajaxWindowTitle").html(title);
	}

			
	$("#TB_closeWindowButton").click(wait_hide);
	
		$("#TB_ajaxContent").append(content);
			$("#TB_window").unload(function () {
				$("#TB_ajaxContent").children(); // move elements back when you're finished
			});
			tb_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 

	document.onkeyup = function(e)
	{ 	
		if (e == null) {
			keycode = event.keyCode;
		} else {
			keycode = e.which;
		}
		if(keycode == 27){
			wait_hide();
		}	
	};
	
}
function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}
function wait_hide() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}
