/**************************************************************

    Script          : wedge help modal
    Version         : Beta 1.1
    Authors         : Umut Ahmet
    Licence         : Attribution-Noncommercial-No Derivative Works 3.0 Unported (http://creativecommons.org/licenses/by-nc-nd/3.0/)
    Usage           : window.addEvent('domready', function(){
                          // new toolbox
                          var tool_ini = new toolbox({
                              unit: '.unit', // class you need to add links that you want to trigger toolbox with
                              container: document.body // where to inject toolbox
                          });                                                 
                      });

**************************************************************/

//start toolbox class
var wedge_modal = new Class({
	
	Implements: [Options,Chain],
	
	options:{                         
        onOpen: new Class(),                    //make sure new class is loaded
        onClose: new Class()                    //make sure new class is loaded 
	},

	initialize: function(options) {
		//set options
		this.setOptions(options);
        //set variables
        this.opened = false; 
                     
        this.load();   
	},
	
    load: function (){
        var self = this; 
        
        /**
        * add Window events & create overlay 
        */
        $(window.document).addEvent('keydown',function(e){                            
            if (e.key == 'esc' && self.opened) self.close();
        }.bind(this));
        
        self.overlay = new Overlay({ container: self.options.container });
         
        /**
        * prepare lightbox
        */
        self.lightbox = new Element('div', { id: 'lightbox', 'class': 'hide', opacity: 0 }).inject(self.options.container, 'inside');
            self.img_container = new Element('div', { id: 'image_container', 'class': 'hide', opacity: 0 }).inject(self.lightbox, 'top');
        
        self.lightbox_fx = self.default_morph(self.lightbox, null, null);
        self.img_fx = self.default_morph(self.img_container, null, null);
        
        var hash = window.location.hash;
        if($defined(hash) && hash.contains('welcome')) {
            self.open('welcome');
        } else {
        
            if($defined($('shop_count'))) {
                var reminder = Cookie.read('mywedge_shopreminder');
                if ($defined(reminder) == false || reminder == false) {
                    if ($('shop_count').get('html') == 0) 
                        self.open('reminder');
                };
            };
            
        };
        
    },
                 
    open: function (type) {
        var self = this; 
        
        self.overlay.show();
        self.img_container.setStyles({ 'margin-top': window.getScrollTop() + 40 });           
        
        switch (type) {
            case 'welcome':
                var image_path = '/assets/front/modal_welcome.jpg';                      
                
            break;
            case 'reminder':
                var image_path = '/assets/front/modal_reminder.jpg';  
                self.dontshow_container = new Element('div', { id: 'dontshow_container' }).inject(self.img_container, 'top');     
                    self.dontshow = new Element('input', { type: 'checkbox', name: 'dont_show', checked: '', events: { click: function(){ self.close() } } }).inject(self.dontshow_container, 'inside');  
                    self.dontshow_text = new Element('span', { html: 'Close and Don\'t show me this again.' }).inject(self.dontshow_container, 'inside');  
                    
            break;
            case 'help':
                var image_path = '/assets/front/modal_help.jpg'; 
            
            break; 
        };
        
        var load_image = new Asset.image(image_path, {                                                
            onload: function(img) {
                img.addEvent('click',function(){ self.close() }).inject(self.img_container, 'bottom'); 
        
                self.lightbox.toggleClass('hide');  
                self.img_container.toggleClass('hide');  
                
                self.lightbox_fx.start({opacity: 1}).chain(function () {
                    self.img_fx.start({opacity: 1});                       
                }); 
                
                if (Browser.Engine.trident4)
                    $$('select').setStyle('visibility', 'hidden');
            }
        }); 
               
        self.opened = true;                             
    }, 
                 
    close: function () {
        var self = this; 
        
        self.lightbox_fx.start({
            opacity: 0
        }).chain( function() { 
            self.img_fx.start({opacity: 0}); 
            self.lightbox.toggleClass('hide');  
            self.img_container.toggleClass('hide'); 

            self.img_container.empty();
            self.overlay.hide();  
            
            if (Browser.Engine.trident4) 
                $$('select').setStyle('visibility', 'visible');  
        });
        
        if ($defined(self.dontshow) && self.dontshow.get('checked')) { 
            Cookie.write('mywedge_shopreminder', true, {
                path: '/',
                duration: 365
            });
        }; 

        self.opened = false;  
    }, 
                 
    default_morph: function (element,duration,link) {
        // generic morph for all animations throughout the tool box 
        if(duration == null) duration = 'short';
        if(link == null) link = 'chain';
        return new Fx.Morph(element,{ duration: duration, transition: 'expo:in:out', link: link });
    }                                                           
    
});
