
function simplePopup(url) {
	window.open(url)
	return false;
}

function confirmExtLegal(url){
	var acceptsTerms = "You are about to leave the Electronic Arts website and go to a website owned by a third party.  Electronic Arts is not responsible for content on third party sites, and our privacy policy does not apply to their information collection practices.  Press OK to access the linked site or press CANCEL to return to your original page.";
	if (confirm(acceptsTerms)) window.open(url);
	return false;
}
function confirmLegal(){
	var acceptsTerms = "Terms & Conditions of Downloading Materials\n\nThe materials provided on this web site are provided \"as is\" without warranties of any kind.  Electronic Arts Inc., its subsidiaries, divisions, affiliates and licensors (\"EA\") disclaim all warranties, either express of implied, including but not limited to, warranties of merchantability and fitness for a particular purpose. To the extent allowed by applicable law, in no event will EA be liable for damages of any kind to your hardware, peripherals or software programs as a result of your download or use of our materials.\n\n You may download one copy of the materials onto a single computer for your personal, non-commercial, home use only, provided you keep intact all copyright, trademark and other proprietary notices.  No materials from this web site may be copied, reproduced, modified, republished, uploaded, posted, transmitted, broadcast or distributed in any way without the express written consent of EA.  Unauthorized use of the materials is a violation of EA's copyright and constitutes infringement of EA's proprietary rights.\n\nTo use these materials, you must agree to the above terms and conditions.  To accept this agreement and proceed with the download, click \"OK\" or click \"Cancel\" to decline.";	
	return confirm(acceptsTerms);
}

function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	openPopup(url, name, width, height, status, scrollbars, moreProperties);
}

function popWallpaper(url, width, height) {
   if(confirmLegal()){
      openCenteredWindow(url,'pop', width, height,'yes');
   }
}

function openCenteredWindow(url, name, width, height){
      openPopup(url, name, width, height,'yes');
}

/* GLOBAL WINDOW POPUP */
function openPopup(url, name, width, height, status, scrollbars, moreProperties) {
	var agent = navigator.userAgent.toLowerCase();
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
      y = (screen.availHeight - height) / 2;
   }
   if (!status) status = '';

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
   
	window.open(url, name, properties);
	return false;
} 

var reviewsWidget = new Class({
	initialize: function(containerID,reviewsArray,btn){
		this.myReviews 		= reviewsArray;
		this.myContainer 	= $(containerID);
		this.myButton 		= this.myContainer.getElement(btn);
		this.myBody			=  this.myContainer.getElement('.content');
		this.myBodyTarget 	=  this.myContainer.getElement('.contentTarget');
		this.myLink 		=  this.myContainer.getElement('.more');
		this.myCredit 		=  this.myContainer.getElement('.credit');
		this.randomKey		= 0;
		
		this.myLink.addEvent('click', function(e) {
			omniLinkCall(this, 'global-review-more');
			return confirmExtLegal(this.href); 
			e.stop();	
		});
		this.myButton.addEvent('click', function(e) {
			var key = $random(0, (this.myReviews.length-1));
			this.showRating(key);
			e.stop();	
		}.bind(this));
		var key = $random(0, (this.myReviews.length-1));
		this.showRating(key);
	},
	showRating: function(key){
		//prevent duplicates from showing in a row 
		if (this.randomKey == key) {
			(key == 0) ? key = this.myReviews.length-1 : key-- ;
		}
		
		var myFx1 = new Fx.Tween(this.myBody,{duration:250});
		var myFx2 = new Fx.Tween(this.myCredit, {duration:250} );
		
		myFx1.start('opacity','0').chain(function(){
			this.myBodyTarget.innerHTML = this.myReviews[key][2];
			myFx1.start('opacity','1');
		}.bind(this));
	
		myFx2.start('opacity','0').chain(function(){
			this.myCredit.innerHTML = this.myReviews[key][1];
			myFx2.start('opacity','1');
		}.bind(this));
		
		this.myLink.setProperty('href',this.myReviews[key][3]);
		
		this.randomKey = key;
	}
	
});

var Overlay = new Class({
    initialize: function(containerID,trigger,popupHeight,autoOpen){
    	this.myContainer 	= $(containerID);
    	this.myFade 		= this.myContainer.getElement('.fade');
    	this.myPopup 		= this.myContainer.getElement('.popup');
    	this.myWrapper 		= this.myContainer.getElement('.wrapper');
    	this.myClose 		= this.myContainer.getElement('.close');

    	if (trigger) {
	    	this.myTriggers		= document.getElements(trigger);
	    	this.myTriggers.each(function(item){
	    		item.addEvent('click', function(e) {	
	    			this.showOverlay();
	    			e.stop();
	    		}.bind(this));
	    	}.bind(this));
    	}
    	
    	this.myPopupHeight	= popupHeight +"px";
    	
    	this.myClose.addEvent('click', function(e) {
    		this.closeOverlay();
    		e.stop();
    	}.bind(this));
    	
    	if (autoOpen) this.showOverlay();
    
    },
    showOverlay: function(){
		this.myFade.setStyle('opacity','0');
		this.myPopup.setStyle('height','0px');
		this.myWrapper.setStyle('opacity','0');
		this.myContainer.setStyle('display','block');
		
		//I6 FIX
		if ((Browser.Engine.trident) && (Browser.Engine.version <= 4)) {
			var topPos = document.documentElement.scrollTop + 274;
			this.myPopup.setStyle('position','absolute');
			this.myPopup.setStyle('top',topPos + 'px');
		}
		
		
		var myFx1 = new Fx.Tween(this.myFade,{duration:500});
		var myFx2 = new Fx.Tween(this.myPopup, {duration:500});
		var myFx3 = new Fx.Tween(this.myWrapper, {duration:500});
		
		myFx1.start('opacity','.8').chain(function(){
			myFx2.start('height',this.myPopupHeight).chain(function(){
				myFx3.start('opacity','1');
			});
		}.bind(this));
		
    },
    closeOverlay: function(){
		this.myContainer.setStyle('display','none');
    }
});


var Polls = new Class({
    initialize: function(containerID,pollID){
    	this.pollURL 		= "polls.action";
    	this.pollID 		= pollID;
    	this.myContainer 	= $(containerID);
    	this.loadPoll();
    },
    loadPoll: function(params){
    	if (!params) params = "";
    	
    	//check cookie - if exists, load resulst pane
    	if(Cookie.read('EAActive-poll-' + this.pollID)) params += "&result=true";
    	
		new Request.HTML({
    		url:this.pollURL + "?pollsName=" + this.pollID + params, 
    		onSuccess: function(responseTree, responseElements, responseHTML){
    			this.loadContent(responseHTML);
			}.bind(this),
			onRequest: function() {
				this.setStatus('Calculating poll data...');
			}.bind(this),
			onFailure: function() {
				this.setStatus('Polls cannot be loaded at this time.');
			}.bind(this)
		}).send();

    },
    loadContent: function(responseHTML){
		this.myContainer.set('html',responseHTML);
		
		if ($('pollSubmit')) {
			$('pollSubmit').addEvent('click', function(e) {	
				this.submitPoll();
				e.stop();
			}.bind(this));
		}

		this.myBars = this.myContainer.getElements('.bar');
		if (this.myBars) this.drawBars();

    },
    submitPoll: function(){
    	
    	//set cookie for later
    	var myCookie = Cookie.write('EAActive-poll-' + this.pollID, true,{duration: 7});
    	   	
    	var myForm 		= this.myContainer.getElement('form');
    	var myVotes 	= myForm.getElements('.vote');
    	var choiceID	= false;
    	   
    	//get raido button values
    	myVotes.each(function(item){
    		if (item.checked) choiceID = item.value;
    	});
	   	var params = "";
	    params += "&result=true";
		params += "&pollQuestionId=" + myForm.getElement('#pollID').value;
		if (choiceID) params += "&choiceId=" + choiceID;   	   	
		   	
	    this.loadPoll(params);
    
    },
    setStatus: function(msg){
		this.myContainer.innerHTML = "<p>" + msg + "</p>";
    },
    drawBars: function(){
		this.myBars.each(function(item){
			var percentage = item.getElement('.percentage');
			var offset = parseInt((273 - (percentage.innerHTML * 2.73 * 100)) * -1);
			var realPercentage = parseInt(percentage.innerHTML * 100) + "%";
			percentage.innerHTML = realPercentage;
			
			var myFx1 = new Fx.Tween(item,{duration:1000});
			
			if (Browser.Engine.trident) {
				myFx1.start('background-position-x',offset+'px');
			} else {
				myFx1.start('background-position',offset + 'px 0px');		
			}
			
		});
    	
    } 
});