$(document).ready(function(){
	forms([
			{form:".login .form form"},
			{form:".search form"}
			]);
	scrolling_header();
	$(window).resize(scrolling_header);
});

function scrolling_header(){
	var head_marg = ($(window).width()-$(".header").width())/2;
	if(head_marg <0){head_marg=0;}
	$(".header").css({left: head_marg+"px"});
}

function forms( options ){
	for(var c=0; c < options.length; c++){
		
		if($(options[c].form).attr("method")){
		
			$(options[c].form+" input").each(function(){
				$(options[c].form)[0].reset(); 									
				var value = $(this).val();
				var type = $(this).attr("type");
	
				
				
				$(this).focus(function(){
					if($(this).attr("type") == "submit") return false;	  
					if($(this).val() == value){
						$(this).val("");
					}
				});
				
				$(this).blur(function(){
					$(this).val().length == 0 ? $(this).val(value) : "";
				});
				
			});//each
			
		}//if
	} //for
}
function validate_form(options){
	/*
	usage:	validate_form({form:" selector "});    the forms ID or CLASS
				category="validate"							to validate fields
				type="email"  									if it is an EMAIL field
				min="5"											to set the MINIMUM required characters
				max="5"											to set the MAXIMUM amount of characters
				
				
				.error 											that is the DEFAULT error class name
	
	by Henri Kokk
		www.henri.ee
	*/
	if($(options.form).find("input").attr("name") !== null){
		
		var o = {
			type: "category",
			btn:	"saada",
			err_class: "error",
			object: options.form
		};
		
		$("[rel='validate']").keyup(function(e){
			if(e.keyCode == 9) return false;
			validates($(this));
		});
		
		$(o.object).submit(function(){
			o.counter=0;
			$(o.object).find("[rel='validate']").each(function(){
				$(this).removeClass(o.err_class);
				validates($(this));
				$(this).attr("class") == o.err_class ? o.counter++ : "";
			});
			
			if(o.counter !== 0) return false;
			
		});
	}
	
	function validates(obj){
		if(obj.attr(o.type) == "email"){
			check_mail(obj.val()) == false ? obj.addClass(o.err_class): obj.removeClass(o.err_class);
		}
		else{
			o.minimum = $(obj).attr("min") || 1;
			o.maximum = $(obj).attr("max") || 9999999999;
			obj.val().length < o.minimum ? obj.addClass(o.err_class): obj.removeClass(o.err_class);
			obj.val().length > o.maximum ? obj.val(obj.val().substring(0, o.maximum)) : "";
		}
		
	}
	
	function check_mail(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
}
   
function notification( options ){
/*
	usage:	first make a style called "notification" and set it 
				to absolute position with all the styles you need.
				for demo add: 
				.notification{position:absolute; z-index:999; background:white; border:#000 1px solid; width:100px;}
				to your style sheet
				
				parameters
				notification({
								 	text : "demo" 				//add the text to it
									fade : "true" 				// should it fade out without click
									transparency: "true" 	// the black layer
									transparency_lvl: "0.7" //black layers transparency lvl
									delay: "3000"				//fade out delay in milliseconds
									speed: "1000"				//fade out speed in milliseconds
									clickTAG: ".close"		//the object to be clicked to close the notification
									type:	"default"  			//for different templates
								 });
	
	by Henri Kokk
		www.henri.ee
*/
	
	var option = {
		fade: 					options['fade'] || "true",
		text: 					options['text'] || "This is a demo text",
		rainbow: 				'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')',
		transparency: 			options['transparency'] || "true",
		transparency_lvl: 	options['transparency_level'] || 0.5,
		transparency_color:	options['transparency_color'] || "#000",
		delay:					options['delay'] || 3000,
		speed:					options['speed'] || 500,
		clickTAG:				options['clickTAG'] || "#auto_notify",
      type:						options['type'] || "default",
		id:						"auto_notify"
	}
   $("body").prepend('<div class="notification" id="'+option.id+'">'+option.text+'</div>');  
   var calculation = {
      left:						($(window).width() - $("#auto_notify").width())/2,
		top:						($(window).height() - $("#auto_notify").height())/2,
		scrollTop:				$(window).scrollTop(),
		doc_height:				$(document).height(),
		doc_width:				$(document).width()
   }
   if(calculation.top < calculation.scrollTop){
      calculation.top = calculation.scrollTop+50;
      calculation.doc_height = calculation.doc_height+100;
   }
   
   

   if(option.transparency == "true"){
      $("body").prepend('<div class="opaque" id="o'+option.id+'">&nbsp;</div>');
      $("#o"+option.id).css({'position':'absolute', 'top':'0px', 'z-index':'999', 'left':'0px','background':option.transparency_color, 'height':calculation.doc_height+'px', 'width':calculation.doc_width+'px'}).fadeTo(0, option.transparency_lvl);
	}
   $("#"+option.id).css({'left':calculation.left+'px', 'top':calculation.top+'px'});
   if(option.fade == "true"){fade_out(option.delay)}
   if(option.fade == "false"){
      $(option.clickTAG).click(function(){fade_out(0);});
      $(option.clickTAG).css({'cursor':'pointer'});
   }
   function fade_out(delay){
      $("#"+option.id).fadeTo(delay, "1.0").fadeOut(option.speed, function(){$(this).remove();});
      if(option.transparency == "true"){
         $("#o"+option.id).fadeTo(delay, option.transparency_lvl).fadeOut(option.speed, function(){$(this).remove();});
      }
   }
}