﻿var loadingTemplate = "<div class=\"loadingTemplate\"></div>";
var ratingsPage = "";

function getRatingPageUrl(entityID, entityType, page, parentSelector) {
    return pathToRatingPages + "/ViewRatings.aspx?EntityID=" + entityID + "&EntityType=" + entityType + "&Culture=" + culture + "&SelectedPage=" + page + "&ResultDivSelector=" + escape(parentSelector);   
}

function OpenDialog(popupID) {
    var $dialog = JQ(popupID);
    $dialog.dialog({ modal: true, autoOpen: false, bgiframe: true, position: ['center', 25] });
    $dialog.dialog('option', 'width', 490).dialog('option', 'height', 630).dialog('open').html(loadingTemplate).dialog('option', 'title', '');  
}

function ShowReviews(entityID, entityType) {
    dialogID = '#dialog';
    
    OpenDialog(dialogID);
    ratingPageUrl = getRatingPageUrl(entityID, entityType, 1, dialogID);

    $dialog = JQ(dialogID);
    JQ.get(ratingPageUrl, {}, function (responseText) {
        $dialog.html(JQ(responseText).find('.wrapper').html());
        $dialog.dialog('option', 'title', $dialog.find('#ratingListPopupTitle').html());
    }, "html");
}

function ChangePage(entityID, entityType, page, parentSelector) {
    $parent = JQ(parentSelector);
    $parent.html(loadingTemplate);
    ratingPageUrl = getRatingPageUrl(entityID, entityType, page, parentSelector);  

    JQ.get(ratingPageUrl, {}, function (responseText) {
        $parent.html(JQ(responseText).find('.wrapper').html());
    }, "html");
}

function ShowRatingDetails(ratingID) {
    var $ratingDetailsDiv = JQ("#ratingDetails");
    $ratingDetailsDiv.dialog({ modal: true, bgiframe: true, autoOpen: false, width: 580, position: ['center', 80] })
                         .dialog('open').html(loadingTemplate).dialog('option', 'title', '');

    JQ.get(pathToRatingPages + "/ViewRatingDetails.aspx?RatingID=" + ratingID + "&Culture=" + culture, {},
             function(responseText) {

                 $ratingDetailsDiv.html(JQ(responseText).find('.wrapper').html());
                 var $tabs = $ratingDetailsDiv.find('.ratingTabs').tabs();

                 $ratingDetailsDiv.find('.annotationText').each(function() {
                     var $annDiv = JQ(this);
                     if ($annDiv.html().replace(/ /g, '') == '')
                         $annDiv.parent().hide();
                     else {
//                         if (ratingCulture != cmsCulture) {
//                              google.language.translate($annDiv.text(), ratingCulture, cmsCulture, function(result) {
//                                if (!result.error)                                                                       
//                                   $annDiv.parent().append('<div class="annotationTranslation"><a class="googleBtn" href="javascript:void(0)" onmouseover="Tip(\'' + String(result.translation).replace("&#39;","\\'") + '\', WIDTH, 200)" onmouseout="UnTip()"><span>' + (window.translateLabel == undefined ? 'translate' : translateLabel) + '</span></a></div>');                                                                     
//                              });
                         }                                                  
                 });

                 $ratingDetailsDiv.dialog('option', 'title', $ratingDetailsDiv.find('#ratingDetailsPopupTitle').html().replace(/^\s*|\s*$/g, ''));
             }, "html");
}

function ShowDetailsTooltip(ratingID, ratingCategory, detailsDivID) {
    var $ratingDetailsDiv = JQ('#' + detailsDivID);
    if ($ratingDetailsDiv.html() != "") {
        TagToTip(detailsDivID, FADEOUT, 0, BGCOLOR, '#FFFFFF', BORDERCOLOR, '#333333', PADDING, 4);
        return;
    }

    JQ.get(pathToRatingPages + "/ViewRatingDetailsForCategory.aspx?RatingID=" + ratingID + "&RatingCategory=" + ratingCategory + "&Culture=" + culture, {},
             function(responseText) {
                 $ratingDetailsDiv.html(JQ(responseText).find('.wrapper').html());

                 $ratingDetailsDiv.find('.annotationText').each(function() {
                     var $annDiv = JQ(this);
                     if ($annDiv.html().replace(/ /g, '') == '')
                         $annDiv.parent().hide();
                     else {
//                         if (ratingCulture != cmsCulture) {
//                              google.language.translate($annDiv.text(), ratingCulture, cmsCulture, function(result) {
//                                if (!result.error)                                                                       
//                                   $annDiv.parent().append('<div class="annotationTranslation"><a class="googleBtn" href="javascript:void(0)" onmouseover="Tip(\'' + String(result.translation).replace("&#39;","\\'") + '\', WIDTH, 200)" onmouseout="UnTip()"><span>' + (window.translateLabel == undefined ? 'translate' : translateLabel) + '</span></a></div>');                                                                     
//                              });
                     }     
                 });
                 TagToTip(detailsDivID, FADEOUT, 0, BGCOLOR, '#FFFFFF', BORDERCOLOR, '#333333', PADDING, 4);
             }, "html");
}  
