$(function(){		
	$(".header .menu img").each(function(){
													 
		var active = $(this).attr("active_src");
		var passive = $(this).attr("src");
		
		var img = new Image();
		
		img.src = $(this).attr("active_src");	 
		
		if( $(this).parents("a").is(".active") ){
		
			$(this).attr("src", active);
			
		}
		
		$(this).parents("a").not(".active").mouseover(function(){
											
			$(this).find("img").attr("src", active);
											
		});
		
		$(this).parents("a").not(".active").mouseout(function(){
											
			$(this).find("img").attr("src", passive);
											
		});
													 
	});

	
	$('[href="#login"]').click(function(ev){
		ev.preventDefault();
		notification({ text: $(".login").html()});
		
	});
	
	if( $(".front").html() ){
		$(".line").css("opacity", "0");
		
		var new_height = $(window).height();
		new_height-= $(".header").height();
		$(".slider").css({ height: new_height-30+"px", opacity: "0"});
		$(".slider").animate({opacity: "0"}, {duration: 3000, queue:true});
		$(".line").fadeIn();
		$(".slider").animate({height: new_height, opacity: "1"}, 1000, function(){
			$(".frontpage").unwrap();
			$(".line").animate({opacity: "1"}, 0, function(){
				$(".line").removeAttr("style");																  
			});
		});
	}
	
	$("[rel='send']").click(function(ev){
		
		ev.preventDefault();
		
		var obj = $(this).parents("table");
		var pic = obj.find("img").attr("src") || '';
		var name = obj.find("h1").text() || '';
		var new_title = obj.find("#new_title").text() || '';
		var prof = obj.find(".profession").text() || '';
		var actions = $(this).attr("href");
		
		notification({
			text: $(".contact_form").html(),
			type: "large"
		});
		
		$(".notification")
			.find("img").attr("src", pic).end()
			.find(".desc b").text(name).end()
			.find(".desc a").text(prof).end()
			.find("form").attr("action", actions);
			
		if( new_title !== "" ){
			$(".notification").find(".desc span").text( new_title);
			
		}
			
		
		if( pic == '' ){ $(".notification .img").remove();}
		if( prof == '' ){ $(".notification .desc a").remove(); }
		
		$(".notification form").forms();
		
	});
	
	$("[href='#map']").click(function(ev){
		
		ev.preventDefault();
		
		
		notification({
				text: $(".map").html(),
				type: "large"
			});
	
	});
	
	
	$(".leader_source td").each(function(){
		
		var change =  $(".group_box.clone").clone();
		
		change.removeClass("clone").find(".middle").html( $(this).html() );
		$(".content .inline").append( change );
														 
	});
	
});

$.fn.forms = function(){		

	if($(this).html() !== null){
		
		$(this)[0].reset();
		
		$(this).blanker();
		
		$(this).find("a[href='#submit']").click(function(ev){
		
			ev.preventDefault();
			$(this).parents("form").submit();
		
		});
		
		$(this).submit(function(){

			var counter = 0;
			
			$(this).find(".required").each(function(){
			
				$(this).validate();
				
				if( $(this).is("[class$='error']") || $(this).parents(".label").is("[class$='error']") ){
					
					counter++;
					
				}
				
			});
			
			
			if( counter > 0 ){
				
				return false;
			
			}
			
		
		
		});
		
		$(this).find(".required").keyup(function(e){
		
			$(this).validate();
		
		});		
		
	}//if
	
}

$.fn.blanker = function(){
	
	$(this).find("input, textarea").each(function(){
													
		$(this).addClass("default");
		
		var type = $(this).attr("type");
		
		$(this).focus(function(){
			if($(this).attr("type") == "submit") return false;	  
			if($(this).attr("value") == $(this).attr("defaultValue")){
				$(this).attr("value", "").removeClass("default");
			}
		});
		
		$(this).blur(function(){
			if( $(this).attr("value").length == 0 || $(this).attr("value") == $(this).attr("defaultValue") ){
				
				$(this).attr("value", $(this).attr("defaultValue")).addClass("default");
			
			}
		});
	});//each	
	
}

$.fn.validate = function(){
			
	var minimum = $(this).attr("min") || 0;
	var maximum = $(this).attr("max") || 999;
	var obj = $(this);
	
	if( obj.attr("value") == obj.attr("defaultValue")
	||  obj.attr("value") == ""
	||  obj.attr("value").length == minimum
	||  obj.attr("value").length > maximum ){
	
		obj.addClass("error").parents(".label").addClass("error");
	
	}
	
	else if( obj.attr("category") == "email" ){
		
		if( check_mail( $(this).attr("value") ) == false ){
			
			obj.addClass("error").parents(".label").addClass("error");
		
		}
		else{
			
			obj.removeClass("error").parents(".label").removeClass("error");
			
		}
		
	}
	
	else{
	
		obj.removeClass("error").parents(".label").removeClass("error");
		
	}																					  
	
}

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 ){
	
	var o ={};
	o.text = options['text'] || 'This is a demo text';
	o.fade = options['fade'] || 'false';
	o.rainbow = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
	o.t_layer = options['t_layer'] || 'true';
	o.t_lvl = options['t_lvl'] || '0.3';
	o.t_color = options['t_color'] || '#000';
	o.delay = options['delay'] || '3000';
	o.speed = options['speed'] || '500';
	o.clickTAG = options['clickTag'] || '.opaque';
	o.type = options['type'] || 'default';
	o.id = 'notification';
	o.flag = 'true';
	o.scroller = "true";
	if(options.t_color == "rainbow"){o.t_color = o.rainbow;}
	
	o.html = '<div class="notification" id="'+o.id+'">'+o.text+'</div>';
	
	if( o.type == "large" ){
		
		o.html = '<div class="notification large" id="'+o.id+'">';
		o.html+= '<div class="top">&nbsp;</div>';
		o.html+= '<div class="middle">'+o.text+'</div>';
		o.html+= '<div class="bottom">&nbsp;</div>';
		o.html+= '</div>';
		
	}
	
	
	$("body").append(o.html);  

	if($(".notification").outerHeight() > $(window).height()-50){$(".notification").css({position:"absolute"}); o.scroller="false";}
	if($(".notification").css("position") == "absolute" && o.scroller == "true"){$(window).scroll(positions);}
	positions(false);
	
	function positions( switcher ){
		
		if(o.flag == "true"){
			var pos = {};
			pos.left = ($(window).width() - $("#"+o.id).outerWidth())/2;
			pos.top = $(window).scrollTop() + (($(window).height() - $("#"+o.id).outerHeight())/2);
	
			if(pos.top <0 || $(".notification").outerHeight() > $(window).height()-50){
				pos.top = $(window).scrollTop()+50;
			}
			
			if($(".notification").css("position") == "fixed"){
				pos.top = (($(window).height() - $("#"+o.id).outerHeight())/2);
			}
			
			if(switcher == false){
				$("#"+o.id).css({'left':pos.left+'px', 'top':pos.top+'px'});
			}
			
			else{
				$("#"+o.id).animate({'left':pos.left+'px', 'top':pos.top+'px'},{duration:300, queue:false});
			}
			
		}
	}
	if(o.t_layer == "true"){
      $("body").append('<div class="opaque" id="opaque_'+o.id+'">&nbsp;</div>');
      $("#opaque_"+o.id).css({'position':'absolute', 'top':'0px', 'z-index':'900', 'left':'0px','background':o.t_color, 'height':$(document).height()+'px', 'width':$(document).width()+'px'}).fadeTo(0, o.t_lvl);
	}
	if(o.fade == "true"){fade_out(o.delay)}
   else{
      $(o.clickTAG+", [href='#close']").click(function(ev){
			ev.preventDefault();
			fade_out(0);
		});
	
      $(o.clickTAG).css({'cursor':'pointer'});
   }
   function fade_out( delay ){
      $("#"+o.id).fadeTo(delay, "1.0").fadeOut(o.speed, function(){$(this).remove();});
      if(o.t_layer == "true"){
         $("#opaque_"+o.id).fadeTo(delay, o.t_lvl).fadeOut(o.speed, function(){$(this).remove();});
      }
		o.flag = "false";
   }
}

