var t,
		t2,
		t3;
		
// cufon replaces first (can't be in the ready() as it will die) 
Cufon.replace('.neo_sans', {fontFamily: 'Neo Sans'});
Cufon.replace('.neo_sans_hover', {hover:true,fontFamily: 'Neo Sans'});
Cufon.replace('.neo_sans_light', {fontFamily: 'Neo Sans Light'});

// popup profiles
var popupProfs =
{
	windowPrint:{
	width:800,
	height:600,
	center:1,
	menubar:1,
	toolbar:0,
	scrollbars:0,
	status:0,
	resizable:0}
};

// collect the query string
function getQuerystring(key, default_)
{
	if (default_==null) default_=""; 
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
		return default_;
	else
		return qs[1];
}

$(document).ready(function(){
  
  // mobile redirect
  if ($(window).width() <= 320 || $(window).height() <= 320) {
    window.location = 'https://mobile.ihotelier.com/mbe/mobile/13279/';
  }
	
	$(".popupwindow").popupwindow(popupProfs);
	// attach event to footer show/hide elements 
	$('a.footer_toggle').click(function(event){
		event.preventDefault();
		toggleFooter('click')
	});
	$('#footer_c').mouseenter(function(event){
		clearTimeout(t)
	});
	$('#footer_c').mouseleave(function(event){
		t=setTimeout("toggleFooter('hover_leave')",7000);
	});
	// footer slide 
	$('#footer_arrow_prev').click(function(event){
		event.preventDefault();
		slideFooter('prev')
	});
	$('#footer_arrow_next').click(function(event){
		event.preventDefault();
		slideFooter('next')
	});
	// enquiries toggle
	$('#enquiry_c').mouseenter(function(event){
		clearTimeout(t2);
		toggleEnquiries('on')
	});
	$('#enquiry_c').mouseleave(function(event){
		t2=setTimeout("toggleEnquiries('off')",2000);
	});
	// copyright fix
	toggleCopyright();
	// animate logo and book now
	//$('#body_logo').mouseenter(function(event){animateLogo('over')})
	//$('#body_logo').mouseleave(function(event){animateLogo('out')})
	$('#body_booknow').mouseenter(function(event){animateBooknow('over')})
	$('#body_booknow').mouseleave(function(event){animateBooknow('out')})
	// next/prev paging guff
	$('#paging_btn_prev').click(function(event){event.preventDefault();rightSlide('prev')});
	$('#paging_btn_next').click(function(event){event.preventDefault();rightSlide('next')});
	// popover events
	$('a.mroom_enq').live('click',function(event){
		event.preventDefault();
		callMroomenq()
	});
	$('a.booking_pop').live('click',function(event){
		event.preventDefault();
		$('#popover_booking').load("/popover/booking/",function(){
			showPopover('#popover_booking');
		});
	});
	$('a.pound_pop').live('click',function(event){
		event.preventDefault();
		showPopover('#popover_pound');
	});
	$('#mroomenq_form').live('submit',function(event){
		event.preventDefault();
		submitMroomform()
	});
	$('#booking_form').live('submit',function(event){
		event.preventDefault();
		submitBookingchange()
	});
	// remove required classes from focued fields 
	$('input.input_text').live("focus",function(){resetField(this.id)});
	$('input.input-checkbox').live("focus",function(){resetField(this.id)});
	$('textarea').live("focus",function(){resetField(this.id)});
	$('select').live("focus",function(){resetField(this.id)});
	
});

$(window).resize(toggleCopyright)

// show/hide the copyright
function toggleCopyright(){
	var bodyr_h=$('#body_r').height(),
			bodycontent_h=parseInt($('div.body_content_body').height()+parseInt($('div.body_content_float').css("padding-top"))+30);
	if(bodyr_h <= bodycontent_h){
		// hide
		$('#copyright_abs').hide();
	}else{
		//show
		$('#copyright_abs').show();
	}
}

// set the interval for the below footer action
function refreshFooterInterval(){t3=setInterval('refreshFooter()',6000)}
function killFooterInterval(){clearInterval(t3)}
// changes the 3rd slot in the footer action
function refreshFooter(){
	var start_pos=0,
			ord_vis=["reviews","news","newsletter","deals"],
			curr_vis,
			ord_len=parseInt(ord_vis.length);
	// work out what one we are on first
	if($('#foottoggle_reviews').is(":visible")){
		curr_vis='reviews';
	}else if($('#foottoggle_news').is(":visible")){
		curr_vis='news';
	}else if($('#foottoggle_newsletter').is(":visible")){
		curr_vis='newsletter';
	}else{
		curr_vis='deals';
	}
	var vis_pos=parseInt(jQuery.inArray(curr_vis,ord_vis)+1);
	if(vis_pos < ord_len){start_pos=vis_pos}
	var new_vis=ord_vis[start_pos];
	// hide the current one
	$('#foottoggle_'+curr_vis).fadeOut("slow",function(){
		// show the new one 
		$('#foottoggle_'+new_vis).fadeIn("slow");
	});
}

// bounce the footer (called from flash) 
function bounceFooter(){
	toggleFooter('click');
	t=setTimeout(function(){toggleFooter('click')},6000);
}
/*function bounceFooter(){
	bounceFooter_u(0);
	setTimeout("bounceFooter_u(0)",1500)
}
function bounceFooter_u(bCnt){
	if(bCnt<=2){
		// it's still not done 2 itterations of the bounce 
		$('#footer_c').animate({bottom: '+=9'}, 200,function(){bounceFooter_d(bCnt+1)})
	}
}
function bounceFooter_d(bCnt){
	$('#footer_c').animate({bottom: '-=9'}, 200,function(){bounceFooter_u(bCnt+1)})
}*/

// toggle the footer to show and hide
function toggleFooter(caller){
	// setup the default check for what we do
	if($('#footer_c').css("bottom")=='-118px'){
		var action='show';
	}else{
		var action='hide';		
	}
	// if this is being called from a hover action (likely) then we override the above
	if(caller=='hover_enter'){
		clearTimeout(t)
		action='show';
	}else if(caller=='hover_leave'){
		action='hide';
	}
	if(action=='show'){
		// show the footer 
		$('#footer_c').animate({bottom: '0px'}, 300,function(){if(typeof footerShown=='function'){footerShown()}});
		if($('#gallery_slide_c').length){
			// we have the thumb slide here, animate it up to stop the footer covering it
			//$('#gallery_slide_c').animate({bottom: '135px'}, 300);
			$('#gallery_slide_c').fadeOut("slow");
		}
		//slideFooter('next','0px',400);
		refreshFooterInterval();
		setTimeout("toggleFooterlabel('show')",300);
	}else{
		// hide the footer
		killFooterInterval();
		$('#footer_c').animate({bottom: '-118px'}, 300,function(){if(typeof footerShown=='function'){footerHide()}});
		if($('#gallery_slide_c').length){
			// got a gallery slide, send it back down
			//$('#gallery_slide_c').animate({bottom: '16px'}, 300);
			$('#gallery_slide_c').fadeIn("slow");
		}
		setTimeout("toggleFooterlabel('hide')",300);
	}	
}
// toggle the show/hide label 
function toggleFooterlabel(display){
	if(display=='show'){
		// show the hide label 
		$('#footer_tab_show').animate({left: '-66px'}, 250);
		$('#footer_tab_hide').animate({left: '0'}, 250);
	}else{
		// show the show label 
		$('#footer_tab_show').animate({left: '0'}, 250);
		$('#footer_tab_hide').animate({left: '-66px'},250);		
	}
}
// slide the footer segment 
function slideFooter(dir,force_pos,speed){
	if(!speed){var speed=300}
	if(!force_pos){
		var currentPos=$('#footer_slide_abs').css('left');
		// this slide goes back and forth, so work out the new position 
		if(currentPos != '-744px'){var newPos='-744px'}else{var newPos='0px'}
	}else{
		var newPos=force_pos;
	}
	// push it
	$('#footer_slide_abs').animate({left: newPos}, speed)
}

// toggle the enquiries layer
function toggleEnquiries(display){
	if(display=='on'){
		$('#enquiry_c').animate({top: '20px'}, 300);
	}else{
		$('#enquiry_c').animate({top: '-55px'}, 300);	
	}
}

// animate the logo tab
function animateLogo(state){
	if(state=='over'){
		// over 
		$("#body_logo").animate({left: '0px'},100);
		$("#body_logo").animate({left: '-3px'},100);
	}else{
		// out 
		$("#body_logo").animate({left: '-6px'},100);
	}
}
// animate the book now tab
function animateBooknow(state){
	if(state=='over'){
		// over 
		$("#body_booknow").animate({top: '55px'},100);
		//$("#body_booknow").animate({top: '55px'},100);
	}else{
		// out 
		$("#body_booknow").animate({top: '52px'},100);
	}
}

// slideshow paging
function rightSlide(dir,page){
	var suffix='',
			dis=392,
			curr_dis=parseInt($("#body_slide_in").css("margin-left").replace("px","").replace("-","")),
			max_dis=parseInt($("#body_slide_in").width());
	// first, clear the selected class
	$('a.paging_page').removeClass('selected');
	// they clicked a page btn, then set a absolute pos for the slide
	if(page){
		var type='abs',
				suffix='px',
				dis=parseInt((dis*page)-dis),
				sym='-';
		// select the page, its very easy to do here as we know it
		$('#paging_page_'+page).addClass('selected');
	}
	// if they have hit a next/prev, then just relative to its current pos please.
	if(dir=='prev'){
		// going back 
		var type='rel',
				sym='+=';
	}else if(dir=='next'){
		// going forward 
		var type='rel',
				sym='-=';
	}
	// work out what page we must be on 
	
	// do the move action
	$('#body_slide_in').animate({
		marginLeft: sym+dis+suffix
	}, 600,function(){
		curr_dis=parseInt($("#body_slide_in").css("margin-left").replace("px","").replace("-",""))
		if(type=='rel'){
			// now we need to work out its page, as they only hit next and prev
			page=parseInt((curr_dis/dis)+1);
			$('#paging_page_'+page).addClass('selected');
		}
		// show.hide the prev button?
		if(curr_dis){
			$('#paging_btn_prev').show()
		}else{
			$('#paging_btn_prev').hide()
		}
		// show/hide the next button?
		curr_dis=parseInt(curr_dis+392);
		if(curr_dis>=max_dis){
			$('#paging_btn_next').hide()
		}else{
			$('#paging_btn_next').show()
		}
	});
}

// show the popover
function showPopover(el){
	if($('#popover_bg').is(":visible")){
		// hide it 
		$(el).slideUp(400,function(){
			$('#popover_bg').slideUp("fast");
			if($.browser.msie && parseInt($.browser.version) < 7){$('#body_r select').show()}
		});
	}else{
		// show it
		if($.browser.msie && parseInt($.browser.version) < 7){$('#body_r select').hide()}
		$('#popover_bg').slideDown("fast",function(){
			$(el).slideDown(400);
		});
	}
}

// function to call the popup (added for flash) 
function callMroomenq(){
	$('#popover_mroomenq').load("/popover/mroomenq/",function(){
		showPopover('#popover_mroomenq');
	});
}

// submit meeting room enq form
function submitMroomform(){
	$.ajax({
		url:"/popover/mroomenq/",
		type:"post",
		cache:false,
		data:$('#mroomenq_form').serialize(),
		beforeSend:function(){
			$('#ajax_mroomenq').show()
		},
		success:function(html){
			$("#popover_mroomenq").html(html);
		},
		complete:function(){
			$('#ajax_mroomenq').hide();
		}
	});
}

// submit booking change form
function submitBookingchange(){
	$.ajax({
		url:"/popover/booking/",
		type:"post",
		cache:false,
		data:$('#booking_form').serialize(),
		beforeSend:function(){
			$('#ajax_booking').show()
		},
		success:function(html){
			$("#popover_booking").html(html);
		},
		complete:function(){
			$('#ajax_booking').hide();
		}
	});
}

// remove required class from focus fields
function resetField(el){
	$("#"+el+"_container").removeClass("errored checkbox_errored");
	// clear it?
	if($("#"+el).hasClass("clear_on_error")){$("#"+el).val("")}
}


// NEWSLETTER
$(document).ready(function(){
	
	// popup the newsletter form... quite important!
	$('a.newsletter_pop').live('click',function(event){
		event.preventDefault();
		loadNewsletter()
	});
	$('a.newsletterunsub_pop').live('click',function(event){
		event.preventDefault();
		showPopover('#popover_newsletter');
	});
	// check for rest of world checkbox
	$('#subscriber_country_world').live('click',function(){
		if($('#subscriber_country_world').is(":checked")){
			// show the rest of world select
			$('#subscriber_country_world_uk').attr("checked",false);
			$('#newslet_country_drop').show("fast")
		}else{
			// hide the rest of world select
			$('#newslet_country_drop').hide("fast")			
		}
	});
	// check for uk checkbox
	$('#subscriber_country_world_uk').live('click',function(){
		if($('#subscriber_country_world_uk').is(":checked")){
			// show the rest of world select
			$('#subscriber_country_world').attr("checked",false);		
			$('#newslet_country_drop').hide("fast")	
		}
	});
	// ajax form submission 
	$('#subscriber_form').live('submit',function(event){
		event.preventDefault();
		submitNewsletter()
	});

});

function loadNewsletter(){
	$('#popover_newsletter').load("/popover/newsletter/",function(){
		showPopover('#popover_newsletter');
	});
}
function loadNewsletterUnsub(email){
	$('#popover_newsletter').load("/popover/newsletter_unsubscribe/",{'email':email},function(){
		showPopover('#popover_newsletter');
	});
}

function submitNewsletter(){
	$.ajax({
		url:"/popover/newsletter/",
		type:"post",
		cache:false,
		data:$('#subscriber_form').serialize(),
		beforeSend:function(){
			$('#ajax_newsletter').show()
		},
		success:function(html){
			$("#popover_newsletter").html(html);
		},
		complete:function(){
			$('#ajax_newsletter').hide();
		}
	});
}

// FLICKR
$(document).ready(function(){
	
	// popup the image pop... quite important!
	$('a.flickr_pop').live('click',function(event){
		event.preventDefault();
		var clicked_ele=this;
		$('#popover_flickr').load("/popover/flickr/",function(){
			var newimg={};
			newimg.src=$(clicked_ele).attr("data-image"),
			newimg.title=$(clicked_ele).attr("data-title"),
			newimg.author=$(clicked_ele).attr("data-author"),
			newimg.author_weblink=$(clicked_ele).attr("data-author_weblink");
			newimg.flickr_id=$(clicked_ele).attr("data-flickr_id");
			writeFlickr(newimg);
			showPopover('#popover_flickr');
		});
	});	
	$('#btn_flickr_prev').live("click",function(event){
		event.preventDefault();
		slideReviews('prev',$(this).attr("data-len"),$(this).attr("data-slider"));
	});
	$('#btn_flickr_next').live("click",function(event){
		event.preventDefault();
		slideReviews('next',$(this).attr("data-len"),$(this).attr("data-slider"));
	});
	$('a.flickr_select').live("click",function(event){
		event.preventDefault();
		setFlickr(this)
	});
	
});

function callFlickr(){
	$('#popover_flickr').load("/popover/flickr/",function(){
		showPopover('#popover_flickr');
	});
}

function setFlickr(obj){
	var curr_src=$('#flickr_large_img').attr("src"),
			newimg={};
	newimg.src=$(obj).attr("data-image"),
	newimg.title=$(obj).attr("data-title"),
	newimg.author=$(obj).attr("data-author"),
	newimg.author_weblink=$(obj).attr("data-author_weblink");
	newimg.flickr_id=$(obj).attr("data-flickr_id");
	if(curr_src != newimg.src){
		$('#ajax_flickr').show();
		$('#flickr_large_cache').html('<img src="'+newimg.src+'" />');
		if ($.browser.msie && parseInt($.browser.version) < 8){
			writeFlickr(newimg);
		}else{
			$("#flickr_large_cache img").load(function(){
				writeFlickr(newimg);
			});
		}
	}
}

function writeFlickr(obj){
	var new_img='<img src="'+obj.src+'" id="flickr_large_img flickr_full_'+obj.flickr_id+'" alt="'+obj.title+'" title="'+obj.title+'" />';
	$('#ajax_flickr').hide();
	$('#flickr_large').fadeOut(300,function(){
		$('#flickr_large').html(new_img);
		$('#flickr_large').fadeIn(300);
		// write other guff 
		$('#flickr_summary').html("<strong>"+obj.title+"</strong>");
		$('#flickr_author_text').html("<a href=\""+obj.author_weblink+"\" target=\"_blank\" class=\"dyellowinvert\">"+obj.author+"</a>");
		// select the thumb
		$('a.flickr_select').removeClass("selected");
		$('#flickr_thumb_'+obj.flickr_id).addClass("selected");
	});
}

function hotspotPop(hid){
	$('#popover_flickr').load("/popover/flickr/",{'hotspot_id':hid},function(){
		showPopover('#popover_flickr');
	});
}

// SLIDER
$(document).ready(function(){
	
	$('#btn_slide_prev').click(function(event){
		event.preventDefault();
		slideReviews('prev',$(this).attr("data-len"),$(this).attr("data-slider"));
	});
	$('#btn_slide_next').click(function(event){
		event.preventDefault();
		slideReviews('next',$(this).attr("data-len"),$(this).attr("data-slider"));
	});
	
});

function slideReviews(dir,len,el){
	var curr_pos=parseInt($(el).css("margin-left").replace("px","").replace("-",""))
			con_len=$(el).width();
	// parse len as an int
	len=parseInt(len);
	// workout what way to go 
	if(dir=='prev'){
		// prev 
		var sym='+=';
		if(curr_pos==0){
			// at the beginning, send them to the end 
			sym='-';
			len=con_len-len;
		}
	}else{
		// next 
		var sym='-=';
		if(curr_pos+len>=con_len){
			// at the end, push them back to 0 
			sym='';
			len='0';
		}
	}
	// slide it 
	$(el).animate({marginLeft: sym+len}, 300);
}
