(function(){
	document.write('<script type="text/javascript" src="/global/scripts/rating/rating.js"></script>');
	document.write('<script type="text/javascript" src="/global/scripts/pt-add-ons/starbox.js"></script>');
})();

var article =  {
	voteLock: false,
	init: function() {
		article.setRatings();
		article.setESRBToolTip();
		article.setSideBarStars();
		// article.setTrackProdBox();
		// article.setTrackXLink();
	},
	
	setRatings: function() {
		// gp ratings
		if ($$('.article-gp-score').length != 0) {
			var target = $$('.article-gp-score')[0];
			new Starbox(target, target.id.split('-')[1],{
				overlay: 'big.png',
				locked: true,
				rated: true
			});
		}
		var target = $$('.article-right-gp-score')[0];
		new Starbox(target, target.id.split('-')[1],{
			locked: true,
			rated: true
		});
		// user review ratings
		if (!$$('.article-right-user-score')[0]) { return };
		var globalID = $$('.article-right-user-score')[0].id.split("-")[2];
		if (Cookie.get('USER')) {
			new Ajax.Request('/user-reviews/ajax/check_vote/', {
				method: 'post',
				parameters: {
					globalID: globalID
				},
				onSuccess: function(t) {
					var obj = $H(t.responseText.evalJSON()).toObject();
					if (obj.success) {
						article.voteLock = true;
					}
				}	
			});
		}
		var target = $$('.article-user-score')[0];
		new Starbox(target, target.id.split('-')[0].split("_")[1],{
			overlay: 'big.png',
			buttons: 5,
			total: target.id.split('-')[1].split("_")[1],
			indicator: '#{average} average rating from #{total} votes',
			locked: article.voteLock,
			rated: article.voteLock,
			onRate: catchArticleReviewRate
		});
		var target = $$('.article-right-gp-score')[0];
		new Starbox(target, target.id.split('-')[1],{
			locked: article.voteLock,
			rated: article.voteLock
		});
		var target = $$('.article-right-user-score')[0];
		var userIndicator = '#{total} votes';
		if (target.className.indexOf("locked") != -1) {
			article.voteLock = true;
			userIndicator = 'Not Yet Released';
		}
		new Starbox(target, target.id.split('-')[0].split("_")[1],{
			buttons: 5,
			total: target.id.split('-')[1].split("_")[1],
			indicator: userIndicator,
			locked: article.voteLock,
			rated: article.voteLock,
			onRate: catchArticleReviewRate
		});
	},
	
	setESRBToolTip: function() {
		if (!$('esrb_button')) { return;}
		new Tip('esrb_button', $('esrb_content'),{
			style: 'darkgrey',
			title: 'ESRB Information',
			hideOn: { element: 'closeButton', event: 'click' },
			hideAfter: 1,
			stem: 'bottomRight',
		 	hook: { target: 'topLeft',tip: 'bottomRight', mouse: false },
			width: 400
		});
	},	

	articleReviewRate: function(el,info) {
		new Ajax.Request('/user-reviews/ajax/user_vote/', {
			method: 'post',
			parameters: {
				score: info.rated,
				globalID: el.id.split("-")[2]
			},
			onSuccess: function(t) {
				var h = $H(t.responseText.evalJSON()).toObject();
				if (!h.success) {
					util.talkbox(t.responseText);
					util.removebox = util.removebox.wrap(
						function(proceed) {
							article.setRatings();
							return proceed();
						}
					);
				} else {
					new Effect.Pulsate(el, {
						from: .5,
						pulses: 3
					});
					try {
						goog.productRate();
					} catch (error) {
						//fail
					}
				}
			}
		});
	},

	resizeImages: function() {
		if (!$$('body')[0].select('div')[0].id || $$('body')[0].select('div')[0].id != 'wrapper') {
			(function(){
				article.resizeImages();
			}).delay(1);
			return false;
		}
		$('content').select('div.inline_image').each(function(divAt) {
			var targetImg = divAt.select('img')[0];
			if (targetImg.getWidth() -2 > (divAt.parentNode.offsetWidth - 40)) {
				targetImg.style.width = (divAt.parentNode.offsetWidth - 40) + "px";
			}
			divAt.style.width = divAt.select('img')[0].getWidth() + "px";
			if (divAt.getWidth() > 450) {
				divAt.addClassName('center');
			}
		});
	},
	print: function() {
		window.print();
	},
	email: function() {
		new Ajax.Request('/article/ajax/email_check/', {
			method:'post',
			parameters:  {
				title: document.title,
				url: window.location.href.replace('#')
			},
			onSuccess: function(t) {
				var h = $H(t.responseText.evalJSON());
				h = h._object;
				util.talkbox(t.responseText);
				if (h.success) {
					var mailFocus = window.setTimeout(function(){
						$('friend_email').focus();
					},100);
				}
			}
		});
	},
	checkEmailForm: function() {
		if (!$F('friend_email') || $F('friend_email').indexOf("@") == -1 || $F('friend_email').indexOf(".") == -1) {
			$('error_mssg').update('Please enter a valid email address').removeClassName('hide');
			new Effect.Pulsate('error_mssg',{
				from: .5,
				pulses: 3
			});
		} else {
			new Ajax.Request('/article/ajax/email_send/', {
				method: 'post',
				parameters: $('emailArticleForm').serialize(),
				onSuccess: function(t) {
					$('talkTarget').update(t.responseText);
					var windowTimer = window.setTimeout(function(){
						util.removebox();
					},5000);
				}
			});
		}
	},
	setSideBarStars: function() {
		if ($$('.user-tab-stars').length == 0) { return false;}
		$$('.user-tab-stars').each(function(tabAt){
		    var score = tabAt.id.split("_")[1];
		    new Starbox(tabAt,score,{
		        locked: true
		    });
		});
	},
	
	// prod box event tracking
	setTrackProdBox: function() {
		// general set up
		if ($$('.prod-box').length == 0) { return false; }
			var targetBox = $('right').select('.box.prod-box')[0];
			var dataPoints = {
				"cat": "mini-prod-box",
				"action": "",
				"label": "",
				"url": ""
			}
			// title link tracking
			var titleLink = targetBox.select('h3')[0].select('a')[0];
			titleLink.rel = titleLink.href;
			titleLink.href = '#';
			var titleData = dataPoints;
			titleData.url = titleLink.rel;
			titleData.action = 'title-click';
			titleData.label = titleLink.innerHTML.strip();
			titleLink.onclick = function() {
				article.trackProdBox(titleData); return false;
			}
			// article type link tracking
			targetBox.select('.prod-nav')[0].select('a').each(function(linkAt,index) {
				linkAt.rel = linkAt.href;
				linkAt.href = '#';
				var linkData = {
					"cat": dataPoints.cat,
					"action": "",
					"label": "",
					"url": ""
				}
				linkData.action = 'article-type-click';
				linkData.label = linkAt.innerHTML.strip() + ' - ' + titleData.label;
				linkData.url = linkAt.rel;
				linkAt.onclick = function() {
					article.trackProdBox(linkData); return false;
				}
			});
	},
	trackProdBox: function(obj) {
		goog.trackProdBox(obj);
		var lagTimer = setTimeout(function() {
			window.location.href = obj.url;
		}, 100);
	},
		
	// x-link event tracking
	setTrackXLink: function() {
		if (!$('clArticles')) { return false;}
		$('clArticles').select('a').each(function(linkAt) {
			var target = linkAt;
			target.rel = target.href;
			var dataStruct = {
				"cat": "article-x-link",
				"action": "",
				"label": "",
				"url": target.rel
			}
			dataStruct.action = document.title.split(" - from GamePro.com")[0].strip();
			dataStruct.label = target.innerHTML.strip();
			target.onclick = function() {
				article.trackXLink(dataStruct);
				return false;
			}
			target.href = '#';
		});
	},
	trackXLink: function(obj) {
		goog.generalTracker(obj);
		var lagTimer = setTimeout(function() {
			window.location.href = obj.url;
		}, 100);
	},
	
	// share function object
	share : {
		shareSite: '',
		init: function() {
			dataReturn = {
				"title": $('article_title').innerHTML.replace(/<span>/gi,'').replace(/<\/span>/gi,''),
				"blurb": $('article_blurb').innerHTML,
				"url": window.location.href.replace('#','')
			}
			return dataReturn;
		},
		digg: function() {
			var dataObject = this.init();
			this.shareSite = 'Digg';
			this.goURL("http://digg.com/submit?phase=2&url=" + encodeURIComponent(dataObject.url)+ "&title=" + encodeURIComponent(dataObject.title)+ "&bodytext=" + encodeURIComponent(dataObject.blurb));
		},
		yahoo: function() {
			var dataObject = this.init();
			this.shareSite = 'Yahoo';
			this.goURL("http://buzz.yahoo.com/article/gameprocom109/" + dataObject.url);
		},
		facebook: function() {
			var dataObject = this.init();
			this.shareSite = 'Facebook';
			this.goURL("http://www.facebook.com/share.php?u=" + dataObject.url);
		},
		delicious: function() {
			var dataObject = this.init();
			this.shareSite = 'Del.icio.us';
			this.goURL("https://secure.del.icio.us/login?url=" + encodeURIComponent(dataObject.url) + "&title=" + encodeURIComponent(dataObject.title) + "&jump=close&partner=addthis&v=4");
		},
		stumbleupon: function() {
			var dataObject = this.init();
			this.shareSite = 'StumbleUpon';
			this.goURL("http://www.stumbleupon.com/submit?url=" + dataObject.url+ "&title=" + dataObject.title);
		},
		goURL: function(newURL) {
			var obj = {
				"cat": 'share-this',
				"action": this.shareSite,
				"label": window.location.href
			}
			goog.generalTracker(obj);
			(function(){
				window.location.href = newURL;
			}).delay(1);
		}
	}
}

function catchArticleReviewRate (el,info) {
	article.articleReviewRate(el,info);
}

document.observe('dom:loaded', article.init);
Event.observe(window, 'load', article.resizeImages);