﻿$.rootURL = 'http://www.questfore.net/firstinsight/';
$.show = true;
$.bigwhich = '';
$.which = '';
$(document).ready(function(){
	$("li.dir").hover(function(){
	        $(this).addClass("hover");
	        $(this).children('ul').css('display','block');
        },function(){
	        $(this).removeClass("hover");
	        $(this).children('ul').css('display','none');
    });
	//allow popup from links if javascript is enabled
	armthepopup();
	evenColumns();
	
	$('a.popup').click(function(){
		var filepath = $(this).attr('title');
		var newHeight = $(document).height();
		$('div.darkbox').height(newHeight);
		$('div.darkbox').fadeIn('fast', function(){
			$('input.filepath').val(filepath);
			$('input.firstName').focus();
			$('small.error').fadeIn('fast');								 
		});
		
	});
	$('.popup .close').click(function(){
		$('div.darkbox').fadeOut('fast');
	});
	
	
	
	$('a.popsubmit').click(function(){
		var good = validate();
		if (good == true){
			ajaxCall();
		} else {
			$('div.popform form p.error').html("Please fill out the required fields.");
			return false;
		}									
	});
	
	///email helper
	$('div.popform form input.email').blur(function(){
		var str = $(this).val();
		if (str.indexOf('@') < 0 || str.indexOf('.') < 0){
			$('small.email').fadeIn('fast');
		} else {
			$('small.email').fadeOut('fast');
		}
	});
	//other required helpers
	//$('div.popform form input.req').bind('blur', function(){
	$('div.popform form input.req').blur(function(){
		var str = $(this).val();
		$.bigwhich = $(this).attr('class');
		$.which = $.bigwhich.replace(' req','');
		if (str.length <= 0){
			$.show = true;
			error();
		} else {
			$.show = false;
			error();
		}
	});
});

function armthepopup(){
	$('a.popup').each(function(){
		$(this).attr('href','#');
		$(this).attr('target','');
	});
}
function error(){
	switch($.which){
		case 'firstName':
			if($.show == true) {
				$('small.firstName').fadeIn('fast');
			} else {
				$('small.firstName').fadeOut('fast');
			}
		break;
		case 'lastName':
			if($.show == true) {
				$('small.lastName').fadeIn('fast');
			} else {
				$('small.lastName').fadeOut('fast');
			}
		break;
		case 'company':
			if($.show == true) {
				$('small.company').fadeIn('fast');
			} else {
				$('small.company').fadeOut('fast');
			}
		break;
	}
	
}
function validate(){
	var problems = $('small.error:visible').length;
	if (problems > 0){
		return false;
	} else {
		return true;
	}
	
}
function ajaxCall(){
	var firstName = $('input.firstName').val();
	var lastName = $('input.lastName').val();
	var email =$('input.email').val();
	var company =$('input.company').val();
	var title =$('input.title').val();
	var phone =$('input.phone').val();
	var file =$('input.filepath').val();
	//var dlinfo = $('div.popform form').serialize();
	var datastring = "firstName="+firstName+"&lastName="+lastName+"&email="+email+"&company="+company+"&title="+title+"&phone="+phone+"&file="+file;
	
	var u = $.rootURL + 'download';
	
	$.ajax({			  
	  type: "POST",
	  url: u,
	  dataType: "json",			  
	  data: datastring,
	  cache: false,
	  success: function(){
	  },
	  error: function(data)
	  {
		  //$('div.popform form').prepend("<p class='error'>We're sorry, there was an error submitting your info</p>");
		  return false;
	  }
	});							
	closebox();
	window.open($('input.filepath').val(),'_blank');
	$('input.req, input.phone, input.title').val('');
}
function closebox(){
	$('div.darkbox').fadeOut('fast');
}
function evenColumns (){
	var firstheight = $('div.col1').height();
	//console.log(firstheight);
	var secondheight = $('div#links').height();
	//console.log(secondheight);
	//outside columns
	var outsideheight = 0;
	if (secondheight!=null){
		if (firstheight>secondheight){
			$('div#links').height(firstheight);
			var outsideheight = firstheight;
		} else if (secondheight>firstheight){
			$('div.col1').height(secondheight);
			var outsideheight = secondheight;
		}		
	}
	
	var middleheight = $('div.grid_9').height();
	//console.log(middleheight);
	if (middleheight!=null){
		if (middleheight>outsideheight){
			$('div#links').height(middleheight);
			$('div.col1').height(middleheight);
		}
	}
}
