/*global $ document*/
window.Alienware = window.Alienware || {};
Alienware.com = Alienware.com || {};
Alienware.com.aliennetwork = Alienware.com.aliennetwork || {};
Alienware.com.aliennetwork.age_verify = function(params) {
    /**
    * @param {String}       params.result      	result of age verification sent from flash
	* @param {String}       params.redirectURL 	URL for redirection sent from flash
    */

	this.result = params.result || "FAIL";
	this.redirectURL = params.redirectURL || "http://www.alienware.com";
	
    this.initialize();
	
}


Alienware.com.aliennetwork.age_verify.prototype = {
    initialize: function() {
		
        $this = this;
        	
        $this.bakeCookies($this.result);
		
		if ($this.result == "PASS") $this.redirect($this.redirectURL);
    },
	
	bakeCookies: function(result) {
		
		function fixDate (date) {
			var base = new Date(0);
			var skew = base.getTime();
			if (skew > 0) date.setTime(date.getTime() - skew);
		}
		
		var expires = new Date();
		fixDate(expires);
		expires.setTime(expires.getTime() + 1 * 24 * 60 * 60 * 1000); // 1 day until expiration
		expires = expires.toGMTString();

		var path = "/";
		var domain = "";
		var secure = "";
		
		var myCookie = "AWAgeGate" + "=" + escape(result) + (expires ? "; expires=" + expires : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    	document.cookie = myCookie;
	},

	redirect: function(url) {
		location.href = url;
    }

};


Alienware.com.aliennetwork.age_verify.eatCookie = function (){
	var prefix = 'AWAgeGate=';
	var c = document.cookie;
	var cookieStartIndex = c.indexOf(prefix);
	
	if (cookieStartIndex == -1) return '';
	var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) cookieEndIndex = c.length;
		
	return unescape(c.substring(cookieStartIndex + prefix.length,cookieEndIndex));
}


Alienware.com.aliennetwork.age_verify.initFlash = function (){
	function getImageName(){
		// image must be named the same as filename with - replaced with _
		var img = window.location.pathname.split( '/' )[window.location.pathname.split( '/' ).length-1];
		img = img.replace(/.aspx/, "");
		img = img.replace(/-/g, "_"); // global replace
		return img;
	}
	
	var imgURL = "http://image.alienware.com/Images/alien_network/game_trailers/thumb_" + getImageName() + ".png";
	var redirectURL = window.location.href;
	var cookie = Alienware.com.aliennetwork.age_verify.eatCookie()
	
	// url param in case of disabled cookies
	if (redirectURL.indexOf("?") > 0) {
		redirectURL += "&vi=p";
	} else {
		redirectURL += "?vi=p";
	}

	// SWFObject 2.1 Code
	var videoPlayerObj = new Object;
	var par = { };
	var att = { };
	var id = "";
		
	par.flashvars = "&imageURL=" + imgURL + "";
	par.flashvars += "&redirectURL=" + redirectURL + "";
	par.flashvars += "&cookie="+ cookie +"";
	par.allowScriptAccess = "always";
	par.wmode = "transparent";
	att = { data:"http://image.alienware.com/Images/alien_network/game_trailers/age_verify.swf", width:"497", height:"370", id:"alien_player1", name:"alien_player1" };
	id = "theater_screen";
	videoPlayerObj = swfobject.createSWF(att, par, id);
}


$(document).ready(function() {

	Alienware.com.aliennetwork.age_verify.initFlash();
	
	//$("a[name='simonSays']").click(function() {
		//new Alienware.com.aliennetwork.age_verify({ "id": $(this).attr("id"), "position": Alienware.com.Utils.GetURLParamVal($(this).attr("href"), "position") });
        //return false;
    //});

});