
// XXX there is too much repetition at the Y level, we need a nicer way of joining all the functionality

// don't want to write "YAHOO" everywhere do we?
var Y = YAHOO;

// the meat
var lightbox;
function setupLightbox () {
    var lbs = Y.util.Selector.query('.forLB');
    var ds = {};
    for (var i = 0; i < lbs.length; i++) {
        var n = lbs[i];
        var a = n.firstChild;
        var img = a.firstChild;
        var id = 'imgLB' + i;
        ds[id] = {
            url:    a.getAttribute('href'),
            title:  img.getAttribute('title'),
        };
        img.setAttribute('id', id);
        a.onclick = function () {
            this.blur();
            lightbox.show(this.firstChild.getAttribute('id'));
            return false;
        };
    }
    if (lbs.length) {
        YAHOO.com.thecodecentral.LightboxPanel.prototype._setHelpPanelBody = function () {
    		this.helpPanel.setBody(
    		'<div class="tcc_helpPanelTitle">Opérations de base:</div>' +
    		'<strong>Clic</strong> pour fermer<br/>' + 
    		'<strong>Tirer</strong> pour déplacer<br/>'+
    		'<br/>' +   
    		'<div class="tcc_helpPanelTitle">Clavier:</div>' + 
    		'<strong>Gauche</strong> - Photo précédente<br/>' +
    		'<strong>Haut/Espace</strong> - Pleine taille/rétrécie<br/>' + 
    		'<strong>Droite</strong> - Photo suivante<br/>' + 
    		'<strong>Bas</strong> - Tooltip<br/>' + 
    		'<strong>Esc</strong> - Fermer<br/>' + 
    		'<strong>F1</strong> - Aide<br/>' + 
    		'<br/>Cliquez ici pour fermer l\'aide<br/>');
    	};
        lightbox = new Y.com.thecodecentral.Lightbox(ds, {
            maskOpacity:    0.8,
            maskBgColor:    "#000",
            hasThumbnails:  true,
            ctrlVisible:    true,
            ctrlOpacity:    0.2,
        });
    }
}

var qtboxData = {};
function setupQTbox () {
    var qtbs = Y.util.Selector.query('.forQTB');
    for (var i = 0; i  < qtbs.length; i ++) {
        var n = qtbs[i]
        var a = n.firstChild;
        var img = a.firstChild;
        var id = 'imgQTB' + i;
        qtboxData[id] = {
            url:            a.getAttribute('href'),
            title:          img.getAttribute('title'),
            videoWidth:     parseInt(n.getAttribute('videoWidth')),
            videoHeight:    parseInt(n.getAttribute('videoHeight'))
        };
        a.setAttribute('id', id);
        a.onclick = function () {
            this.blur();
            showQTBox(this.getAttribute('id'));
            return false;
        };
    };
}

function showQTBox (id) {
    var data = qtboxData[id];
    if (!data) alert("No video data for object!");
    var h = 16 + data.videoHeight; // count the controller
    var w = data.videoWidth;
    var wH = h + 20; // window size
    var wW = w + 20;
    
    qtbox = new Y.widget.Panel('videoPanel', {
        close:              true,
        draggable:          true,
        modal:              true,
        // keylisteners...
        visible:            false,
        effect:             { effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25},
        //monitorresize:      false,
        fixedcenter:        true,
        width:              wW + 'px',
        height:             wH + 'px',
        zIndex:             1000,
        constaintoviewport: true,
    });        
    
    qtbox.setHeader(data.title);
    var body =
    "<object width='" + w + "' height='" + h + "'"                              +
          " classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B'"               +
              " codebase='http://www.apple.com/qtactivex/qtplugin.cab'>"        +
       "<param name='src' value='" + data.url + "'/>"                           +
       "<param name='autoplay' value='true'/>"                                  +
       "<param name='controller' value='true'/>"                                +
       "<embed src='" + data.url + "' width='" + w + "' height='" + h + "'"     +
            " autoplay='true' controller='true'"                                +
            " pluginspage='http://www.apple.com/quicktime/download/'></embed>"  +
     "</object>";
    qtbox.setBody(body);
    qtbox.render('qtBoxContainer');
    qtbox.hideEvent.subscribe(function () { qtbox.destroy(); })
    qtbox.show();
}

// when we can start
function BEGIN () {
    setupLightbox();
    setupQTbox();
}

// load dependencies
var loader = new Y.util.YUILoader();
loader.addModule({
    name:       "lightbox",
    type:       "js",
    fullpath:   "/js/lightbox/Lightbox-min.js",
    varName:    "YAHOO.com.thecodecentral.Lightbox",
    requires:   ['utilities', 'container']
});
loader.require('selector');
loader.require('lightbox');
loader.loadOptional = true;
loader.onSuccess = BEGIN;
loader.onFailure = function () { alert("Failed to load parts of the site, you may encounter issues."); };

loader.insert();
