/*
 *  ===================
 *  GLOBAL VARS 
 *  ===================
*/
GLOBAL = (function(){return this;}).call();
CFG = {columnWidth:245, debug:false };

/*
 *  ===================
 *  SITE FUNCTIONS 
 *  ===================
*/
//CHECKS IF BROWSER MEETS REQUIREMENTS, IF NOT REDIRECT
function compatibilityCheck() {
	var passed = true;
	//console.log('video', (Modernizr.video) ? true : false );
	//console.log('fontface', Modernizr.fontface);
	passed = ($.support.ajax) ? true : false;
	passed = (Modernizr.video) ? true : false;
	passed = (Modernizr.fontface) ? true : false;
	passed = (Modernizr.opacity) ? true : false;
	//passed = false;
	
	if(!passed) {
		window.location = '/fallback';
	}
}


//CHECKS IF THERE IS AN ACTIVE CAMPAIGN, IF TRUE IT REDIRECTS USER
//Requires: bonData
function campaignCheck() {
	var cookie = $.cookie('campaign');
	console.log('DOCUMENT::campaignCheck()', bonData.campaigns, location, cookie);
	// Check if this is the landing page
	// If landing page check for campaign cookie
	// If campaign is set then check if campaign matches
	// If campaign matches do nothing
	// If campaign does not match, redirect
	var newvisit = false;
	// IF CAMPAIGN HAS EXPIRED, DO NOTHING
	var now		= new Date();
	//console.log('campaign expDate', expDate, now );
	//console.log('campaign', bonData.campaigns, Date.now() );
	if(Date.now() < bonData.campaigns.end_timestamp*1000) {
		if(location.pathname == "/" && location.hash == ""){
			if(typeof cookie == 'string'){
				if(cookie == bonData.campaigns.ID ){
					//ALREADY VISITED THIS CAMPAIGN
					//IF THIS IS A PERMANENT REDIRECT, THEN DO SO
					if(bonData.campaigns.redirect){
						newvisit = true;
					}
				} else {
					//VISITED ANOTHER CAMPAIGN
					newvisit = true;
				}
			} else {
				newvisit = true;
			}
		}
	}
	if(newvisit){
		// SET NEW COOKIE
		$.cookie('campaign', bonData.campaigns.ID, {expires:bonData.campaigns.expires});
		// REDIRECT
		window.location = bonData.campaigns.url;
	}
}


/*
 *  ===================
 *  CORE FUNCTIONS 
 *  ===================
*/
// STRING MASK
String.prototype.rot13 = function(){
    return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
};

// DEBUGGER 
if(CFG.debug){
	if(GLOBAL.console){
		
	} else {
		GLOBAL.console.log = function () {};
	}
} else {
	GLOBAL.console = {};
	GLOBAL.console.log = function () {};
}

// PLUGINS
(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = Math.ceil((ph-ah) / 2);
		$(this).css('margin-top', mh);
	});
};

// CENTER FLOATS
$.fn.centerFloats = function() {
	return this.each(function (i) {
		var w = 0;
		$(this).children().each(function () {
			w += $(this).outerWidth(true);
		});
		$(this).width(w);
		//console.log('centerFloats', w);
	});
}

})(jQuery);




//UTILS
Utils = {
	getItemByKeyValue : function(_key, _value, _group) {
		var res = false;
		for (var i in _group) {
			if( _group[i][_key] == _value ){
				res = _group[i];
			} 
		}
		return res;
	}
}



