var $j = jQuery.noConflict();
var page_check = location.pathname;
var gift_vars;

$j(document).ready(function () {
	
	
	
	$j("#sub_page_sidebar").ready(
			  function () {
						
				  var subpage_sidebar_height = $j(".subpage_sidebar").height();
						var content_height = $j("#content").height();
						
						if( subpage_sidebar_height  < content_height )
						{
							var ccc = $j("#content").height() - 5 - 214;
							var ccc = ccc + "px";
							
							$j("#sub_page_sidebar").css("height", ccc);
							
						}
						else
						{
							//alert("Not");
						}
						
			    });	
	
	$j("#about_sidebar").ready(
	  function () {
				
		  var subpage_sidebar_height = $j(".subpage_sidebar").height();
				var content_height = $j("#content").height() +5;
				
				if( subpage_sidebar_height  < content_height )
				{
					var ccc = $j("#content").height() - 5;
					var ccc = ccc + "px";
					
					$j("#about_sidebar").css("height", ccc);
					
				}
				else
				{
					//alert("Not");
				}
				
	    });	
	
	
	$j("#blog_sidebar").ready(
			  function () {
						
				  var subpage_sidebar_height = $j(".subpage_sidebar").height();
						var content_height = $j("#content").height() +5;
						
						if( subpage_sidebar_height  < content_height )
						{
							var ccc = $j("#content").height() - 5;
							var ccc = ccc + "px";
							
							$j("#blog_sidebar").css("height", ccc);
							
						}
						else
						{
							//alert("Not");
						}
						
			    });	
	
	
	
	/* Set form response opacity to 0 */
	$j(".form_response").css('opacity',0);
	
	/* Set the footer below the content 
	$j("#content").ready(
	  function () {
				new_footer_top_margin = $j("#content").height();

				$j("#footer").css('margin-top', new_footer_top_margin);
	    });
	*/
	
	
	$j('.submit_button').click(
			function ()
			{
				var message;
				message +='&persons_name='+ $j("input[name=persons_name]").val();
				message +='&company_name='+ $j("input[name=company_name]").val();
				message +='&phone_number='+ $j("input[name=phone_number]").val();
				message +='&email_address='+ $j("input[name=email_address]").val();
				message +='&how_can_we_help='+ $j('input[name=how_can_we_help]:checked').val()
				message +='&tell_us_more='+ $j("textarea[name=tell_us_more]").val();
				
				$j(".form_response").css('opacity',0);
				
				$j.post(  "http://www.bluedaring.com/BlueDaring/wp-content/themes/bluedaring/send-form.php", 
						  message ,
						  function(data){
							if(data == '1 ')
							{
								$j(".form_response").html('Thank you for your inquiry.');
							}
							else
							{
								$j(".form_response").html('There was a problem with sending your message.');
							}
							$j(".form_response").animate({opacity:1},'slow');
						  }, 
						  "text");

			}		
	);
	
	$j('#thanksgiving09submit').click(
				function ()
				{
					$j('#thanks_status').html('');
					
					var email     = $j('#thanks_email').attr('value');  
					$j.ajax({  
					            type: "POST",  
					            url: "http://www.bluedaring.com/BlueDaring/wp-content/themes/bluedaring/thanksgiving09/check_email.php",  
					            data: "email="+ email,  
					            success: function(msg)
								{  
									if (msg == "")
									{
										$j('#thanks_status').html('We don\'t have a record of your email, please try again.');
									}
									if(msg == "0")
									{
										location.href = "http://www.bluedaring.com/your-sweet-deal?email=" + encodeURIComponent(email);
									}
									if (msg == "1")
									{
										$j('#thanks_status').html('You have already redeemed your sweet deal.');
									}
								}
					       });
					
					
				}		
		);	
		
		
		
		
		
		
		$j('#thanksgiving_strategy_deal, #thanksgiving_marketing_deal').click(
					function ()
					{
						var email     = gift_vars['email'];
						
					//	alert( $j(this).attr("id") );
						
						if ( $j(this).attr("id") == "thanksgiving_strategy_deal") 
						{
							gift_type = "Strategy";
						} 
						
						if ( $j(this).attr("id") == "thanksgiving_marketing_deal") 
						{
							gift_type = "Communications";
						}
						
						$j.ajax({  
						            type: "POST",  
						            url: "http://www.bluedaring.com/BlueDaring/wp-content/themes/bluedaring/thanksgiving09/gift_delivered.php",  
						            data: "email="+ email + "&gift_type="+ gift_type,  
						            success: function(msg)
									{  
										location.href = "http://www.bluedaring.com/thanksgiving-confirmation?email=" + encodeURIComponent(email);
									}
						       });
					

					}		
			);
		
		
		
		
		
		
	
		
		//checking for the thanksgiving page
		if (page_check.indexOf("your-sweet-deal") != -1)
		{
			var email     = getUrlVars(window.location.href)["email"];
			email = decodeURIComponent(email);

			$j.ajax({  
			            type: "POST",  
			            url: "http://www.bluedaring.com/BlueDaring/wp-content/themes/bluedaring/thanksgiving09/return_info.php",  
			            data: "email="+ email,  
			            success: function(msg)
						{  
							gift_vars = getUrlVars(msg);
							//alert(gift_vars['first_name']);
							$j('#thanksgiving_first_name').html( gift_vars['first_name'] );
							$j('#thanksgiving_company_name').html( gift_vars['company_name'] );
						
						}
			       });
			
		}
		if (page_check.indexOf("thanksgiving-confirmation") != -1)
		{
			var email     = getUrlVars(window.location.href)["email"];
			email = decodeURIComponent(email);
			
			$j.ajax({  
			            type: "POST",  
			            url: "http://www.bluedaring.com/BlueDaring/wp-content/themes/bluedaring/thanksgiving09/return_info.php",  
			            data: "email="+ email,
			            success: function(msg)
						{  
							gift_vars = getUrlVars(msg);
							$j('#gift_type').html( gift_vars['gift_type'] );
							$j('#thanksgiving_email').html( gift_vars['email'] );
						
						}
			       });
		}
	

});


function getUrlVars(varstring) {
    var vars = {};
    var parts = varstring.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}



