//
//	jQuery Modal Window - modal.js
//	Created By: Dylan Hudson
//	Created Date: 3.10.10
//
var site_url = "http://www.elocallisting.com/";



$(document).ready(function(){
	var window_height = $(window).height();
	var window_width = $(window).width();
	var overlay_height = $(document).height();
	
	
	
	$('.open_modal').click(function(){
		var rel = $(this).attr('rel');
		var href = $(this).attr('href');
		switch(rel){
			case 'customer-service':
				var id = 'contact-modal-email';
				break;
			default:
				if(href=='how-it-works'){
					var id = 'how-it-works';
				}else {
					var id = 'contact-modal';
				}
			}
		
		$('#modal').load(site_url+'modal/modal.html #'+id, function(){	
					
			var modal_height = $('#'+id).outerHeight();  
			var modal_width = $('#'+id).outerWidth();
						
			var top = (window_height-modal_height)/2;  
			var left = (window_width-modal_width)/2;  
			
			$('#'+id).css({'top' : top , 'left' : left});
			openModal(id,rel);
		});
		return false;
	});
			
	function openModal(id,rel){
		
		$('form').live('submit',function(){
			var action = $(this).attr('action');
			
			if($('#contact-modal #full_name').length){
				var fullname = $('#contact-modal #full_name').val(); 
				var fullname = fullname.split(' ');
				var firstname = fullname[0];
				var lastname = fullname[1];
				$('.modal_window #first_name').val(firstname);
				$('.modal_window #last_name').val(lastname);
			}
			
			var data = $(this).serialize()
			if($('#modal #phone_number').length > 0){
				var phoneid = $('#modal #phone_number');
			}else if($('#modal #phone').length > 0){
				var phoneid = $('#modal #phone');
			}
			
			if(phoneid.val() == '' || phoneid.val() == null || phoneid.val() == 'undefined'){
				$('.modal-info p').html('<strong style="color:#ff0000">Please fill out all required fields!</strong>')
			}else{
				// console.log($('#lead_source').val());
				if($('#lead_source').val() === "General Info"){
					var action = "http://www.elocallisting.com/modal/general_mail.php?";
				}
				var modal_height = $('.modal_window').outerHeight();  
				var modal_width = $('.modal_window').outerWidth();
				$('.modal_window').css({'height':modal_height,'width':modal_width});
				//console.log(action+data); 
				$(this).load(action+data);
				$(this).fadeOut('fast',function(){
					$('#thank-you-modal').fadeIn('fast');
				});
			}
			
			return false;
		});
	
		$('body').append('<div id="overlay" style="position:fixed;top:0;left:0;background:#000;opacity:0;display:block;width:100%;z-index:500"></div>');
		
		$('#overlay').css('height' , overlay_height);
		
		$('html,body').animate({scrollTop:0},500);
		
		$('#overlay').fadeTo(1000,0.75);
		
		$('#'+id).fadeIn(1000);
		
		switch(rel){
			case 'corporations':
				var leadsource = "Web - Corporations";
				var intro = "Corporations - Grow your business with a leader in online lead generation.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'publishers':
				var leadsource = "Web - Publishers";
				var intro = "Want to learn more about our Publisher Programs?  Request More Info.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'resellers':
				var leadsource = "Web - Resellers";
				var intro = "Want more information on how to join our Reseller Program?  Request More Info.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'agencies':
				var leadsource = "Web - Agencies";
				var intro = "Interested in our Agency Partnership Program?  Request More Info.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'affiliates':
				var leadsource = "Web - Affiliates";
				var intro = "Start earning revenue today with our Affiliate Program.  Request More Info.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'brokers':
				var leadsource = "Web - Brokers";
				var intro = "Start earning revenue today with our Affiliate Program.  Request More Info.";
				var recordtype = "01280000000BTux" //reseller
				break;
			case 'general_info':
				var leadsource = "General Info";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				break;
			case 'general_sales':
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
			case 'solutions':
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
			case 'clicks2calls':
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
			case 'quick-glance-dashboard':
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
			case 'search-solutions':
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
			default:
				var leadsource = "Web - General Sales";
				var intro = "Fill out this form, and one of our agents will get your started today!";
				var recordtype = "01280000000BR54"; //small business
				break;
		}
		if(id !== 'how-it-works'){
		$('#lead_source').val(leadsource);};
		$('#recordType').val(recordtype);
		$('.modal-info p').html(intro);
		
		//alert(leadsource+' + '+recordtype+' + '+intro);
				
		$('#overlay, .close_modal').click(function(){
			closeModal();
		});
		
		$(document).keydown(function(e){
			if (e.keyCode == 27) {
				closeModal();
			};
		});
	};
	
	function closeModal(){
		$('#overlay').fadeOut(1000);
		$('.modal_window').fadeOut(500);
		setTimeout("$('#overlay,.modal_window').remove();", 500);
	};

});



