﻿
    window.onload = setupPage;
    window.onresize = setupPage; 
    window.onscroll = setupPage;
    var tintDiv;
    var ww = 0, wh = 0;         // window dimensions
    var dw = 0, dh = 0          // document dimensions
    var sx = 0, sy = 0;         // scroll x & y
    var hh = 19;                // header height
    var pi = "Progress.gif";    // progress image path

    // Setup
    // ---------------------------------------------------   
     
    function setupPage() { 
        try
        {
            tintDiv = document.getElementById("modaltintdiv");     
            getDimensions();    
            sizeTintDiv();
            setModalPosition();
        }catch(err){
        }
    }

    function getDimensions() {     
       // Window dimensions
       if (window.innerWidth) {
          ww = window.innerWidth;
          wh = window.innerHeight;      
       }
       else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
          ww = document.documentElement.clientWidth;
          wh = document.documentElement.clientHeight;
       }
       else if (document.body && document.body.clientWidth && document.body.clientHeight) {
          ww = document.body.clientWidth;
          wh = document.body.clientHeight;
       }
       // Document dimensions
       if (document.documentElement && document.documentElement.scrollWidth && document.documentElement.scrollHeight) {
          dw = document.documentElement.scrollWidth;
          dh = document.documentElement.scrollHeight;
       }
       else if (document.body && document.body.scrollWidth && document.body.scrollHeight) {
          dw = document.body.scrollWidth;
          dh = document.body.scrollHeight;
       }
       else if (document.body && document.body.clientWidth && document.body.clientHeight) {
          dw = document.body.clientWidth;
          dh = document.body.clientHeight;
       }    
    }    
    
    function getScrollXY() {
        sx = 0, sy = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
            sy = window.pageYOffset;
            sx = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
            sy = document.body.scrollTop;
            sx = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
            sy = document.documentElement.scrollTop;
            sx = document.documentElement.scrollLeft;
        }
    }
    

    // Modal functions  
    // ---------------------------------------------------

    function openModal(hrefTitle, imageUrl, imageTitle, padding, ratio){          
        var width = 0;
        var height = 0;
        
        // Calculate size of modal
        if(wh > ww || ((wh - padding) / ratio > ww)){
            width = ww - padding;
            height = width * ratio;
        }
        else {
            height = wh - padding;
            width = height / ratio;            
        } 
        
        if(width > 0 && height > 0){
            openModalDim(hrefTitle, imageUrl, imageTitle, width, height);  
        }    
    }
    
    function openModalDim(hrefTitle, imageUrl, imageTitle, width, height){
        opacity("modaltintdiv", 0, 70, 400); 

        var winW = ww, winH = wh;

        //Check if microsoft of netscape
        if (parseInt(navigator.appVersion)>3) {
         if (navigator.appName=="Netscape") {
          winW = window.innerWidth;
          winH = window.innerHeight;
         }
         if (navigator.appName.indexOf("Microsoft")!=-1) {
          winW = document.documentElement.clientWidth;
          winH = document.documentElement.clientHeight;
         }
        }
        
        if (winW == 0 | winH == 0)
        {
            winW = ww;
            winH = wh;
        }

        var scaleInt = 0;
        if (winW > winH)
        {
            if (height > winH | width > winW)
            {
                if (width > height)
                {
                    scaleInt = (winH / width) * 0.9;
                    width = width * scaleInt;
                    height = height * scaleInt;
                }else{
                    scaleInt = (winH / height) * 0.9;
                    width = width * scaleInt;
                    height = height * scaleInt;
                }
            }
        }else{
            if (height > winH | width > winW)
            {
                if (width > height)
                {
                    scaleInt = (winW / width) * 0.9;
                    width = width * scaleInt;
                    height = height * scaleInt;
                }else{
                    scaleInt = (winW / height) * 0.9;
                    width = width * scaleInt;
                    height = height * scaleInt;
                }
            }
        }
        
        if (width < 150)
        {
            setModalSize(width +  150, height);
        }else{
            setModalSize(width, height);
        }
        
        setModalPosition();
        
        setTimeout("opacity('modalouter', 0, 100, 200)", 200);
            
        getImage(hrefTitle, 'modalimage', imageUrl, imageTitle, width, height);
        
        //var img = document.getElementById('img');
        //img.src = imageUrl;
        //alert(img.src);
        
    }
    
    function openModalDimFLV(hrefTitle, flvUrl, imageTitle, width, height){
        try{
            opacity("modaltintdiv", 0, 70, 400); 
            
            var winW = ww, winH = wh;

            //Check if microsoft of netscape
            if (parseInt(navigator.appVersion)>3) {
             if (navigator.appName=="Netscape") {
              winW = window.innerWidth;
              winH = window.innerHeight;
             }
             if (navigator.appName.indexOf("Microsoft")!=-1) {
              winW = document.documentElement.clientWidth;
              winH = document.documentElement.clientHeight;
             }
            }
            
            if (winW == 0 | winH == 0)
            {
                winW = ww;
                winH = wh;
            }

            var scaleInt = 0;
            if (winW > winH)
            {
                if (height > winH | width > winW)
                {
                    if (width > height)
                    {
                        scaleInt = (winH / width) * 0.9;
                        width = width * scaleInt;
                        height = height * scaleInt;
                    }else{
                        scaleInt = (winH / height) * 0.9;
                        width = width * scaleInt;
                        height = height * scaleInt;
                    }
                }
            }else{
                if (height > winH | width > winW)
                {
                    if (width > height)
                    {
                        scaleInt = (winW / width) * 0.9;
                        width = width * scaleInt;
                        height = height * scaleInt;
                    }else{
                        scaleInt = (winW / height) * 0.9;
                        width = width * scaleInt;
                        height = height * scaleInt;
                    }
                }
            }
            
            if (width < 150)
            {
                setModalSize(width +  150, height);
            }else{
                setModalSize(width, height);
            }
            
            setModalPosition();
            
            setTimeout("opacity('modalouter', 0, 100, 200)", 200);
                
            getImageFLV(hrefTitle, 'modalimage', flvUrl, imageTitle, width, height);
        }catch(err){
        }
    }
    
    function setModalSize(width, height)
    {
        try
        {
            var ms = document.getElementById("modalouter").style;       
            ms.width = width + "px";
            ms.height = (height + hh) + "px"; 
        }catch(err){
        }
    }
    
    function setModalPosition()
    {
        try{
            var ms = document.getElementById("modalouter").style;            
            var h = ms.height.substring(0, ms.height.length - 2);
            var w = ms.width.substring(0, ms.width.length - 2);
            
            
            getScrollXY();
            ms.top = ((sy + wh / 2) - ((h - hh) / 2) - 10) + "px";
            ms.left = ((sx + ww / 2) - (w / 2) - 10 ) + "px";       
        }catch(err){
        }  
    }

    function closeModal(flash){
        try{
            //if (flash == "true")
            //{
            StopFlashMovie();
            //}
            //alert('test1');
            opacity("modalouter", 100, 0, 200); 
            //alert('test2');
            opacity("modaltintdiv", 70, 0, 200); 
            //alert('test3');
            setTimeout("document.getElementById('modalimage').src = ''", 200);
            //alert('test4');
        }catch(err){
            alert(err);
        }
    }    
        
    function sizeTintDiv() {   
       tintDiv.style.height = ((wh > dh) ? wh : dh) + "px";
       tintDiv.style.width = ((ww > dw) ? ww : dw) + "px";
    }    
          	
    function getImage(hrefTitle, placeHolder, imageUrl, imageTitle, width, height){        
        var pHolder = document.getElementById(placeHolder);    
        pHolder.width = 100;
        pHolder.height = 16;        
        pHolder.src = pi;        
        pHolder.style.margin = ((height / 2) - 8) + "px 0 0 0";      
        
        var img = document.createElement('img');
        img.onload = function(evt){ 
            pHolder.style.margin = "0 0 0 0";     
            //pHolder.src = '';   
            pHolder.width = width;
            pHolder.height = height;
            pHolder.src = this.src;     
        }
        
        imageUrl = imageUrl.replace("~/", "");
        
        img.src = imageUrl;
        //alert(img.src);
        
        if (hrefTitle == null || hrefTitle == "")
        {
            hrefTitle = imageUrl;
        }
        
        document.getElementById('imageTitle').innerHTML = "<a href='" + hrefTitle + "'>" + imageTitle + "</a>";   
    }     
    
    function getImageFLV(hrefTitle, placeHolder, flvUrl, imageTitle, width, height){        
        try{
            if (imageTitle == null || imageTitle == "")
                imageTitle = "[NO TITLE]";
            
            var pHolder = document.getElementById(placeHolder);    
            pHolder.width = 100;
            pHolder.height = 16;        
            pHolder.src = pi;        
            pHolder.style.margin = ((height / 2) - 8) + "px 0 0 0";      
            
    //        var img = document.createElement('img');
    //        img.onload = function(evt){ 
    //            pHolder.style.margin = "0 0 0 0";     
    //            //pHolder.src = '';   
    //            pHolder.width = width;
    //            pHolder.height = height;
    //            pHolder.src = this.src;     
    //        }
    //        img.src = imageUrl;
            //alert(img.src);
            
            flvUrl = flvUrl.replace("~/", "");
            
            var FLVObj = "<object id=\"myFlashMovie\"" +
                            "codeBase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0\"" +
                            "height=" + height + " width=" + width + " align=\"middle\" " +
                            "classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\">" + 
                            "<param NAME=\"Movie\" VALUE=\"" + flvUrl + "\">" +
                            "<param NAME=\"Play\" VALUE=\"1\">" +
                            "<param NAME=\"Loop\" VALUE=\"-1\"><param NAME=\"Quality\" VALUE=\"High\">" +
                            "<param NAME=\"Scale\" VALUE=\"ShowAll\">" +
                            "<param NAME=\"BGColor\" VALUE=\"#FFFFFF\">" +
                            "<embed src=\"" + flvUrl + "\" swliveconnect=\"true\" quality=\"high\" bgcolor=\"#ffffff\" width=\"" + width + "\" height=\"" + height + "\" name=\"myFlashMovie\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></embed></object>";
             
            if (document.getElementById('modalimagediv') != null)
            {   
                var modimgdiv = document.getElementById('modalimagediv')    
                modimgdiv.innerHTML = FLVObj;
            }
            
            if (document.getElementById('imageTitle') != null)
            {
                if (hrefTitle == null || hrefTitle == "")
                {
                    document.getElementById('imageTitle').innerHTML = imageTitle;
                }else{
                    document.getElementById('imageTitle').innerHTML = "<a href='" + hrefTitle + "'>" + imageTitle + "</a>";
                }
            }  
            //alert(flvUrl); 
        }catch(err){
        }
    }   
        
    function showIt(id) {   
        try{
           var object = document.getElementById(id);
           object.style.display = "block";      
       }catch(err){
       }  
    }

    function hideIt(id) {  
        try{
            var object = document.getElementById(id);          
            object.style.display = "none";
        }catch(err){
        }
    }  
    
    
    // Fade functions
    // http://www.brainerror.net/scripts_js_blendtrans.php
    // ---------------------------------------------------
    
    function opacity(id, opacStart, opacEnd, millisec) {  
        try{  
            var object = document.getElementById(id);
            if(opacStart == 0){
                changeOpac(0, id);
                showIt(id); 
            }
            else  if(opacEnd == 0){
                setTimeout("hideIt('" + id + "')", millisec); 
            }
            
            // Speed for each frame
            var speed = Math.round(millisec / 100);
            var timer = 0;

            // Determine the direction for the blending, if start and end are the same nothing happens
            if(opacStart > opacEnd) {
                for(i = opacStart; i >= opacEnd; i--) {
                    setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
                    timer++;
                }
            } 
            else if(opacStart < opacEnd) {
                for(i = opacStart; i <= opacEnd; i++) {
                    setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
                    timer++;
                }
            }
        }catch(err){
        }       
    }    

    // Change the opacity for different browsers
    function changeOpac(opacity, id) {
        try{
            var object = document.getElementById(id).style;        
            object.opacity = (opacity / 100);
            object.MozOpacity = (opacity / 100);
            object.KhtmlOpacity = (opacity / 100);
            object.filter = "alpha(opacity=" + opacity + ")";
        }catch(err){
        } 
    }  
    
    //Gets the flashmovie
    function getFlashMovieObject(movieName){
      if (window.document[movieName]) 
      {
          return window.document[movieName];
      }
      if (navigator.appName.indexOf("Microsoft Internet")==-1)
      {
        if (document.embeds && document.embeds[movieName])
          return document.embeds[movieName]; 
      }
      else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
      {
        return document.getElementById(movieName);
      }
    }
    
    function StopFlashMovie(){
    try{
        //var flashMovie=getFlashMovieObject("myFlashMovie");
        //var flashMovie=window["FLVPlayer_Main"];
        
        //flashMovie.StopPlay();
        
        //Due to some sort of glitch we simple kill the active X object by deleting the HTML
        //This seems to stop the flash video and completly unload it.
        if (document.getElementById('modalimagediv') != null)
            {   
                var modimgdiv = document.getElementById('modalimagediv')    
                modimgdiv.innerHTML = "";
            }
        //alert(flashMovie.classid);
        }catch(err){
        //alert(err);
        }
    }
  