
/**************************************************************************************
*                           COMPANION COMPUTERS LIMITED | CCL 
*           www.companioncomputers.co.uk | info@companioncomputers.co.uk
***************************************************************************************
*              www.compan.net | www.ccloe.co.uk | www.codebean.co.uk
***************************************************************************************
*   Application : WDS
*        Author : Andrew Scott (andrew@compan.net) | km0|km0ti0n (km0ti0n@gmail.com)
*          Date : 01/03/2007
*       Version : 0.0.1
*   Description : Standard script for use with wds web site
*     Copyright : Companion Computers Ltd. | CCL (c) 1987 / 2007 All rights Reserved.
***************************************************************************************
*       Licence : The duplication, distribution and modification for this code is  
*               : prohibited under international, and UK, law.  This code can only be 
*               : used with prior written persission and concent of CCL.  This header
*               : header must be retained, with the sole exception of compression,
*               : where any peer javascript libs / files too must be compressed.
***************************************************************************************
*    Dependancy : Core / Base / XAP
***************************************************************************************

***************************************************************************************
*         Class : pathosdesign
*   Description : Class to wrap global references and application logic
***************************************************************************************/
function wds(){}; _this = wds.prototype = new Base();

/* Define Private variables */
_this.constructor		= wds();
_this.ClassName			= "wds";
_this.XAP				= new XAP();
_this.bAutoFill         = false;

/* Define Public Properties */
_this.aBasketItems;
_this.aBasketIndex;
_this.nDelay            = 1000;  /* Delay before the server is updated */
_this.nBaseCost         = 0;
_this.nRangeSubTotal    = 0;
_this.nGrandTotal       = 0;

_this.nCurIndex = 0;



/**************************************************************************************
*        Method : picPop
*        Params : cUrl(string)
*       Returns : bool : True if window opened | False if didn't open
*   Description : Calls web service to update the session object's Baskett
***************************************************************************************/
_this.picPop = function(cUrl, cCaption, eAnchor){

    var self = this;
    
    if( this.bPopUpVisiable ){
        return false;
    }
    /* Get Item code */
    //var cItemCode = cCaption.split("[")[1].split("]")[0];
    
    var cMessage = ""; 
    cMessage += "              cUrl : " + cUrl + "\n"; 
    cMessage += "           eAnchor : " + eAnchor + "\n"; 
    cMessage += "eAnchor.multipopup : " + eAnchor.getAttribute("multipopup")  + "\n"; 
    cMessage += "   eAnchor.outHTML : " + eAnchor.outerHTML + "\n"; 

    if( !this.ePopHolder ){
        this.ePopHolder = this.NewObj("div", {id:"ePopHolder"}, document.body );
        this.eLoading   = this.NewObj("img", {src:"/images/ImageLoading.gif", id:"Loading"}, this.ePopHolder ); 
        this.eCaption   = this.NewObj("p", {className:"picPopCaption"}, this.ePopHolder); 
        this.eClosePop  = this.NewObj("a", {href:"#", onclick:function(){return false;}, id:"aClosePop"}, this.ePopHolder, "close");
        this.eBotStrip  = this.NewObj("div", {id:"BotStrip"}, this.ePopHolder ); 
        
        top.fnClose = function(){ return self.popUpClose(); };
        Events.addHandler( this.ePopHolder, "click", top.fnClose );
    }
  
    var nHeight     = 55;
    var nWidth      = 54;
    var nSHeight    = this.ePopHolder.offsetHeight;
    var nSWidth     = this.ePopHolder.offsetWidth;
    var nTop        = (nSHeight / 2 - ( nHeight / 2) ) ;
    var nLeft       = (nSWidth  / 2 - ( nWidth  / 2) ) ; 
    
    this.eLoading.style.top       = nTop + "px";
    this.eLoading.style.left      = nLeft + "px";
    
    this.eLoading.style.display     = "block";
    this.ePopHolder.style.top       = document.body.scrollTop; 
    this.ePopHolder.style.display   = "block";
    this.eBotStrip.style.display    = "none";;
    
    this.eCaption.style.display    = "none";
    
    this.eClosePop.style.display    = "none";
    
    this.eClosePop.style.left   = (this.ePopHolder.offsetWidth/2) - 50 + "px"; 

    if( this.ePopImage ){
        this.ePopHolder.removeChild( this.ePopImage );
    }
    
/*    if( cCaption ){
        this.eCaption.innerHTML = aClient[this.nCurIndex];
    }else{
        this.eCaption.innerHTML = "";
    }*/
    // here
       // this.eCaption.innerHTML = aClient[this.nCurIndex];
   
    
    /* Check the JS imageRef var */
    var cImageRefs = aImages;
    var cTemp = aClient;
    
    if( cImageRefs.length > 1 ){
        
        this.bShowNav = true;
            
        /*if( !this.ePopUpNext ){
            this.ePopUpNext = this.NewObj("a", {href:"#", id:"ImageSwapperNext"}, this.ePopHolder, "next >>");
        }
        if( !this.ePopUpPrev ){
            this.ePopUpPrev = this.NewObj("a", {href:"#", id:"ImageSwapperPrev"}, this.ePopHolder, "<< prev");
        }
*/
        /* Find the index  */
        for( var i=0; i<cImageRefs.length; i++){
            
            if( cImageRefs[i] == cUrl ){
                this.nCurIndex = i;
            }
        }
        
     
        
        if( cImageRefs.length -1 > this.nCurIndex ){ 
            this.ePopUpNext.href = cImageRefs[this.nCurIndex+1];
            this.ePopUpNext.onclick = function(e){e=e||event; e.cancelBubble = true;  self.showNextImage( this.href, cTemp[this.nCurIndex+1], this, 1 ); return false;};
            this.ePopUpNext.style.display = "block";
            this.ePopUpNext.style.left   = (this.ePopHolder.offsetWidth/2) + 40 + "px"; 
            this.ePopUpNext.className = ""; 
        }else{
            this.ePopUpNext.className = "disabled"; 
            this.ePopUpNext.onclick = function(e){ if(window.event) window.event.cancelBubble = true; return false; }
        }
        
        if( this.nCurIndex > 0 ){
            this.ePopUpPrev.href = cImageRefs[this.nCurIndex-1]
            this.ePopUpPrev.onclick = function(e){e=e||event; e.cancelBubble = true;  self.showNextImage( this.href, cTemp[this.nCurIndex-1], this, -1 ); return false;};
            this.ePopUpPrev.style.display = "block";
            this.ePopUpPrev.style.left   = (this.ePopHolder.offsetWidth/2) - 125 + "px"; 
            this.ePopUpPrev.className = ""; 
        } else {
            this.ePopUpPrev.className = "disabled"; 
            this.ePopUpPrev.onclick = function(e){ if(window.event) window.event.cancelBubble = true; return false; }
        }
    } else{
    
        this.bShowNav = false;
    
        if( this.ePopUpNext ){
            this.ePopUpNext.className = "disabled"; 
            this.ePopUpNext.onclick = function(e){ if(window.event) window.event.cancelBubble = true; return false; }
        }
        if( this.ePopUpPrev ){    
            this.ePopUpPrev.className = "disabled"; 
            this.ePopUpPrev.onclick = function(e){ if(window.event) window.event.cancelBubble = true; return false; }
        }
    }
    
   
    if( this.ePopUpNext ){
        this.ePopUpNext.style.display    = "none";
    }
    
    if( this.ePopUpPrev ){
        this.ePopUpPrev.style.display    = "none";
    }
    
    this.ePopImage = this.NewObj("img", {id:"ePopImage"}, this.ePopHolder);
    this.ePopImage.style.display = "block";
    this.ePopImage.style.visibility = "hidden";
    this.ePopImage.onload = function(){ self.popUpLoaded(); }
    this.ePopImage.src = cUrl;
    
    /* Test of multipopup and render an additional link */
    /*if( eAnchor.getAttribute("multipopup") ){
        var cAltURL = eAnchor.getAttribute("multipopup");
        if( !this.ePopUpNext ){
            this.ePopUpNext = this.NewObj("a", {href:"#", id:"ImageSwapper"}, this.ePopHolder, "swap image");
        }
        this.ePopUpNext.multipopup = cUrl;
        this.ePopUpNext.onclick = function(e){e=e||event; e.cancelBubble = true;  self.showNextImage( cAltURL, cCaption, this ); return false;};
        this.ePopUpNext.style.display = "block";
        this.ePopUpNext.style.left   = (this.ePopHolder.offsetWidth/2) - 125 + "px"; 
      
    
    }else{
        if( this.ePopUpNext ){
            this.ePopUpNext.style.display = "none"; 
        }
    }
    */
    return false;
}

/**************************************************************************************
*        Method : popUpLoaded 
*        Params : n/a
*       Returns : n/a
*   Description : Callback from the onload event and displays the image
***************************************************************************************/
_this.popUpLoaded = function(){
    
    var self = this;
    
    this.eLoading.style.display    = "none";
    //this.ePopImage.style.display   = "block";
    
    var nHeight     = this.ePopImage.height;
    var nWidth      = this.ePopImage.width;
    var nHHeight    = this.ePopHolder.offsetHeight;
    var nHWidth     = this.ePopHolder.offsetWidth;
    var nTop        = (nHHeight / 2 - ( nHeight / 2) ) -10;
    var nLeft       = (nHWidth  / 2 - ( nWidth  / 2) ) -10;
    
    this.ePopImage.style.top       = nTop;
    this.ePopImage.style.left      = nLeft;
    //this.ePopImage.style.display   = "block";
    this.ePopImage.style.visibility = "visible";
    
    this.eBotStrip.style.left       = nLeft;
    this.eBotStrip.style.top        = nTop + nHeight;
    this.eBotStrip.style.width      = nWidth + 20;
    this.eBotStrip.style.display    = "block";
    
    this.eCaption.style.display    = "block";
    
    Events.addHandler( this.ePopImage, "contextmenu", top.fnClose );
    
    
    /* Push the Captoin down */
    this.eCaption.style.top = nTop + nHeight + 17 + "px";

    /* Position the close link */
    this.eClosePop.style.top    = nTop + nHeight + 35 + "px";
    this.eClosePop.style.left   = (nHWidth/2) - 50 + "px"; 
    //this.eClosePop.style.left   = (nHWidth/2) + 50 + "px"; 

    /* Re-Position Image Button */  
    if( this.ePopUpNext ){
        this.ePopUpNext.style.top    = nTop + nHeight + 35 + "px";
        this.ePopUpNext.style.left   = (nHWidth/2) + 40 + "px"; 
    }
       
    if( this.ePopUpPrev ){
        this.ePopUpPrev.style.top    = nTop + nHeight + 35 + "px";
        this.ePopUpPrev.style.left   = (nHWidth/2) - 125 + "px"; 
    }

    if( this.ePopUpNext && this.bShowNav ){
        this.ePopUpNext.style.display    = "block";
    }
    
    if( this.ePopUpPrev && this.bShowNav ){
        this.ePopUpPrev.style.display    = "block";
    }
    
    this.eClosePop.style.display    = "block";

    //this.ePopImage.oncontextmenu = function(){ alert("The file type being saved or retrieved has been blocked"); return false;}
    
}

/**************************************************************************************
*        Method : popUpClose
*        Params : n/a
*       Returns : n/a
*   Description : Closes the popup and detaches the events
***************************************************************************************/
_this.popUpClose = function(){
    var self = this;
    this.eCaption.style.top = "55%";
    this.eClosePop.style.top = "60%";
    this.ePopHolder.style.display = "none";
    Events.removeHandler( this.ePopImage, "contextmenu" );
    return false;
}



/**************************************************************************************
*        Method : showNextImage 
*        Params : n/a
*       Returns : n/a
*   Description : Simple TX method to callback the local picPop method which 
*               : will render the next Image
***************************************************************************************/
_this.showNextImage = function(cUrl, cCaption, eAnchor, nDirection){
    /* ATM I don't need to swap out */
    this.nCurIndex += nDirection;
    this.picPop(cUrl, cCaption, eAnchor);    
} 




/**************************************************************************************
*      Function : picPopup
*        Pramas : cUrl(string) cCaption(string)
*       Returns : bool
*   Description : Opens a URL of an image into a popup window
***************************************************************************************/
function picPopup(/*string*/ cUrl, /*string*/ cCaption, eAnchor /* element */ ){
    return bResult = oWDS.picPop(cUrl, cCaption, eAnchor);
}

/**************************************************************************************
*      Function : setIndex
*        Pramas : nIndex (int)
*       Returns : bool
*   Description : Opens a URL of an image into a popup window
***************************************************************************************/
function setIndex(/*int*/ nIndex){
    if ( oWDS ) {
        oWDS.nCurIndex = nIndex;
    }
}



/**************************************************************************************
*                           PUBLIC METHODS & VARIABLES
***************************************************************************************/
var oWDS;

/**************************************************************************************
*      Function : initPathosDesign
*        Params : n/a
*       Returns : n/a
*   Description : Creates a new Pathos Design onload
***************************************************************************************/
function initWDS(){
    oWDS = new wds();
    oWDS.Parent            = null;
    oWDS.ParentElement     = null;
    //oPathosDesign.nBaseCost         = nRangeTotal + nGrandTotal ; 
    //oPathosDesign.Init();
    //document.getElementById("footer").style.marginLeft = "1px";
    //document.getElementById("footer").style.top = document.getElementById("main").scrollHeight - 107 + "px";
}


/**************************************************************************************/
//AddEvent(window, "load", initPathosDesign);    /* Start the application */
/**************************************************************************************
*                                   END OF FILE
***************************************************************************************
***************************************************************************************
***************************************************************************************/
