/* 
Templatica scripts
written Alen Grakalic of cssglobe.com & templatica.com
*/

$(document).ready(function(){	
	pngfix();
	nav();
	validate();
	rotate();
});

this.nav = function(){	
	var navId = "nav";
	$("#"+ navId +" li").hover(
	  function () {
		$("ul:first",this).show();
		$("a:first",this).addClass("over");
	  }, 
	  function () {
		$("ul",this).hide();
		$("a",this).removeClass("over");
	  }
	);	
};

this.validate = function(){
	
	var valid;
	var err = "This field is required.";
	var errEmail = "Please provide a valid email address."
	var errPhone = "Phone number is required.";
	
	$("form").submit(function(){												 
														 
		$(".error",this).remove();
		valid = true;
		
		$(".required",this).each(	function(i) {
			if( $(this).hasClass("email") ){
				checkEmail(this);
			} else if ( $(this).hasClass("phone") ){
				checkPhone(this);
			} else {
				check(this);
			};
		})
	
		return valid;	
	});
	
	this.check = function(obj){
		if ($(obj).val() == ""){
			var errormsg = ( $(obj).attr("title") != "" ) ? $(obj).attr("title") : err;
			error(obj,errormsg);
			valid = false;
		};
	};
	this.checkEmail = function(obj){
		var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
		var val = $(obj).val();
		if (val.search(regEx) == -1){
			var errormsg = ( $(obj).attr("title") != "" ) ? $(obj).attr("title") : errEmail;
			error(obj,errormsg);
			valid = false;
		};
	};	
	this.checkPhone = function(obj){
		var regEx = /[\d\s_]{6,}/;
		var val = $(obj).val();
		if(val.search(regEx) == -1){
			var errormsg = ( $(obj).attr("title") != "" ) ? $(obj).attr("title") : errPhone;
			error(obj,errormsg);
			valid = false;
		};
	};	
	this.error = function(obj,errormsg){
		var parent = $(obj).parent();
		parent.append("<span class=\"error\">"+ errormsg +"</span>");
		$("span.error",parent).hide().show("fast");
	};		

};

/* easy content rotate */
this.rotate = function(){

	$(".rotate").each(function(){
										
		var obj = this;								
		var pause = 8000; 
		var length = $("li",obj).length; 
		var temp = 0;		
		var randomize = false;	
		
		function getRan(){
			var ran = Math.floor(Math.random()*length) + 1;
			return ran;
		};
		function show(){	
			if (randomize){
				var ran = getRan();
					while (ran == temp){
					ran = getRan();
				}; 
				temp = ran;		
			} else {
				temp = (temp == length) ? 1 : temp+1 ; 
			};
			$("li",obj).hide();	
			$("li:nth-child(" + temp + ")",obj).fadeIn("slow");		
		};
		show(); setInterval(show,pause);	
	
	});
	
	
	
	
};

/* png fix */
this.pngfix=function(){var ie55=(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)==4&&navigator.appVersion.indexOf("MSIE 5.5")!=-1);var ie6=(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)==4&&navigator.appVersion.indexOf("MSIE 6.0")!=-1);if(jQuery.browser.msie&&(ie55||ie6)){$("*").each(function(){var bgIMG=$(this).css('background-image');if(bgIMG.indexOf(".png")!=-1){var iebg=bgIMG.split('url("')[1].split('")')[0];$(this).css('background-image','none');$(this).get(0).runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+iebg+"',sizingMethod='crop')"}})}};
