var jcar = null;
var jcar_index = 0;
var msie = false;

function reset_jcar_numlist()
{
	if (jcar_index<0) {
		key = Math.abs(parseInt(jcar_index % jcar_count)) - 1;
	} else {
		key = jcar_count - parseInt(jcar_index % jcar_count) - 1;
	}
	
	$('#numlist a img').attr('src', assets+'img/dot2.png');
	$('#numlist a img').eq(key).attr('src', assets+'img/dot.png');
	
	$('#reservation_buttons li').hide().eq(key).show();
}

function reset_date_fonts()
{
	Cufon.set('fontFamily', 'Gotham Condensed');
	Cufon.replace('#fake_date', {
		fontStyle: 'Gotham Condensed',
		hover: true
	});
}


function checkEmail(email) 
{
	var re = new
	RegExp("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$");

	if (!email.match(re)) {
		alert('Please provide a valid email address');
		return false;
	}
}

function caculate_main_focus()
{
	var max = 8;
	var row_height = 26;
	
	$('ul.nav li[id^="main_nav_"]').each(function(i, _obj){
		obj = $(_obj);
		has_sub = obj.find('ul.ico_subnav').length;
		sub_count = 0;
		
		if (has_sub) {
			focus_pos = false;
			sub_count = obj.find('ul.ico_subnav li').length;
			
			obj.find('ul.ico_subnav li').each(function(j, __obj){
				if (focus_pos==false && $(__obj).attr('rel')=='focus') {
					focus_pos = j;
				}
			});

			if (focus_pos<=i && (max-focus_pos)>=sub_count) {
				up_rows = focus_pos + 1 - i;
				ul_wrap = obj.find('ul.ico_subnav').eq(0);

				pt = parseInt(ul_wrap.css('padding-top'));
				result = (pt - row_height*up_rows - row_height - 10);
				npt = result+'px';
				
				obj.attr('up-rows', npt);
				
			}
			
			newbg = assets+'img/new_nav_bg'+sub_count+'.png';
			obj.attr('newbg', newbg).attr('sub_count', sub_count);
			
			obj.hover(function(){
				_this_ = $(this);
				_h_ = (50+row_height*_this_.find('li').length)+'px';
				_uprows_ = _this_.attr('up-rows');
				_newbg_ = 'url:('+_this_.attr('newbg')+')';
				_ul_ = _this_.find('ul.ico_subnav').eq(0);
				_sub_count_ = parseInt(_this_.attr('sub_count'));
				
				_ul_.css({
					'height': _h_,
					'margin-top': _uprows_,
					'background-image': _newbg_
				});
				
				if (_sub_count_>0) {
					_ul_.addClass('nav_bg'+_sub_count_);
				}
			});
			
			
		}
	});
}

function preload_images()
{
	_images = new Array();
	_images.push(assets+'img/dot.png');
	_images.push(assets+'img/new_nav_bg1.png');
	_images.push(assets+'img/new_nav_bg2.png');
	_images.push(assets+'img/new_nav_bg3.png');
	_images.push(assets+'img/new_nav_bg4.png');
	_images.push(assets+'img/new_nav_bg5.png');
	_images.push(assets+'img/new_nav_bg6.png');
	_images.push(assets+'img/new_nav_bg7.png');
	_images.push(assets+'img/booknow2.jpg');
	_images.push(assets+'img/SBfa2.png');
	_images.push(assets+'img/SBfa_on.png');
	_images.push(assets+'img/SBtr_on.png');
	_images.push(assets+'img/L1_on.png');
	_images.push(assets+'img/L2_on.png');
	_images.push(assets+'img/nav_arrow.png');
	
	for(i in _images) {
		pus = new Image();
		pus.src = _images[i];
	}
}

function funcBox()
{
	var div1=document.getElementById("box_1");
	var div2=document.getElementById("box_2");
	if(div1.style.display=='block')
	{
		div2.style.display='block';
		div1.style.display='none'
	}
	else
	{
		div1.style.display='block';
		div2.style.display='none'
	}
}
function changeBox()
{
	window.setInterval("funcBox()",12000);
}

$(function(){
	msie = $.browser.msie;
	
	preload_images();
	caculate_main_focus();
	
	$('#numlist li a img:last').attr('src', assets + 'img/dot.png');
	
	$(".gallery_imgs a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed:'normal', 
		theme:'facebook', 
		slideshow:6000, 
		autoplay_slideshow: true,
		callback: function(){
		}
	});

	$('a[href="#"]').click(function(){
		return false;
	});
	
	$('#tr_icon, #tw_icon, #fb_icon').hover(function(){
		src = $(this).attr('src');
		new_src = src.replace('.png', '_on.png');
		
		$(this).attr('src', new_src);
	}, function(){
		src = $(this).attr('src');
		new_src = src.replace('_on.png', '.png');
		
		$(this).attr('src', new_src);
	});
	
	$('#re_left, #re_right').hover(function(){
		src = $(this).attr('src');
		new_src = src.replace('.png', '_on.png');
		
		$(this).attr('src', new_src);		
	}, function(){
		src = $(this).attr('src');
		new_src = src.replace('_on.png', '.png');
		
		$(this).attr('src', new_src);	
	});
	

	//	BOOK NOW WIGET	START
	$('#btn_reservation_submit').click(function(){
		$('form[name="omni_reservation_form"]').trigger('submit');
	});
	
	d = new Date();
	today_year = d.getFullYear();
	today_month = d.getMonth()+1;
	if ("1" == today_month) {
		today_month = "Jan";
	} else if ("2" == today_month) {
		today_month = "Feb";
	} else if ("3" == today_month) {
		today_month = "Mar";
	} else if ("4" == today_month) {
		today_month = "Apr";
	} else if ("5" == today_month) {
		today_month = "May";
	} else if ("6" == today_month) {
		today_month = "Jun";
	} else if ("7" == today_month) {
		today_month = "Jul";
	} else if ("8" == today_month) {
		today_month = "Aug";
	} else if ("9" == today_month) {
		today_month = "Sep";
	} else if ("10" == today_month) {
		today_month = "Oct";
	} else if ("11" == today_month) {
		today_month = "Nov";
	} else if ("12" == today_month) {
		today_month = "Dec";
	} else {}
	today_day = d.getDate();
	if (today_day<10) {
		today_day = '0'+today_day;
	}
	today = today_day+'-'+today_month+'-'+today_year;
	
	tomorrow_year = d.getFullYear();
	tomyear=tomorrow_year;
	tomorrow_month = d.getMonth()+1;
	if ("1" == tomorrow_month) {
		tomorrow_month = "Jan";
	} else if ("2" == tomorrow_month) {
		tomorrow_month = "Feb";
	} else if ("3" == tomorrow_month) {
		tomorrow_month = "Mar";
	} else if ("4" == tomorrow_month) {
		tomorrow_month = "Apr";
	} else if ("5" == tomorrow_month) {
		tomorrow_month = "May";
	} else if ("6" == tomorrow_month) {
		tomorrow_month = "Jun";
	} else if ("7" == tomorrow_month) {
		tomorrow_month = "Jul";
	} else if ("8" == tomorrow_month) {
		tomorrow_month = "Aug";
	} else if ("9" == tomorrow_month) {
		tomorrow_month = "Sep";
	} else if ("10" == tomorrow_month) {
		tomorrow_month = "Oct";
	} else if ("11" == tomorrow_month) {
		tomorrow_month = "Nov";
	} else if ("12" == tomorrow_month) {
		tomorrow_month = "Dec";
	} else {}
	tomorrow_day = d.getDate();
	if (tomorrow_day<10) {
		tomorrow_day = '0'+tomorrow_day;
	}
	tomorrow = tomorrow_day+'-'+tomorrow_month+'-'+tomyear;
	$("#reservation_date").datepicker({
	    closeText: 'Close',
	    dateFormat: 'dd-M-yy',
	    defaultDate: 0,
	    minDate: '+0',
	    maxDate: '+1Y',
	    showAnim: 'slideDown',
	    showButtonPanel: true,
	    onSelect: function(date,thisPicker){
			date = $(this).datepicker("getDate");
			date.setDate(date.getDate()+1);
			$("#reservation_checkout_date").datepicker("setDate",date).datepicker("option", "minDate", date);
	    },
		beforeShow: function(){
			$('div.spicyselect ol').hide();
			setTimeout(function () {$('#ui-datepicker-div').css("z-index", 6000);}, 100);
		}
	}).val(today);
	
	$("#reservation_checkout_date").datepicker({
	    closeText: 'Close',
	    dateFormat: 'dd-M-yy',
	    defaultDate: 0,
	    minDate: '+0',
	    maxDate: '+1Y',
	    showAnim: 'slideDown',
	    showButtonPanel: true,
	    onSelect: function(date,thisPicker){
	    },
		beforeShow: function(){
		}
	}).val(tomorrow);
	
	$('#fake_date').text(today).click(function(){
		$('#reservation_date').trigger('focus');
	});
	
	$('#reservation_date').val(today);
	reset_date_fonts();
	
	$("#reservation_nights, #reservation_rooms, #reservation_adults").click(function(){
		$('#reservation_date').datepicker('hide');
		$('div.spicyselect ol').hide();
	}).spicyselect();
	
	$('a.date').click(function(){
		rel = $(this).attr('rel');
		if (rel) {
			$('#'+rel).trigger('focus');
		}
		
		return false;
	});
	
	//	BOOK NOW WIGET END
	
	//	RESERVATIONS START
	$('div.rb').jcarousel({
		scroll: 1,
		animation: 400,
		wrap: 'circular'
	});
	
	jcar = $('div.rb').data('jcarousel');
	jcar_count = $('#numlist a').length;
	$('a#reservations_left').click(function(){
		jcar.prev();
		
		--jcar_index;
		reset_jcar_numlist();

		return false;
	});
	
	$('a#reservations_right').click(function(){
		jcar.next();
		
		++jcar_index;
		reset_jcar_numlist();

		return false;
	});	
	
	$('#numlist a').each(function(i){
		$(this).click(function(){
		
			jcar_index = jcar_count-i-1;

			jcar.scroll(jcar_index+1);
			reset_jcar_numlist();
			
			return false;
		});
	});
	
	//	RESERVATIONS END
	
	//	CONTENT'S READMORE START
	$('a.readmore, a.readless').click(function(){
		_this = $(this);
		rel = _this.attr('rel');
		
		p = $('div.'+rel);
		c = $('div.Content');
		d = $('div#dark_shadow');
		
		offset = 34;

		if (p.hasClass('hide')) {
			p.show().removeClass('hide');
			$('a.readmore[rel="'+rel+'"]').hide();
			$('a.readless[rel="'+rel+'"]').show();
			
			h = p.height();
			ph = c.height();
			nh = h+ph+offset;
			
			c.css('height', nh+'px');
			
			$('#dark_shadow').css('height', (nh-161)+'px');
		} else {
			h = p.height();
			ph = c.height();
			nh = ph - h  - offset;
			
			c.css('height', nh+'px');		
			p.addClass('hide').hide();
			
			$('a.readmore[rel="'+rel+'"]').show();
			$('a.readless[rel="'+rel+'"]').hide();
			
			d.css('height', '120px');
		}
		
		return false;
	});
	//	CONTENT'S READMORE END
	
	//	HERO IMAGES START
	$('#hero_images').cycle({
        timeout: 6000
    });	
	//	HERO IMAGES END
	
	//	LOGO URL START
	$('div._interior #site_logo').wrap('<a href="/"></a>');
	//	LOGO URL END
	
	$('.nav li[rel^="_R"]').hover(function(){
		rel = $(this).attr('rel');
		$(this).find('ul.'+rel).show();
	}, function(){
		rel = $(this).attr('rel');
		$(this).find('ul.'+rel).hide();	
	});
	
	$('ul.ico_subnav a').css('color', '#fff').hover(function(){
		
		$(this).css({
			'color': '#5A4F45',
			'font-size': '13px',
			'line-height': '26px'
		});
	}, function(){
		$(this).css({
			'color': '#ffffff',
			'font-size': '13px'
		});
	});
	
	//	Cufon font's fixing
	Cufon.set('fontFamily', 'TradeGothic');
	Cufon.replace('.icohare_trade_gothic', {
		fontStyle: 'TradeGothic',
		hover: true
	});
	
	Cufon.set('fontFamily', 'TradeGothicBold');
	Cufon.replace('.icohare_trade_gothic_bold', {
		fontStyle: 'TradeGothic',
		hover: true
	});	
	
	Cufon.set('fontFamily', 'TradeGothicLight');
	Cufon.replace('.icohare_trade_gothic_light', {
		fontStyle: 'TradeGothic',
		hover: true
	});		
	
	
	//Gotham Condensed
	Cufon.set('fontFamily', 'Gotham Condensed');
	Cufon.replace('.icohare_gotham_condensed', {
		fontStyle: 'Gotham Condensed',
		hover: true
	});	
	
	//Trade Gothic BoldTwo
	Cufon.set('fontFamily', 'TradeGothic_BoldTwo');
	Cufon.replace('.icohare_trade_gothic_bold_two', {
		fontStyle: 'TradeGothic_BoldTwo',
		hover: true
	});		
	//get width
	var my_height=$(".artcle").innerHeight();
	$(".SubContent").css('height',my_height);
	//form js
	
	$('#arrival_date,#event_date_start,#event_date_end,#proposal,#event_date').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,#event_date_a').click(function(){
		_key_ = $(this).attr('id');
		$('#'+_key_.replace('_a', '')).trigger('focus');
		return false;
	});
	
	$('#contactus_submit,#meeting_submit,#wedding_submit').click(function(){
		_key_ = $(this).attr('id');
		$('#'+_key_.replace('_submit', '')).trigger('submit');		
		return false;
	});
	
	$('#contactus_reset, #meeting_reset,#wedding_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);
		}
	});
	
	$(".sitemap li:first-child").find('span').hide();
	
	if ($.browser.msie) {
		$('a.bkn').click(function(){
			target = $(this).attr('target');
			url = $(this).attr('href');
			
			if (target=='_blank') {
				window.open(url);
			} else {
				window.location = url;
			}
			
			return false;
		});
	}
	
	$("a[href$='.pdf']").attr("target","_blank");
});
