﻿addLoadListener(setlinks);

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}


    
//      function pageLoad() {
//      JavaScriptOn();
//      UpdateContent("Startup:0:0:0:0");
//      }
    
    function setlinks()
    {
        var links= document.getElementsByTagName("a");
        
        for (var i=0; i<links.length; i++)
        {
            var jsattr= links[i].getAttribute("jsattr");
            if ((jsattr!=null) && (jsattr!=""))
            {
            links[i].href = "javascript:UpdateContent('" + jsattr + "','')";
            links[i].setAttribute("jsattr","");
            }
            
//            
        }
    }

    

    function JavaScriptOn()
    {
     //   $get('JSTest').value = "true";
    
    }
 
    function ReceiveServerData(rValue)
    {    

        var commandstr= rValue.toString();
        var commands= commandstr.split('^');
        for (var i in commands)
        {
        if (commands[i].length>1)
            {

            var argstr= commands[i];
            var args= argstr.split('|');
         
            switch (args[0])
                {
                case "Init": Initialize(); break;
                case "ShowCategoryList": ShowCategoryList(args[1]); break;
                case "ShowCategoryListTitle": ShowCategoryListTitle(args[1]); break;
                case "ShowCategoryItems": ShowCategoryItems(args[1]); break;
                case "SetCategoryItemsTitle": SetCategoryItemsTitle(args[1]);break;
                case "ShowItem": ShowItem(args[1]);break;
                case "SetItemTitle": ShowItemTitle(args[1]); break;
                case "ClearItem": ClearItem(); break;
                case "SetPreviewImage": SetPreviewImage(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); break;
                case "SetThumbStyle": SetThumbStyle(args[1],args[2]);break;
                case "ShowImage": ShowImage(args[1],args[2],args[3]); break;
                case "Preload": Preload(args[1]); break;
                default: break;
                }
            }
        }
 
    }
    
    function Initialize()
    {
        dw = document.getElementById("ctl00_DetailWrapper");
        if (dw != null)
            dw.className= "hidden";
            
        ciw = document.getElementById("ctl00_CategoryItemsWrapper");
        if (ciw != null)
            ciw.className= "hidden";       
            
        clw = document.getElementById("ctl00_CategoryListWrapper");
        if (clw != null)
            clw.className= "hidden";               
    }
    
    function Preload(argstr)
    {
        var args= argstr.split('##');
        if (args[0]=="Images")
            PreloadImages(args[1]);
    }
    
    function PreloadImages(argstr)
    {
    
        var args= argstr.split('#');
        var imgs= [];
        for (var i in args)
        {
            imgs[i]= new Image();
            imgs[i].src= args[i];
        }

    }
    
    function ShowCategoryList(text)
    {
        document.getElementById("ctl00_CategoryList").innerHTML = text;
        document.getElementById("ctl00_GroupMenuPanel").innerHTML = "";
        document.getElementById("ctl00_CategoryListWrapper").className= "sub_center";
        MoveToTop("ctl00_CategoryListWrapper");
    }
    
    function ShowCategoryListTitle(text)
    {
        document.getElementById("CategoryListTitle").innerHTML = text;
    }
    
    function ShowCategoryItems(text)
    {
        document.getElementById("ctl00_CategoryItems").innerHTML = text;
        document.getElementById("ctl00_ItemMenuPanel").innerHTML = "";
        document.getElementById("ctl00_CategoryItemsWrapper").className= "sub_center";
        ClearItem();
        MoveToTop("ctl00_CategoryItemsWrapper");
    }
    
    function SetCategoryItemsTitle(text)
    {
        document.getElementById("CategoryItemsTitle").innerHTML = text;
    }
    
    function ShowItem(text)
    {
        document.getElementById("ctl00_ItemDetail").innerHTML = text;
        document.getElementById("ctl00_ContentPanel").innerHTML = "";
        document.getElementById("ctl00_DetailWrapper").className= "sub_center";
        MoveToTop("DetailWrapper");
    }
    
    function ShowItemTitle(text)
    {
        document.getElementById("ItemDetailTitle").innerHTML = text;
    }
    
    function ClearItem()
    {
        document.getElementById("ItemDetailTitle").innerHTML = "";
        document.getElementById("ctl00_ItemDetail").innerHTML = "";  
        document.getElementById("ctl00_ContentPanel").innerHTML = "";  
        document.getElementById("ctl00_DetailWrapper").className= "hidden";    
    }
    
    
    function SetPreviewImage(path, width, height, caption, group, item, page, selection)
    {

        var preview= document.getElementById("GalleryPreviewImage");
        if (preview==null)
            preview= document.getElementById("ctl00_GalleryPreviewImage");        
            
        var divParent= preview.parentNode;

        var imgNew = document.createElement('img');
        imgNew.src = path;
        imgNew.width= Number(width);
        imgNew.height= Number(height);
        
        imgNew.id = 'GalleryPreviewImage';
//alert("path= " + imgNew.src + "\nwidth= " + imgNew.width + " height= " + imgNew.height + "\nID= " + imgNew.id);     
        divParent.replaceChild(imgNew, preview);
        
        var link= document.getElementById("GalleryPreviewLink");
        if (link==null)
            link= document.getElementById("ctl00_GalleryPreviewLink");

        var dest= "javascript:UpdateContent('ShowImage:" + group + ":" + item + ":" + page + ":" + selection + "','')";
       
        link.setAttribute("href", dest);
        
        
        var captent= document.getElementById("PreviewCaption");
        if (captent==null)
            captent= document.getElementById("ctl00_PreviewCaption");
          

        while (captent.firstChild!=null)
            {
            var child= captent.firstChild;
//            alert("Removed child");
            captent.removeChild(child);
            }
        var newcap= document.createTextNode(caption);

        captent.innerHTML= caption;

        MoveToTop("GalleryPreviewImage");
    }
    
    function SetThumbStyle(name,style)
    {   
        var thumb= document.getElementById(name)
        if (thumb==null)
            thumb= document.getElementById("ctl00_"+ name);
        if (thumb!=null)
//            alert(name + " was not found");
//        else 
            thumb.className= style;
    }
    
    function ShowImage(url,width,height)
    {
        var argstr= 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,status=yes,toolbar=no,menubar=no,location=no';
        var win = window.open(url, '', argstr);
    }
    
    function MoveToTop(objName)
    {
//        var posx= 0;
//        var posy= 0;
//       element= document.getElementById(objName);
//       
//       while(element !=null)
//       {
//            posx+= element.offsetLeft;
//            posy+= element.offsetTop;
//            element= element.offsetParent;
//       }
//       
//       if (typeof window.pageYOffset != 'undefined')
//        {
//            window.pageXOffset= posx;
//            window.pageYOffset= posy;
//        }

//        else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
//        {
//            document.documentElement.scrollLeft= posx;
//            document.documentElement.scrollTop= posy;
//        }

//        else if (typeof document.body.scrollTop != 'undefined')
//        {
//            document.body.scrollLeft=posx;
//            document.body.scrollTop=posy;
//        }

    }