/*
Site: gaytravel.com
Page: assets/js/main.js
===========================================================
masugadesign.com | Copyright 2011
===========================================================*/
$(document).ready(function() {



	$(".user").hoverIntent();

	// This handles all sidebar tab groups
	$('.tab-link').click(function(event) {
		if ($(this).parent().hasClass('inactive'))
		{
			var tab_group = $(this).parent().parent().attr('id');
			var tab_to_show = $(this).parent().attr('id').substring(1);
			$('#'+tab_group+' .active').removeClass('active').addClass('inactive');
			$('#'+tab_group+'_section .tab-content').hide();
			$('#'+tab_to_show).show();
			$('#'+tab_to_show+' li').attr('display', 'list-item');
			$(this).parent().removeClass('inactive').addClass('active');
			
		}
		
		return false;
	});
	

	// ryan - not working Nov 04 2011
	//$('.profile-tab-link').click(function() {
	//	if ($(this).parent().hasClass('inactive'))
	//	{
	//		var tab_to_show = $(this).parent().attr('id').substring(1);
	//		$('#profile-tabs .active').removeClass('active').addClass('inactive');
	//		$('#member-tab-section .tab-content').css('display', 'none');
	//		$('#'+tab_to_show).css('display', 'block');
	//		$(this).parent().removeClass('inactive').addClass('active');
	//		return false;
	//	}
	//});

	
	$('.gallery-image-link').click(function(){
		if (!$(this).parent().hasClass('active'))
		{
			var blog_photo_id = $(this).attr('data-image-box-id');
			$(this).parent().siblings().removeClass('active');
			$(this).parent().addClass('active');
			$('#'+blog_photo_id).siblings().fadeOut('fast', function() {
				$('#'+blog_photo_id).fadeIn('fast');
			});
		}
		return false;
	});


	// bg wrapping ad
  if ($('.bgAd a').attr("href"))
  {
  	$('.adRight').css('cursor','pointer');
  	$('.adLeft').css('cursor','pointer');
  
  	$('.bgAdClick').click(function() {
     		window.location = $('.bgAd a').attr("href");
      	return false;
  	});
  }
  else
  {
  	$('.adRight').css('cursor','default');
  	$('.adLeft').css('cursor','default');
  }

	// Nav Menu JS

	// Sets the height of the last destination ul to full height, so long as there
	//	actually is multiple ul's.
	$('#nav div ul').not(':first-child').height('440');
	$('#nav ul').hover(
		function() {
			$(this).closest('li').addClass('active');
		},
		function() {
			$(this).closest('li').removeClass('active');
		}
	);
	
	// Tabifies content tabs, shows up in hotels, cruises, tours.
	
	$("#member-tab-section").tabs();
	$("#member-edit-tab-section").tabs();
	
	$(".content-tabs").tabs();
	
	// Specific to the hotel amenities tab, seperates into 3 columns.
	
	// var $lists = $("#tab-amenities ul"),
	// 		perColumn = Math.ceil($lists.length / 3),
	// 		startColumn = 0,
	// 		endColumn = 0;
	// 	for(i=0; i<3; i++){
	// 		startColumn = i * perColumn;
	// 		endColumn = (i + 1) * perColumn;
	// 		$lists.slice(startColumn, endColumn).wrapAll("<div />");
	// 	}


	  $(document).ready(function() {
	  	$(".fancybox-galleria-link").fancybox({
			height : 550
		});
	  });
// Favorites/Hearts
// --------------------------------------------------------
	
	// Hearts on destination pages
	$('a.show-love').click (function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("id");
		var cleanID = (thisID.substring(5));
		var link2 = '/utils/favorite-count/'+cleanID;
		$(this).hide();
		$('.favorite-status').text('Thanks!');
		$.get(link, function(data) {
			$('.favorite-status').delay(800).fadeOut(1000);
			$.get(link2, function(data) {
				$('.favorite-count').html(data);
			});
		});
		return false;
	});

	// Hearts: remove a trip in 'my trips' tab
	$('a.my-cruise-remove, a.my-tour-remove').click ( function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("data-entryid");
		$(this).replaceWith('<p>Trip removed!</p>');
		$.get(link, function(data) {
			$("#trip"+thisID).delay(500).fadeOut(1000);
		});
		return false;
	});

	
	// Hearts: add on cruise page
	$('a.go-cruise').click (function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("id");
		var cleanID = (thisID.substring(8));
		var link2 = '/utils/favorite-count/'+cleanID;
		$(this).hide();
		$('.cruise-status').text('Cruise added!');
		$.get(link, function(data) {
			$('.cruise-status').delay(800).fadeOut(1000);
			$.get(link2, function(data) {
				$('.cruise-count').html(data);
				$('.remove-link').html('You\'ve hearted this!');
			});
		});
		return false;
	});

	// Hearts: remove on cruise page
	$('a.cruise-remove').click ( function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("id");
		var cleanID = (thisID.substring(8));
		var link2 = '/utils/favorite-count/'+cleanID;
		$(this).parent().hide();
		$('.cruise-status').text('Cruise removed');
		$.get(link, function(data) {
			$('.cruise-status').delay(800).fadeOut(1000);
			//$('.add-link').html('<a href=\"/utils/favorite-action/'+cleanID+'" class=\"go-cruise\" id=\"entry_a_'+cleanID+'\"><em>I want to go!</em></a>');
			$.get(link2, function(data) {
				$('.cruise-count').html(data);
			});
		});
		return false;
	});



	// Hearts: add on tour page
	$('a.go-tour').click (function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("id");
		var cleanID = (thisID.substring(8));
		var link2 = '/utils/favorite-count/'+cleanID;
		$(this).hide();
		$('.tour-status').text('Tour added!');
		$.get(link, function(data) {
			$('.tour-status').delay(800).fadeOut(1000);
			$.get(link2, function(data) {
				$('.tour-count').html(data);
				$('.remove-link').html('You\'ve hearted this!');
			});
		});
		return false;
	});

	// Hearts: remove on tour page
	$('a.tour-remove').click ( function() {
		var link = $(this).attr('href');
		var thisID = $(this).attr("id");
		var cleanID = (thisID.substring(8));
		var link2 = '/utils/favorite-count/'+cleanID;
		$(this).parent().hide();
		$('.tour-status').text('Tour removed');
		$.get(link, function(data) {
			$('.tour-status').delay(800).fadeOut(1000);
			$.get(link2, function(data) {
				$('.tour-count').html(data);
			});
		});
		return false;
	});


	// Delete user photo gallery
	$('a.gallery-delete').click ( function() {
		var answer = confirm("Delete selected gallery?");

		if (answer){
			var link = $(this).attr('href');
			var thisID = $(this).attr("data-entryid");
			$.get(link, function(data) {
				$(this).replaceWith('Removed');
				$("#galleryrow"+thisID).delay(500).fadeOut(1000);
			});
		}
		return false;
	});

	// set all input.clearout default values according to data-default attribute
	$("input.clearout").each(function(){
		$(this).val( $(this).attr("data-default") );
	});

	// clear input.clearout on focus, if still in default
	$("input.clearout").focus(function() {
		if($(this).val()==$(this).attr("data-default")) {
			$(this).val("");
		}
	});

	// if field is empty afterward, add text again
	$("input.clearout").blur(function() {
		if($(this).val()=="") {
			$(this).val($(this).attr("data-default"));
		}
	});
	
	$('.search-box').submit(function() {
		var location = $(this).attr('data-location');
		var search_string = $('#search-box-'+location).val();
		if (search_string == '')
			return false;
	});
	
	// Columnize Elements
	//  Destinations pages
	$("#destination-list li").columnize({
		columns: 4,
		removeParent: true
	});
	// Amenities tab
	$("#tab-amenities ul").columnize({
		columns: 3,
		wrap: '<div />'
	});
	
	$('#filter-form').submit(function(event) {
		var filter_type = $(this).attr('data-filter-type');
		drillDownFilter(event, filter_type);
	});

	// advertiser login modal
	$('a.advertlogin').zoombox({
       theme       : 'zoombox', //available themes : zoombox,lightbox, prettyphoto, darkprettyphoto, simple
       opacity     : 0.8,       // Black overlay opacity
       duration    : 800,       // Animation duration
       animation   : false,     // Do we have to animate the box ?
       width       : 600,       // Default width
       height      : 600        // Default height
   });

	
}); /* =================================================> close doc ready */

/*	Columnizer Plugin
	Author: Jacob Russell, Masuga Design
	Date: Nov 21, 2011
	
	Use it on the elements you wish to split into groups.  In the case of a list,
	this would be the li's, not the ul.

	Settings:
		columns: number of groups you want to split the elements into
		wrap: what element should wrap each group (default 'ul') as a self closed tag (i.e. '<ul />')
		removeParent: true to remove the parent, false to keep it (default false)
*/

(function( $ ){
	$.fn.columnize = function(options) {
	
		// Settings
  		var settings = $.extend( {
			'columns'	: 2,
			'wrap'	: '<ul />',
			'removeParent'	: false
		}, options);
		
		// Base Variables
		var perColumn = Math.ceil(this.length / settings.columns),
			startColumn = 0,
			endColumn = 0;
		
		// Remove wrapping element	
		if(settings.removeParent){
			this.unwrap();
		}
		
		// Wrap in groups
		for(i=0; i<settings.columns; i++){
			startColumn = i * perColumn;
			endColumn = (i + 1) * perColumn;
			this.slice(startColumn, endColumn).wrapAll(settings.wrap);
		}

		return this;
  	};
})( jQuery );

// Filter for Plan Section
function drillDownFilter(event, type) {
	
	event.preventDefault();
	// Set variables
	var linkKind = $("#kind").val();
	var linkDest = $("#country").val();
	var linkDestNarrow = $("#country-drilldown").val();
	var linkSection = "/" + $("#section-filter").val();
	
	// Decide which destination we're using
	if(linkDestNarrow){ linkDest = linkDestNarrow; }
	
	// Add slashes
	if(linkKind){ linkKind = "/" + linkKind; }
	if(linkDest){ linkDest = "/" + linkDest; }
	
	// Build the URL
	if(type=="members")
		var linkURL = linkSection + linkKind + linkDest;
	else
		var linkURL = linkDest + linkSection + linkKind;
	window.location.href = linkURL;
}



/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);

/**
* MAIN IMAGE CAROUSELS ON SINGLE ENTRY HOTELS/TOURS/CRUISES/INDEX PAGES
*/

function mycarousel_initCallback(carousel) {
    jQuery('.switcher-link').live('click', function() {
	    carousel.startAuto(0);
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		  $('.switcher span.active').each(function() {
				$(this).replaceWith('<a id="switcher-link-'+$(this).html()+'" class="switcher-link" href="#" >'+$(this).html()+'</a>');
		  });
		  $(this).replaceWith('<span id="switcher-link-'+$(this).html()+'" class="active" >'+$(this).html()+'</span>');
        return false;
    });

    jQuery('#main-carousel-next').bind('click', function() {
	    carousel.startAuto(0);
        carousel.next();
        return false;
    });

    jQuery('#main-carousel-prev').bind('click', function() {
	    carousel.startAuto(0);
        carousel.prev();
        return false;
    });
}

function switcherStyleChange(mycarousel, li_ob, position, state)
{
	$('.switcher span.active').each(function() {
			$(this).replaceWith('<a id="switcher-link-'+$(this).html()+'" class="switcher-link" href="#" >'+$(this).html()+'</a>');
	  });
	  $('#switcher-link-'+position).replaceWith('<span id="switcher-link-'+position+'" class="active" >'+$('#switcher-link-'+position).html()+'</span>');
}


// Ride the carousel...
$(document).ready(function() {
    $("#main-image-carousel").jcarousel({
	    auto: 3,
	    wrap: 'last',
        scroll: 1,
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
		itemVisibleInCallback: switcherStyleChange
    });
});


// Add/Confirm a Friend AJAX
$(document).ready(function() {
	
	$('.add-friend').click(function() {
		var href = $(this).attr('href');
		var add_link = $(this);
		$.ajax(href, {
			dataType : "html",
			type: "GET",
			success : function(data) {
				$('.add-friend').replaceWith('<span>'+data+'</span>');
			},
			error : function(data) {
				$('.add-friend').replaceWith('<span>'+data+'</span>');
			}
		});	
		return false;
	});
	
	$('.confirm-friend').click(function() {
		var href = $(this).attr('href');
		var id   = $(this).attr('id');
		$.ajax(href, {
			dataType : "html",
			type: "GET",
			context: $('#'+id),
			success : function(data) {
				$(this).parent().parent().replaceWith('<li>'+data+'</li>');
			},
			error : function(data) {
				$(this).parent().parent().replaceWith('<li>'+data+'</li>');
			}
		});	
		return false;
	});
});



