/*
	Global JS Functions (jQuery)
	Version:		1.0
	Created:		2011-03-17
	Auth:			Andrew Hamel | 6ft LLC
*/

$(document).ready(function() {
    
	var page;
	var index = 0;
	var home_banners = new Array(
		{ title:'Welcome', head:'To The Grand Mayan', subhead:'Luxury, Inspired by Ancient Culture.', img:'images/home_banner1.jpg' },
		{ title:'Adventures', head:'In Absolute Luxury', subhead:'On Mexico\'s Most Exotic Beaches.', img:'images/home_banner2.jpg' },
		{ title:'Experience', head:'Paradise Perfected', subhead:'Find Your True Calling In The Lap Of Luxury.', img:'images/home_banner3.jpg' }
	);
	
	function nextBanner(){
		index = index + 1 > (home_banners.length - 1) ? 0 : index + 1;
		$('#banner_holder').fadeOut(750, loadBanner);
		//$('#debug').html('Index: ' + index + '<br>');
	}
	
	/*function loadBanner(){
		var o = home_banners[index];
		$('#banner').attr('src', o.img );
		$('#bancop-lrg').html( o.title );
		$('#bancop-orng-bg').html( o.head );
		$('#bancop-tan').html( o.subhead );
		$.ajax({
		  url: o.img,
		  success: function(){ showBanner(); }
		});
	}*/
	
	function loadBanner(){
		var o = home_banners[index];
		$('#banner').attr('src', o.img );
		$('#bancop-lrg').html( o.title );
		$('#bancop-orng-bg').html( o.head );
		$('#bancop-tan').html( o.subhead );
		//$('#debug').append('Load image ' + o.img + '<br>');
		$.ajax({
		  url: "images/banners.html",
		  cache: false,
		  error: function(xhr, err){
			  //$('#debug').append('File load error occurred?<br>readyState: ' + xhr.readyState + ' status: ' + xhr.status + ' err: ' + err);
		  },
		  success: function(){ showBanner(); }
		});
	}
	
	function showBanner(){
		$('#banner_holder').fadeIn(750, function(){ setTimeout(nextBanner, 8000); });
	}
	
	$('#lang').click(function(){
		var loc = '';
		var lang = $(this).attr('rel');
		var curlocarr = window.location.pathname.split("/");
		var curloc = curlocarr.pop();
		if(lang=="sp")
			loc = 'http://thegrandmayan.com/sp/' + curloc; // http://www.thegrandmayan.com.mx
		else
			loc = 'http://thegrandmayan.com/' + curloc;
		document.location = loc;
	});
	
	// kick things off
	setTimeout(nextBanner, 6000);
	
	$('.micon').click(function () {
		var iurl = $(this).attr("rel");
		var ititle = $(this).children("img").attr("alt");
		$("#imgBanner").attr("src", iurl);
		$("#imgtitle").html(ititle);
	});
	
	if( typeof(jcarousel) != "undefined" ){
		$('#mycarousel').jcarousel({
			wrap: 'circular'
		});
	}
	
	$("a.mthumb").each(function() {
		$(this).click(function () {
			var mainImage = $(this).attr("rel");
			var $con = $(this).parent().parent().parent().parent();
			$con.children('img').attr("src", mainImage);
		});
	});
	
	$("li.close").click(function(){
		$(this).toggleClass("open");
		return false;
	});
	
});


