$(document).ready(function () {
	//hide content box and have it transition in
	var location = window.location.href.split('/');
	var page = location[location.length-1];
	
	//check for index to provide a different transition
	if (page == 'index.html') {
		$('#header, #nav, #contentarea, #footer').css('display', 'none');
		$('#header').slideDown(1000)
		$('#footer').slideDown(1000)
		$('#nav, #contentarea').slideDown(1000);
	} else {
		$('#contentarea').css('display', 'none');
		$('#contentarea').slideDown(1000);
	}
	
	//ensure that content is reshown when back button is clicked
	window.onunload = function (){
		$('#contentarea').slideDown(1000);
	}
	
	//Transition between pages
	$('a.transition').click(function(event){
		event.preventDefault();
		var location = this.href.split('/');
		var page = location[location.length-1];

		//check for index and transition whole page
		if (page == 'index.html') {
			linkLocation = this.href;
			$('#header, #nav, #contentarea, #footer').slideUp(1000, redirectPage);
		} else {
			linkLocation = this.href;
			$('#contentarea').slideUp(1000, redirectPage);
		}
	});
	
	//redirect to appropriate page
	function redirectPage() {		
		window.location = linkLocation;
	}
	
	//apply clickable class to nav li's
	$('#nav_inner ul > li > a').addClass('ui-corner-all');
	$('#nav_inner ul > li > a').addClass('ui-state-default');
	$('#nav_inner ul > li > a').hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	);
	
	// add active class to button for current page
	$('a[href='+page+']').addClass('ui-state-active');
	
	//drop down lease options while hovering
	$('#leasedrop').css({'display': 'none'});
	$('#lease').mouseenter(function(){
		$('#leasedrop').slideDown(500);
	});
	
	$('#lease').mouseleave(function(){
		$('#leasedrop').slideUp(500);
	});
	
	//drop down products options while hovering
	$('#productsdrop').css({'display': 'none'});
	$('#products').mouseenter(function(){
		$('#productsdrop').slideDown(500);
	});
	
	$('#products').mouseleave(function(){
		$('#productsdrop').slideUp(500);
	});
	
	// position twitter widget to show on hover
	$('#twitter_large').css({'display': 'none'});
	$('#twitter').mouseenter(
		function (){
			$('#twitter_large').slideDown(500);
		});
	$('#twitter').mouseleave(
		function (){
			$('#twitter_large').slideUp(500);
		});
		
	// position facebook widget to show on hover
	$('#recommend_box').css({'display': 'none'});
	$('#recommend').mouseenter(
		function (){
			$('#recommend_box').slideDown(500);
		});
	$('#recommend').mouseleave(
		function (){
			$('#recommend_box').slideUp(500);
		});

	// position contact widget to show on hover
//	$('#dialog').dialog({
//				autoOpen: false,
//				modal: true,
//				width: 435,
//				show: "blind", 
//				hide: "blind"
//			});

//			$('#contact > a').click(function() {
//				$('#dialog').dialog('open');
//				return false;
//			});
			
	// create accordion style divisions for benefits page
	$('#benefits').accordion({
		collapsible: true,
		active: 0,
		autoHeight: false
	});
	$('#lease_benefits').accordion({
		collapsible: true,
		active: 0,
		autoHeight: false
	});
	$('#testimonials').accordion({
		collapsible: true,
		active: 0,
		autoHeight: false
	});	
	
	// create tabbed interface for dispensing station
	$('#liquor_dispensing_station').tabs();
	$('#liquor_storage_rack').tabs();
	$('#portable_cocktail_bar').tabs();
	$('#upgrade_to_probar').tabs();
	$('#lease_a_probar').tabs();

	//create slideshow for homepage
	$('#slideshow').hover(function (){
			//change to pic 1
			$('#slideshow > img').fadeOut(2500, function(){
				$('#slideshow > img').attr({'src': 'images/probar-slideshow-1.jpg'});
				$('#slideshow > img').fadeIn(2500, function(){
					//change to pic 2
					$('#slideshow > img').fadeOut(2500, function(){
						$('#slideshow > img').attr({'src': 'images/probar-slideshow-2.jpg'});
						$('#slideshow > img').fadeIn(2500, function(){
							//change to pic 2
							$('#slideshow > img').fadeOut(2500, function(){
								$('#slideshow > img').attr({'src': 'images/probar-slideshow-3.jpg'});
								$('#slideshow > img').fadeIn(2500, function(){
									//change to pic 2
									$('#slideshow > img').fadeOut(2500, function(){
										$('#slideshow > img').attr({'src': 'images/probar-slideshow-4.jpg'});
										$('#slideshow > img').fadeIn(2500, function(){
											//change to pic 2
											$('#slideshow > img').fadeOut(2500, function(){
												$('#slideshow > img').attr({'src': 'images/probar-slideshow-5.jpg'});
												$('#slideshow > img').fadeIn(2500, function(){
													//change to pic 2
													$('#slideshow > img').fadeOut(2500, function(){
														$('#slideshow > img').attr({'src': 'images/probar-slideshow-6.jpg'});
														$('#slideshow > img').fadeIn(2500, function(){
															//change to pic 2
															$('#slideshow > img').fadeOut(2500, function(){
																$('#slideshow > img').attr({'src': 'images/probar-slideshow-0.jpg'});
																$('#slideshow > img').fadeIn(2500);
															});
														});
													});
												});
											});
										});
									});
								});
							});
						});
					});
				});
				
			});
	});
	
	// turn galleries into accordions
	$('#gallery').accordion({
		autoHeight: false
	});
	$('#gallery img').addClass('ui-corner-all');
	
	//Create mouse slidable 360 gallery
	var oldx = 1;
	var imgNum = 1;
	
	$('#bar360').mousemove(function(e){
		var x = e.pageX;
			if (x > oldx & imgNum == 55) {
				imgNum = 1;
			}
			if (x > oldx) {
				imgNum += 1;
			}
			if (x < oldx & imgNum == 2) {
				imgNum = 56;
			}
			if (x < oldx) {
				imgNum -= 1;
			}
		$("#bar360img").attr("src", "images/360/portable_liquor_dispenser_"+imgNum+".png");
		oldx = x;
	});
	// end of mouse slideable 360 gallery
	
	// CONTACT FORM JAVASCRIPT
		$('#send_message').click(function(e){
            
            //stop the form from being submitted
            e.preventDefault();
            
            /* declare the variables, var error is the variable that we use on the end
            to determine if there was an error or not */
            var error = false;
            var name = $('#name').val();
            var email = $('#email').val();
            var subject = $('#subject').val();
            var message = $('#message').val();
            
            /* in the next section we do the checking by using VARIABLE.length
            where VARIABLE is the variable we are checking (like name, email),
            length is a javascript function to get the number of characters.
            And as you can see if the num of characters is 0 we set the error
            variable to true and show the name_error div with the fadeIn effect. 
            if it's not 0 then we fadeOut the div( that's if the div is shown and
            the error is fixed it fadesOut. 
            
            The only difference from these checks is the email checking, we have
            email.indexOf('@') which checks if there is @ in the email input field.
            This javascript function will return -1 if no occurence have been found.*/
            if(name.length == 0){
                var error = true;
                $('#name_error').fadeIn(500);
            }else{
                $('#name_error').fadeOut(500);
            }
            if(email.length == 0 || email.indexOf('@') == '-1'){
                var error = true;
                $('#email_error').fadeIn(500);
            }else{
                $('#email_error').fadeOut(500);
            }
            if(subject.length == 0){
                var error = true;
                $('#subject_error').fadeIn(500);
            }else{
                $('#subject_error').fadeOut(500);
            }
            if(message.length == 0){
                var error = true;
                $('#message_error').fadeIn(500);
            }else{
                $('#message_error').fadeOut(500);
            }
            
            //now when the validation is done we check if the error variable is false (no errors)
            if(error == false){
                //disable the submit button to avoid spamming
                //and change the button text to Sending...
                $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
                
                /* using the jquery's post(ajax) function and a lifesaver
                function serialize() which gets all the data from the form
                we submit it to send_email.php */
                $.post("send_email.php", $("#contact_form").serialize(),function(result){
                    //and after the ajax request ends we check the text returned
                    if(result == 'sent'){
                        //if the mail is sent remove the submit paragraph
                         $('#cf_submit_p').remove();
                        //and show the mail success div with fadeIn
                        $('#mail_success').fadeIn(500);
                    }else{
                        //show the mail failed div
                        $('#mail_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#send_message').removeAttr('disabled').attr('value', 'Send The Message');
                    }
                });
            }
        });
	// END CONTACT FORM JAVASCRIPT
	
});








































