//GLOBALES
var data = new Array();

var client = {
  nav: {
    IE:    !!(window.attachEvent && !window.opera),
    Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
  },
  autrevar: {
    var1: !!(window.attachEvent && !window.opera),
    var2: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
  }
};



//Creer un event///////////////////////////////
function addEvt(elem, evt, action) {

    if(elem.addEventListener) {
        elem.addEventListener( evt , action,false);
    } else if(elem.attachEvent) {
        elem.attachEvent( 'on'+evt ,action );
    }

}
function rmEvt(elem, evt, action) {

    if(elem.removeEventListener) {
        elem.removeEventListener( evt , action,false);
    } else if(elem.detachEvent) {
        elem.detachEvent( 'on'+evt ,action );
    }

}
///////////////////////////////////////////////
//Initialisation///////////////////////////////
addEvt(window, "load", initPage );

function initPage() {

    if( $("tellme") && $("shoutbox") ) {
        $('idtellprenom').value="<your name>";
        $('idtellemail').value= "<your mail>";
        $('idtelltexte').value= "<your message>";
        $('idtellsign').value=  "";
        $('idshoutprenom').value="<your name>";
        $('idshouttexte').value= "<your message>";
        
        addEvt( $("idtellprenom"), "focus", focusInput );
        addEvt( $("idtelltexte"),  "focus", focusInput );
        addEvt( $("idtellemail"),  "focus", focusInput );
        addEvt( $("idshoutprenom"),"focus", focusInput );
        addEvt( $("idshouttexte"), "focus", focusInput );
        addEvt( $("idtellprenom"), "blur", blurInput );
        addEvt( $("idtelltexte"),  "blur", blurInput );
        addEvt( $("idtellemail"),  "blur", blurInput );
        addEvt( $("idshoutprenom"),"blur", blurInput );
        addEvt( $("idshouttexte"), "blur", blurInput );
        
        addEvt( $("idhideitem"), "mousedown", switchAddShout );
        
        addEvt( $("idtellprenom"),  "keyup", putSignature );
        addEvt( $("idshoutprenom"), "keyup", putSignature );
    }//endif
    
    if( $("artwork") ) {
        
        div_liste = document.getElementsByTagName("div");
        var1 = Array.findAllDivs( div_liste, /^art[0-9]{1,5}/i );
        for( var b = 0; b < var1.length; b++ ) {
            var id = var1[b].getAttribute("id").substring(3,10);
            var nbimg = 0;
            var liste_img = $("images"+id).getElementsByTagName("img");
            var liste_img = Array.findAllDivs( liste_img, /^img[0-9]{1,5}/i );
            for ( var c in liste_img){
                nbimg++;
            }
            data[id] = new Array();
            data[id]["nbimg"] = nbimg-1;
            
            //alert(data[id]["nbimg"]);
            //Swith affichage infos
            
            addEvt( document.getElementById("switchinf"+id), "mousedown", rolloverArtwork = function() {
                if( client.nav.IE ) var me = self.document.activeElement;
                else var me = this;
                
                var id = me.getAttribute("id").substring(9,14);

                if( id > 0 ) {
                    var bttn = me.style;
                    var info = $("artinfo"+id).style;
                
                    if( info.display == "block" ){
                        info.display = "none";
                    } else {
                        info.display = "block";
                    }
                }
            } );
            
            //Interactivite bttns prev&next
            addEvt( document.getElementById("prev"+id), "mousedown", prevart = function() {
                if( client.nav.IE ) var me = self.document.activeElement;
                else var me = this;
                
                var id = me.getAttribute("id").substring(4,10);
                if( id > 0 ) {
                    var var2 = $("images"+id);
                    var2.scrollTop = var2.scrollTop - var2.offsetHeight;
                
                    initArt( id , data[id]["nbimg"] );
                    var num = var2.scrollTop / var2.offsetHeight + 1
                    $("num"+id+"a"+num).style.fontWeight="bold";
                    $("num"+id+"a"+num).style.color="#ffffff";
                }
            } );
            addEvt( document.getElementById("next"+id), "mousedown", nextart = function() {
                if( client.nav.IE ) var me = self.document.activeElement;
                else var me = this;
                
                var id = me.getAttribute("id").substring(4,10);
                if( id > 0 ) {
                    var var2 = $("images"+id);
                    var2.scrollTop = var2.scrollTop + var2.offsetHeight;
                
                    initArt( id , data[id]["nbimg"] );
                    var num = var2.scrollTop / var2.offsetHeight +1
                    $("num"+id+"a"+num).style.fontWeight="bold";
                    $("num"+id+"a"+num).style.color="#ffffff";
                }
            } );
            
        }//endfor
        
    }//endif
    
    if( $("datacatalog") ) {
    
        div_liste = $("data").getElementsByTagName("img");
        var1 = Array.findAllDivs( div_liste, /^dataimage[0-9]{1,5}/i );
        
        for( var b = 1; b <= var1.length; b++ ) {
            
            addEvt( document.getElementById("bttnimage"+b), "mousedown", chgart = function() {
                if( client.nav.IE ) var me = self.document.activeElement;
                else var me = this;
                
                var num = me.getAttribute("id").substring(9,13);
                var var1 = $("images");
                //window.alert(": "+num+" : "+ var1.offsetHeight +" : "+var1.scrollTop);
                var1.scrollTop = var1.offsetHeight * ( num -1 );
            } );
            
        }//endfor
    }//endif

};

///////////////////////////////////////////////
//Alias des document.getElementById()
function $() {

    var elems = new Array();
    for (var a = 0; a < arguments.length; a++) {
        var elem = arguments[a];
        if(typeof elem == "string")
            elem = document.getElementById(elem);
        if(arguments.length == 1)
            return elem;
        elems.push(elem);
    }
    return elems;

}
//Alias des document.getElementByClass()
function $$() {

    var elems = new Array();
    var elem = arguments[0];
    window.alert( "longueur :"+document.length );
    for (var a = 0; a < document.length; a++) {
        if(document[a].className= elem){
            elems.push(document[a]);
        }
    }
    return elems;

}
//Trouve une string dans une liste/////////////
Array.find = function(liste, item){

    for(var a=0; a<liste.length; a++){
        if(liste[a] == item){
            return a;
        }
    }
    return -1;

}

//Trouve des listesdedivs par leur nom , ou juste une div/////////
Array.findAllDivs = function(liste, texte){

    resultat = new Array();
    for( var a = 0; a < liste.length; a++ ) {
        if( texte.test(liste[a].getAttribute("id")) ) {
            resultat.push(liste[a]);
        }
    }
    if(resultat.length==1) resultat = resultat;
    return resultat;

}
///////////////////////////////////////////////
//La signature est identique au nom dans tellme et shoutbox
function putSignature() {
	if( client.nav.IE ) var me = self.document.activeElement;
	else var me = this;

    $("idtellprenom").value = me.value;
    $("idtellsign").value = me.value;
    $("idshoutprenom").value = me.value;

}

///////////////////////////////////////////////
function focusInput() {
	if( client.nav.IE ) var me = self.document.activeElement;
	else var me = this;
	
	var liste0 = ["abcdefg","<your message>","<your mail>","<your name>"];
	if( Array.find(liste0, me.value) > 0 ) {
		me.value="";
	}
	
}

function blurInput() {
	if( client.nav.IE ) var me = self.document.activeElement;
	else var me = this;
	
	//window.alert( me.value );
    if(me.value=="" ) {
        switch(me.name) {
            case 'prenom': me.value="<your name>"; break;
            case 'texte': me.value="<your message>"; break;
            case 'email': me.value="<your mail>"; break;
        }
    }
	
}
///////////////////////////////////////////////
//Affiche / Cache le formulaire deshoutbox/////
function switchAddShout() {

    var1 = $("idtxtitem").style;
    if( var1.display == 'none' ) {
        var1.display = 'block';
        $("idbttnhide").innerHTML="hide";
    } else {
        var1.display = 'none';
        $("idbttnhide").innerHTML="show";
    }

}
///////////////////////////////////////////////
//Initialise les elems de l'art////////////////
function initArt(id,nb) {

    for( var a = 0; a <= nb; a++ ) {
        var num = a+1;
        $("num"+id+"a"+num).style.fontWeight="normal";
        $("num"+id+"a"+num).style.color="#fd0245;";
    }

}
///////////////////////////////////////////////
//Initialise les elems de l'art////////////////

