﻿var firstLoad = false;
$(document).ready(function() {
    AllNewsContentJS();
    filterByDropDownList();
    firstLoad = true;
    // Initialize history plugin.
    // The callback is called at once by present location.hash.
    $.historyInit(pageload);
    attachPaging();
});

function pageload(num) {
    // restore ajax loaded state
    //num = num-1
    var currNum = 1;
    if (num) {
        currNum = num;
    }
    if (!firstLoad) {
        PagingNewsList($("div.CP a[filter=" + currNum + "]"));
        $.scrollTo("div.AllNewsContent", 1000);
    }
    firstLoad = false;
}

function AllNewsContentJS() {
    $("div#editorsDV").mouseover(function() {
        $(this).css("overflow", "scroll");
        $(this).css("overflow-x", "hidden");
    }).mouseout(function() { $(this).css("overflow", "hidden"); });
    //attachPaging();
}
function attachPaging() {
    $("div.CP a").attr("rel", "history");

    $("div.CP a").each(function(i) {
        if ($(this).attr("class") != "CPS" && $(this).attr("class") != "CND" && $(this).attr("class") != "Pdim") {
            $(this).attr("href", "#" + $(this).attr("filter"));
        }
    });

    $("div.CP a").click(function() {
        if ($(this).attr("class") != "CPS" && $(this).attr("class") != "CND" && $(this).attr("class") != "Pdim") {

            if ($(this).attr("rel") == "history") {
                var hash = this.href;
                hash = hash.replace(/^.*#/, '');
                // moves to a new page. 
                // pageload is called at once. 
                $.historyLoad(hash);
            }
            //PagingNewsList(this);
            //$.scrollTo("div.AllNewsContent", 1000);              
        }
    });
}
function PagingNewsList(Obj) {
    var qstrparams, page;
    try {
        qstrparams = $(Obj).attr("qstrparams");
        page = $(Obj).attr("filter");
        LoadLatestNewsJson("AjaxPages/AllNewsList.aspx", "page=" + page + "&" + qstrparams);
    }
    catch (er) {
        //alert(er)
        qstrparams = document.location.search.substring(1);
        hashArr = document.location.href.split('#');
        page = hashArr[hashArr.length - 1];
        LoadLatestNewsJson("AjaxPages/AllNewsList.aspx", "page=" + page + "&" + qstrparams);
    }
    return false;
}
function LoadLatestNewsJson(PageUrl, QString) {
    $("#AllNewsDV").html("<img class='loading' src='images/loading.gif' />");
    $.post(PageUrl,
                         QStrToJson(QString),
                          function(data) {
                              $("#AllNewsDV").html(data.newsList);
                              attachPaging();
                              return false;
                          },
                          "json");
}

function filterByDropDownList() {
    $("div.AllNewsContent div div.SRFilter select").change(function() {
        dropDownChanged();
    });

    $("div.latestNews div.SRFilter select").change(function() {
         relateNewsDropDownChanged(this);
    });
}

function dropDownChanged() {
    var query = document.location.search.substring(1);
    var blogType = ($("select#BlogsType option:selected").val() == undefined) ? 0 : $("select#BlogsType option:selected").val();
    var NumOfDays = $("select#durations option:selected").val();
    var OrederBy = $("select#FilterBy option:selected").val();
    var params = query + "&OrderBy=" + OrederBy + "&NumOfDays=" + NumOfDays + "&blogType=" + blogType;
    LoadLatestNewsJson("AjaxPages/AllNewsList.aspx", params);
}
function relateNewsDropDownChanged(obj) {
    var params = $(obj).attr("params");    
    var NumOfDays =$(obj).children("option:selected").val();
    LoadRelatednewsFilteredJson("AjaxPages/RelatedNewsFiltered.aspx", params + "&NumOfDays=" + NumOfDays + "&isfiltered=true", $(obj).attr("relatenews"));
}
function LoadRelatednewsFilteredJson(PageUrl, QString,DivID) {
    $("#" + DivID + "DV").html("<img class='loading' src='images/loading.gif' />");
    $.post(PageUrl,
                         QStrToJson(QString),
                          function(data) {
                              $("#" + DivID + "DV").html(data.relatednews);
                              return false;
                          },
                          "json");
}

