var RATING_ASSOCIATIVE_ID = null;
var SUCCESSFULLY_VOTE_MESSAGE;
var ADD_COMMENT;
var TYPE_VOTE;
var SUBMIT;
var CANCEL;
var INSERT_BLANK_TEXT_VALIDATION;
var SPGAMES_MODULETYPE = "AC_SinglePlayerGames";

function CreateRateCommentPopup(type) {  
    var popup = "";
    var imgsrc = "";
    if (type=="1") imgsrc = "c_images/plus.gif";
    else imgsrc = "c_images/minus.gif";
    
    popup = "<div id='popupRateID' class='popupWinShortProf' style='display:none; position:absolute;z-index:999999' > " + 
                "<table width='100%' border='0' cellpadding='0' cellspacing='0'>" +
                    "<tbody><tr>" + 
                        "<td class='popupTransBack' height='10'></td>" +
                        "<td class='popupTransBack' height='10'></td>" +
                        "<td class='popupTransBack' height='10'></td>" +
                     "</tr> <tr>" + 
                            "<td class='popupTransBack' width='10'></td>" +
                            "<td style='padding: 0px;' bgcolor='#ffffff'>" +	
	                            "<div style='padding: 2px;'>" + 
                                "<table style='border: 1px solid rgb(255, 255, 255);' width='100%' border='0' cellpadding='0' cellspacing='0'>" +
                                    "<tbody><tr>" + 
                                        "<td class='homeBoxtitle' style='padding: 0px;'><div style='float: left;'><img src='" + imgsrc + "' />" +
                                        "</div><div style='float: left; padding-left: 10px;'>" + TYPE_VOTE + "</div>" + 
                                        "<div style='float: right; margin-right: 5px; margin-top: 5px;'><a href='#' onclick='DropRatePopup();'> <img src='c_images/x_dn.gif' id='ImageCloseID' border='0'></a></div></td>" + 
                                        "</tr><tr><td height='1'></td></tr></tbody></table></div>" + 
                                "<div><table class='homePadTab' width='100%' border='0' cellpadding='0' cellspacing='0'>" + 
                                "<tbody><tr>" + 
                                    "<td style='padding: 10px 0px 0px;' valign='top'></td>" + 
                                    "<td style='padding: 10px 0px 0px;' valign='top'>&nbsp;<strong>" + SUCCESSFULLY_VOTE_MESSAGE + "</strong></td>" + 
                                    "</tr><tr>" + 
                                        "<td style='padding: 10px 0px 0px;' valign='top' width='32%' align='right'><strong>" + ADD_COMMENT + ":&nbsp; </strong></td>" +
                                        "<td style='padding: 10px 0px 0px;' valign='top' width='68%'><div>" +
                                        "<textarea name='textfield' id='RatePopupTextFieldID' mLength='500' cols='50' rows='3'></textarea><span></span><span id='ValidationTextID' class='red'></span></div></td>" +
                                    "</tr><tr>" + 
                                    "<td style='padding: 0px;'>&nbsp;</td>" + 
                                    "<td><input class='btn_smblu'  id='ButtonCmdID' value='" + SUBMIT + "' type='button' onclick='AddCommentToRateAction($(\"#RatePopupTextFieldID\").attr(\"value\"));' ></td>" +
                                    "</tr></tbody></table>" +
                                "<table style='border: 1px solid rgb(255, 255, 255); margin-top: 5px;' width='100%' border='0' cellpadding='0' cellspacing='0'>" +
                                    "<tbody><tr>" +
                                        "<td colspan='3' style='border-top: 1px solid rgb(153, 153, 153); padding: 5px; background-color: rgb(229, 229, 229);' align='right'><input class='cancel' value='" + CANCEL + "' onclick='DropRatePopup();' type='button'></td>" +
                                        "</tr></tbody></table></div></td>" +
                                        "<td class='popupTransBack' width='10'></td></tr>" + 
                                        "<tr><td class='popupTransBack' height='10'></td>" +
                                        "<td class='popupTransBack' height='10'></td>" +
                                        "<td class='popupTransBack' height='10'></td></tr></tbody></table></div>";

   $("#LoadClientPopupsDynamicallyID").append(popup);           
   setMaxLength();
   }       

function DropRatePopup(){
    $("#LoadClientPopupsDynamicallyID").children().remove("#popupRateID");    
}


function centerRatePopup(type){ 
 DropRatePopup();
 CreateRateCommentPopup(type) 
 //request data for centering  
 var windowWidth = document.documentElement.clientWidth;  
 var windowHeight = document.documentElement.clientHeight;  
 var popupHeight = $("#popupRateID").height();  
 var popupWidth = $("#popupRateID").width();  
 var scrollLeft = getScrollXY().X;
var scrollTop  =  getScrollXY().Y;
var clientWidth = GetClientBounds().width;
var clientHeight = GetClientBounds().height;
var element = $("#popupRateID")[0];
element.style.position = "absolute";
element.style.left = scrollLeft+((clientWidth-popupWidth)/2)+'px';
element.style.top = scrollTop+((clientHeight-popupHeight)/2)+'px';   
element.style.display = "block";

// //centering  
//$("#popupRateID").css({  
// "position": "absolute",  
// "top": windowHeight/2-popupHeight/2,  
// "left": windowWidth/2-popupWidth/2,
// "display": "block"   
// }); 
 };


function AddCommentToRateAction(body){    
    var moduleid = $(".vote").attr("moduleid");
    var mtype = $(".vote").attr("moduletype");
    var url = "/WebServices/Rating/RatingAkazooItemService.asmx/AddCommentToRateAction";
    var data = "ModuleID=" + moduleid + "&type=" + mtype + "&rateid=" + RATING_ASSOCIATIVE_ID + "&body=" + encodeURIComponent(body);
    arg = {};
    arg.mark = RATING_ASSOCIATIVE_ID;
    body = body.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if (body == null || body=="")
    {
        $("#ValidationTextID").empty();
        $("#ValidationTextID").append(INSERT_BLANK_TEXT_VALIDATION);
    }
    else
        $(this).ServiceRateInvoke(url, data, "ADD_COMMENT", this, arg); 
}

function RateAction (mark){
    var moduleid = $(".vote").attr("moduleid");
    var mtype = $(".vote").attr("moduletype");
    var url = "/WebServices/Rating/RatingAkazooItemService.asmx/RateUserAkazooItem";
    var data = "ModuleID=" + moduleid + "&type=" + mtype + "&mark=" + mark; 
    arg = {};
    arg.mark = mark;
    $(this).ServiceRateInvoke(url, data, "RATE_ACTION", this, arg); 
}

(function($){
    $.fn.ServiceRateInvoke = function(uri, data, type, container, arg){                
         $.ajax({
                        url: uri,
                        data: data,
                        type: "POST",
                        contentType: "application/x-www-form-urlencoded",
                        dataType: "xml",
                                                                   
                    success: function(res){                      
                    var error = null;
                    $(res).find("Error").each(function(){error = $(this).text(); });
                        if (error == 0){                            
                               $(res).find("WrappedResources").find("Pair").each(function() {
                                  var marker = $(this); 
                                  if (type && type == "RATE_ENABLED"){
                                      if (marker.children('Key').text() == "USER_RATE_ENABLED"){
                                            if (marker.children('Value').text() == "False"){                                                
                                                $("#RatePlusActionID").children("img").attr("src", "c_images/plus.gif");
                                                $("#RateMinusActionID").children("img").attr("src", "c_images/minus.gif");         
                                                //bind click methods
                                                $("#RatePlusActionID").bind("click", function(e){RateAction(1);});
                                                $("#RateMinusActionID").bind("click", function(e){RateAction(-1);});                                                    
                                                $("#RatePlusActionID").attr("style", "cursor:pointer");
                                                $("#RateMinusActionID").attr("style", "cursor:pointer");
                                            }
                                            else{
                                                $("#RatePlusActionID").children("img").attr("src", "c_images/plus_inactive.gif");
                                                $("#RateMinusActionID").children("img").attr("src", "c_images/minus_inactive.gif");
                                                $("#RatePlusActionID").attr("style", "cursor:default");
                                                $("#RateMinusActionID").attr("style", "cursor:default");                                                   
                                           }
                                      }                               
                                }
                                if (type && type=="RATE_ACTION"){                                    
                                    if (marker.children('Key').text() == "ASSOCIATED_RATE_ID"){ 
                                        
                                        $("#RatePlusActionID").unbind();                                   
                                        $("#RateMinusActionID").unbind();
                                        $("#RatePlusActionID").children("img").attr("src", "c_images/plus_inactive.gif");
                                        $("#RateMinusActionID").children("img").attr("src", "c_images/minus_inactive.gif");
                                        $("#RatePlusActionID").attr("style", "cursor:default");
                                        $("#RateMinusActionID").attr("style", "cursor:default");                                                                   
                                        RATING_ASSOCIATIVE_ID = marker.children('Value').text();
                                        if (arg.mark > 0)
                                            $(".figure").append("<span class='green' style='font-size:10px;'>(+1)</span>");
                                        else
                                            $(".figure").append("<span class='red' style='font-size:10px;'>(-1)</span>");    
                                        //specially for single player games we don't want that add comment action after rating
                                        var mtype = $(".vote").attr("moduletype");
                                        if (mtype != SPGAMES_MODULETYPE)                                       
                                            centerRatePopup(arg.mark); 
                                    }
                                }
                                if (type && type=="ADD_COMMENT"){
                                    DropRatePopup();
                                }
                            });
                        }
                        //ERROR
                        else{
                            if (type && type=="ADD_COMMENT")
                               DropRatePopup();        
                             $(res).find("WrappedResources").find("Pair").each(function() {
                                      var marker = $(this); 
                                      if (marker.children('Key').text() == "GENERIC_ERROR"){                                            
                                            $("#ErrorPopUp").attr("style", "display:block");
                                            $("#ErrorPopUpMsg").html(marker.children('Value').text()); 
                                            LayoutErrorPopUp();
                                      }                                         
                                  });      
                        }                    
                    },                                                        
                    error:  function(xhr) {                          
                        alert('network error rate');                                     
                        return;
                    }
            });
        }        
    })(jQuery); 

function Votes_Init(){
    
    //Get moduleID, moduleType
    var moduleid = $(".vote").attr("moduleid");
    var mtype = $(".vote").attr("moduletype");
    
    //Make a service call to determine if user rate is enabled
    var url = "/WebServices/Rating/RatingAkazooItemService.asmx/GetRateEnabledStatus";
    var data = "ModuleID=" + moduleid + "&type=" + mtype;    
    
    var isratepermitted = ($(".vote").attr("isratepermitted")==1)?true:false;
    if (!isratepermitted){       
    }
    else{       
        $(this).ServiceRateInvoke(url, data, "RATE_ENABLED", this, null);    
    }
    
}

jQuery(document).ready(function() {    
    try{Sys.WebForms.PageRequestManager.getInstance().add_endRequest(Votes_Init);}catch(e){}        
    Votes_Init();
});