/**************************************************************
 Script		: AjaxGallery
 Version		: 1.0
 Authors		: Leonard Martin
 Desc		: Makes Ajax call to load extedned content for gallery
 **************************************************************/
//start menu class

var AjaxGallery = new Class({

    //implements
    Implements: [Options],
    
    //options
    options: {
        trigger: 'div.content a'
    },
    
    //initialization
    initialize: function(gallery, options){
        //set options
        this.setOptions(options);
        this.gallery = gallery;
        
        var triggers = this.gallery.getElements(this.options.trigger);
        
        triggers.each(function(a){
            a.addEvent('click', function(e){
                e.stop();

				
				$$('.galleryStyle03').each(function(b){
					b.setStyle('display', 'none');
				});
				
                this.open(a);
            }
.bind(this));
        }
.bind(this));
        
    },
    
    open: function(a){
		
    	var row = a.getParents('div.row')[0], box = this.getBox(row);
		box.getElement('.galleryStyleInner03').set('html', '<div class="ajaxLoader"></div>');
		
        var href = a.getProperty('href'), xhr = new Request({
            url: href,
            method: 'get',
            onComplete: function(html){
            
                var doc = Elements.from(html), content = doc.getElement('.ajaxContent');
                
                content.each(function(b){
                    if (b != null) {
                        try {
                            box.getElement('.galleryStyleInner03').set('html', b.get('html'));
                        } 
                        catch (e) {
                            console.dir(e);
                        }
                    }
                });
                
                $$('.galleryStyle01 li').hover(function(){
                    $(this).getElement('.galleryOverlay').setStyle('display', 'block');
                }, function(){
                    $(this).getElement('.galleryOverlay').setStyle('display', 'none');
                });
            }
        });
        
        $$('.galleryStyle02').each(function(j){
			j.getElements('div.active').removeClass('active')
		});
        a.getParents('div.content').addClass('active');
        
        xhr.send();
        
    },
    
    getBox: function(row){
        var box = row;
        
        if (!box.getChildren('div.themeDisplayBox').length) {
			
            var container = new Element('div', {
                'class': 'galleryStyle03 themeDisplayBox row'
            }), 
			inner = new Element('div', {
                'class': 'galleryStyleInner03'
            }), 
			box = container.grab(inner);
        }
        box.setStyle('display', 'block');
        return box.inject(row, 'after');
        
    }
    
});

