//prevent console.log from throwing an error
if(typeof console  != "object"){ console ={ log : function(){}} }

/*------------------------------------------------------------------
utility methods
------------------------------------------------------------------*/
// turns urls into links & truncates over 30 chars
String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
		var theDisplayURL = $(url.link(url)).text();
		var truncatedURL = theDisplayURL.length > 30 ? theDisplayURL.substring(0,27) + "..." : theDisplayURL;
		return truncatedURL.link(truncatedURL);
	});
};
String.prototype.capitalizeFirstLetter = function(){
    return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.isValidZipCode = function() {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(this));
}
var addCommas = function(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//returns the named url param
function getUrlParam( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	
	if( results == null ){
		return "";
	}else{
		return results[1];
	}
}

//array.indexOf for IE
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

//setting some vars based on some cookeez
var isOfAge = readCookie('donq_agegate');
var currRegion = readCookie('donq_region');

/*------------------------------------------------------------------
url redirects for age gate and ladydata hash
------------------------------------------------------------------*/
(function(){
	//parsin' some url right here
	var url = window.location.href;
	var url2 = url.substring(url.indexOf('//') + 2);
	var splt = url2.split('/');
	
	//this is for the ladydata hash redirect
	if(splt.length >=3 && splt[1] == '#'){
		var endPath = url2.substring(url2.lastIndexOf('#/'));
		var newUrl = 'http://'+ splt[0] +'/ladydata/' + endPath;
		window.location = newUrl; 
	}
	
	//this checks the age gate cookie and redirects to the age gate page, if necessary
	//this is a list of the restricted sections
	var restrictedSections = ['holiday','recipes', 'rum', 'finder', 'events'];
    var specialUrls = ['halloween','Halloween','holiday','Holiday','spring2011','Spring2011','spring','Spring','Summer','summer']
    var restrictedApps = restrictedSections.concat(specialUrls);
    
	//get the site root
	root = splt[0];
	
	//get the name of the section you're in
	var app = splt[1] != null ? splt[1]: '';

    // set the potential redirect url
	var redirURL = 'http://'+ root +'/age_gate/';

    // debug
    // console.log("restrictedSections=" + restrictedSections);
    // console.log("specialUrls="+specialUrls);
    // console.log("restrictedApps=" + restrictedApps);
    // console.log("splt[0]="+ splt[0]);
    // console.log("splt[1]="+ splt[1]);
    // console.log("splt[2]="+ splt[2]);    
    // console.log('isOfAge='+isOfAge);
    // console.log("app="+ app);
    // console.log("redirURL="+ redirURL);
    // console.log("restrictedApps.indexOf(app)=" + restrictedApps.indexOf(app));

	if((isOfAge == 'false' || isOfAge == null) && restrictedApps.indexOf(app) != -1){
        // don't ageGate deep linking of rum (but do for recipes)
		if(!(splt[2] && app == 'rum')){
		    window.location.href = redirURL + '?r='+ url ;		    
		}
	}
})();

//the below uses the google webfont loader library to control the FOUT
if(!document.getElementById('ie_font_style')){
	(function(){
		var d = document, e = d.documentElement, s = d.createElement('style');
		s.textContent = "body *{visibility:hidden} body{background:url('/assets/general/images/ajax-loader_snake.gif') 50% 352px no-repeat #121211}";
		// e.firstChild.appendChild(s);
		WebFont.load({
			typekit: {
				id: 'ifu1jtm'
			},
			loading: function() {
			},
			active: function() {
				// s.parentNode.removeChild(s);
			},
			inactive: function() {
				// s.parentNode.removeChild(s);
			}
		});	
	})();
}
