//------------------------------------
//	THELIVINGROOM.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.5
//------------------------------------

//////////////////////////
// CUFON

//
// GILL SANS - SANS SERIF
//

Cufon.replace('h1, h2, h3, .cufon_black', { 
	fontFamily: 'gillsans',
	textShadow: '1px 1px rgba(255,255,255,0.7)', 
	hover: true
});

Cufon.replace('.tag,form button,.button,#toolbar label, .cufon_white',{ 
	fontFamily: 'gillsans',
	textShadow: '1px 1px rgba(0,0,0,0.5)'
});

Cufon.replace('#footer h4', { 
	fontFamily: 'gillsans',
	textShadow: 'none'
});

//
// GARAMOND - SERIF
//

Cufon.replace('h1 em, h2 em, h3 em', { //Dark text light background, white shadow 
	fontFamily: 'garamond',
	textShadow: '1px 1px rgba(255,255,255,0.5)'
});

Cufon.replace('span.day', {  //Light text dark background, dark shadow 
	fontFamily: 'garamond',
	textShadow: '2px 2px rgba(0,0,0,0.5)'
});


////////////////////////////
// BEGIN JQUERY

$(function(){
	
	//////////////////////////
	// SELECT REPLACEMENT
	
	$('form .field select').simpleSelect();
	
	$('#toolbar select').simpleSelect({
		'defaultText':	'Choose a different location',
		'before':		function(){
			$(this).next('button').remove();
		}
	}).bind({
		'change':		function(){
			location.href = $(this).val();
		}
	});
	
	
	$('.join input').live({
		'focus':	function(){
			$(this).parent('.join').addClass('focus');
		},
		'blur':		function(){
			$(this).parent('.join').removeClass('focus');
		}

	});
	
	
	//////////////////////////
	// TWITTER
	
	$('#twitter > div').tweet({
		'username':		$('#twitter').data('feed'),
		'type':			$('#twitter').data('type'),
		'list':			$('#twitter').data('list')
	});
	
	$('#twitter[data-type=list] .follow').twitterSelect();
	
	//////////////////////////
	// FIND MY NEAREST
	
	function reloadCufon(){
	
		Cufon.replace('#location_picker h3,#location_picker button', { 
			fontFamily: 'gillsans',
			textShadow: '1px 1px #2f001d'
		});
		
	}
	
	$('a[href$="/food"]').location({
		'title':	'View our menus',
		'url':		'/food/[location]',
		'before':	function(){
			reloadCufon();
		}
	});
	
	$('a[href$="/locations"]').location({
		'title':	'Choose a location',
		'url':		'/location/[location]',
		'before':	function(){
			reloadCufon();
		}
	});
	
	$('a[href$="/promotions"]').location({
		'title':	'View our promotions',
		'url':		'/promotions/[location]',
		'before':	function(){
			reloadCufon();
		}
	});
	
	$('a[href$="/private-hire"]').location({
		'title':	'Organise a private party',
		'url':		'/private-hire/[location]',
		'before':	function(){
			reloadCufon();
		}
	});
	
	$('a[href$="/events"]').location({
		'title':	'View our events',
		'url':		'/events/[location]',
		'before':	function(){
			reloadCufon();
		}
	});
  
	
	//////////////////////////
	// EXTERNAL LINKS
	
	$('a[href*="http://"]:not([href*="'+location.hostname+'"]):not([class*="internal"]), .external').live('click',function() {
	    window.open($(this).attr('href'));
	    return false;
	});

});

////////////////////////////
// PLUGINS

(function($) {

	//////////////////////////
	// SELECT PLUGIN
	
	$.fn.simpleSelect = function(settings) {
		
		// Settings
		var defaults = {
			defaultText:	null,
			before:			function(){},
			ready:			function(){},
			click:			function(){},
			change:			function(){}
		};
		
		var o = $.extend(defaults, settings);
		
		return this.each(function(){
		
			var $this = $(this);
				
			o.before.apply($this);
			
			if( !$('html').hasClass('ie6') ){
				
				var extraClass = $this.attr('class') == undefined ? '' : ' ' + $this.attr('class');
				
				$this.wrap('<span class="select' + extraClass + '"/>');
				
				var $s = $this.parent('.select');
				
				if( o.defaultText == null ){
				
					var initial = $this.find('option:selected').text();
					
				}else{
				
					var initial = o.defaultText;
				
				}
				
				$s.prepend('<span class="body">' + initial + '</span><a href="#"><span></span></a>');
				
				$this.css({
					'width':		$s.outerWidth() == 0 ? '100%' : $s.outerWidth(),
					'height':		$s.outerHeight() == 0 ? '100%' : $s.outerHeight(),
					'opacity':		0
				}).bind({
					change:		function(){
					
						$s.find('.body').text($this.find('option:selected').text());
						
						o.change.apply($this);
						
					},
					click:		function(){
				
						o.click.apply($this);
					
					},
					focus:		function(){
					
						$s.addClass('focus');
					},
					blur:		function(){
						
						$s.removeClass('focus');
						
					}
					
				});
				
				o.ready.apply($this);
			
			}
	
		});

	};
	
	
	//////////////////////////
	// SCROLL TO
	
	$.fn.scrollTo = function(settings){
	
		// Settings
		var defaults = {
			'top':	false,	// Set to true for back to top
			'offset':	0	// Y position can be altered
		};
		
		var o = $.extend(defaults, settings);
		
		return this.click(function(e){
		
			$this = $(this);
			max = $(document).height() - $(window).height();
		
			// Go to top as a fallback or if specified
			if( o.top || $this.attr('href') == '#' ){
				y = 0;
			}else{
				y = $($this.attr('href')).offset().top + o.offset;
			}
			
			// If the page isn't long enough just go as far as possible
			if( y > max ){
				y = max;
			}
		
			// Animate
			$('html, body').animate({
				'scrollTop':	y
			},'600','easeInOutExpo');
			
			// Stop badness
			e.preventDefault();
		});
	};

})(jQuery);

//////////////////////////
// EASING
 
jQuery.extend( jQuery.easing,{
    easeInOutExpo: function (x, t, b, c, d){
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    },
    easeInOutQuart: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
        return -c/2 * ((t-=2)*t*t*t - 2) + b;
    },
    easeOutQuart: function (x, t, b, c, d) {
        return -c * ((t=t/d-1)*t*t*t - 1) + b;
    },
    easeOutExpo: function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    easeInExpo: function (x, t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    }
});
 
/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */
 
jQuery.timer = function(time,func,callback){
    var a = {timer:setTimeout(func,time),callback:null}
    if(typeof(callback) == 'function'){a.callback = callback;}
    return a;
};
 
jQuery.clearTimer = function(a){
    clearTimeout(a.timer);
    if(typeof(a.callback) == 'function'){a.callback();};
    return this;
};
