//Dropdown
$(document).ready(function(){
	$('.header').find('.nav').find('li').hover(function(){
		$(this).find('ul:first-child').css('display','block');									
	},function(){
		$(this).find('ul:first-child').css('display','none');	
	});
	$('a').each(function(e){
		if($(this).attr('href').match('http://')){
			$(this).attr('target','_blank');
		}
	});
	/*
	$('a').click(function(e){
		if($(this).attr('href').match('http://')){
			e.preventDefault();
			window.open(
						$(this).attr('href'),
						'',
						'status=1,toolbar=0,location=1,menubar=0,directories=0,resizeable=1,scrollbars=1,height='+
						($(window).height() / 2)+
						',width='+
						($(window).width() / 2)
						);
		}
	});
	*/
	$('#requestafreesolarevaluation').submit(function(e){
		var errors = new Array();
		var names  = new Array();
		var types  = new Array('input','textarea','select');
		for(i=0; i<types.length; i++){
			$(this).find(types[i]).each(function(){
				if($(this).val() == '' || ($(this).tagName().toUpperCase() == 'SELECT' && $(this).val() == 'Choose...') ){
					errors.push($(this));
					names.push($(this).attr('name'));
					error($(this));
				}else{
					de_error($(this));	
				}
			});
		}
		if(errors.length > 0){
			var msg = "The Following Fields Are Required: \n"+names.join("\n");
			alert(msg);
			e.preventDefault();
		}
	});
	
	
	$('a[class="presentation"]').click(function(e){
		/*e.preventDefault();
		window.open(
					$(this).attr('href'),
					'',
					'status=1,toolbar=0,location=1,menubar=0,directories=0,resizeable=1,scrollbars=1,height=725,width=1000');
		*/
		
	});
	
	$('.filepath').hover(function(){
		var div = $(this);
		var f 	 = /module/gi;
		var i	 = 0;
		while(!$(div).attr('class').match(f)){ 
			div = $(div).parent();
			i++;
			if(i > 20){ return false; }
		}	
		var width = 'width:'+$(div).width()+'px; ';
		var height= 'height:'+$(div).height()+'px; ';
		var style = width+height;
		if($(this).parent().css('position') == 'relative'){
			var top	  = 'top:'+($(div).offset().top-$(this).parent().offset().top)+'px; ';
			var left  = 'left:'+($(div).offset().left-$(this).parent().offset().left)+'px; ';
			style += top+left;
		}
		$(this).parent().append('<div class="selected-module" style="'+style+'"></div>');
	},function(){
		var div = $(this);
		var f 	 = /module/gi;
		var i	 = 0;
		while(!$(div).attr('class').match(f)){ 
			div = $(div).parent();
			i++;
			if(i > 20){ return false; }
		}
		$(div).find('.selected-module').remove();
	});
});

function error(input){
	if($(input).tagName().toUpperCase() == 'SELECT'){
		var t = $(input).tagName().toUpperCase();
		while($(t).tagName().toUpperCase() != 'P'){
			t = $(t).parent();	
		}
		$(t).find('label').css('color','#F00');
	}else{
		$(input).css('border','1px solid #F00');	
	}
}
function de_error(input){
	if($(input).tagName().toUpperCase() == 'SELECT'){
		var t = $(input).tagName().toUpperCase();
		while($(t).tagName().toUpperCase() != 'P'){
			t = $(t).parent();	
		}
		$(t).find('label').attr('style','');
	}else{
		$(input).attr('style','');	
	}
}

$.fn.tagName = function() {
    return this.get(0).tagName;
}
