﻿var lastNewsDate = -1;
var lastNewsIndex = -1;
var lastScrollTop = 200;
var lastBulkIndex = 0;
var isLoadPending = true;

var IEBrowser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

function afterLoad()
{
  img = new Image();
  img.src = "image/ver2/li_star_3.gif";

    window.onscroll = function scrollPage()
    {
        if (pageName == "news/photo/index_old") {
            if (document.body.scrollTop > lastScrollTop)
            {
                if (isLoadPending == false)
                {isLoadPending = true;setTimeout("getNewsStream(lastNewsDate, lastNewsIndex)", 1200);}
            }
        }
    }
    if (pageName == "news/photo/index_old") {
        if (lastNewsDate == -1)
        {lastNewsDate = parseInt(lastNewsYear)+1 + "0000";lastNewsIndex = 0;}
        getNewsStream(lastNewsDate, lastNewsIndex);
    }
    if (pageName == "search/index")
    {document.getElementById("tbWordStr").focus();}
}

var ArrBox1InnerY = new Array (0,0,0);
var lastBox1ItemIndex = new Array (0,0,0);;
function scrollBox1(val,divID,id)
{
    if ((lastBox1ItemIndex[id] + val < 0) | (lastBox1ItemIndex[id] + val > 4))
    {
        return;
    }

    lastBox1ItemIndex[id] = lastBox1ItemIndex[id] + val;
		box1ScrollTo(val,divID,id);
}

function box1ScrollTo(val,divID,id)
{
    var canSet = false;
    if (val > 0)
    {
        if (ArrBox1InnerY[id] < lastBox1ItemIndex[id] * 22)
        {
            canSet = true;
            ArrBox1InnerY[id] += 3;
            setTimeout("box1ScrollTo(" + val + ",'"+ divID +"',"+id+")", 20);
        }
    }
    else
    {
        if (ArrBox1InnerY[id] > lastBox1ItemIndex[id] * 22)
        {
            canSet = true;
            ArrBox1InnerY[id] -= 3;
            setTimeout("box1ScrollTo(" + val + ",'"+ divID +"',"+id+")", 20);
        }
    }
    if (canSet)
    {
        setTimeout(divID + ".style.top = -1 * ArrBox1InnerY["+id+"]", 0);
    }
}

var ArrBox2InnerX = new Array (0,0,0);
var lastBox2ItemIndex = new Array (0,0,0);
function scrollBox2(val,divID,id)
{
    if ((lastBox2ItemIndex[id] + val < 0) | (lastBox2ItemIndex[id] + val > 5))
    {
        return;
    }

    lastBox2ItemIndex[id] = lastBox2ItemIndex[id] + val;
		box2ScrollTo(val,divID,id);
}

function box2ScrollTo(val,divID,id)
{
    if (val > 0)
    {
        if (ArrBox2InnerX[id] < lastBox2ItemIndex[id] * 95)
        {
            ArrBox2InnerX[id] += val * 5;
            setTimeout("box2ScrollTo(" + val + ",'"+ divID +"',"+id+")", 5);
        }
    }
    else
    {
        if (ArrBox2InnerX[id] >= lastBox2ItemIndex[id] * 95)
        {
            ArrBox2InnerX[id] += val * 5;
            setTimeout("box2ScrollTo(" + val + ",'"+ divID +"',"+id+")", 5);
        }
    }
    setTimeout(divID + ".style.right = -1 * ArrBox2InnerX["+id+"]", 0);
}

var ArrBoxYearInnerX = new Array (0,0,0,0,0);
var lastBoxYearItemIndex = new Array (0,0,0,0,0);
function scrollBoxYear2(val,divID,id,num)
{
    if ((lastBoxYearItemIndex[id] + val < 0) | (lastBoxYearItemIndex[id] + val > num))
        return;

    lastBoxYearItemIndex[id] = lastBoxYearItemIndex[id] + val;
		boxYearScrollTo(val,divID,id);

		}
function scrollBoxYear(val,divID,id)
{
    if ((lastBoxYearItemIndex[id] + val < 0) | (lastBoxYearItemIndex[id] + val > 3))
        return;

    lastBoxYearItemIndex[id] = lastBoxYearItemIndex[id] + val;
		boxYearScrollTo(val,divID,id);
}

function boxYearScrollTo(val,divID,id)
{   //because of different size in audio-year scroll box in top and bottom
    var constMove = 220;
    if (id==1)
        constMove = 110;
    else if (id==3)
       constMove = 300;
    else if (id==4)
       constMove = 240;

    if (val > 0)
    {
        if (ArrBoxYearInnerX[id] < lastBoxYearItemIndex[id] * constMove)
        {
            ArrBoxYearInnerX[id] += val * 10;
            setTimeout("boxYearScrollTo(" + val + ",'"+ divID +"',"+id+")", 5);
        }
    }
    else
    {
        if (ArrBoxYearInnerX[id] >= lastBoxYearItemIndex[id] * constMove)
        {
            ArrBoxYearInnerX[id] += val * 10;
            setTimeout("boxYearScrollTo(" + val + ",'"+ divID +"',"+id+")", 5);
        }
    }
    setTimeout(divID + ".style.right = -1 * ArrBoxYearInnerX["+id+"]", 0);
}

var ArrBoxKeywordInnerY = 0;
var lastBoxKeywordItemIndex = 0;
function scrollBoxKeyword(val,divID,tabsCount)
{
    if ((lastBoxKeywordItemIndex + val < 0) | (lastBoxKeywordItemIndex + val > tabsCount-1))
    {
        return;
    }

    lastBoxKeywordItemIndex = lastBoxKeywordItemIndex + val;
		boxKeywordScrollTo(val,divID);
}

function boxKeywordScrollTo(val,divID)
{
    var canSet = false;
    if (val > 0)
    {
        if (ArrBoxKeywordInnerY < lastBoxKeywordItemIndex * 27)
        {
            canSet = true;
            ArrBoxKeywordInnerY +=  3;
            setTimeout("boxKeywordScrollTo(" + val + ",'"+ divID +"')", 10);
        }
    }
    else
    {
        if (ArrBoxKeywordInnerY > lastBoxKeywordItemIndex * 27)
        {
            canSet = true;
            ArrBoxKeywordInnerY -=  3;
            setTimeout("boxKeywordScrollTo(" + val + ",'"+ divID +"')", 10);
        }
    }
    if (canSet)
    {
        setTimeout(divID + ".style.top = -1 * ArrBoxKeywordInnerY", 0);
    }
}

function loadXMLFromString(xmlText)
{
    var xmlDoc;

    // IE
    if (window.ActiveXObject)
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xmlText);
    }
    // Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlText, "text/xml");
    }
    else
    {
        showOldNavigatorVersion();
    }

    return xmlDoc;
}

function loadTemplateXML()
{
    return loadXMLFromString("<?xml version='1.0' encoding='utf-8'?><root/>");
}
function getHttpRequest()
{
    var xhr;
    if (window.XMLHttpRequest)
    {xhr = new XMLHttpRequest();}
    else
    if (window.ActiveXObject)
    {
        try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e)
        {
            try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e2)
            {
                try {  xhr = new XMLHttpRequest();     }
                catch (e3)
                {xhr = false;}
            }
        }
    }

    return xhr;
}

function getNewsStream(fromIDate, fromIndex)
{
    var xhr = getHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                var rDoc = loadXMLFromString(xhr.responseText);

                var midX = document.body.clientWidth / 2;
                var midY = document.body.clientHeight / 2;

                var divPhotos = document.getElementById('divPhotos');
                var rN = rDoc.documentElement.childNodes[0];
                var row = (lastBulkIndex / 2) + (lastBulkIndex % 2) ;
              //childNode[0] is Meta Field Description
                for (var j = 1; j < rN.childNodes.length; j++)
                {
                  var row1N = rN.childNodes[j];

                    var divPhotoAlbum = document.createElement('div');
                    var id = row1N.childNodes[0].childNodes[0].nodeValue;

                    var title = new String(findNode(row1N, "title"));
                    var yyyy = new String(findNode(row1N, "start_year"));
                    var mm = new String(findNode(row1N, "start_month"));
                    var dd = new String(findNode(row1N, "start_day"));

                    title = dd + "/" + mm + "/" + yyyy + " - " + title;

                    if (title.length > 80)
                    {
                        title = title.substr(1, 80) + "...";
                    }
                    divPhotoAlbum.style.position = "relative";
                    divPhotoAlbum.style.width = "300px";
                    divPhotoAlbum.style.height = "220px";
                    divPhotoAlbum.style.margin = "10px";

                    divPhotoAlbum.innerHTML += generatePhotoAlbumTable(id, title);
                    divPhotos.appendChild(divPhotoAlbum);
                }
                lastBulkIndex = lastBulkIndex + rN.childNodes.length-1;

                var lastNode = rN.childNodes[rN.childNodes.length - 2];
                lastNewsDate = parseInt(findNode(lastNode, "start_year"))*10000+parseInt(findNode(lastNode, "start_month"))*100+parseInt(findNode(lastNode, "start_day"));
                lastNewsIndex = findNode(lastNode, "id");

                if (document.body.scrollTop > 0)
                {
                    document.body.scrollTop = document.body.scrollTop + 70;
                }
                lastScrollTop = document.body.scrollTop;
                isLoadPending = false;
            }
            else
            {
                showRequestError(xhr.status);
            }
        }
    }

    xhr.open("GET", "news-stream@nti=" + newsTypeID + "&fd=" + fromIDate + "&fdi=" + fromIndex, true); //true: No wait
    xhr.send("");
}

function getPhotoAlbumStream(id, typeCode)
{
    var xhr = getHttpRequest();
    xhr.open("GET", "photo-album-stream@id=" + id + "&t=" + typeCode, false); //true: No wait
    xhr.send("");

    var rDoc = loadXMLFromString(xhr.responseText);

    var rN = rDoc.documentElement.childNodes[0];
    var res = new Array();
    for (var j = 0; j < rN.childNodes.length; j++)
    {
        res[j] = rN.childNodes[j];
    }

    return res;
}

function findNode(rN, name)
{
    if (rN == null) return;
    if (rN.childNodes == null) return;
    for (var i = 0; i < rN.childNodes.length; i++)
    {
        if (rN.childNodes[i].nodeName == name)
        {
            if (rN.childNodes[i].childNodes[0] == null) return "";
            else return rN.childNodes[i].childNodes[0].nodeValue;
        }
    }
    return null;
}

function generatePhotoAlbumTable(albumID, title, img1)
{

    var s = "<div id='divAl_" + albumID + "' style='width: 100%; height: 100%'><table width='100%' cellpadding='0' cellspacing='0' border='0'" +
            "style='; border: 0px; height: 100%'>" +
            "<tr>" +
              "<td width='42px' height='41' align='center'>" +
                "<img src='image/photo/top-right.gif' width='42px' height='41' border='0'/>" +
              "</td>" +
              "<td width='100%' height='41' style='background-color: white;text-align: right; font-size: 8pt; border-top: 2px solid #273c82; padding: 1px'>" + title +
              "</td>" +
              "<td height='41' align='center' " +
                "style='background-image: url(image/photo/top-left.gif); width: 29px'>" +
                "<a onclick='javascript:openAlbum(" + albumID + ");' id='lnkAl_" +
                    albumID + "' style='cursor: pointer'>" +
                    "<img src='image/photo/link-h.gif' width='29' height='41' border='0'/></a>" +
              "</td>" +
            "</tr>" +
            "<tr>" +
              "<td align='center' colspan='3' style='background-color: #E1E6EA; width: 100%; height: 100%; border: 2px solid #273c82'>" +
                "<div id='divAlPhs_" + albumID + "' style='width: 100%; height: 100%; text-align: center; vertical-align: middle; padding: 2px'>" +
                    "<img style='display: none' border='1' style='border-color: black' id='imgAlbum_" + albumID + "'/>" +
                "</div>" +
              "</td>" +
            "</tr>" +
          "</table></div>";

    setTimeout("loadAlbumImage(" + albumID + ");", 5);

    return s;
}

function loadAlbumImage(albumID)
{
    var xhr = getHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {

                var rDoc = loadXMLFromString(xhr.responseText);

                var rN = rDoc.documentElement.childNodes[0];
                var res = new Array();

                //for (var j = 0; j < rN.childNodes.length; j++)
                {
                  //  if (j == 0)
				  var j=1;
                    {
                        var img = rN.childNodes[j];
                        var d1 = new String(findNode(img, "idt"));
                        var id = findNode(img, "id");
                        var img1Path = albumID + "A/" + findNode(img, "h");
                        document.getElementById("imgAlbum_" + albumID).src = "ndata/news/" + img1Path;
                        document.getElementById("imgAlbum_" + albumID).style.display = "";
                        document.getElementById("lnkAl_" + albumID).setAttribute("def-image-url", img1Path);
                        //break;
                    }
                }
            }
            else
            {
                showRequestError(xhr.status);
            }
        }
    }

    xhr.open("GET", "photo-album-stream@id=" + albumID + "&t=A", true);
    xhr.send("");
}

function focusOnCtrl(firstControlID)
{
    var defCtrl = document.getElementById(firstControlID);
    if (defCtrl != null)
    {
        defCtrl.focus();
    }
}

function showRequestError(code)
{
    //alert("ارتباط با سرويس دهنده قطع است.\r\n" + "کد: " + code);
}

function changeImageSrc(img, newPath)
{
    img.src = newPath;
}
var tempXml = null;

var currBoxWidth = 0;
var currBoxHeight = 0;
var currBoxScale = 1;
function scrollWidth(id)
{
    var divAl = document.getElementById('divAl_' + id);
    var divAlCnt = divAl.parentNode;
    currBoxWidth += 15;
    divAlCnt.style.width = currBoxWidth;
    if (currBoxWidth <= 630)
    {
        setTimeout("scrollWidth('" + id + "')",3);
    }
    else
    {
        divAlCnt.style.width = 630;

        var divAlPhs = document.getElementById("divAlPhs_" + id);
        var divAlLnk = document.getElementById("lnkAl_" + id);

        divAlPhs.style.overflowY = "scroll";

        var imgList = getPhotoAlbumStream(id, "A");
        var s = "<table width='100%' cellpadding='3' cellspacing='0' border='0'>";
		//i=0 is Meta filed description
        for (var i=1; i < imgList.length; i++)
        {
            var img1 = imgList[i];
            var d1 = new String(findNode(img1, "idt"));
            var pid = findNode(img1, "id");
            var img1Path = id + "A/" + findNode(img1, "h");

            s += "<td align='center' width='33%'><a href='photo-album@id=" + id + "&pid=" + pid + "'>" +
                "<img style='border: 1px solid #333333;' src='ndata/news/" + img1Path + "'/></a></td>";

            if (i % 3 == 2)
            {
                if (i > 0)
                {
                    s += "</tr>";
                }
                s += "<tr>";
            }
        }
        s += "</table>";
        divAlPhs.innerHTML = s;

        divAlLnk.setAttribute("state", "open");
        divAlLnk.innerHTML = "<img src='image/photo/link.gif' width='29' height='41' border='0'/>";
    }
}

function scrollHeight(id)
{
    var divAl = document.getElementById('divAl_' + id);
    var divAlCnt = divAl.parentNode;
    currBoxHeight += 15;
    divAlCnt.style.height = currBoxHeight;
    if (currBoxHeight <= 340)
    {
        setTimeout("scrollHeight('" + id + "')",10);
    }
}

function openAlbum(id)
{
    var divAl = document.getElementById("divAl_" + id);
    var divAlCnt = divAl.parentNode;

    var divAlLnk = document.getElementById("lnkAl_" + id);

    currBoxWidth = 300;
    currBoxHeight = 220;

    var isOpen = true;

    if (divAlLnk.attributes["state"] == null)
    {
        isOpen = false;
    }
    else
    if ((divAlLnk.attributes["state"].value == "") | (divAlLnk.attributes["state"].value == "null"))
    {
        isOpen = false;
    }

    if (!(isOpen))
    {
        currBoxScale = 1;

        divAlCnt.style.zIndex = "500";

        scrollWidth(id);
        scrollHeight(id);
    }
    else
    {
        currBoxScale = -1;
        divAlCnt.style.zIndex = null;

        var divAlPhs = document.getElementById("divAlPhs_" + id);

        divAlPhs.style.overflowY = "";
        divAlPhs.innerHTML = "<img src='ndata/news/" + divAlLnk.attributes["def-image-url"].value + "'/>";
        divAlCnt.style.width = currBoxWidth;
        divAlCnt.style.height = currBoxHeight;

        divAlLnk.setAttribute("state", null);
        divAlLnk.innerHTML = "<img src='image/photo/link-h.gif' width='29' height='41' border='0'/>";
    }
}

var currAlpha = 0;
function setAlpha(ctrlID)
{
    var ctrl = document.getElementById("Section_Box");
    if (currAlpha >= 100)
    {
        ctrl.style.filter = null;
    }
    else
    {
        ctrl.style.filter = "alpha(opacity=" + currAlpha + ")";
    }
    currAlpha += 20;
    if (currAlpha <= 100)
    {
        setTimeout("setAlpha('" + ctrlID + "')",1);
    }
}

function updateNews()
{
    var doc = loadXMLFromString(tempXml);
    var section = document.getElementById("Secton_Box_Container");

    var title = new String(doc.documentElement.childNodes[0].childNodes[0].getElementsByTagName("c")[0].childNodes[0].nodeValue);

    section.innerHTML = "";
    var divContent = document.createElement("div");
    divContent.innerHTML += "<h3>" + title + "</h3>";
    divContent.innerHTML += "<div>" + doc.documentElement.childNodes[0].childNodes[0].getElementsByTagName("f")[0].childNodes[0].nodeValue +
        "</div>";
    section.appendChild(divContent);

//    currAlpha = 0;
//    section.style.filter = "alpha(opacity=0)";
    section.disabled = false;
//    setAlpha("Section_Box");

    doc = null;
    tempXml = null;
}

function getList(ctrl, url) //content
{
    var obj = document.getElementById(ctrl);

    var xhr = getHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                obj.innerHTML = xhr.responseText;
            }
            else
            {
                showRequestError(xhr.status);
            }
        }
    }

    xhr.open("POST", url, true);
    xhr.send("");
}

function postComment()
{
    var xhr = getHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                alert(xhr.responseText);
            }
            else
            {
                showRequestError(xhr.status);
            }
        }
    }

    xhr.open("POST", "others-post-comment", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("txtCaptcha="+document.getElementById("txtCaptcha").value
          +"&txtName="+document.getElementById("txtName").value
          +"&txtEmail="+document.getElementById("txtEmail").value
          +"&txtComment="+document.getElementById("txtComment").value);
}

