$(document).ready(function(){
	
	// DROPDOWNS
	$('.sf-navbar').superfish({
		autoArrows:  false,
		delay:       200,
        speed:       0
	});

	// HOVER
	$(".sf-navbar > li, .hover").hover( 
		// over
		function(){
			var theImg = $(this).find('img:first');
			// store the original image src
			theImg.data( "originalsrc" , theImg.attr("src") );
			
			// add -over to the image unless it is already -over
			if (!theImg.attr("src").match(/-over/) ) {
				theImg.attr( "src", theImg.attr("src").replace(/\./, "-over.") );
			}
			
		},
		
		// out
		function(){
			var theImg = $(this).find('img:first');
			// replace the original src
			if (theImg.data("originalsrc") != null ) {
				theImg.attr("src", theImg.data("originalsrc")).data("originalsrc", null);
			}
		}
	);

	// TABS
	$('.tabnav li a').click(function(e){
		e.preventDefault();
		var theTab = $(this);
		var theDiv = theTab.attr('href').substring(1);
				
		theTab.closest('.tabnav').find('a').removeClass('active');
		theTab.addClass('active');
		
		theTab.closest('.tabs').find('.tabPanel').hide();
		$('#' + theDiv).show();
		
	});
	
	
	// showRefine
	
	$("#showRefine, .showRefine").click(function(e){
		e.preventDefault();
		$("#refineBox").slideToggle();
	});

	
	// turn off caching
	$.ajaxSetup( {cache : false} );
	
	// ADD TO SUITCASE
    $(".addSuitcase").click(function(e){
		e.preventDefault();
		$(this).nextAll('.suitcaseCommentForm').slideToggle();
		
    });	
	
	
	// SAVE TO SUITCASE
	$(".suitcaseForm").submit(function(e){
		e.preventDefault;
		
		$this = $(this);
		var listingId = $this.find('input[name=listing_id]').attr('value');
		var params = $this.serialize();
		
		$.post(
			"/cgi-bin/listings/suitcase/suitcase.cgi", 
			'todo=add&' + params,
		   	function(data){
				if (data < 0) {
					alert("There was an error saving to your suitcase");
				} else {
					$this.parents('.suitcaseButtonContainer').find('.addSuitcase').hide();
					$this.parents('.suitcaseButtonContainer').find('.removeSuitcase').show();
					$('#suitcaseCount').html(data);
				}
			
				$this.parents('.suitcaseCommentForm').slideUp();

				// track it
				var name = $this.parents('.listing').find(".listingName").html() + " (" +  listingId + ")";
				trackEvent('Interact With Businesses', 'Add to Suitcase', name, null);
				
			});
		return false;
	});
	
	// REMOVE FROM SUITCASE
    $(".removeSuitcase").click(function(e){
		e.preventDefault();
		$this = $(this);
		var listingId = $this.attr('href').substring(1);
		
		$.post(
			"/cgi-bin/listings/suitcase/suitcase.cgi", 
			'todo=delete&listing_id=' + listingId,
		   	function(data){
				if (data < 0) {
					alert("There was an error deleting from your suitcase");
				} else {
					// swap button
					$suitcaseButtonContainer = $this.parents('.suitcaseButtonContainer');
					$suitcaseButtonContainer.find('.removeSuitcase').hide();
					$suitcaseButtonContainer.find('.addSuitcase').show();
					
					// remove comments
					$listing = $this.parents('.listing');
					$suitcaseComment = $listing.find('.suitcaseComment');
					$suitcaseComment.slideUp('fast', function(){
						$suitcaseComment.remove();
					});
					
					// if in suitcase view
					if ($("#suitcase").length) {
						// remove listing from page
						$listing.slideUp('fast', function(){
							$listing.remove();
						});
						// remove from map
						map.removeOverlay(gmarkers[listingId]);
					}
					
					$('#suitcaseCount').html(data);
				}
							
			});
	});	

	$(".cancelSuitcase").click(function(e){
		e.preventDefault();
		$(this).parents('.suitcaseCommentForm').slideUp();
	});
	
	
	// OPEN EMAIL SUITCASE
    $(".emailSuitcase").click(function(e){
		e.preventDefault();
		$(this).nextAll('.suitcaseEmailFormContainer').slideToggle();
	
    });	
	
	// EMAIL SUITCASE
	$(".suitcaseEmailForm").submit(function(e){
		e.preventDefault;
		
		$this = $(this);
		
		if (!$this.find('input[name=to]').attr('value')) {
			alert("Recipient's Email is required");
			return false;
		}
		
		if (!$this.find('input[name=from_email]').attr('value')) {
			alert("Your Email is required");
			return false;
		}
		
		if (!$this.find('input[name=from_name]').attr('value')) {
			alert("Your Name is required");
			return false;
		}
		
		
		var params = $this.serialize();
		
		$.post(
			"/cgi-bin/listings/suitcase/email_suitcase.cgi", 
			params,
		   	function(data){
				var $parent = $this.parents('.suitcaseEmailFormContainer');
				if (data < 0) {
					alert("There was an error emailing to your suitcase");
					$parent.slideUp();
				} else {				
					$parent.slideUp();
					$parent.after('<div id="emailSent" style="display : none; color : #5C8727; position : absolute;">Email has been sent.</div>');
					$('#emailSent').fadeIn();
					
					setTimeout(function(){
						$("#emailSent").fadeOut('slow',function(){
							$("#emailSent").remove();
						});
					}, 5000);
					
				}
				// track it
				trackEvent('Interact With Businesses', 'Email Suitcase', null, null);
				
			});
		return false;
	});
	
	
	// CANCEL EMAIL SUITCASE
	$(".cancelSuitcaseEmail").click(function(e){
		e.preventDefault();
		$(this).parents('.suitcaseEmailFormContainer').slideUp();
	});
	
	
	// SUITCASE SIDEBAR TEASER
	var flashvars = {};
	var params = {
		  bgcolor : "#578699",
		  wmode: 'opaque'
	};
	var attributes = false;
	swfobject.embedSWF("/flash/tripplanner.swf", "suitcaseBanner", "125", "45", "6.0.0", "", flashvars, params, attributes);
	

	
	
});





window.onload = function() {
	// PRELOAD

	$(".sf-navbar > li, .hover").each(
		function() {
			var theImg = $(this).find('img:first');
			if (!theImg.attr("src").match(/-over/) ) {
				$("<img>").attr( "src", theImg.attr("src").replace(/\./, "-over.") );
			}
		}
	);

}

function track(url) {
    firstTracker._trackPageview(url);
    secondTracker._trackPageview(url);
}

function trackEvent(category, action, optional_label, optional_value) {
	firstTracker._trackEvent(category, action, optional_label, optional_value);
    secondTracker._trackEvent(category, action, optional_label, optional_value);
}
