﻿//nemWidget.js
//2008 Paul Graves paul.graves@nemisys.uk.com
//Nemisys ajax widget

//nemWidget class
var nemWidget = new Class({
    initialize: function(el){
        var widget,heading,content,settings;
        this.widget = el;
             
        //build the header element
        //<div class="heading">
		//<strong class="heading-move">TITLE</strong>
		//CONTROLS
		//</div>
        this.heading = this.widget.getChildren(".nemHeading")[0];
        this.handle = this.heading.getChildren(".nemWidgetHandle")[0];
        //this.handle.innerHTML = this.widget.title; Title is now in the raw html
        this.heading.adopt(this.handle);
        this.widget.grab(this.heading,'top');
        
        //build controls
        this.controls = new Element("UL",{'class':'nemControls'});
        this.heading.adopt(this.controls);
        
        this.body = this.widget.getFirst(".nemBody");
        this.content = this.body.getFirst(".nemContent");
        this.footer = this.widget.getFirst(".nemFooter");
        
        //build the settings panel
        if(this.widget.getFirst(".nemSettings")){
            this.settings = this.widget.getFirst(".nemSettings");
            this.settings.content = new Element("DIV",{'class':'nemSettingsContent'}); 
            this.settings.adopt(this.settings.content);
            this.settings.controls = new Element("DIV",{'class':'nemSettingsControls'});
            this.settings.controls.adopt(new Element("INPUT",{'type':'button','class':'nemSettingsSave','value':'Save','events':{'click':(function(){this.updateSettings(null,null,true);}).bind(this)}})); 
            this.settings.adopt(this.settings.controls);
            //build the toggle for the settings panel
            this.settingsToggleControl = new Element("LI",{'class':'nemSettingsOpen'}).adopt(new Element("A",{'href':'javascript:;','html':'settingsToggle','events': {'click': (function(){this.toggleSettings();}).bind(this)}}));
            this.controls.adopt(this.settingsToggleControl);
            this.toggleSettings();
        }else{
            this.settings = "";
        }
        
        //close control
        //<li class="nemClose"><a href="#">close</a></li>
        if(this.widget.hasClass('close')){
            //builds control and sets up remove event 
            this.controls.adopt(new Element("LI",{'class':'nemClose'}).adopt(new Element("A",{'href':'javascript:;','html':'close','events': {'click': (function(){this.hide();}).bind(this)}})));
        }
        //toggle control
        //<li class="nemToggle"><a href="#">toggle</a></li>
        if(this.widget.hasClass('toggle')){
            //build the toggle control and add its events
            this.toggleControl = new Element("A",{'class':'nemContentOpen','href':'javascript:;','html':'toggle','events': {'click': (function(){this.toggleContent();}).bind(this)}})
            this.controls.adopt(new Element("LI",{'class':'nemToggle'}).adopt(this.toggleControl));
        }  
        
        this.heading.adopt(new Element("DIV",{'class':'clear','html':'&nbsp;'}));

        //hide or show the content initially by default
        if(this.body.hasClass('nemHidden')){
            this.toggleContent();           
        }
        
        //get initial content - removed as it causes too long a delay before the nemWidgetPanel is initliased
        if(this.body.getProperty('location')){
           this.updateContent();
        }   
        //get initial settings content
        if(this.settings!=""){
            this.initSettings();
             
        }
        this.reinitContent();
        //this.widget.setStyles({'position':'relative','left':'','top':''});  this causes an error in ie6
    },
    //hides the widget
    hide:function(){
        this.widget.hide();
        this.updateCookie();
    },
    //shows the widget
    show:function(){
        this.widget.show();
        this.updateCookie();
    },
    //toggle the content visible/hidden
    toggle:function(){
        this.widget.toggle();
        this.updateCookie();
    },
    //slides the content in/out and changes the togglers class
    toggleContent:function(slide){
        //if noslide then toggle without a transition
        if(slide==false){
            this.body.toggleNoSlide();
        }else{
            this.body.toggleSlide(); 
        }
        //change the class of the toggler button
        this.toggleControl.toggleClass('nemContentClosed');
        this.toggleControl.toggleClass('nemContentOpen');
        
    },
    toggleSettings:function(slide){
        if(slide==false){
            this.settings.toggleNoSlide();
        }else{
            this.settings.toggleSlide(); 
        }
        //change the class of the toggler button
        this.settingsToggleControl.toggleClass('nemSettingsClosed');
        this.settingsToggleControl.toggleClass('nemSettingsOpen');
    },
    //updates the content of this widget with the given url and params
    updateContent:function(urlIn,params){
        if(urlIn!=null)
            this.body.setProperty('location',urlIn);
        else
           urlIn = this.body.getProperty('location')
    
        var updateRequest = new Request({method:'get',async:true,url:urlIn,onSuccess: (function(txt) {this.content.set('html', txt);this.content.removeClass('nemLoading');this.reinitContent();}).bind(this),
                                                                onRequest: (function() {this.content.removeClass('nemFailure');this.content.innerHTML="";this.content.addClass('nemLoading');}).bind(this),
                                                                onFailure: (function() {this.content.removeClass('nemLoading');this.content.addClass('nemFailure');}).bind(this)});
        updateRequest.send(params+"&"+Date());
        this.content.removeHeight();
    },
    //re-initalises any nemExpandables within the updated content
    reinitContent:function(){
        //do nemExpandables
        this.content.getElements('.nemExpandable').each(
                                function(el,i){
                                    var tempExpandable = new nemExpandable(el);
                                }
                                );
        //do nemTabs
        this.content.getElements('.nemTabs').each(
                                function(el,i){
                                    var tempTab = new nemTabs(el);
                                }
                                );                   
                                
    },
    updateSettings:function(urlIn,params,refreshContent){
          if(urlIn!=null)
            this.settings.setProperty('location',urlIn);
          else
           urlIn = this.settings.getProperty('location')
         this.settings.content.getFirst("form").set({method: 'get'});
         this.settings.content.getFirst("form").send();
        var updateRequest = new Request({method:'post',async:true,url:urlIn,onSuccess: (function(txt) {this.settings.content.set('html', txt);}).bind(this),
                                                                onFailure: (function() {this.settings.addClass('nemFailure');}).bind(this)});
        updateRequest.send(params+"&"+Date());
        this.settings.removeHeight();
        
        if(refreshContent==true)
            this.updateContent();
    },
    initSettings:function(urlIn){
          if(urlIn!=null)
            this.settings.setProperty('location',urlIn);
          else
           urlIn = this.settings.getProperty('location')
       
        var updateRequest = new Request({method:'post',async:true,url:urlIn,onSuccess: (function(txt) {this.settings.content.set('html', txt);}).bind(this),
                                                                onFailure: (function() {this.settings.addClass('nemFailure');}).bind(this)});
        updateRequest.send();
        this.settings.removeHeight();
        
    },
    updateCookie: function(){ 
        if(_nemWidgetPanel.updateCookie!=null)
        _nemWidgetPanel.updateCookie();
    },
    test:function(){
        alert('test function called!');
    }
});



