EA.namespace('Ao2.TheGame');

$thegame = EA.Ao2.TheGame = {
	init: function() {
		
		function onRatingOver() {
       		$j(this).addClass('rating-over').prevAll().addClass('rating-over');
    	}
    	function onRatingOut() {
        	$j(this).removeClass('rating-over').prevAll().removeClass('rating-over')
    	}
    	
    	function notLoggedInOver() {
    		$j(this).children('.login-rating').show();
    		$j(this).children('.item-rating, .thegame-rating').hide();
    	}
    	
    	function notLoggedInOut() {
    		$j(this).children('.login-rating').hide();
    		$j(this).children('.item-rating, .thegame-rating').show();
    	}

		if (PageInfo.isLoggedIn) {
	    	$j('.media-content-topbar .item-rating li').live('mouseover', onRatingOver);
    		$j('.media-content-topbar .item-rating li').live('mouseout', onRatingOut);
		} else {
    		$j('.rating-box').live('mouseover', notLoggedInOver);
    		$j('.rating-box').live('mouseout', notLoggedInOut);
		}
    	
    	$thegame.loadSidebar(PageInfo.pageName,3,PageInfo.curPage,PageInfo.region);
    	$thegame.loadComments(PageInfo.pageName,PageInfo.itemId,10,1,PageInfo.region);	
	},
	loadSidebar: function (pageName,pageSize,pageNum,region) {
    	pagerUrl = "/"+region+"/"+pageName+"!pager?pageSize="+pageSize+"&curPage="+pageNum;
    	$j("#media-left-column").load(pagerUrl);
	},
	rateVehicle: function (id,star,region) {
   		ratingUrl = "/"+region+"/vehicles/"+id+"!star?stars="+star + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, '');
   		$j('#rating_'+id).load(ratingUrl,{});
	},
	rateWeapon: function (id,star,region) {
   		ratingUrl = "/"+region+"/weapons/"+id+"!star?stars="+star + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, '');
   		$j('#rating_'+id).load(ratingUrl,{});
	},
	rateCharacter: function (id,star,region) {
   		ratingUrl = "/"+region+"/characters/"+id+"!star?stars="+star + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, '');
   		$j('#rating_'+id).load(ratingUrl,{});
	},
	rateFeature: function (id,star,region) {
   		ratingUrl = "/"+region+"/features/"+id+"!star?stars="+star + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, '');
   		$j('#rating_'+id).load(ratingUrl,{});
	},
	loadComments: function(pageName,id,pageSize,pageNum,region){
		commentUrl = "/"+region+"/"+pageName+"/"+id+"/comments?_cC=" + pageSize + "&_cP="+pageNum + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, '');
    	$j("#thegame-comment-content").load(commentUrl, function(){
    		$j(".flag-comment").click(
				function () {
    				var id = $j(this).siblings('.tb_flagbox').attr("id");
					tb_show("","#TB_inline?width=300&height=150&amp;inlineId="+id, null);
					}
					);	
			$j(".close-share").click(
				function () {
					tb_remove();
					}
				);
			$j('#spinner').hide();
			$j('#loading').hide();
			$j(".share-box").fadeIn("fast");
			});
	},
	sendComment: function(pageName,id,region) {
		//$j('#spinner').show();
		$j(".share-box").fadeOut("fast");
		var commentUrl = "/"+region+"/"+pageName+"/"+id+"!comment";
		c = $j('#comment-text-area').val();
		data = {
			userComment: c
		};
		$j.post(commentUrl,data,function(data){
			$thegame.loadComments(pageName,id,10,1,region);
			
         });
        
	},
	flagComment: function (pageName,id,commentId,region) {
		data = {
			cid: commentId
		};
		flagUrl = "/"+region+"/"+pageName,"/"+id+"!flag";
		$j.post(flagUrl,data,function(data){
			tb_remove();
		});
	},
	sendToFriend:function (pageName,id,region) {
		var email = $j('#send-email-input').val();
		if (email) {
			var url = "/"+region+"/"+pageName+"/"+id+"!email?toEmail=" + email;
			$j('#email-content-wrap').load(url);
		}
	}	
		
};

$j(document).ready($thegame.init);



