/**************************************************************************************
*                           COMPANION COMPUTERS LIMITED | CCL 
*           www.companioncomputers.co.uk | info@companioncomputers.co.uk
***************************************************************************************
*              www.compan.net | www.ccloe.co.uk | www.codebean.co.uk
***************************************************************************************
*   Application : Metartec UPS Systems
*        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 Metartec web site
*     Copyright : Companion Computers Ltd. | CCL (c) 1987 / 2008 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 : Metartec
*   Description : Class to wrap global references and application logic
***************************************************************************************/
function Metartec(){}; _this = Metartec.prototype = new Base();

/* Define Private variables */
_this.constructor		= Metartec();
_this.ClassName			= "Metartec";
_this.oPopupController  = null;

/**************************************************************************************
*        Method : Init
*        Params : n/a
*       Returns : n/a
*   Description : Standard Method to be called when the new Object of the
*               : class is instantiated.
***************************************************************************************/
_this.Init = function(){
    this.PreInit();
    this.setScreenSize();
    this.referenceStaticElements();
    this.attachToAJAX();
    this.attachToPopupController();
    this.roundContentElements();
}
 
_this.attachToAJAX = function(){
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
        function(){
            oMetartec.CCL_endRequestHandler();
        }
    );
}   
       
    

/**************************************************************************************
*        Method : showProductPopups
*   Description : Displays and positions first found Popup and 
*               : the supporting CSS is set active
***************************************************************************************/
_this.showProductPopups = function(){
    oPopup = document.getElementById("PopupWrapper"); 
    this.eBGPopup = document.getElementById("CCL_bgOverlay");
    var nPopupHeight = 500;

    /* IE7 or later uses .body all others use documentElement */
    var nScrollTop      = document.documentElement.scrollTop    ? document.documentElement.scrollTop        : document.body.scrollTop;
    var nClientHeight   = document.documentElement.clientHeight ? document.documentElement.clientHeight     : document.body.clientHeight;
    var nOffsetWidth    = document.documentElement.offsetWidth  ? document.documentElement.offsetWidth      : document.body.offsetWidth;
    var nScrollHeight   = document.getElementById("container").offsetHeight;
    
    /* Position the Popup in the center */    
    with( this.eBGPopup.style){
        top     = "0px";
        height  = nScrollHeight + "px"; 
        zIndex  = 980000;
        display = "block";
    }
    
    with( oPopup.style ){
        left    = ((nOffsetWidth - 700)  / 2 ) + "px";
        top     = ( nScrollTop + ( ( nClientHeight - nPopupHeight )/ 2 ) + "px" );
        zIndex  = 990000;
        display = "block";
        if(bFF){ KillClass(oPopup, "FFHF"); }
    }

    if( !bFF ){ document.body.style.overflow = "hidden"; }
}


/**************************************************************************************
*        Method : hideProductPopups
*   Description : Displays and positions first found Popup and 
*               : the supporting CSS is set active
***************************************************************************************/
_this.hideProductPopups = function(){
    oPopup = document.getElementById("PopupWrapper"); 
    this.eBGPopup = document.getElementById("CCL_bgOverlay");
  
    /* Position the Popup in the center */    
    with( this.eBGPopup.style){
        display = "none";
    }
    
    if(!bFF){
        with( oPopup.style ){
            display = "none";
        }
    }else{
        AddClass(oPopup, "FFHF");
    }

    if( !bFF ){ document.body.style.overflow = ""; }
}

_this.attachToPopupController = function(){
    
    this.oPopupController = new PopupController();
    this.oPopupController.Parent = this;
    this.Controls.Add( this.oPopupController );
}



/**************************************************************************************
*        Method : referenceStaticElements
*        Params : n/a
*       Returns : n/a
*   Description : Load up custom HTML elements
***************************************************************************************/
_this.referenceStaticElements = function(){
}


/**************************************************************************************
*        Method : roundContentElements
*        Params : n/a
*       Returns : n/a
*   Description : Rounds Elements found with selected Classes
***************************************************************************************/
_this.roundContentElements = function(){
    this.aWideElements = getElementsByClassName(document, "wideContent");
    for(var i = 0; i<this.aWideElements.length; i++){
        this.roundElement(  this.aWideElements[i]   );
    }
    this.aThinElements = getElementsByClassName(document, "thinContent");
    for(var i = 0; i<this.aThinElements.length; i++){
        this.roundElement(  this.aThinElements[i]   );
    }
    
    
    
}


/**************************************************************************************
*        Method : roundElement
*        Params : n/a
*       Returns : n/a
*   Description : Adds the Element to round 
***************************************************************************************/
_this.roundElement = function( eRounded ){

    /* If it's been rounded before, remove old elements */
    if( eRounded.oRoundedElements != null )
    {
        for( cElement in eRounded.oRoundedElements ){
            var eElement = eRounded.oRoundedElements[cElement];
            eElement.parentNode.removeChild( eElement );       
        }
    }

    eRounded.oRoundedElements = {};
    
    this.createRoundedElement("top", eRounded);
    this.createRoundedElement("right", eRounded); 
    this.createRoundedElement("bottom", eRounded);
    this.createRoundedElement("left", eRounded); 

    this.createRoundedElement("cornerTRight", eRounded); 
    this.createRoundedElement("cornerBRight", eRounded); 
    this.createRoundedElement("cornerBLeft", eRounded); 
    this.createRoundedElement("cornerTLeft", eRounded); 
    
    
    /* set dynamic height */
    
    /* Measure these for the corner* elements */
    var nCornerHeight = eRounded.oRoundedElements["cornerTRight"].offsetHeight;
    var nCornderWidth = eRounded.oRoundedElements["cornerTRight"].offsetHeight;
    
    var nHeight = eRounded.offsetHeight;
    var nWidth  = eRounded.offsetWidth;
    
    eRounded.oRoundedElements["top"].style.left         = nCornderWidth + "px";
    eRounded.oRoundedElements["top"].style.height       = nCornerHeight + "px";
    eRounded.oRoundedElements["top"].style.width        = nWidth - (2*nCornderWidth) + "px";
    
    eRounded.oRoundedElements["right"].style.top        = nCornerHeight + "px";
    eRounded.oRoundedElements["right"].style.height     = nHeight - (2*nCornerHeight) + "px";
    eRounded.oRoundedElements["right"].style.width      = nCornderWidth + "px";
    
    eRounded.oRoundedElements["bottom"].style.left      = nCornderWidth + "px";
    eRounded.oRoundedElements["bottom"].style.height    = nCornerHeight + "px";
    eRounded.oRoundedElements["bottom"].style.width     = nWidth - (2*nCornderWidth) + "px";
    
    eRounded.oRoundedElements["left"].style.top         = nCornerHeight + "px";
    eRounded.oRoundedElements["left"].style.height      = nHeight - (2*nCornerHeight) + "px";
    eRounded.oRoundedElements["left"].style.width       = nCornderWidth + "px";
    

}
    
/**************************************************************************************
*        Method : createRoundedElement
*        Params : n/a
*       Returns : n/a
*   Description : Adds the Element to round 
***************************************************************************************/
_this.createRoundedElement = function(cElementType, eRounded ){
    eRounded.oRoundedElements[cElementType] = this.NewObj("div", {className:cElementType},eRounded); 
}

/**************************************************************************************
*        Method : setScreenSize
*        Params : n/a
*       Returns : n/a
*   Description : Sets the scroll bars based on the size of the rez
***************************************************************************************/
_this.setScreenSize = function(){
    if( document.body.offsetWidth< 1024){
    }
}

_this.CCL_endRequestHandler = function(){
    this.oPopupController.actionPopups();
    this.fixLayout();
}

/**************************************************************************************
*        Method : fixLayout
*        Params : n/a
*       Returns : n/a
*   Description : Sets the scroll bars based on the size of the rez
***************************************************************************************/
_this.fixLayout = function(){
    var divFixLayout = document.getElementById("divFixLayout");
    if( divFixLayout != null){
        divFixLayout.style.display = "none";
        setTimeout( function(){
		    divFixLayout.style.display = "block";
		    oMetartec.roundContentElements();
	    }, 1);

    }   
}

/***************************************************************************************
*         Class : PopupController
*   Description : Class to wrap global references and application logic
***************************************************************************************/
function PopupController(){}; _this = PopupController.prototype = new Base();

/* Define Private variables */
_this.constructor		= PopupController();
_this.ClassName			= "PopupController";
_this.oPopupController  = null;

/**************************************************************************************
*        Method : Init
*   Description : Standard Method to be called when the new Object of the
*               : class is instantiated.
***************************************************************************************/
_this.Init = function(){
    this.referenceStaticElements();
}


/**************************************************************************************
*        Method : referenceStaticElements
*   Description : Load up custom HTML elements
***************************************************************************************/
_this.referenceStaticElements = function(){
    this.eBGPopup = document.getElementById( "CCL_bgOverlay" ); 
}

/**************************************************************************************
*        Method : actionPopups
*   Description : Checks for and shows any popups
***************************************************************************************/
_this.actionPopups = function(){
    if( this.hasPopups() ){
        this.showPopups();
    }else{
        this.hidePopups();
    }
}


/**************************************************************************************
*        Method : hidePopups
*   Description : Ensures that if no popups are in view none of 
*               : the supporting CSS is active
***************************************************************************************/
_this.hidePopups = function(){
    with( this.eBGPopup.style){
        display = "none";
        if( !bFF ){ document.body.style.overflow = ""; }
    }
}


/**************************************************************************************
*        Method : showPopups
*   Description : Displays and positions first found Popup and 
*               : the supporting CSS is set active
***************************************************************************************/
_this.showPopups = function(){
    if( this.aPopups.length <= 0 ){ return; }
    oPopup = this.aPopups[0]; 
    var nPopupHeight = 460;

    /* IE7 or later uses .body all others use documentElement */
    var nScrollTop      = document.documentElement.scrollTop    ? document.documentElement.scrollTop        : document.body.scrollTop;
    var nClientHeight   = document.documentElement.clientHeight ? document.documentElement.clientHeight     : document.body.clientHeight;
    var nOffsetWidth    = document.documentElement.offsetWidth  ? document.documentElement.offsetWidth      : document.body.offsetWidth;
    var nScrollHeight   = document.getElementById("container").offsetHeight;
    
    /* Position the Popup in the center */    
    with( this.eBGPopup.style){
        top     = "0px";
        height  = nScrollHeight + "px"; 
        zIndex  = 980000;
        display = "block";
    }
    
    with( oPopup.style ){
        left    = ((nOffsetWidth - 713)  / 2 ) + "px";
        top     = ( nScrollTop + ( ( nClientHeight - nPopupHeight )/ 2 ) + "px" );
        zIndex  = 990000;
        display = "block";
    }

    if( !bFF ){ document.body.style.overflow = "hidden"; }
}


/**************************************************************************************
*        Method : hasPopups
*   Description : Checks for and references any CCL_Popups
***************************************************************************************/
_this.hasPopups = function(){
    this.aPopups = document.getElementsByClassName( "CCL_popup" ); 
    if( this.aPopups.length > 0 ){ return true; }else {return false;}
    
}

/**************************************************************************************
*                           PUBLIC METHODS & VARIABLES
***************************************************************************************/
var oMetartec;

/**************************************************************************************
*      Function : __initMetartec
*        Params : n/a
*       Returns : n/a
*   Description : Creates a new docherty onload
***************************************************************************************/
function __initMetartec(){
    //__roundCorners();
    oMetartec                   = new Metartec();
    oMetartec.Parent            = null;
    oMetartec.ParentElement     = null;
    oMetartec.Init();

    
    
}


/**************************************************************************************
*      Function : __roundCorners
*        Params : n/a
*       Returns : n/a
*   Description : Rounds the Corners using Nifty
***************************************************************************************/
function __roundCorners(){
    if(!NiftyCheck()){return;}
    Rounded("div.wideContent", "#ffffff", "#cccccc", 30, 30);
    
}



/**************************************************************************************
*      Function : setFormRef
*        Params : n/a
*       Returns : n/a
*   Description : Sets the public references to form elements for Address lookup
***************************************************************************************/
var cAddress1, cAddress2, cAddress3, cTown, cCounty, cPostCode;
function setFormRef( oRef ){
    cAddress1   = oRef.cAddress1;
    cAddress2   = oRef.cAddress2;
    cAddress3   = oRef.cAddress3;
    cTown       = oRef.cTown;
    cCounty     = oRef.cCounty;
    cPostCode   = oRef.cPostCode;
}


/**************************************************************************************
*                           FLASH CONTROL CLASSES
***************************************************************************************/


//var cXmlPopupPlayerContent	 =  "http://metartec-ecom-002.ccloe.co.uk/cclflash/metartec/PopupPlayer.xml";
//var aPopupPlayer 	     =	["swfPopupWrapper", 	"http://metartec-ecom-002.ccloe.co.uk/cclflash/metartec", 	"Popup_Player.swf", "700", 	"500", 	"9", "#ffffff", "Metartec Limited"];
//var aInstallSwf				 = ["expressInstall"];
//var cXmlGenericContent		 =  "http://metartec-ecom-002.ccloe.co.uk/cclflash/metartec/metartec.xml";

var cXmlPopupPlayerContent	    = "";
var aPopupPlayer 	            = "";
var aInstallSwf				    = "";
var cXMLDomains		            = "";
var hMovieLoading               = -1;
var lMovieLoaded                = false;
var cHost                       = ""; 

/**************************************************************************************
*      Function : loadFlash
*        Params : cHost
*       Returns : n/a
*   Description : Creates the Flash players
***************************************************************************************/
function loadFlash( cHost, cProduct ){
    
    oMetartec.showProductPopups(); 
    
    if( !lMovieLoaded ){
        var cHost               = "http://" + cHost;
        
        cXmlPopupPlayerContent  = cHost + "/cclflash/metartec/PopupPlayer.xml";
        
        aPopupPlayer		    = ["swfPopupWrapper", cHost + "/cclflash/metartec", "Popup_Player.swf", "700", 	"500", 	"9", "#ffffff", "Metartec Limited"];
        aInstallSwf			    = ["expressInstall"];		
        cXMLDomains	            = cHost + "/cclflash/metartec/metartec.xml?rnd=" + (new Date()).getTime();

        oMetartec.cProductPopupCode = cProduct;
            
	    hMovieLoading = setInterval(checkMovieLoadState, 100);
    	
        /* Start Flash */
        oCompanFlashControl = new CompanFlashControl(); 
        oCompanFlashControl.init( "oCompanFlashControl" ); 
    }
    

}


/**************************************************************************************
*      Function : checkMovieLoadState
*        Params : none
*       Returns : n/a
*   Description : Checks for the loaded state of the movie
***************************************************************************************/
function checkMovieLoadState() {
	if (lMovieLoaded) {
		clearInterval(hMovieLoading);
		showPopup( oMetartec.cProductPopupCode ); 
	}
}	

/**************************************************************************************
* Public access to Flash Player 
***************************************************************************************/
function showPopup(cProduct)				{oCompanFlashControl ? oCompanFlashControl.swfShowPopup(cProduct) 			: false;}
function bypassFlashUpdate()				{oCompanFlashControl ? oCompanFlashControl.swfBypassUpdate() 				: false;}
function stopMedia()                        {oCompanFlashControl ? oCompanFlashControl.htmlStopMedia() 					: false;}

var oCompanFlashControl;

/***************************************************************************************
*         Class : CompanFlashControl
*   Description : Class to wrap global references and application logic
***************************************************************************************/
function CompanFlashControl(){}
var _this = CompanFlashControl.prototype = new Object();
_this.constructor	= CompanFlashControl;
_this.className     = "CompanFlashControl";
_this.name;
_this.swfPopupPlayerControl;



/**************************************************************************************
*        Method : Init
*        Params : n/a
*       Returns : n/a
*   Description : Standard Method to be called when the new Object of the
*               : class is instantiated.
***************************************************************************************/
_this.init = function (cName) {

	// passed from HTML
	this.name = cName;
    if ( Boolean(typeof aPopupPlayer != 'undefined' && aPopupPlayer) ) {
	    this.swfInstall = new InstallSwf();
	    this.swfInstall.parent = this;
	    this.swfInstall.init(this.name + ".swfInstall", aInstallSwf[0], aPopupPlayer[1]);	   	
	    if (this.swfInstall.loaded()) {   	
		    this.swfInit();	
	    }
    }
}

_this.swfBypassUpdate = function () {
	this.swfInstall.hide();	
	this.swfInit();
}

_this.swfShowPopup = function (cProduct) {	
	this.swfPopupPlayerControl.swfShowPopup(cProduct)
}

_this.swfInit = function () {
	
  if ( Boolean(typeof aPopupPlayer != 'undefined' && aPopupPlayer) )  {
      this.swfPopupPlayerControl = new popupPlayerControl();
	    this.swfPopupPlayerControl.parent = this;
	    this.swfPopupPlayerControl.init(this.name + ".swfPopupPlayerControl", aPopupPlayer, cXMLDomains, cXmlPopupPlayerContent);	
	}
	
}


/***************************************************************************************
* Popup Player Controls
***************************************************************************************/
_this.htmlPopupPlayerStopMedia	= function () {
	if(this.swfPopupPlayerControl) {
		// this.swfPopupPlayerControl.swfDeletePlayer();
		delete this.swfPopupPlayerControl;
	}
}

/***************************************************************************************
* Common Access
***************************************************************************************/
_this.htmlStopMedia	= function () {
	if ( Boolean(typeof aPopupPlayer != 'undefined' && aPopupPlayer) ) { this.htmlPopupPlayerStopMedia(); }		
}

_this.showMovieStatus = function(cStatus) {
    switch( cStatus ){
        case "Movie:Loaded" : 
            lMovieLoaded = true;
            break;
        case "Close" : 
            oMetartec.hideProductPopups(); 
            break;
    }
}

_this.swfStatus = function(cName, cStatus) {
    //alert( "  Name : " + cName + "\n" + "Status : " + cStatus );
	switch (cName) {
		case "oCompanFlashControl.swfPopupPlayerControl":	
			this.showMovieStatus(cStatus);
			break;
	}
}			


/***************************************************************************************
* DEFINES 
***************************************************************************************/

function fixDotNetForm(){

    alert( theForm.action );
    var cURI = ""; 
    if( theForm.action.indexOf( "http://" ) > -1 ){
        /* Gecko returns the full URI, we need to trim off the path */
        var cDom  = theForm.action.match(/http:\/\/[\-,\_,\w,\.,]*/ig);
        var cPath = theForm.action.match( /default.aspx[\d,\D]*/ig);
        cURI = cDom + "/" + cPath;
        
    }else{
        /* Others Return relative URI, just pre-append "/" */
        cURI = theForm.action.replace(/[a-z,:,\/\/,\/,\.,\-\_]*default\.aspx/i, "/default.aspx");
    }
    alert( cURI ); 
    theForm.action = theForm._initialAction = cURI;    
  //  theForm.action = cDom + "/" + cPath;";
//    
}


/**************************************************************************************/
AddEvent(window, "load", __initMetartec);    /* Start the application */
/**************************************************************************************
*                                   END OF FILE
***************************************************************************************
***************************************************************************************
***************************************************************************************/