// 
//  surrey.js
//  thesurrey cor Javascript file
//  
// 

// Global Cufon initializers
Cufon.registerFont(window.appFonts['Gotham Light']);
Cufon.registerFont(window.appFonts['Gotham Medium']);
Cufon.registerFont(window.appFonts['Gotham Book']);
Cufon.registerFont(window.appFonts['Roman Type']); // HTF Didot M-42 Medium
Cufon.registerFont(window.appFonts['Roman Type2']); // HTF Didot M-24 Medium
Cufon.registerFont(window.appFonts['Roman Type2Italic']); // HTF Didot M-24 Medium Italic
Cufon.registerFont(window.appFonts['RomanLight']); // HTF Didot L06 Light


Cufon.replace('#mainnav>li>a',{hover:true,fontFamily:'Gotham Medium',textShadow:'#999 0 0.5px 0.5px'});
Cufon.replace('#left-nav>li>a',{hover:true,fontFamily:'Gotham Medium',letterSpacing:'2px'});
Cufon.replace('#res-left-nav>li>a',{hover:true,fontFamily:'Gotham Medium',letterSpacing:'2px'});
Cufon.replace('#content .details h1',{fontFamily:'Roman Type'});
Cufon.replace('#content .details h2',{fontFamily:'Roman Type'});
Cufon.replace('#content .details h3',{fontFamily:'RomanLight',letterSpacing:'1px'});
Cufon.replace('#footer .details address span',{fontFamily:'Gotham Book'});
Cufon.replace('#footer .details address em',{fontFamily:'Roman Type2Italic'});
Cufon.replace('.email-form fieldset legend',{fontFamily:'Gotham Medium'});
Cufon.replace('.details h4',{fontFamily:'Gotham Medium'});
Cufon.replace('#content .details a.learn-more',{hover:true, fontFamily:'Gotham Medium', letterSpacing:'1px'});
Cufon.replace('#content .details a.offer-link',{hover:true, fontFamily:'Gotham Medium', letterSpacing:'1px'});
Cufon.replace('#content .details a.right-arrow',{hover:true, fontFamily:'Gotham Medium'});
Cufon.replace('#content .details a.back-arrow',{hover:true, fontFamily:'Gotham Medium'});
Cufon.replace('#filter-interests ul li label',{fontFamily:'Gotham Medium'});
Cufon.replace('.right-offers h4',{fontFamily:'Gotham Medium'});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$.preloadImages("/images/radioon.gif", "/images/radiooff.gif", "/images/termsTop.png", "/images/termsMid.png", "/images/termsBot.png", "/images/contact_box.png", "/images/res-submit-on.png", "/images/res-submit.png", "/images/searchbox2.gif", "/images/subnav.png");

// Main Surrey object definition
var Surrey = {
	
	init:function(page)
	{
		$('ul').find('li:last').addClass('last-child');
		$('ul').find('li:first').addClass('first-child');
		
		this.initEmailForm();
		this.initReserveForm();
		this.initCustomInputs('.form .defaultLabel','.form .default'); // label,input
		this.initCustomRadios('#filter-interests ul li input, .general_form  .defaultRadios ul li input'); //use this
		this.initImageRollover('#reserve-now-btn');
		this.initCustomSelect('#reserve-form select, #contact_form select.custom-contact, .general_form select');	
		this.contactDatePicker();
		this.initRolloverButtonTags('#contact-submit');
		
		var page = page || 'default';
		this.page = page;

		// Render UI effects per page
		switch(page){
			case 'offer_details':
				$('#content .details a.terms').mouseover(function()
				{
				  Surrey.initTermsContext(this);
				});
			break;
		}
	},
	
	initTermsContext:function(element)
	{
		var popText = '<h4 style="font:normal 10px/15px Arial,sans-serif;">TERMS AND CONDITIONS</h4>Based on availability for stays from July 16 &ndash; December 31, 2009. The offer includes one bottle of wine, breakfast for 2, turn down service and late-checkout per stay. Must be 21 years old to obtain bottle of wine. Not applicable to groups. May not be combined with any other offer. Offer may be discontinued at any time. Concert tickets must be purchased separately.';
		
		var offset = $(element).offset({relativeTo:$(document)});
		var d = $('<div style="visibility:hidden;z-index:1000;position:absolute;top:'+offset.top+'px;left:'+(offset.left-170)+'px;width:226px;min-height:50px"></div>');
		var dt = $('<div style="background:url(/images/termsTop.png) no-repeat;height:22px;float:left;width:380px;"></div>');
		var dm = $('<div style="background:url(/images/termsMid.png) repeat-y; color:#fff; font:normal 11px/15px Arial,sans-serif; padding:0px 30px;float:left;width:320px;"></div>');
		var db = $('<div style="background:url(/images/termsBot.png) no-repeat;height:30px;float:left;width:380px;"></div>');
		d.append(dt,dm,db);
		dm.html(popText);
		$('body').append(d);
		d.css({top:offset.top-d.height(),visibility:'visible'});
		
		var leaveTerms = function(e)
		{
			d.remove();
			$(this).unbind('mouseout', leaveTerms);
		}				
		$(element).bind('mouseout', leaveTerms);		
	},
	
	datePickers:function(){
		$('.surrey-datepicker').datepicker();		
		$('.surrey-pickdate').click(function(){ $(this).prev().focus(); });
	},
	
	contactDatePicker:function(){
		$('.contact-datepicker').datepicker();		
		$('.contact-pickdate').click(function(){ $(this).prev().focus(); });
	},
	
	initEmailForm:function()
	{
		var f = $('.email-form')
		if(f.length)
		{
			var l = $('label',f), i = $('input',f), b = $('button',f);
			
			l.bind('click',function(e)
			{
				$(this).hide();
				i.focus();
			});
			
			i.bind('blur',function(e)
			{
				if(this.value.replace(/^\s+?|\s+?$/g,'') == ''){
					l.show();
				}
			});
			
			if(i.val() != '')
			{
				l.hide();
			}
		}
	},
	
	initFormCalendars:function(formDiv,inputCont,label,input)
	{
		var f = $(formDiv);
		if(f.length)
		{
			var cont = $(inputCont),l = $(label,f),i=$(input,f);
			cont.bind('click',function(e)
			{
				l.hide();
				i.focus();
			});
			i.bind('blur',function(e)
			{
				if(this.value.replace(/^\s+?|\s+?$/g,'') == ''){
					l.show();
				}
			});			
			if(i.val() != '')
			{
				l.hide();
			}
			i.change(function() {
				if (i.val() != '')
				{
						l.hide();
				} 	
			});
		}
	},	
	
	initCustomInputs:function(label,input)
	{		
		var l = $(label), i = $(input);
			
		l.click(function()
		{
			$(this).hide();	
			$(this).parent().find('input:first').focus();
		});
		
		i.blur(function()
		{
			if(this.value.replace(/^\s+?|\s+?$/g,'') == ''){
				$(this).parent().find('label').show();
			}
		});
	},
	
	initRolloverButtonTags:function(b) // b is the id of the button tag
	{
		 $(b).hover(function() {
			$(this).find('img').attr('src', $(this).find('img').attr("src").split(".").join("-on."));
		  }, function() {
			$(this).find('img').attr('src', $(this).find('img').attr("src").split("-on.").join("."));
		  });
	},
	
	initImageRollover:function(i) // i is id of the image
	{
		$(i).hover(
		function() {
			$(this).attr('src', $(this).attr("src").split(".").join("-on."));
		},
		function() {
			$(this).attr('src', $(this).attr("src").split("-on.").join("."));
		});
	},
	
	initReserveForm:function()
	{
		// close reserve div
		$('#close_form').click(function() {
			$('#drop-cont').css({top:'-0px'}).animate({top:'-300px'}, 1200);
		});
		//open reserve div
		$('#reserve-now-btn').click(function() {
			$('#drop-cont').css({top:'-300px'}).animate({top:'0px'}, 600);		 
		});
		this.initFormCalendars('#reserve-form','.arr-cont','.arrDateLabel','.arrDate'); // formDiv,inputCont,label,input)	
		this.initFormCalendars('#reserve-form','.dep-cont','.depDateLabel','.depDate');
		this.initRolloverButtonTags('#reserve-submit'); 
		this.initImageRollover('#close_form'); // id of image
		this.datePickers();	// Widget datepickers
	},
	
	initCustomSelect:function(path)
	{		
		var inst = this;
		
		inst.hideSelects = function(){
			$('.custom-select').each(function(){ $(this).css({zIndex:8}).find('.custom-select-list').hide();});
			$(document).unbind('click', inst.hideSelects);
		}
		
		inst.showSelect = function(e){
			e.stopPropagation();
			inst.hideSelects();
			var rsel = $(this).parent(), list = $(this).parent().find('div.custom-select-list');
			rsel.css({zIndex:1000});
			list.css({zIndex:1000}).slideDown('fast');
			$(document).bind('click', inst.hideSelects);
		}	
		
		inst.selectOption = function(e){
			e.stopPropagation();
			var atag = $(this), text = atag.text(), value = atag.attr('rel'), id = atag.attr('target'), rsel = $('#custom-select-replaced-'+id);			
			$('#custom-select-'+id).text(text);
			$('#'+id).val(value);				
			$('#custom-list-'+id).slideUp('fast',function(){ rsel.css({zIndex:8}); $(this).css({zIndex:8}) });
			return false;
		}
		
		inst.generateCustom = function(){
			var sel = $(this), text = this.options[this.selectedIndex].text, value = this.options[this.selectedIndex].value, id = this.id;
			var s = '<div class="custom-select" id="custom-select-replaced-'+id+'">' +
			'<div class="custom-select-box" id="custom-select-'+id+'" unselectable="on" style="-moz-user-select:none">'+text+'</div>' +
			'<div class="custom-select-list" id="custom-list-'+id+'"><ul class="clearfix">';
			var ops = this.options;
			for(i=0; i< ops.length;i++){
				s += '<li><a href="javascript:void(0)" rel="'+ops[i].value+'" target="'+id+'">'+ops[i].text+'</a></li>';
			}
			s += '</ul></div></div>';	
			var box = $(s).css({marginTop:sel.css('marginTop'),marginBottom:sel.css('marginBottom')});
			sel.after(box).hide();
			return box;			
		}
		
		$(path).each(function(){
			var box = inst.generateCustom.call(this);			
			box.find('div.custom-select-list>ul>li>a').click(inst.selectOption);			
			box.find('div.custom-select-box').click(inst.showSelect);			
		});	
	},

	initCustomRadios:function(options){
		var ops = {onClick:function(){}};
		
		if(options instanceof Object){
			for(k in options){
				ops[k] = options[k];
			}
		}
		
		var radioOn = 'images/radioon.gif';
		var radioOff = 'images/radiooff.gif';
		var clickCallback = typeof clickCallback != 'undefined' ? clickCallback : function(){};
		
		$('input[type=\'radio\']').each(function(){
			var img = $('<img>').attr('src', this.checked ? radioOn : radioOff).css({cursor:'pointer'});	
			
			function checkSelected(e){
				e.preventDefault();
				var me = $(e.data.element);
	
				$('input[name=\''+me.attr('name')+'\']').each(function(){
					$(this).prev('img').attr('src', radioOff);
					this.checked = false;
				});
				
				me.prev('img').attr('src', radioOn);
				me.get(0).checked = true;	
				
				ops.onClick.call(me);			
				
			}
			
			$(img).bind('click', {element:this}, checkSelected);							
			$(this).before(img).hide();
			$(this).next('label').bind('click',{element:this},checkSelected);	
			
		});	
	}		
}



	
