// JavaScript Document

var cityDDState = 0;
var cityDDViewState = 0;
$(document).ready(function() {
		
	$("#showAdForm").click(function(event) {
		event.preventDefault();
		$("#adForm").slideToggle();
	});
	$('#adForm .adFormClose').click(function(event) {
		event.preventDefault();
		$("#adForm").slideUp();
	});
	$('#cityDDArrow').mouseover(function(event) {
		event.preventDefault();
		if(!cityDDState)
		{
			cityDDState = 1;
			$("#cityDropDown").slideDown();
		}
	});
	$("#cityDropDown").mouseover(function(event) {
		cityDDViewState = 1;
		cityDDState = 1;
	});
	$("#cityDropDown").mouseout(function(event) {
		event.preventDefault();
		cityDDViewState = 0;
		setTimeout(function(){
			if(!cityDDViewState)
			{
				cityDDState = 0;
				$("#cityDropDown").slideUp();
			}
		}, 100);
	});
	$("#cityDropDown ul").mouseover(function(event) {
		cityDDViewState = 1;
		cityDDState = 1;
	});
	$("#cityDropDown ul").mouseout(function(event) {
		cityDDViewState = 0;
	});
	$("#cityDropDown li").mouseover(function(event) {
		cityDDViewState = 1;
		cityDDState = 1;
	});
	$("#cityDropDown li").mouseout(function(event) {
		cityDDViewState = 0;
	});
	$('#tabs .small')
	.css( {backgroundPosition: "-232px 0"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(-232px -76px)"}, function(){
			$(this).css( {backgroundPosition: "-232px -120px"} )
		});
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-232px 0)"})
	});
	$('#tabs .medium')
	.css( {backgroundPosition: "-121px 0"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(-121px -76px)"}, function(){
			$(this).css( {backgroundPosition: "-121px -120px"} )
		});
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-121px 0)"})
	});
	$('#tabs .large')
	.css( {backgroundPosition: "0 0"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0 -76px)"}, function(){
			$(this).css( {backgroundPosition: "0 -120px"} )
		});
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(0 0)"})
	});
	if($('body').width() <= 1016)
		$('#main').css('overflow', 'hidden');
});

var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var button = document.createElement('div');
	button.className='uploadBtn';
	button.innerHTML='Upload';
	fakeFileUpload.appendChild(button);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

/*
	For form validation, the 1st element of the form should be an error message container
	which has class name 'error'
*/
function validate(form)
{
	var inputs = form.getElementsByTagName('input');
	
	for(var p = 0; p < inputs.length; p++)
	{
		var ip = inputs[p];
		if($(ip).hasClass('email'))
		{
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($(ip).val()) == false)
			{
				$(form).children().each(function(){
					if($(this).hasClass('error'))
					{
						$(this).html('Please insert a valid email address');
					}
				});
				return false;
			}
		}
		if($(ip).hasClass('req'))
		{
			if($.trim($(ip).val()) == '')
			{
				$(form).children().each(function(){
					if($(this).hasClass('error'))
					{
						$(this).html(ip.id + ' is missing');
					}
				});
				return false;
			}
		}
	}
	$(form).find('.toggleval').each(function() {
	  if($(this).val() == $(this).data("defText")) {
		$(this).val("");
	  }
	});
	return true;
}

function setTabs()
{
	var links = new Array();
	$.each($('#tabs li'), function(){ 
		links.push($(this).children()[0].href);
	});
	var idx = 0;
	$.each($('#tabs li'), function(){ 
		$(this).click(function(){
			window.location = links[idx++];
		});
	});
}

//Disable form submit on enter keypress
function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}




//anchor scroll to up or down
$(document).ready(function() { 	$("a.anchorScroll").anchorAnimate() });
jQuery.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({	speed : 2100 }, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top -30;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}