//The only global var, well it has to be
var ie6 = false;

$(function()
{
	autoclearInputFields();	
	//For positioning, so it sticks to the bottom of the browser
	var conHeight = parseInt($('#container').height());
	//Super footer interaction
	//footerInit();	
	//Footer position if content height is less than window height
	footerPosition(conHeight);
	//Reviews Show/Hide 
	if($('#local-reviews').is('#local-reviews')) reviewsToggle();
	//Facilities tooltip
	if($('.facilities').is('.facilities')) tooltip();
	
	$(window).resize(function(){
		footerPosition(conHeight);
	});
	
	$('.panels li').addClass('js');
	
	if(ie6)
	{
		centerImage($('#container'));
		
		$(window).resize(function(){
			centerImage($('#container'));
		});
				
		if($('.functions').is('.functions')) forceRedraw();
	}
});

function centerImage(elem)
{
	elem.css({left:(document.documentElement.clientWidth / 2) - (elem.width() / 2)});
};

function autoclearInputFields() 
{	
	// For each .autoclear input element
	$('.autoclear').each(function() {
		var element = $(this);
		var defaultValue;
		// store the default value if there is one
		if(defaultValue = element.val()) {
			element.data('default', defaultValue);
			// and then bind events..
			element.bind({
				// to clear the field on focus
				focus: function() {
					if(element.val() === element.data('default')) {
						element.val('');				
					}
				},
				// and replace the field on blur
				blur: function() {
					if(!element.val()) {
						element.val(element.data('default'));
					}
				}
			});
		};
	});
};

function footerPosition(conHeight)
{
	var el = $('#footer');
	
	var windowHeight = $(window).height() + 195;
		
	if(windowHeight > conHeight + 55)
	{
		var diff = windowHeight - conHeight - 41;	
		el.css({'marginTop':diff});
	}
	else
	{
		el.removeAttr('style');
	}
};

function forceRedraw()
{
	var functions = $('.functions');
	functions.bind('redraw', function() {
		functions.css('position', 'static').css('position', 'absolute');
	});
};

function footerInit()
{
	var open = false;
	var footer = $('#footer');
	var block = footer.find('.block');
	
	//Hide footer block
	block.hide();
	
	//Add click to arrow
	footer.find('.right-cap').click(function(){
		if(!open)
		{
			block.show();		
			$.scrollTo('#footer',500);
			footer.find('a.right-cap').addClass('open-cap');
			open = true;
		} else {
			block.slideUp();
			footer.find('a.right-cap').removeClass('open-cap');
			open = false;
		};
		
		return false;
	});
};


function reviewsToggle()
{
	var sMore = 'More';
	var sHide = 'Hide'
	
	$('a.toggleLink').show();
	$('.toggle').hide();
	
	$('a.toggleLink').click(function() {
		var oThis = $(this)
		var currentState = oThis.html();
		
		if (currentState == sMore){
			oThis.parent().parent().find('.toggle').slideDown();
			oThis.html(sHide);
		} else {
			oThis.parent().parent().find('.toggle').slideUp();
			oThis.html(sMore);
		};
		
		return false;
	});
};

function tooltip()
{
	$('body').append('<div class="tooltip"></div>');
	
	var tooltip = $('.tooltip');
	
	$('.facilities').find('img').hover(
		function(){
			var text = $(this).attr('alt');
			
			tooltip.html('<p>'+text+'</p>').css({
				'top':$(this).offset().top - 15,
				'left':($(this).offset().left - tooltip.width()/2) + 8,
				'display':'block'
			});
		},
		function(){
			tooltip.removeAttr('style').html('');
		}
	);
}
