
/* !--- Details --- */
/* --------------------------------------------------------------------------------------------------------------

Site Javascript
Author:  

 ---------------------------------------------------------------------------------------------------------------- */


// Define namespace
var SITE = {};

SITE.settings = {
	debug: false
};


/* !--- Browser Data ---                                                                                          */
/* -------------------------------------------------------------------------------------------------------------- */

SITE.browserData = function($){
	
	// iPhone
	var iphone = (( typeof window.orientation !== 'undefined' ) && ( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) ));
	
	// Flash
	var flash = {
		has: false,
		minor: 0,
		major: 0,
		release: 0
	};
	
	if( typeof swfobject !== 'undefined' && swfobject.getFlashPlayerVersion ){
		
		var v = swfobject.getFlashPlayerVersion();
		
		if( v.major > 0 ){
			flash.has = true;
			flash.minor = v.minor;
			flash.major = v.major;
			flash.release = v.release;
		}
	}

	// IE
	var ie = ( $.browser.msie ) ? $.browser.version : false;
	
	return {
		iphone: iphone,
		ie: ie,
		flash: flash
	}
	
}(jQuery);


/* !--- Initialise ---                                                                                            */
/* -------------------------------------------------------------------------------------------------------------- */

SITE.init = function($){
	
	// First make sure we can debug using console
	if(!window.console){
		window.console = {};
		
		methods = ['trace', 'log', 'info', 'debug', 'warn', 'error'];
		
		for(var key in methods){
			window.console[methods[key]] = function(msg){};
		};
	};
	
	// Add css hooks to the html element
	var html = $('html');
	
	html.addClass('js-active');
	
	if(SITE.browserData.flash.has){
		html.addClass('flash-active');
	}
	
}(jQuery);


/* !--- Utilities ---                                                                                             */
/* -------------------------------------------------------------------------------------------------------------- */

SITE.utils = function($){

	function printProps(obj, objName, console){
		var output;
	
		for (var prop in obj) {
			output += objName + "." + prop + " = " + obj[prop] + "\n" ;
		}
		
		return output;
	}
	
	function newWindow(e){
		if( e.type == 'click' || e.type == "keypress" && e.keyCode == 13 ){
			var url = $(this).attr('href');
			
			window.open( url );
			
			e.preventDefault();
		}
	}
	
	function preloadImages(imgs){
		$.each(imgs, function(i, image){
			var img = $('<img>').attr('src', image);		
		});
	}
	
	return {
		printProps: printProps,
		newWindow: newWindow,
		preloadImages: preloadImages
	};
	
}(jQuery);


/* !--- Utilities ---                                                                                             */
/* -------------------------------------------------------------------------------------------------------------- */
 /*var sltime= null;
 var timedelay=7000;
function slilePromo(offerId){		
	$.get( 'offers/offer-'+offerId+'.html', null, showPromoSlide, 'html' );						
	 var promoPagination2 = $('#promo-offer-pagination');
	promoPaginationLinks2 = $('a', promoPagination2);
	promoPaginationLinks2.removeClass('current');
	$('#a' + offerId).addClass('current');
	if (promoPaginationLinks2.length >offerId) 
	    offerId++ 
	else offerId=1;
	sltime=setTimeout("slilePromo(" + offerId + ");",timedelay);
}
function showPromoSlide(data, textStatus, XMLHttprequest){
    
	$('#promo-offer').fadeOut('normal', function(){	
		
		$(this).html(data).fadeIn('normal', function(){		
			inProgress = false;
		});
	});
	
}

$(function(){
	
	var promoPaginationLinks; 		// array of promo links within the pagination
    var slidetime=null;
    var promoPaginationCurrent; 	// used to track which promotion is currrently being displayed
    var promoPaginationRequested;
    var inProgress = false;			// flag to track if a promo has been requested
    var promo = $('#promo');
    var promoOffer = $('#promo-offer');
    var promoPagination = $('#promo-offer-pagination');
   
	
	
	
	
	if( promo.length && promoOffer.length && promoPagination.length ){		
		promoPaginationLinks = $('a', promoPagination);		
		promoPaginationCurrent = promoPaginationLinks.filter('.current');		
		promoPaginationLinks.bind('click', getPromo);
		if (promoPaginationLinks.length>=2)	sltime=setTimeout("slilePromo(2);",timedelay);
	}
	
	
	function getPromo(e){		
		var offerId =$(this).attr('id').replace('a',''); // $(this).attr('href').split('=')[1];		
		if( offerId && !inProgress ){
			promoPaginationRequested = $(this);		
			$.get( 'offers/offer-'+offerId+'.html', null, showPromo, 'html' );				
			inProgress = true;
		}	else{
		    //
		}					
		e.preventDefault();
		//var obj= promoPaginationLinks[1];
		
		//alert( promoPaginationCurrent)
		
		
	}
	
	
	function showPromo(data, textStatus, XMLHttprequest){
		
		promoOffer.fadeOut('normal', function(){	
			
			$(this).html(data).fadeIn('normal', function(){		
				inProgress = false;
			});
			
			promoPaginationCurrent.removeClass('current');
		   
			promoPaginationRequested.addClass('current');
		
			promoPaginationCurrent = promoPaginationRequested;
		
			promoPaginationRequested = null;
			
		});
	}
		
});*/
