var qt = null;

function imageGallery() {
    if ($('img_nav') && $$('#img_nav li.img').length > 1) {
        $$('#img_nav li.img').each(function(l, i) {
            var a = l.getFirst('a');
            var url = a.getProperty('href');
            
            a.setProperties({
                'href' : '#',
                'onclick' : 'return false;'
            });
            
            a.addEvent('click', function() {
                if (!l.hasClass('active')) {
                    $('wrapper').addClass('loading');
                    var load = new Asset.image(
                        url, 
                        {
                            onload: function() {
                                if ($('media').getElement('img')) {
                                    $('media').getElement('img').setProperty('src', url);
                                } else {
                                    var n_img = new Element('img', {
                                        'src': url,
                                        'alt': ''
                                    });
                                    $('media').empty();
                                    n_img.inject($('media'));
                                }
                                
                                if (l.getParent().getFirst('.active')) {
                                    l.getParent().getFirst('.active').removeClass('active');
                                }
                                l.addClass('active');
                                $('wrapper').removeClass('loading');
                            }
                        }
                    );
                    
                }
            });
        });
    }
}

function getVideo(con, src, w, h, tg) {
    if (!tg.getParent().hasClass('active')) {
        if (qt === null) {
            qt = new Quickie(src, {  
                  id: 'qtmovie',  
                  width: w,  
                  height: h,    
                  attributes: {   
                      autoplay: 'true',
                      cache: 'true',
                      controller: 'true',
                      kioskmode: 'true'
                  }  
            });
        }
        
        $('media').empty();
        qt.inject($('media'));
        
        if (tg.getParent().getParent().getFirst('.active')) {
            tg.getParent().getParent().getFirst('.active').removeClass('active');
        }
        tg.getParent().addClass('active');
    }
    return false;
}

window.addEvent('domready', function() {
    $$('.toggler').each(function(tl) {
        tl.setProperty('href', '#');
        tl.setProperty('onclick', 'return false');
    });
    
    var subs = $$('.submenu');
    var show_sub = false;
    subs.each(function(s) {
        if (s.hasClass('show_sub') && show_sub === false) {
            show_sub = subs.indexOf(s);
        }
    });
    
    var accordion = new Accordion($$('.toggler'),$$('.submenu'), {
        opacity: 0,
        alwaysHide: true,
        display: false,
        show: show_sub
    });
    
    $$('a').each(function(a) {
        a.addEvent('click', function() {
            if (this.blur) { this.blur(); }
        });
    });
    
    imageGallery();
});


