// JavaScript Document

function popUp(url,w,h,scrol){
	if(!scrol){
		srol = 'auto';
	}
	newwindow = window.open(url,'popUp','toolbar=0,scrollbars='+scrol+',location=0,statusbar=0,menubar=0,resizable=0,width='+w+',height='+h+',left='+Math.round((window.screen.width/2)-(w/2))+',top='+Math.round((window.screen.height/2)-(h/2)));
	newwindow.focus();
}


// jQuery

$.fn.delay = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

$(document).ready(function(){
	
	
	$('#slideshow').cycle({ 
		fx:    'fade', 
		speed:  1000,
		pause: 1,
		random: 1,
		timeout:  4000 
	});
	
	$('#slideshow').mouseover(function(){
		$('#pauseIcon').fadeIn("slow");
	});
	
	$('#slideshow').mouseout(function(){
		$('#pauseIcon').fadeOut("slow");
	});
	
	
	$('#banner_slideshow').cycle({ 
		fx:			'fade', 
		speed: 		1000,
		pause:		0,
		random: 	1,
		timeout:  	4000,
		height: 	'250px'
	});
		
	var windowShadeHeight = $('#connect-with-us').height();
	var windowShadeUp = 0 - (windowShadeHeight-28);
	$('#connect-with-us').hide();
	
	sentUp = true;
	
	$('#connect-with-us-tab').click(
		function () { 
				if( sentUp == false ){
				
					$('#connect-with-us').slideUp( function(){ $('#connect-with-us-tab').css( { 'top':'-2px' } ) } );
					
					sentUp = true;
				
				}else{
					$('#connect-with-us').slideDown(500, function(){ $.scrollTo( '#connect-with-us', 500 ) } ); 
					$('#connect-with-us-tab').css( { 'top':'-3px' } );
					
					sentUp = false;
				
				}
			
			} 
	);
	
	$('#connect-with-us #close').hover(
		function(){
			$('#connect-with-us #close').css('background-position','0 bottom');
		},
		function(){
			$('#connect-with-us #close').css('background-position','0 top');
		}
	);
	
	
	$('#connect-with-us #close').click(
		function(){ 
			$('#connect-with-us').slideUp( function(){
				$('#connect-with-us-tab').css( { 'top':'-2px' } )
			});
			
			sentUp = true;
			
		}
	);
	
	
	$('#connect-with-us #submit').click(
		function(){
			
			// Get values
			var fullname = $("#simpleContact input#fullname").val();
			var phone = $("#simpleContact input#phone").val();
			var email = $("#simpleContact input#email").val();
			var reference = $("#simpleContact input#reference").val();
			var inquiry_type = $("#simpleContact select#inquiry_type").val();
			var dest = $("#simpleContact input#dest").val();
			
			// Validate that sometyhing is being sent
			var valid = true;
			var errorMessages = new Array();
			$('#contact_form #errorMessages').html("");
			if( fullname=='' ){
				errorMessages.push("Please provide your name.");
				valid = false;
			}
			
			if( phone=='' && email=='' ){
				errorMessages.push("Please provide a phone number or email address so that we may contact you.");
				valid = false;
			}
						
			if( inquiry_type=='' ){
				errorMessages.push("Please tell us what you would like us to contact you about.");
				valid = false;
			}
			
			if( fullname.indexOf('http://')>-1 || phone.indexOf('http://')>-1 || email.indexOf('http://')>-1 || reference.indexOf('http://')>-1 ){
				errorMessages.push("Please, do not include links in this correspondence.");
				valid = false;
			}
			
			if( valid ){
				var dataString = 'fullname='+fullname+'&phone='+phone+'&email='+email+'&reference='+reference+'&inquiry_type='+inquiry_type+'&dest='+dest;
				$.ajax({
					type: "POST",
					url: "/sites/default/themes/reynoldsplantationcom/handlers/windowshade_contact_form.php",
					data: dataString,
					
					success: function() {
						$('#contact_form').html("<div id='message'></div>");
						$('#message').html("<h2>Contact Form Submitted!</h2>")
						.append("<p>We will be in touch soon.</p>")
						.hide()
						.fadeIn(1500);
					},
					
					error: function(){
						$('#contact_form #errorMessages').append("<p>We sincerely apologize, but it appears that something is wrong with our form. This error has automatically been reported to our site technicians. Please consider using our other contact forms located <a href=\"/contact\">here</a>.</p>");
					}
				
				});
			}else{
				$('#contact_form #errorMessages').append("<h2>Information is missing</h2><ul>");
				for(i=0; i<errorMessages.length; i++){
					$('#contact_form #errorMessages').append("<li>"+errorMessages[i]+"</li>");
				}
				$('#contact_form #errorMessages').append("</ul>");
				$('#contact_form #errorMessages').fadeIn(500).fadeTo(4000,1).fadeOut(500);
			}
			return false;
		}
	);
	
	
	$('#share-this > div').hide();
	if( $('#banner_slideshow').length == 0 ){
		$('#share-this > div').css('top','15px');
		$('#share-this > div').css('background-image','url(/sites/default/themes/reynoldsplantationcom/images/gui/shareThisBubbleFlipped.png)');
		$('#share-this .social-media-icon').css('margin-top','6px');
		shared_top = "30px";
	}else{
		shared_top = "-27px";
	}
	
	$('#share-this').hover(
		function(){
			if( !$.browser.msie ){
			// use this when moved to jQuey 1.3 if( jQuery.support.opacity ){
				$('#share-this > div').css('top','0');
				$('#share-this > div').animate({ 
					opacity: "toggle",
					top: shared_top
				}, 'fast' );
			}else{
				$('#share-this > div').show();
			}
		},
		function(){
			if( !$.browser.msie ){
			// use this when moved to jQuey 1.3 if( jQuery.support.opacity ){
				$('#share-this > div').animate({ 
					opacity: "toggle",
					top: "0"
				}, 'fast' );
			}else{
				$('#share-this > div').hide();
			}
		}
	);
	
	$('#sidebar-left').height( $('#content-right-column').height() );
	
});