
$(document).ready(function() {

	 /**	PNG Fix! IE6... will the pain ever stop.
	 -----------------------------------------------------------------------------------------------*/
	DD_belatedPNG.fix('#store-title-container, #regular-gas-container, #plus-gas-container, #supreme-gas-container, #diesel-gas-container, #store-title-bottom, #store-logo, .more-btn');
	
	/**	JQ Corners
	 -----------------------------------------------------------------------------------------------*/
	$('.store-container-content, #content-container, .white-container, .white-w-border-container, .pink-container, .blue-container, #footer').corner('10px');
	$('.content-tab-active, .content-tab, .header-tab, .header-tab-active, #header-search').corner('10px top');
	$('.tabbed-content-container').corner('10px bottom');
	$('#footer-logo').corner('4px');

	 /** 	Coupons
	 -----------------------------------------------------------------------------------------------*/
	$('#coupon1').css("background-image", "url(files/0/good-gas-banner.gif)");
	$('#coupon1').click(function(){ getURL("http://www.fuelinggood.com/guarantee") });

	/**	Email update window animation
	 -----------------------------------------------------------------------------------------------*/
	$('.get-email-updates').click(function() {
		$('#email-pop-up').fadeIn('fast');
	});
	$('#email-pop-up-close').click(function() {
		$('#email-pop-up').fadeOut('fast');
	});

	 /**	Email list sign up per store
	 -----------------------------------------------------------------------------------------------*/
	$('form[name=email-list-sign-up]').submit(function(){
		
		var errors	= false,
			msg  	= new String("<h1>Get Email Updates</h1><p>Please correct the following errors</p><ul>"),
			valid 	= new Boolean();
	
		if($('#first_name').val() == "") {
			errors 	= true;
			msg 	+= "<li>First name is required</li>";
		}
		
		if($('#last_name').val() == "") {
			errors 	= true;
			msg 	+= "<li>Last name is required</li>";
		}
		
		if($('#email').val() == "") {
			errors = true;
			msg 	+= "<li>Email address is required</li>";
		} else {
			if ($.validateEmail($('#email').val()) == false) {
				errors 	= true;
				msg 	+= "<li>Email address is not valid</li>";
			}
		}
		
		msg 		+= "</ul>";
		
		if (errors == true) {
			$.prompt(msg);  
			return false;
		}

		$.ajax({ 
			type: "post",
			url: "ajax/email-list-sign-up.php", 
			data: $('form[name=email-list-sign-up]').serialize(),
			dataType: "json",
			success: function(data){
				$('#email-pop-up').fadeOut('fast');
				if (data.status == 'success')
					$.prompt('<h1>Get Email Updates</h1>You have successfully signed up for email updates and special offers from this location');
				else if (data.status == 'exist')
					$.prompt('<h1>Get Email Updates</h1>The email address provided is already associated with this locations email updates and special offers list');
			}
		});

		return false;
	});

	/**	Employment Positions
	 -----------------------------------------------------------------------------------------------*/
	$('.position').click(function(){ 
		$('input[name=position_id]').val($(this).attr('value'));	
	});

	 /** Get clicked employment position.
	 -----------------------------------------------------------------------------------------------*/
	$("a[href=#get-position]").fancybox({
		'titlePosition'		: 'none',
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000000',
		'onStart'	 		: function(){
			$.ajax({ 
				type: "post",
				url: "ajax/get-position.php", 
				data: { 'id': $('input[name=position_id]').val() },
				dataType: "json",
				success: function(data){
					//set record data
					$('#pop-up-title').html('Position: ' + data.position);
					$('#position-title').html(data.position);
					$('#position-description').html(data.description);
					$('#position-compensation').html(data.compensation);
					//Set hidden input position_id
					$('input[name=position_id]').val(data.id);
				}
			});
		}
	});

	 /** Apply Online
	 -----------------------------------------------------------------------------------------------*/
	$('form[name=apply]').submit(function(){
		
		var errors	= false,
			msg  	= new String("<h1>Employment Position</h1><p>Please correct the following errors</p><ul>"),
			valid 	= new Boolean();
	
		if($('input[name=apply_name]').val() == "") {
			errors 	= true;
			msg 	+= "<li>Full Name is required</li>";
		}
		
		if($('input[name=apply_email]').val() == "") {
			errors = true;
			msg 	+= "<li>Email address is required</li>";
		} else {
			if ($.validateEmail($('input[name=apply_email]').val()) == false) {
				errors 	= true;
				msg 	+= "<li>Email address is not valid</li>";
			}
		}
		
		if($('textarea[name=apply_message]').val() == "") {
			errors 	= true;
			msg 	+= "<li>Message / Text Resume is required</li>";
		}
		
		msg 		+= "</ul>";

		if (errors == true) {
			$.prompt(msg);  
			return false;
		}

		$.ajax({ 
			type: "post",
			url: "ajax/apply-online.php", 
			data: $('form[name=apply]').serialize(),
			dataType: "html",
			success: function(data){
				//Close box
				$.fancybox.close();

				//Empty form fields
				$('input[name=apply_name]').val('');
				$('input[name=apply_email]').val('');
				$('textarea[name=apply_message]').val('');

				if (data.status == 'success')
					$.prompt('<h1>Employment Position</h1>Your application has been submitted to the appropriate personel.');
				else if (data.status == 'failed')
					$.prompt('<h1>Employment Position</h1>An error occured while submitting your application. Please try again.');
			}
		});

		return false;
	});	

	/**	Store Photo's Slide Show
	 -----------------------------------------------------------------------------------------------*/
	$("#store-photo").cycle( { pause: 1, speed:  1800 });

	/**	About us, Maps, Specials & Employment tabs
	 -----------------------------------------------------------------------------------------------*/
	$('#about-us-tab').click(	function(){ $.swapTabs('about') });
	$('#map-tab').click(		function(){ $.swapTabs('map') });
	$('#specials-tab').click(	function(){ $.swapTabs('specials') });
	$('#employment-tab').click(	function(){ $.swapTabs('employment') });
	
	$.swapTabs = function( tab ){
		$('#about-us-tab, #map-tab, #specials-tab, #employment-tab').removeClass('content-tab, content-tab-active');
		$('#about-us-tab, #map-tab, #specials-tab, #employment-tab').addClass('content-tab');
		$('#about-us-content, #map-content, #specials-content, #employment-content').hide();
		
		if (tab == "about") {
			$('#about-us-tab').addClass('content-tab-active');
			$('#about-us-content').fadeIn('slow');
		}
			
		if (tab == "map") {
			$('#map-tab').addClass('content-tab-active');
			$('#map-content').fadeIn('slow');
			
			$.getMap( $('[name=latitude]').val(), $('[name=longitude]').val(), 'map');
		}
			
		if (tab == "specials") {
			$('#specials-tab').addClass('content-tab-active');
			$('#specials-content').fadeIn('slow');
		}
			
		if (tab == "employment") {
			$('#employment-tab').addClass('content-tab-active');
			$('#employment-content').fadeIn('slow');
		}
			
	}
	
	//Show specials over about us if exists
	if (specials == true)
		$.swapTabs('specials');

	 /** Google Maps
	 -----------------------------------------------------------------------------------------------*/
	var geocoder,
		location,
		status,
		point,
		directionsDisplay 	= new google.maps.DirectionsRenderer(),
		directionsService 	= new google.maps.DirectionsService(),
		geocoder 			= new google.maps.Geocoder();
	
	 /** Google Maps - Render map
	 -----------------------------------------------------------------------------------------------*/
	$.getMap = function( latitude, longitude, target) {

		point 	= $.getLatLong( latitude, longitude );

		var options = {
			zoom: 8,
			center: point,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
		};

		//Get map
		var map  	= new google.maps.Map( document.getElementById(target), options );

		//Set directions
		directionsDisplay.setMap(map);
	    directionsDisplay.setPanel(document.getElementById("driving-directions"));

		//Add marker
		var marker 	= new google.maps.Marker({
			position: point,
			map: map,
			icon: BASE_IMAGE_URL + "marker.png"
		});

		//Define info window
		var infowindow = new google.maps.InfoWindow({
			content: $('[name=full-address]').val()
		});

		//Listener for on click marker
		google.maps.event.addListener(marker, 'click', function() {
      		infowindow.open(map,marker);
    	});

	};

	$.getDirections = function( address ) {

		//Set end point to address
		var end = address;

		var request = {
			origin:end, 
			destination:point,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};

		directionsService.route(request, function(response, status) {
		  if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		  }
		});

	};

	$.getLatLong 	= function( latitude, longitude ) {
	
		var point = new google.maps.LatLng( latitude, longitude );

		return point;
	}

	 /** Launch Get Driving Directions Fancy Box form
	 -----------------------------------------------------------------------------------------------*/
	$("a[href=#get-directions]").fancybox({
		'titlePosition'		: 'none',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayColor'		: '#000000'
	});

	 /** Call map into fancy box... 
	 -----------------------------------------------------------------------------------------------*/
	$("a[href=#get-directions]").click(function(){
		$.getMap( $('[name=latitude]').val(), $('[name=longitude]').val(), 'directions-map');	
	})

	 /** Get Directions From Google Maps and Display
	 -----------------------------------------------------------------------------------------------*/
	$('form[name=submit-directions]').submit(function(){
										
		if ($('#from_address').val() == "") {
			$.prompt('<h1>Get Directions</h1><p>Please correct the following errors: </p><ul><li>From location is required</li></ul>'); 
			return false;
		}

		//Erase driving directions
		$('#driving-directions').html('');

		//Get Directions
		$.getDirections( $('#from_address').val() )
		$('#driving-directions').append('<div id="print-map" onclick="printMap()">Click Here To Print</div>');
		$('#driving-directions').fadeIn();

		return false;

	});

	 /** Survey Functionality
	 -----------------------------------------------------------------------------------------------*/
	var answer1 	= new String(),
		answer2 	= new String(),
		answer3 	= new String();

	$('#next1, #next2, #next3').attr("disabled","disabled");
	$('#next1, #next2, #next3').animate({ opacity: .5 });
	
	//Group1
	$('[name=group1]').click(function(){
		   $('#next1').animate({ opacity: 1.0 }); 
		   $('#next1').removeAttr("disabled");
		   answer1 	= $(this).val();
	});
	$('#next1').click(function(){
		$('#question1').hide();
		$('#question2').show();
	});

	//Group2
	$('[name=group2]').click(function(){
		   $('#next2').animate({ opacity: 1.0 }); 
		   $('#next2').removeAttr("disabled");
		   answer2 	= $(this).val();
	});
	$('#next2').click(function(){
		$('#question2').hide();
		$('#question3').show();
	});

	//Group3
	$('[name=group3]').click(function(){
		   $('#next3').animate({ opacity: 1.0 }); 
		   $('#next3').removeAttr("disabled");
		   answer3 	= $(this).val();
	});
	$('#next3').click(function(){
		$('#question3').hide();
		$('#question4').show();
	});

	$('[name=pnumber]').mask("(999) 999-9999");

	$('form[name=survey]').submit(function(){

		if ($(this).validationEngine({ returnIsValid:true, inlineValidation:false, validationEventTriggers:'keyup blur' }) == false)
			return false;

		$.ajax({ 
			type: "post",
			url: "ajax/submit-survey.php", 
			data: { '1' : answer1, '2' : answer2, '3' : answer3, '4' : $('[name=comments]').val(), 'store_id' : $('[name=store_id]').val(), 'first_name' : $('[name=fname]').val(), 'last_name' : $('[name=lname]').val(), 'phone_number' : $('[name=pnumber]').val(), 'email' : $('[name=eaddress]').val() },
			dataType: "json",
			success: function(data){
				if (data.status == 'success')
					$.prompt('<h1>Give Us Your Feedback</h1>Your feedback has been received. We appreciate your interest.');
				else if (data.status == 'failed')
					$.prompt('<h1>Give Us Your Feedback</h1>An error occured while submitting your feedback.');
				
				//reset form
				$('form[name=survey]')[0].reset();
				$('#question4').hide();
				$('#thank-you').show();
				
			}
		});

		return false;

	});

	 /** Privacy Policy FancyBox
	 -----------------------------------------------------------------------------------------------*/
	$("#privacy-policy, #terms-of-use").fancybox({
		'transitionIn'		:	'swing',
		'transitionOut'		:	'swing',
		'titlePosition'		:	'inside',
		'centerOnScroll'	: 	true,
		'autoDimensions'	:	false,
		'width'				: 	800,
		'height'			: 	490
	});
	$("#disclaimer, #help").fancybox({
		'transitionIn'		:	'swing',
		'transitionOut'		:	'swing',
		'titlePosition'		:	'inside',
		'centerOnScroll'	: 	true,
		'autoDimensions'	:	false,
		'width'				: 	600,
		'height'			: 	90
	});


});

/** Get URL Coupons and Community Events
-----------------------------------------------------------------------------------------------*/
function getURL( url ) {
	window.open(url);
}

/** Launch Google maps Print Map
-----------------------------------------------------------------------------------------------*/
function printMap() {
	window.open('http://maps.google.com/maps?f=d&source=s_d&hl=en&geocode=true&mra=ls&ie=UTF8&z=12&pw=2&saddr=' + $('#from_address').val() + '&daddr=' + $('#address').val() + '&sll=&sspn=');
}



