//alert("panel");
// Set a global var to hold the ever incrementing z-index
var z_index = 1000;

//function panel( element_id, left, top )
function panel( element_id, left, top, bottom, right, fade_time, display )
{ 
    var draggable;
    var element_close_button;

    sobj_top = new String(top) ;
    sobj_left = new String(left) ;
    sobj_bottom = new String(bottom) ;
    sobj_right = new String(right) ;

    top = (top != 'auto' && !sobj_top.match("%") ? top + "px" : top );
    left = (left != 'auto' && !sobj_left.match("%") ? left + "px" : left );
    bottom = (bottom != 'auto' && !sobj_bottom.match("%") ? bottom + "px" : bottom );
    right = (right != 'auto' && !sobj_right.match("%") ? right + "px" : right );

    // If there's a bottom, then make it a top:100%. later we'll set a negative top-margin;
    // this is because the drag pops back to the top in IE for elements positioned with bottom
    this.margin_top = '0px';
    this.margin_left = '0px';

//    if(bottom != 'auto')
//    {
//        top = '100%';
//        this.margin_top = "-" + bottom; // later we'll add the height of the panel to this
//    }
//
//    //right = (right != 'auto' ? right + "px" : right );
//    if(right != 'auto't con   )
//    {
//        left = '100%';
//        margin_left = "-" + right; // later we'll add the width of the panel to this
//    }

    //alert(element_id + "," +  left + "," +  top + "," +  bottom + "," +  right + "," +  fade_time);
    //properties
    this.element_id = element_id; 
    this.left = left;
    this.top = top; 
    this.bottom = bottom; 
    this.right = right; 
    this.position = 'absolute';
    this.element = ''; 
    this.fade_time = fade_time; 
    this.display = display; 

    //alert(this.element_id + "," +  this.left + "," +  this.top + "," +  this.bottom + "," +  this.right + "," +  this.fade_time);

    //methods
    this.init = init;
    this.fadeIn = fadeIn;
    this.getProperties = getProperties;
    this.savePropertiesToCookie= savePropertiesToCookie;
    this.getPropertiesFromCookie = getPropertiesFromCookie;
	this.setDraggable = setDraggable;
	this.formPanelSubmit= formPanelSubmit;

    // Does the element exist in the HTML?
    this.element = adg.getElement(this.element_id); 

    //alert("panel.init:" + this.element);


    if(this.element)
    {
        // -----------------------------------------------------------------------------------------
        // Basic properties for panels: Z-Index, display properties etc
        // -----------------------------------------------------------------------------------------
        // TODO - DEBUG THIS IN IE
        //this.element.setAttribute('onmousedown','adg.setZIndex(this)');
        //this.element.setAttribute('style', 'display:none;z-index:1;');

        // -----------------------------------------------------------------------------------------
        // Set the defualt settings with which the object was initialised
        // -----------------------------------------------------------------------------------------
        this.element.style.left = this.left;
        this.element.style.top = this.top; 
        this.element.style.bottom = this.bottom; 
        this.element.style.right = this.right;
        this.element.style.position = 'absolute';
        // Actually, this is covered by the fade in at the end of this class definition
        //this.element.style.display = this.display;
        //alert(this.element_id + " = " + this.display);

//        // A list of heights, hard coded
//        if(
//            this.element_id == 'newsletter' ||
//            this.element_id == 'newsletter-confirmation' ||
//            this.element_id == 'member-loggedin' ||
//            this.element_id == 'member-login' ||
//            this.element_id == 'site-search'
//        )
//        {
//            var element_height = 117;
//        }
//        else
//        {
//            var element_height = 0; // This should never get used
//        }

//        if(this.top == '100%')
//        {
//            this.margin_top = this.margin_top - element_height + 'px';
//            this.element.style.marginTop = this.margin_top;
//        }
//
//        // A default element width for use with 'right' below
//        //var element_width = 343;
//
//        // TODO - The right align stuff if required
//        //if(this.left == '100%')
//        //{
//        //    this.margin_left = this.margin_left - this.element_width + 'px';
//        //    this.element.style.marginLeft = this.margin_left;
//        //}
//
        // -----------------------------------------------------------------------------------------
        // ...or just get it from the cookie :)
        // -----------------------------------------------------------------------------------------
        if(
            this.element_id != 'content-panel'
        ){
	        var a_properties = null;
	
	        if(a_properties = this.getPropertiesFromCookie(this.element_id))
	        {
	            //alert(this.element_id + ": " + a_properties[6]);
	            this.left = a_properties[1];
	            this.top = a_properties[2];
	            // Note: These next two are not used because 'left' and 'top' are saved in the cookie
	            //this.margin_top = a_properties[3];
	            //this.margin_left = a_properties[4];
	            this.fade_time  = a_properties[5];
	            this.display  = a_properties[6];
	
	            //alert("GETTING: " + this.element_id + "," +  this.left + "," +  this.top + "," +  this.margin_top + "," +  this.margin_left + "," +  this.fade_time + "," + this.display);
	
	            this.element.style.left = this.left;
	            this.element.style.top = this.top; 
	            // Actually, this is covered by the fade in at the end of this class definition
	            //this.element.style.display = this.display; 
	        }
        }


        // -----------------------------------------------------------------------------------------
        // Setting the draggability of the panels
        // -----------------------------------------------------------------------------------------

        // Skip the panels with forms on them for now - have to debug the drag in IE
        if(
            this.element_id != 'video-03' &&
            //this.element_id != 'newsletter' &&
            //this.element_id != 'newsletter-confirmation' &&
            //this.element_id != 'member-loggedin' &&
            //this.element_id != 'member-login' &&
            //this.element_id != 'site-search' &&
            this.element_id != 'video-01' &&
            this.element_id != 'video-02' &&
            this.element_id != 'video-04' &&
            this.element_id != 'content-panel'
        ){
            this.setDraggable(true);
        }

        // start to make the object name
        var object_name = 'obj_';

        re = /-/g; 
        object_name = object_name + element_id.replace(re, "_");

        // Make the panel draggable, but then turn it off so that in IE we can write in the
        // <input> fields
        // TODO - DEBUG THIS IN IE
        //this.element.setAttribute('onmouseup', object_name + '.setDraggable(false)');

        // Experiments to overcome problems with the drag in IE
/*
        for(i in this.element.childNodes)
        {
            if(this.element.childNodes[i].nodeType==1 && !this.element.childNodes[i].onclick)
            {
                for(j in this.element.childNodes[i].childNodes)
                {
                    if(this.element.childNodes[i].childNodes[j].nodeName=='DIV')
                    {
                        this.element.childNodes[i].childNodes[j].setAttribute('onmousedown', object_name + '.setDraggable(true)');
                        //alert("SETTING onmousedown for " + this.element.childNodes[i].childNodes[j].nodeName + " - " + this.element.childNodes[i].childNodes[j].src);

                    }
                }

                break;
            }
        }

//        for(i in this.element.childNodes)
//        {
//            if(this.element.childNodes[i].nodeType==1 && !this.element.childNodes[i].onclick)
//            //if(this.element.childNodes[i].nodeType==1 && this.element.childNodes[i].onclick)
//            {
//                alert(this.element.childNodes[i].getAttribute('onmousedown'));
//                this.element.childNodes[i].setAttribute('onmousedown', object_name + '.setDraggable(true)');
//
//                break;
//            }
//        }

*/
        // -----------------------------------------------------------------------------------------

        // -----------------------------------------------------------------------------------------
        // Finally, fade in the panel (if it's not hidden) 
        // -----------------------------------------------------------------------------------------
        // Don't fade in the ones that are hidden
        if(this.display != 'none')
        {
            this.fadeIn(this.fade_time);
        }
    }
} 

	function formPanelSubmit(confirmation_panel_object){
        //alert(this.element_id);
        //alert(confirmation_panel_object.element_id);
        this.display = 'none';
        confirmation_panel_object.display = 'block';
        this.savePropertiesToCookie( this.element_id , this.getProperties());
        confirmation_panel_object.savePropertiesToCookie(confirmation_panel_object.element_id , confirmation_panel_object.getProperties());
        return true;
    }

	function setDraggable(value){  
	    if(!value && this.draggable ) {  
	    //if( !value ) {  
          //alert(value + ": " + this.element.id);
	      this.draggable.destroy();  
	      delete this.draggable;  
          this.savePropertiesToCookie( this.element_id , this.getProperties());
	    }  
	    else if( value && !this.draggable ){  
	    //else if( value ){  
          //alert(value + ": " + this.element.id);
	      this.draggable = new Draggable(this.element, { noDroppables: true, zindex: z_index } );  
	    }
	}

    function savePropertiesToCookie( element_id, a_values )
    {
        var value = null;

	    //alert("adg.init");
        for (var i=0; i < a_values.length; i++)
        {
            if(value)
            {
                value += "::::" + a_values[i];
            }
            else
            {
                value = a_values[i];
            }
        }

        //alert("SETTING: " + value);

        adg.setCookie( element_id, value );
	}

    function getPropertiesFromCookie( element_id )
    {
        //alert(element_id);
        var ret = null;
        var properties = adg.getCookie( element_id );

        if(properties)
        {
            //alert("getPropertiesFromCookie - " + element_id + ": " + properties);
            obj_properties = new String(properties);
            a_properties = obj_properties.split('::::');
            ret = a_properties;
        }

        return ret;
	}

    function init()
    {
        //alert("panel.init:" + this.element_id + " = ...");
    }

    function getProperties()
    {
            //alert("getProperties - setting 'this.margin_top': " + this.margin_top);
        if(this.element)
        {
            var a_properties = new Array(this.element_id, this.element.style.left, this.element.style.top, this.element.style.marginTop, this.element.style.marginRight, this.fade_time, this.display );
        }
        else
        {
            var a_properties = new Array( this.element_id, this.left, this.top, this.margin_top, this.margin_left, this.fade_time, this.display );
        }
            return a_properties;
    }

    function fadeIn(fade_time)
    {
        // Do fade in stuff
        //new Effect.Appear(this.element, {duration: fade_time,fps: 12,queue:'end',afterFinish: savePropertiesToCookie( this.element_id , this.getProperties())});
        new Effect.Appear(this.element, {duration: fade_time,fps: 12,queue:'end'});
    }

adg.addEvent(window, 'load', panelFactory.initialisePanels, false);
