/*
   SiteComponents version:
   6.6.4.1, tag SC_6_6_4_1, created Tue Oct 27 21:28:23 +0100 2009
   $Name:  $
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype. If Scriptaculous is
               available the tooltip will use some nice effects.
    ============================================================================

    SiteComponents ratinglisttile.js

    Enhancement of the AppbaseEntityRatingListTile
    
*/

var TabbedEntityList = Class.create({
    
    initialize: function(element) {
        this.element = element;
        this.cookie = new CtCookie();
        this.tabs = element.select('div.tab');
        this.cookieKey = 'open_tabs_' + this.element.id;
        this.tabNum = 0;
        
        if(this.cookie.get(this.cookieKey) == null) {
            this.activeTab = this.tabs[0];
        } else {
            this.activeTab = $(this.cookie.get(this.cookieKey));
        }
        
        this.tabs.each(this.initializeTab.bind(this));
    },
    
    initializeTab: function(tab) {
        if(tab != this.activeTab) {
            tab.down().next().hide();
        }
        
        // Lets observe the header link and add code to toggle the visible tab
        tab.down().down().observe('click', this.toggleActiveTab.bindAsEventListener(this));
        
        this.tabNum++;
    },
    
    toggleActiveTab: function(event) {
        event.stop();
        
        var clickedTab = event.findElement('div');
        if(this.activeTab != clickedTab) {
            var contentToHide = this.activeTab.down().next();
            var contentToShow = clickedTab.down().next();
            
            try {
                new Effect.SlideUp(contentToHide, { duration: .2 });
                new Effect.BlindDown(contentToShow, { duration: .2 });
            } catch (err) {
                console.log(err);
                // Probably errs because of missing scriptaculous, lets toggle
                // the elements using plain prototype instead
	            contentToHide.hide();
	            contentToShow.show();
            }
            
            this.activeTab = clickedTab;
            
            // Let's set the active tab in a cookie
            this.cookie.set(this.cookieKey, this.activeTab.id); 
        }
    }
    
});

