﻿/// <summary>
///     Product:        Enewspaper
///     Classname:    	Common script
///     Version:        02.05 |	12 February, 2009 - 10 March, 2009
///     Purpose: 
///         The class common java script programming tasks.
/// 
///     Copyright 2009 by Digital Secure Co. All Rights Reserved. 
/// </summary>
/// <remark>
///     For Implement NewsCut mechanism do these task :
///
///     In Page.xslt put this line at Generating Image Map block.
///     <!-- .....hold news image [news cut] per news ID - only the text news has news cut !............... -->
///     NewsCutItems.put('<xsl:value-of select ="nws_Id"/>', '<xsl:value-of select ="NewsPhoto/prp_ImageURL"/>');
///
///     In Common.js put this lint at showContentForArea(divId) function
///      // hold current active news id.
///     SetCurrentNewsID(divId);
///
/// </remark>
/// <IsMultiEdition>True</IsMultiEdition>
/// <classVersion>006</classVersion>
///<EditingHistory>
///    Name:       Soren
///    Date:       10 August, 2009
///    Reason of change :
///         - Implement NewsView(newsID) function in order to  call a web service.
///</EditingHistory>
///<EditingHistory>
///    Name:       Soren
///    Date:       24 August, 2009
///    Reason of change :
///         - Remove NewsViewed function.
///         - add ShowRatingPanel() function.
///</EditingHistory>


// hold the Average-Rating [current value] per NewsID
//var NewsRateingItems = new Hashtable();

var CurrentNewsID = '0';

// hold the Web Service Utility URL
var NewsUtilityWebServiceURL = '';
var RatingEventGrabberURL = '';

/// Hold current NewsId.
function SetCurrentNewsID(newsID, WebPath) {
    CurrentNewsID = newsID;
    MakeAVisit(newsID);
    ShowRatingPanel();
    var commentIfram = document.getElementById("script-console");
    if ((commentIfram != '') || (commentIfram != null))
        commentIfram.src = WebPath + 'Comments/index.aspx?NewsID=' + CurrentNewsID;
}


// Submit the search
// the type should be "LastNewspaper" OR "CurrentNewspaper";
function SubmitQuickSearch(type) {
    var txt = document.getElementById("txtSearch").value

    if (txt.length < 3) {
        alert('برای انجام عمل جستجو باید یک کلمه با حداقل 3 حرف در مکان مورد نظر وارد کنید');
        return;
    }
    else {
        document.getElementById("limitation").value = type.toString();

        // Get Newspaper Number From Parent HTML Page
        document.getElementById("newspaperNumber").value = document.getElementById("nsn_Number").getAttribute("value");

        document.searchForm.submit();
    }
}

// init web news web service utility
function initRatingService(ratingEventGrabberURL, newsUtilityWebServiceURL) {
    RatingEventGrabberURL = ratingEventGrabberURL;
    NewsUtilityWebServiceURL = newsUtilityWebServiceURL;
}

// first call a web service to obtain current news reating average.
// show the 5 star rating panel for current news with specific news reating averag.
function ShowRatingPanel() {
    // clean the Rating div...
    document.getElementById('RatingHolder').innerHTML = '';

    jQuery.ajax({
        type: "POST",
        url: NewsUtilityWebServiceURL + "/GetNewsRateAverage",
        //        url: "http://dsserver/Sepide/ServiceCenter/NewsUtility.asmx/GetNewsRateAverage",
        data: '{nws_Id : "' + CurrentNewsID + '"}',
        //        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            var currentNewsRateAverage = data.d;
            jQuery('#RatingHolder').rater(RatingEventGrabberURL, { style: 'basic', curvalue: currentNewsRateAverage });
        }
         , error: function(xmlHttpRequest, status, err) {
             // Debug Only
             //            alert('Sorry! Error happens.' + err);
             // when an error occur - init with 0 strar High Light.. 
             jQuery('#RatingHolder').rater(RatingEventGrabberURL, { style: 'basic', curvalue: 0 });
         }
    });

}

function MakeAVisit(NewsId) {
    // call web service
    //alert(NewsId);

    jQuery.ajax({
        type: "POST",
        url: "/ServiceCenter/NewsUtility.asmx/MakeAVisit",
        /*url: "http://dsserver/den/ServiceCenter/NewsUtility.asmx/NewspaperNumberViewed", */
        data: '{NewsId : "' + NewsId + '"}',
        //        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            message = data.d;
            //alert(message);
            //if (document.getElementById("NewspaperNumberViewCount") != null) {
            //    document.getElementById("NewspaperNumberViewCount").innerHTML = 'تعداد بازدید : ' + message;
            //}
        }
         , error: function(xmlHttpRequest, status, err) {
             // Debug Only
             //alert('Sorry! Error happens in "MakeAVisit()" , detail : ' + err);
         }
    });
}
    
   

