jQuery(document).ready(function(){

	jQuery("li.tabs").tabs("li div.panes");
	jQuery("ul#tabshp").tabs("div#panes > div.panes > ul");
	
	jQuery('#picklist > a.button').click(function(){
		return false;
	});
	
	jQuery.ajaxSetup({ 
		headers: {"X-Requested-With":"XmlHttpRequest"} 
	});

	api = jQuery("#slide").scrollable({ 
		size: 5, 
	 	activeClass: 'hover',
	 	hoverClass: 'active',
		api: true
	});
	
	if(location.href.match(/\.com\/$|\.net\/$/))
	{
		activateLinks();
	}
	
	jQuery('a.topdestlist').click(function(){
		
		// Change H2 text
		h2text = 'Top 10 ' + jQuery(this).attr('title') + ' Destinations';
		jQuery('#top10 > h2').text(h2text);
		
		// Hide Types list
		jQuery('#picklist').css({
			'overflow' : 'hidden',
			'height' : '33px'
		});

		// rewind the scrollable container
		api.begin();

		jQuery.ajax({
			type: "GET",
			url: jQuery(this).attr('href'),
			data: "",
			dataType: "json",
			success: function(json){
			//	console.log(this.url);
				jQuery('#thumbs').html('');
			//	alert( "Data: " + json );
				counter = 0;
				jQuery.each(json, function(i, item){
					counter++;
					element = populateDestinations(counter, item.destination, item.images[0], item.container);
				//	console.log(element);
					jQuery('#thumbs').append(element);
				});

				// Re-activate links
				activateLinks();
				api.reload();
			}
		});

		return false;
	});

	jQuery('#picklist').hover(
		function(){
			jQuery('#picklist').css({
				'overflow' : 'visible',
				'height' : 'auto'
			})
		},
		function(){
			jQuery('#picklist').css({
				'overflow' : 'hidden',
				'height' : '33px'
			})
		}
	);

	jQuery('li.topNavDropDown').hover(
		function(){
			jQuery(this).css({
				'overflow' : 'visible',
				'height' : 'auto'
			})
		},
		function(){
			jQuery(this).css({
				'overflow' : 'hidden',
				'height' : '33px'
			})
		}
	);
});

function activateLinks()
{
	jQuery('#thumbs > div').click(function(){
		jQuery.ajax({
			type: "GET",
			url: "/destination/" + jQuery(this).attr('title') + '/' + jQuery(this).attr('rel'),
			data: "",
			dataType: "html",
			success: function(html){
			//	alert( "Data: " + html );
				jQuery('#destinationdata').html(html);
			}
		});
	});

	first = jQuery("#thumbs > div").eq(0);
	loadFirst(first.attr('title'), first.attr('rel'));
	
}

function loadFirst(destination, container)
{
		
		jQuery.ajax({
			type: "GET",
			url: "/destination/" + destination + '/' + container,
			data: "",
			dataType: "html",
			success: function(html){
			//	alert( "Data: " + html );
				jQuery('#destinationdata').html(html);
			}
		});
}

function populateDestinations(i, name, image, container)
{
	cssClass = (i == 1)? 'hover' : null;
	return jQuery('<div title="' + name + '" rel="' + container + '" class="' + cssClass + '"><div class="topdestimage"><img src="/assets/' + image + '" /></div><h3><em>' + i + '. </em>' + name + '</h3></div>');
}
