/**
 * @method TimezoneDetect - From http://www.michaelapproved.com/articles/timezone-detect-and-ignore-daylight-saving-time-dst/
 */
function TimezoneDetect(){
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 100; //set initial offset high so it is adjusted on the first attempt
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;

    //go through each month to find the lowest offset to account for DST
    for (intMonth=0;intMonth < 12;intMonth++){
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);

        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
            intOffset = (dtDate.getTimezoneOffset() * (-1));
        }
    }

    return intOffset;
};

/**
 * @method setEditionCoookie - gregs (mediadog) 2010-02-11
 */
function setEditionCookie() {
	var cookieName = "EDITION";	
	
	/* Do nothing if cookie already set. */
	if (document.cookie.indexOf(cookieName) != -1) {		
		return;
	}
	
	/* offset = offset in hours */
	var offset = TimezoneDetect() / 60;
	
	/* default edition = 'alberta' */
	var edition = 'alberta';
	/*
		Canada Time Zones - From http://en.wikipedia.org/wiki/Time_in_Canada
		------------
		Edition
		------------
		Alberta = UTC-7
		British Columbia = UTC-8 (PST) / UTC-7 (PDT) (we will use 8 to avoid Alberta overlap) 
		Manitoba / Saskatchewan = UTC-6 (CST)
		Ontario = UTC-5 (EST) / UTC-4 (EDT)

	*/	
	switch (offset) {
		case -8:
			edition = 'british-columbia';
		break;
		case -7:
			edition = 'alberta';
		break;		
		case -6:
			edition = 'manitoba-saskatchewan';
		break;
		case -5:
		case -4:
			edition = 'ontario';
		break;
	};
	
	/* Set the cookie, expire in 30 days. */
	var cookieExpire = new Date();
	cookieExpire.setTime(cookieExpire.getTime() + 3600000 * 24 * 30);
	document.cookie = cookieName + '=' + edition + ';expires=' + cookieExpire.toGMTString();
	/* Reload so that Application.cfc knows about the edition cookie. */
	if (document.cookie.indexOf(cookieName) != -1) {			
		window.location.reload();
	}
	
	/* ASSERT: If we are here it means a cookie could not be set so don't do anything. */	
};
setEditionCookie();

