﻿var loadingTemplate = "<div class=\"loadingTemplate\"></div>";
var ratingsPage = "";

function ShowReviews(entityID, entityType) {
    var $dialog = JQ("#dialog");
    $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', '');

    ratingsPage = pathToRatingPages + "/ViewRatings.aspx?EntityID=" + entityID + "&EntityType=" + entityType + "&Culture=" + culture + "&SelectedPage=1"; // + selectedPage;

    JQ.get(ratingsPage, {}, function(responseText) {
        $dialog.html(JQ(responseText).find('.wrapper').html());
        $dialog.dialog('option', 'title', JQ('#ratingListPopupTitle').html());

    }, "html");
}

function ChangePage(pageNumber) {
    JQ("#dialog").html(loadingTemplate);
    ratingsPage = ratingsPage.replace(/SelectedPage=[0-9]+/, 'SelectedPage=' + pageNumber);

    JQ.get(ratingsPage, {}, function(responseText) {
        JQ("#dialog").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();
                 var ratingCulture = $ratingDetailsDiv.find('.RatingCulture').text().toLowerCase();
                 var cmsCulture = culture.substring(0,2).toLowerCase();
                 
                 $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, COPYCONTENT, false, CLICKCLOSE, true);        
        return;
    }

    JQ.get(pathToRatingPages + "/ViewRatingDetailsForCategory.aspx?RatingID=" + ratingID + "&RatingCategory=" + ratingCategory + "&Culture=" + culture, {},
             function(responseText) {
                 $ratingDetailsDiv.html(JQ(responseText).find('.wrapper').html());
                 var ratingCulture = $ratingDetailsDiv.find('.RatingCulture').text().toLowerCase();               
                 var cmsCulture = culture.substring(0,2).toLowerCase();
                                   
                 $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="translation">"' + result.translation +'"<div class="googleLabel">' + (window.translatedWith == undefined ? 'translated with' : translatedWith) + '</div></div>');                                                                                                                                          
                            });
                         }
                     }                             
                 });
                 TagToTip(detailsDivID, FADEOUT, 0, BGCOLOR, '#FFFFFF', BORDERCOLOR, '#333333', PADDING, 4, COPYCONTENT, false, CLICKCLOSE, true);
             }, "html");
}  
