//------------------------------------
//	CHEZ.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.4.2
//				Cufon
//				Trade Gothic font
//	Version:	0.1
//------------------------------------

////////////////////////////
// CUFON

/*Cufon.replace('.content h1, h2, header nav a, .intro, .more, footer h3');

Cufon.CSS.ready(function(){
	setTimeout("$('.line:not(#header_line), .skyline').updateLines();",20);
});*/


////////////////////////////
// BEGIN JQUERY

jQuery(function(){

	// Load Google Maps API
	//loadGoogleMapsApiScript();

	// Ban the moose. BAD MOOSE!
	var mooseBan = false;
	
	// Add in the decorative lines
	//jQuery('header,footer').addLines();
	
	/*jQuery('#vouchers').addLines({
		side: 'left',
		skyline: true
	});*/
	
	// Input placeholder
	if( 'placeholder' in document.createElement('input') === false ){
	
		jQuery('input').each(function(){

			var $el = $(this);
			
			// skip if we do not have the placeholder attribute
			if( !$el.is('[placeholder]') )
				return;

			// we cannot do password fields, but supported browsers can
			if( $el.is(':password') )
				return;

			$el.bind('focus.placeholder', function(){
				if( this.value == $el.attr('placeholder' ) )
					$el.val('');
			});
			
			$el.bind('blur.placeholder', function(){
				if(this.value == '')
					$el.val( $el.attr('placeholder') )
            });

            $el.triggerHandler('blur');

			// Prevent incorrect form values being posted
			$el.parents('form').submit(function(){
				$el.triggerHandler('focus.placeholder');
			});
			
		});

	}
	
	// External links
	/*$('a[href*="http://"]:not([href*="'+location.hostname+'"]):not([class*="internal"]), a.external').live('click',function() {
	    window.open($(this).attr('href'));
	    return false;
	});*/
	
	// Back to top
	function backToTop(){
	
		var $w = $(window);
		var $btt = $('footer li:eq(1)');
		
		function check(){
			if( $(document).height() > $w.height() ){
				$btt.show();
			}else{
				$btt.hide();
			}
		}
		
		$btt.click(function(){
			$('html,body').animate({scrollTop:0},400,'easeInOutExpo');
			return false;
		});
		
		$w.resize(function(){
			check();
		});
		
		check();
		
	}
	
	backToTop();

});

jQuery.extend( jQuery.easing,{
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	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;
	}
});

(function($){

	$.fn.addLines = function(options) {
		
		var defaults = {
			lines: true,
			side: 'full',
			skyline: false,
			skypos: 'bottom'
		},
		settings = $.extend({}, defaults, options);
		
		return this.each(function(){
				
			var $e = $(this);
		
			var top = {'top': $e.offset().top + 5};
			var bottom = {'top': $e.offset().top + $e.outerHeight() - 6};
			
			if( settings.skypos == 'bottom' ){
				var skylineCss = {'top': $e.offset().top + $e.outerHeight() - 164};
			}else{
				var skylineCss = {'top': $e.offset().top - 154};
			}
				
			var tagOrId = $e.get(0).tagName.toLowerCase();
			if( tagOrId != 'header' && tagOrId != 'footer' ){
				tagOrId = '#' + $e.attr('id');
			}
				
			/*if( settings.lines ){
			
				$('<span/>',{
					'class':	'line ' + settings.side,
					'css':		top
				}).appendTo('body').attr('data-for', tagOrId).attr('data-pos','top');
				
				$('<span/>',{
					'class':	'line ' + settings.side,
					'css':		bottom
				}).appendTo('body').attr('data-for', tagOrId).attr('data-pos','bottom');
				
			}*/
			
			/*if( settings.skyline ){

				$('<span/>',{
					'class':	'skyline ' + settings.side,
					'css':		skylineCss
				}).appendTo('body').attr('data-for', tagOrId).attr('data-pos', settings.skypos);
			}*/

		});
		
	}
	
	$.fn.updateLines = function() {
	
		return this.each(function(){
				
			var y = '';
			var lineFor = $(this).attr('data-for');
			var pos = $(this).attr('data-pos');
			
			if( pos == 'top'){
				if( $(this).hasClass('skyline') ){
					$e.offset().top - 154;
				}else{
					y = $(lineFor).offset().top + 5;
				}
			}else if( pos == 'bottom' ){
				if( $(this).hasClass('skyline') ){
					y = $(lineFor).offset().top + $(lineFor).outerHeight() - 164;
				}else{
					y = $(lineFor).offset().top + $(lineFor).outerHeight() - 6;
				}
			}

			$(this).css({top:y});
			
		});
	
	}
	
	// CENTER TITLES

	$.fn.centerTitle = function() {

		return this.each(function(){

			var $a = $(this).find('article:hidden');
			var $t = $a.find('.text');
			
			if( $a.size() > 0 ){
				
				// Variables
				var s = 300;
				var e = 'easeOutExpo';
				
				// Show article
				$a.show();
				
				// Get text height and hide
				var textH = $t.height();
				$t.hide();
				
				// Get the margins
				var m = '-' + Math.round( $a.outerHeight() / 2 ) + 'px';
				var mt = '-' + Math.round( ( $a.outerHeight() + textH ) / 2 ) + 'px';
				
				// Get the height
				var h = $a.height();
				
				// Set height & margin
				$a.css({height:h, marginTop:m});
				
				// Show the text again
				//$t.show();
				
				
				
				/*ARTICLE REVEAL HERE*/
				
				// Rollover and roll out
				//$a.parent().mouseenter(function(){ ORIGINAL
				  $("article").mouseenter(function(){			  
					$t.show(); //show text
					$a.stop([]).animate(
										{height: h + textH, marginTop: mt}
										,s,e); //expand caption
					
					
				});
				
				$a.parent().mouseleave(function(){
					$a.stop([]).animate({height: h, marginTop: m},s,e,function(){
						$t.hide();
					});
				});
			}
		});

	}

})(jQuery);


/////////////////////////////
//	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;
};

// GET BROWSER NAME/VERSION

jQuery.getBrowser = function() {
	$t = $('html');
	if( $t.hasClass('chrome') ){
		b = 'chrome';
	}else if( $t.hasClass('safari') && !$t.hasClass('chrome') ){
		b = 'safari';
	}else if( $t.hasClass('ff3') || $t.hasClass('ff2') ){
		b = 'firefox';
	}else if( $t.hasClass('ff4') ){
		b = 'ff4';
	}else if( $t.hasClass('opera') ){
		b = 'opera';
	}else if( $t.hasClass('ie9') ){
		b = 'ie9';
	}else if( $t.hasClass('ie8') ){
		b = 'ie8';
	}else if( $t.hasClass('ie7') ){
		b = 'ie7';
	}else if( $t.hasClass('ie6') ){
		b = 'ie6';
	}else{
		b = 'unknown'
	}
	
	return b;
}


////////////////////////////
// LOAD GOOGLE MAPS API

/*function googleMapsApiLoaded() {
	googleMapsEnabled = true; 	
}

function loadGoogleMapsApiScript() { 
	googleMapsEnabled = false;
	
	var script = document.createElement("script"); 
	script.type = "text/javascript"; 
	script.src = "http://maps.google.com/maps?file=api&v=2&key="+googleMapsApiKey+"&async=2&callback=googleMapsApiLoaded";
	document.body.appendChild(script); 
}*/
