﻿// <summary>
//     Product:     Enewspaper
//     Classname:   Java Script
//     Version:     02.02 
//     Programmer:  Elham Esmaieli[elhamesmaieli@gmail.com] 
//     Purpose: 
//         All required JS to make HTML page dynamic.
// 
//     Copyright 2009 by Digital Secure Co. All Rights Reserved. 
// </summary>
// <IsMultiEdition>False</IsMultiEdition>
// <classVersion>002</classVersion>
// <EditingHistory>
//     Name:       Elham
//     Date:       22 Dec, 2008
//     Reason of change :
//         - Creating  document.
// </EditingHistory>
// <EditingHistory>
//     Name:       Elham
//     Date:       20 Jun, 2009
//     Reason of change :
//         - Editing "createImageMap()" & "createImageMap4ImageNews()" function to show border in correct position
// </EditingHistory>


var panState = '';//Keeps div id of opened sub title pan
var LastNewsContent = '0'; //A global variable to always keeps last news content id

/* -------Global Functions------------  */
//This function changes the background image of next and previous buttons
function buttonBgOverOut(imgId, imgSrc){
    document.getElementById(imgId).src='url(' + imgSrc + ')';
}

//This function make highlight btn during mouse over
function buttonImgOverOut(imgId, imgSrc){
    document.getElementById(imgId).src = imgSrc;
}

//This function change class name
function changeClass(objId, objClass){
    document.getElementById(objId).className = objClass;
}

//This function make highlight pagging number by clicking
function changePageNumber(){
    var len = document.getElementById('pageNumberHolder').childNodes.length;
    for(var i=0; i<len; i++){       
        if(document.getElementById('pageNumberHolder').childNodes.item(i).className == 'paggNumberingHolderSelected white'){
            document.getElementById('pageNumberHolder').childNodes.item(i).className = 'paggNumberingHolder black';
            break;
        }        
    }    
}

//This function creat next pagging button in both conditions, disabled or enabled
function putNextPagging(nextParam, src, thmColor){
    if (nextPageURL == ''){
        document.getElementById('next').innerHTML = '<img src="' + src + 'images/theme_' + thmColor + '/pagging/vataneEmrooz_nextDis.jpg" width="18" height="18" border="0" alt="بعدی" />';
    }
    else{    
        document.getElementById('next').innerHTML = '<img src="' + src + 'images/theme_' + thmColor + '/pagging/vataneEmrooz_paggingNext.jpg" width="18" height="18" border="0" alt="بعدی" onclick="goUrl(\'' + nextParam + '\');" />';
    }      
}

//This function creat previous pagging button in both conditions, disabled or enabled
function putPrevPagging(prevParam, resource, thmColor){
    if (prevParam == ''){
        document.getElementById('prev').innerHTML = '<img src="' + resource + 'images/theme_' + thmColor + '/pagging/vataneEmrooz_prevDis.jpg" width="18" height="18" border="0" alt="قبلی"/>';
    }
    else{ 
        document.getElementById('prev').innerHTML = '<img src="' + resource + 'images/theme_' + thmColor + '/pagging/vataneEmrooz_paggingPrev.jpg" width="18" height="18" border="0" alt="قبلی" onclick="goUrl(\'' + prevParam + '\');" />';
    }       
}

//This function set the height of all effected element height by newspaper image height
function setAllHeightyImageSize(imgH){
    if (imgH > 782){
        document.getElementById('mapNewsPaperHolder').style.height = imgH + 'px';
        document.getElementById('mapDivHolder').style.height = imgH + 'px';
        document.getElementById('newspaperPageHolder').style.height = (parseInt(imgH) + 24) + 'px';
        document.getElementById('newspaperPageInside').style.height = imgH + 'px';
        document.getElementById('paggingHolderRH').style.height = (parseInt(imgH)) + 'px';
        document.getElementById('paggingHolderLH').style.height = (parseInt(imgH)) + 'px';    
        document.getElementById('rightSidebar').style.height = (parseInt(imgH) + 65) + 'px';    
        document.getElementById('leftSidebar').style.height = (parseInt(imgH) + 65) + 'px';    
        document.getElementById('bodyHolder').style.height = (parseInt(imgH) + 65) + 'px';    
        document.getElementById('mainBody').style.height = (parseInt(imgH) + 64) + 'px';
        document.getElementById('rSide').style.height = (parseInt(imgH) + 53) + 'px';    
        document.getElementById('rSideIn').style.height = (parseInt(imgH) + 39) + 'px';    
        document.getElementById('lSide').style.height = (parseInt(imgH) + 53) + 'px';
        document.getElementById('lSideIn').style.height = (parseInt(imgH) + 39) + 'px';
    }
}

//This function set the height of all effected element height by newspaper image height
function setInternalHeightSize(imgH){
    document.getElementById('internalBodyHolder').style.height = imgH + 'px';
    document.getElementById('internalRightSidebar').style.height = imgH + 'px';
    document.getElementById('internalMainBody').style.height = (parseInt(imgH) - 1) + 'px';
    document.getElementById('internalLeftSidebar').style.height = imgH + 'px';
    
    document.getElementById('pageContentHolder').style.height = (parseInt(imgH) - 51) + 'px';    
    document.getElementById('rSide').style.height = (parseInt(imgH) - 12) + 'px';    
    document.getElementById('rSideIn').style.height = (parseInt(imgH) - 12) + 'px';
    document.getElementById('lSide').style.height = (parseInt(imgH) - 12) + 'px';    
    document.getElementById('lSideIn').style.height = (parseInt(imgH) - 12) + 'px';    
}

/* -------Show and hide right pan tree------------  */
//--------Main title------This function expand and collaps the main papaer title side pan menu
function showHideNewsTitle(divId, HTMLResourcePath, thmColor){
    stsN = document.getElementById(divId).style.display;
    if(stsN == 'none'){
        document.getElementById(divId).style.display = 'block';
        document.getElementById('tree_'+divId).src = HTMLResourcePath + 'images/theme_' + thmColor + '/sideBar/vataneEmrooz_closeTree.jpg';
        return;
        }
    else if(stsN == 'block'){
        document.getElementById(divId).style.display = 'none';
        document.getElementById('tree_'+divId).src = HTMLResourcePath + 'images/theme_' + thmColor + '/sideBar/vataneEmrooz_openTree.jpg';
        return;
        }        
}

//-------Sub title------This function expand and colaps sub title of side pan
function showHidePageIndex(divId, HTMLResourcePath, thmColor){
    //panState holds the open pan div id but it shouldn't be equal to current div id to close it 
    if ((panState != '') && (panState != divId)){
        document.getElementById(panState).style.display = 'none';
        document.getElementById('tree_'+panState).src = HTMLResourcePath + 'images/theme_' + thmColor + '/sideBar/vataneEmrooz_openTree.jpg';
    } 
    //sts holds the status of div pan display 
    sts = document.getElementById(divId).style.display;    
    //show and hide status of div pan 
    if(sts == 'none'){
        document.getElementById(divId).style.display = 'block';        
        document.getElementById('tree_'+divId).src = HTMLResourcePath + 'images/theme_' + thmColor + '/sideBar/vataneEmrooz_closeTree.jpg';
        panState = divId;
        return;
        }
    else if(sts == 'block'){
        document.getElementById(divId).style.display = 'none';
        document.getElementById('tree_'+divId).src = HTMLResourcePath + 'images/theme_' + thmColor + '/sideBar/vataneEmrooz_openTree.jpg';
        return;
        }        
}



/* -------News Content Related Functions----  */
//This function visible the Content Div
function showNewsContentDiv(){
    newsTitleSts = document.getElementById('newsContentHolder').style.display;
    hideLastHighlight();
    document.getElementById('newsContentHolder').style.display = 'block';
}

function hideNewsContentDiv(){    
    hideLastHighlight();
    document.getElementById('newsContentHolder').style.display = 'none';        
    LastNewsContent = '0';// set lastNewsContent to zero(empty)
}

//This function show highlight map area border by getting an ID
function lightContent(divId){
    showHighlightImageMap('map_' + divId);
}

//This function hide highlight map area border by getting an ID
function hideLightContent(divId){
    hideHighlightImageMap('map_' + divId);
}

//This function first hides last news map highlight, newspaper headlines and all newsContent except the current news content
//It works on clicking each map area highlight
function showContentForArea(divId){
    
    // hold current active news id.
    SetCurrentNewsID(divId);
    
    hideLastHighlight(); //hide last map area highlight
    LastNewsContent = 'newsContent_' + divId;
    hideAllContentDiv();
    showNewsContentDiv();
    document.getElementById('content_' + divId).style.display = 'block';
    showHighlightImageMap('map_' + divId);    
}

//This function returns an object by id
function $(id){
    return document.getElementById(id);
}

//This function gets object id and makes an array of all tagName which choosed
function $$(item, tagName){
    if (typeof(item) == "string"){
        if ($(item) != null)
            return $(item).getElementsByTagName(tagName);
        else
            return null;
    }
    else{
        if (item.getElementsByTagName != null){
            return item.getElementsByTagName(tagName);
        }
        else{
           return null; 
        }
    }
}

//This function hides all news content Div
function hideAllContentDiv(){
    var items = $$("allContent", "div");
    for (var i = 0; i < items.length; i++){
        document.getElementById(items[i].id).style.display = 'none';
    }	    
}

//This function first hides last news map highlight, newspaper headlines and all newsContent except the current news content
//It works on clicking each map area highlight
function showImageForArea(divId,URLAddress){    
    goUrlInNewWindow(URLAddress);
}



/* -------Redirect to specific URL address ------------ */
//This function gets a url and changes pages url by that
function goUrl(URLAddress) {
    if (URLAddress != "") {
      document.location.href = URLAddress;
    }
  }
  
//This function gets a url aand open it in new window
function goUrlInNewWindow(URLAddress) {
    if (URLAddress != "") {
      window.open(URLAddress);
    }
  } 
  
//This function gets the global variable value to hide previous map area highlight
function hideLastHighlight(){
    if (LastNewsContent != 0){
        i = getNewsId(LastNewsContent);
        lni = 'map_' + i;
        document.getElementById(lni).style.border='none';
    }
}

//This function gets coords of map shape and creates a div element for each one and by newsId parameters gives them a unice id
//in onmouseover event calls "showHighlightImageMap(divId)" to highlight the div
//in onmouseout event calls "hideHighlightImageMap(divId)" to hide div
//in onclick event calls "showContentForArea(newsId)" to show related news content
function createImageMap(xyStr, newsId){    
    var xy = new Array();
    for(var i=0; i<3; i++){
        f = xyStr.indexOf(',');
        xy[i] = xyStr.substring(0,f);
        xyStr = xyStr.substring(f+1); 
    }
    xy[3] = xyStr;			    
    w = parseInt(xy[2]) - parseInt(xy[0]);
    h = parseInt(xy[3]) - parseInt(xy[1]);
    xy[0] = parseInt(xy[0]) - 4;
    xy[1] = parseInt(xy[1]) - 4;
    document.getElementById('mapDivHolder').innerHTML += '<div id="map_'+ newsId +'" style="position:absolute; left:' + xy[0] + 'px; top:' + xy[1] + 'px; width:' + w + 'px; height:' + h + 'px; " onmouseover="showHighlightImageMap(\'map_'+ newsId +'\');"  onmouseout="hideHighlightImageMap(\'map_'+ newsId +'\');" onclick="showContentForArea(\''+ newsId +'\');">&nbsp;</div>';    
}

//This function gets coords of map shape and creates a div element for each one and by newsId parameters gives them a unice id
//in onmouseover event calls "showHighlightImageMap(divId)" to highlight the div
//in onmouseout event calls "hideHighlightImageMap(divId)" to hide div
//in onclick event calls "showImageForArea(newsId)" to show related news content
function createImageMap4ImageNews(xyStr, newsId ,URLAddress){    
    var xy = new Array();
    for(var i=0; i<3; i++){
        f = xyStr.indexOf(',');
        xy[i] = xyStr.substring(0,f);
        xyStr = xyStr.substring(f+1); 
    }
    xy[3] = xyStr;			    
    w = parseInt(xy[2]) - parseInt(xy[0]);
    h = parseInt(xy[3]) - parseInt(xy[1]); 
    xy[0] = parseInt(xy[0]) - 4;
    xy[1] = parseInt(xy[1]) - 4;
    document.getElementById('mapDivHolder').innerHTML += '<div id="map_'+ newsId +'" style="position:absolute; left:' + xy[0] + 'px; top:' + xy[1] + 'px; width:' + w + 'px; height:' + h + 'px; " onmouseover="showHighlightImageMap(\'map_'+ newsId +'\');"  onmouseout="hideHighlightImageMap(\'map_'+ newsId +'\');" onclick="showImageForArea(\''+ newsId +'\' , \''+ URLAddress+ '\')">&nbsp;</div>';    
}

//This function checks div and if it is not the current highlight div for current news content makes it hidden
function hideHighlightImageMap(divId){
    i = getNewsId(LastNewsContent);
    lni = 'map_' + i;
    if(lni != divId){
        document.getElementById(divId).style.border = 'none';
        document.getElementById(divId).style.cursor = 'default';
        }
}

//This function highlights div by gives it a background color 
function showHighlightImageMap(divId){
    document.getElementById(divId).style.border = 'solid 4px #ffcc00'; 
    document.getElementById(divId).style.cursor = 'pointer';  
}

//This function gets "LastNewsContent" global variable value to finds the last news Id
function getNewsId(newsContentId){
    u = newsContentId.indexOf('_');
    n = newsContentId.substring(u+1);
    return n;
}

//Pass the query string parametre to this function to get the value
function querySt(str) {
    hu = window.location.search.substring(1);
    if(hu == '')
    {
        return '';
    }
    gy = hu.split("?");
    for (i=0 ; i<gy.length ;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == str) 
        {
            return ft[1];
        }
    }
    return '';
}    

// This function get URL Base and make print version link
// note : if news ID avalable
function goPrintVersion(URLBase) {
    if (LastNewsContent != '0') 
    {
      goUrlInNewWindow(URLBase + LastNewsContent);
    }
    else
    {
        alert('لطفاً یک خبر را انتخاب کنید');
    }
  } 



/* ------- display Archive Div ------------ */  
var archiveSts = false;
//This function visible the archive date drop down list by clicking on 'آرشیو'
function showArchiveDiv(){
    if(!archiveSts){
        document.getElementById('archiveHolder').style.display = 'block';
        archiveSts = true;
        return;
    }
    else{
        document.getElementById('archiveHolder').style.display = 'none';
        archiveSts = false;
        return;
    }
}

//This function visible the archive date drop down list by clicking on search button
function hideArchiveDiv(){
    var year = $('ddlYear').options[$('ddlYear').selectedIndex].value;
    var month = $('ddlMonth').options[$('ddlMonth').selectedIndex].value;
    var day = $('ddlDay').options[$('ddlDay').selectedIndex].value;
          
    if ( year == '' || month == '' || day == '')
        return;
    else{
        document.getElementById('archiveHolder').style.display = 'none';
        archiveSts = false;
    }
}


//This function make move icon hidden if the browser would be Net scape
function hideMoveIcn(){
    var browserVer = navigator.appName;          
    if (browserVer == 'Netscape'){        
        document.getElementById('movmentIcn').style.display = 'none';
        document.getElementById('movmentTxt').style.display = 'none';
        }
    else        
        return;
}

//This function make move icon hidden if the browser would be Net scape
function hidePdfIcn(prm){    
    if (prm != '')
        document.getElementById('pdfIcn').style.display = 'block';
    else        
        return;
}

//This function help page number to be sorted correctly by numeric value. 
function sortNumber(a,b){
    return a - b;
}



//This function genareted sorted page numbering html code
var pageNumbers = new Array();
var pageLink;
var i = 0;
var selectPage;
          
function creatPaggingNumber(){
    var end = pageNumbers.length;    
    pageNumbers.sort(sortNumber);
    for(var i=0; i<end; i++){
        var tdTag = document.createElement("td");   
        tdTag.align ="center";
        if(pageNumbers[i] == selectPage){              
            tdTag.innerHTML = "<div class='paggNumberingHolderSelected white'>" + pageNumbers[i] + "</div>";   
        }
        else{            
            var c = pageLink.indexOf('Page/');
            c = parseInt(c) + 5;
            var link = pageLink.substring(0,c);                        
            link = link + pageNumbers[i] + '/index.htm';                       
            tdTag.innerHTML = "<div onclick='goUrl(\"" + link + "\");' class='paggNumberingHolder black'>" + pageNumbers[i] + "</div>";   
        }  
        
        document.getElementById('pageNumberHolder').appendChild(tdTag); 
    }
}
