/**
 *		Main script of tigg
 *
 *
*/
var t = n = count = 0;
var jcar = null;
var pcount = 0;
var pidx = 0;
var parsed = [];
var promos_timeout = 4000;
var promos_timer = 0;
var cufon_enabled = true;
var webkit_booknow_fixed = false;
var booknow_first_5_reached = false;
	
function show_auto(auto)
{
	if (jcar.animating==false) {
		auto = auto ? auto : 0;
		n = n>=(count-1) ? 0 : ++n;

		$('#numlist li').eq(n).trigger('click');
	}
}

/**
 *	Cufon fonts fix
 *
 */
function fix_fonts()
{
	Cufon.set('fontFamily', 'Trade Gothic LT STD');
	Cufon.replace('.trade_gothic', {
		fontStyle: 'Trade Gothic LT Std',
		hover: true
	});
	
	$('.listItem dt a span').hover(function(){
		Cufon.set('fontFamily', 'Trade Gothic LT STD').replace($(this), {
			fontStyle: 'Trade Gothic LT Std',
			color: '#333'
		});	
	}, function(){
		Cufon.set('fontFamily', 'Trade Gothic LT STD').replace($(this), {
			fontStyle: 'Trade Gothic LT Std',
			color: '#fff'
		});
	});
	
	Cufon.replace('.trade_gothic_bold', {
		fontStyle: 'Trade Gothic LT Std',
		hover: true
	});	
	
	Cufon.set('fontFamily', 'Modern');
	Cufon.replace('.modern', {
		fontStyle: 'Modern'
	});
	
	Cufon.set('fontFamily', 'copper');
	Cufon.replace('.copper', {
		fontStyle: 'copper',
		fontSize: '12px'
	});

	Cufon.set('fontFamily', 'MinionPro');
	Cufon.replace('.minion_pro', {
		fontStyle: 'MinionPro'
	});	
	
	Cufon.replace('.minion_pro_photo_gallery', {
		fontStyle: 'MinionPro',
		fontSize: '16px'
	});
	
	if (!$.browser.msie) {
		Cufon.replace('#newslist li h1:first', {
			fontStyle: 'MinionPro'
		});
		parsed.push('0');
	} else {
		Cufon.replace('.minion_pro_off', {
			fontStyle: 'MinionPro',
			fontSize: '21px'
		});
		parsed.push('0');
		Cufon.now();
	}
}

$(function(){

	//	fonts fix start
	if (cufon_enabled) {
		fix_fonts();
	}
	// fonts fix end
	
	//	reset some links target
	$('a.blank').attr('target', '_blank');
	
	//	follow us start
	$('#follow_us_twitter').mouseover(function(){
		$('#follow_us_twitter_img').attr('src', assets+'images/twitter_on.png');
	}).mouseout(function(){
		$('#follow_us_twitter_img').attr('src', assets+'images/twitter.png');
	});
	
	$('#follow_us_facebook').mouseover(function(){
		$('#follow_us_facebook_img').attr('src', assets+'images/facebook_on.png');
	}).mouseout(function(){
		$('#follow_us_facebook_img').attr('src', assets+'images/facebook.png');
	});	
	
	//	follow us end
	
	//	read more
	$('a.read_more').click(function(){
		aid = $(this).attr('id');
		key = aid.replace('read_more_', '');
		content_div = $('#read_more_content_'+key).slideToggle('fast');
		$(this).toggleClass('read_more').toggleClass('read_more_opened');
		
		txt = $(this).text();
		if (txt=='READ MORE +') {
			$(this).text('READ LESS -');
		} else {
			$(this).text('READ MORE +');
		}

		return false;
	});
	
	//	set logo of left part
	$('div.logo').click(function(){
		window.location = root_url;
	});

   	//	left book now start
	count = $('#newslist li').length;
	$("#newslist a:not(:first-child)").hide(); 
	
	$('#numlist li').click(function(e){
		clearInterval(promos_timer);
		if (jcar.animating==false) {
			flag = e.clientX ? true : false;
			
			n = $(this).text()-1;
			zz = n+1;
			
			if (zz==5 && !booknow_first_5_reached) {
				booknow_first_5_reached = true;
			}
			
			jcar.unlock();
			if (!flag) {
				jcar.next();
			} else {
				jidx = $.jcarousel.intval(zz);
				jcar.scroll(jidx);
			}
			jcar.lock();
			
			key = jcar.first
			if ($.browser.msie) {
				idx = key % count;
				
				$('#newslist').css('margin-bottom', '-10px');
				if (booknow_first_5_reached) {
					$('#booknow_pagetotal').css('margin-top', '2px');
				}				
			} else {
				idx = key;
				
				if (booknow_first_5_reached) {
					$('#booknow_pagetotal').css('margin-top', '9px');
				}
				$('#newslist').css('margin-bottom', '-17px');
			}

			$('#btn_book_now').text($('li [index="'+key+'"]').attr('button_text'));
			
			if (!$.browser.msie && $.inArray(idx,parsed)<0) {
				if (cufon_enabled) {
					Cufon.set('fontFamily', 'MinionPro').replace('#newslist li[jcarouselindex="'+idx+'"] h1', {
						fontStyle: 'MinionPro',
						fontSize: '21px'
					});
					Cufon.now();
				}
				parsed.push(idx);
			} else {
				text = $('#newslist li[jcarouselindex="'+key+'"] h1').text();
				$('#newslist li[jcarouselindex="'+key+'"] h1').empty().text(text);
				if (cufon_enabled) {
					Cufon.set('fontFamily', 'MinionPro').replace('#newslist li[jcarouselindex="'+key+'"] h1', {
						fontStyle: 'MinionPro',
						fontSize: '21px'
					});
					Cufon.now();					
					$('#newslist li h1 cufontext').css('font-size', '22px');
				}
			}
			
			$('.btn_booknow').hide();
			$('#btn_booknow_'+n).css('display', 'block');
			$(this).removeClass("number1").addClass("number").siblings().addClass("number1");
		}
		promos_timer = setInterval('show_auto(1)', promos_timeout);
	});
	
	promos_timer = setInterval('show_auto(1)', promos_timeout);
	$('#newslist').hover(function(){
		clearInterval(promos_timer);
		jcar.lock();
	}, function(){
		promos_timer = setInterval('show_auto(1)', promos_timeout);
		jcar.unlock();
	});
	
	$("#newslist").jcarousel({
		scroll: 1,
		auto: 6,
		animation: 400,
		wrap: "circular"
	})	
	jcar = $('#newslist').data('jcarousel');
	if (!$.browser.webkit) {
		jcar.lock();
	}	
	//	left book now end
	
    // Remove last <span>|</span>
    $("#topnav span:last-child").css({
        "display": "none"
    })
    
    $("#mainnav li ul p span:last-child").css({
        "display": "none"
    })

	//	right menum start
	$("#gallerytag").mouseover(function(){
		$(this).find(".photoListItem").show();
	});
	$("#gallerytag").mouseout(function(){
		$(this).find(".photoListItem").hide();
	});
	//	right menum end
	
	//	main menum start
	$("#mainnav li").mouseover(function(){
		this_obj = $(this);
		this_obj.find("ul").show();
		
		if (!this_obj.hasClass('is_current')) {
			this_obj.addClass("current");
		}
	}).mouseout(function(){
		this_obj = $(this);
		this_obj.find("ul").hide();
		
		if (!this_obj.hasClass('is_current')) {
			this_obj.removeClass("current");
		}
	});
	//	main menum end
	
	// 	hero images start
	$('#slideshow').cycle({
        timeout: 6000
    });
	//	hero images end

	//	left promos start
	pcount = $('#ad ul').children().length;
    $("#ad ul").jcarousel({
        scroll: 0,
        auto: 6,
        animation: 400,
        wrap: "circular",
		initCallback: function(e,s) {
			pidx = 0;
		},
		itemLoadCallback: function(){
			pidx = pidx+1;
		}
    });
    
    $('#ad ul li a').hover(function(){
    	$('.leftAD').addClass('leftAD_hover');
    }, function(){
    	$('.leftAD').removeClass('leftAD_hover');
    });
	//	left promos end
	
	d = new Date();
	today_year = d.getFullYear();
	today_month = d.getMonth()+1;
	if (today_month<10) {
		today_month = '0'+today_month;
	}
	today_day = d.getDate();
	if (today_day<10) {
		today_day = '0'+today_day;
	}
	today = today_month+'/'+today_day+'/'+today_year;
	t = d.getTime();
	d = new Date(t+1000*3600*8);
	tomorrow_year = d.getFullYear();
	tomorrow_month = d.getMonth()+1;
	if (tomorrow_month<10) {
		tomorrow_month = '0'+tomorrow_month;
	}
	tomorrow_day = d.getDate() + 1;
	if (tomorrow_day<10) {
		tomorrow_day = '0'+tomorrow_day;
	}
	tomorrow = tomorrow_month+'/'+tomorrow_day+'/'+tomorrow_year;	

	$("input[name=checkInDate]").datepicker({
	    closeText: 'Close',
	    dateFormat: 'mm/dd/yy',
	    defaultDate: 0,
	    minDate: '+0',
	    maxDate: '+1Y',
	    showAnim: 'slideDown',
	    showButtonPanel: true,
	    onSelect: function(date,thisPicker){
			date = $(this).datepicker("getDate");
			date.setDate(date.getDate()+1);
			$("input[name=checkOutDate]").datepicker("setDate",date).datepicker("option", "minDate", date);
	    }
	}).val(today);
	
	$("input[name=checkOutDate]").datepicker({
	    closeText: 'Close',
	    dateFormat: 'mm/dd/yy',
	    defaultDate: +1,
	    minDate: '+1',
	    maxDate: '+1Y',
	    showAnim: 'slideDown',
	    showButtonPanel: true
	}).val(tomorrow);
	
	$('#check-in-date-a').click(function(){
		$('input[name=checkInDate]').trigger('focus');
		return false;
	});
	
	$('#check-out-date-a').click(function(){
		$('input[name=checkOutDate]').trigger('focus');
		return false;
	});

	$("#adults, #kids").spicyselect();
	
	$('#check_rate').click(function(){
		$('#bookform').trigger('submit');
	});
	
	//	Contact us form start
	$('#arrival_date,#event_date_start,#event_date_end,#proposal').datepicker({
	    closeText: 'Close',
	    dateFormat: 'mm/dd/yy',
	    defaultDate: 0,
	    minDate: '+0',
	    maxDate: '+5Y',
	    showAnim: 'slideDown',
	    showButtonPanel: true		
	});
	
	$('#arrival_date_a,#event_date_start_a,#event_date_end_a,#proposal_a').click(function(){
		_key_ = $(this).attr('id');
		$('#'+_key_.replace('_a', '')).trigger('focus');
		return false;
	});
	
	$('#contactus_submit,#meeting_submit').click(function(){
		_key_ = $(this).attr('id');
		$('#'+_key_.replace('_submit', '')).trigger('submit');		
		return false;
	});
	
	$('#contactus_reset, #meeting_reset').click(function(){
		_key_ = $(this).attr('id');
		$('#'+_key_.replace('_reset', '')).trigger('reset');	
		return false;
	});
	
	$('#contactus_country_select').change(function(){
		val = '';
		$(this).find('option').each(function(){
			if ($(this).attr('selected')) {
				val = $(this).text();
			}
		});
		if (val!='') {
			$('#contactus_country').val(val);
		}
	});

	
	if ($.browser.webkit && cufon_enabled) {
		$('#numlist').find('cufon').css('margin-top', '20px');
	}
	//	Contact us form end
	
	$('#btn_book_now').text('book now');
	if (cufon_enabled) {
		Cufon.set('fontFamily', 'Trade Gothic LT STD').replace('#btn_book_now');
	}

	//	fix some css
	$('#main_content ul').wrap('<div class="listItem"></div>');
	
	//	follow us
	$('.Bottom dt.static_block ul li').each(function(){
		txt = $(this).find('div.attribute-image').html();
		$(this).html(txt);
	});
});

