var mainUrl = './?nh=';
var cell = null;
var labelsOnSrc = 'images/labels_on.gif';
var labelsOffSrc = 'images/labels_off.gif';
var showingImageWL = true;
var currentMediaType = 0;

window.onload = function(){
    initMenu();
    if(!contentType){
        //Cell init
        cell = new Cell($('imgCell'),$('mapCell'));
        cell.imageNormalSrc = 'images/cell/cellule.jpg';
        cell.imageLabelsSrc = 'images/cell/cellule_labels.jpg';
        cell.onCellClick = onMainCellClick;
        
        cell.addSection('Microtubules','images/cell/cellule_microtubules.jpg',24);
        cell.addSection('Mitochondries','images/cell/cellule_mythocondrie.jpg',25);
        cell.addSection('Appareil de Golgi','images/cell/cellule_appgolgi.jpg',20);
        cell.addSection('Centrioles','images/cell/cellule_centrioles.jpg',49);
        cell.addSection('Lysosomes','images/cell/cellule_lysosomes.jpg',23);
        cell.addSection('Noyau','images/cell/cellule_noyau.jpg',26);
        cell.addSection('Réticulum endoplasmique','images/cell/cellule_reticulum.jpg',27);
        cell.addSection('Cytosol','images/cell/cellule_cytosol.jpg',19);
        cell.addSection('Ribosomes','images/cell/cellule_ribosomes.jpg',28);
        cell.addSection('Membrane cellulaire','images/cell/cellule_membrane.jpg',18);
    }else{
        switch(contentType){
            case 1:
                resetMedias();
    	        if(mainImagePath)
    	            showImage(mainImagePath);
                break;
        }
    }
}

function onMainCellClick(value){
    location.href = mainUrl + value
}
function switchCellMode(){
    cell.switchMode();
    $('imgSwitchMode').writeAttribute('src',($('imgSwitchMode').readAttribute('src') == labelsOnSrc ? labelsOffSrc : labelsOnSrc));
}

function navBack(){
    if(navHistory.length > 1){
        navHistory.length = (navHistory.length - 1);
        location.href = mainUrl + navHistory.join(',');
    }
}

function goToContent(contentId){
    navHistory[navHistory.length] = contentId;
    location.href = mainUrl + navHistory.join(',');
}

function resetMedias(){
    $('ImageMediaContainer').hide();
    $('QTMediaContainer').hide();
    
    if($('imgImage')) $('imgImage').src = 'images/image_norm.gif';
    if($('imgQTSmall')) $('imgQTSmall').src = 'images/qtsmall_norm.gif';
    if($('imgQT')) $('imgQT').src = 'images/qt_norm.gif';
    if($('imgVideoSmall')) $('imgVideoSmall').src = 'images/videosmall_norm.gif';
    if($('imgVideo')) $('imgVideo').src = 'images/video_norm.gif';
}

function showQT(fileName,btnId,imgSrc){
    resetMedias();
    showingImageWL = true;
    $('QTMediaContainer').show();
    $('QTMediaContainer').update(mediaQTHTMLPrefix + fileName + mediaQTHTMLSuffix);
    
    currentMediaType = 1;
    $(btnId).src = imgSrc;
}
function showImage(fileName){
    resetMedias();
    var imageSrc = mainImagePath;
    if(mainImageWLPath.length > 0 && !showingImageWL){
        imageSrc = mainImageWLPath;
        showingImageWL = true;
        $('imgImage').src = 'images/imagewl_slct.gif';
    }else{
        showingImageWL = false;
        $('imgImage').src = 'images/image_slct.gif';
    }
    
    currentMediaType = 0;
    $('ImageMedia').src = imageSrc;
    $('QTMediaContainer').update();
    $('ImageMediaContainer').show();
    
}

function openGlossary(glossaryId){
    var url = 'glossary.php#';
    if(typeof glossaryId != 'undefined')
        url += glossaryId
	openWindowsAndCenter(url,400,580);
}

function openLargePicture(fileName, captionText){
    closeLargePicture();
    
    var oImgWin = $("divLargeImgWin");
    var oDivImg = $("divLargeImg");
    var oDivCaption = $("divLargeImgCaption");
    
    oDivImg.update();
    oDivImg.appendChild(new Element("img", {src:fileName, width: 522, height:null, style:"z-index: 0.5"}));
    if(captionText)oDivCaption.update(captionText.escapeHTML());
    
    
    var w = document.viewport.getWidth();
    var l = document.viewport.getScrollOffsets().left;
    
    var imgLeft = l + (w/2) - 280; 
    oImgWin.setStyle({left:imgLeft + "px", top:"100px"});
    oImgWin.show();
}

function closeLargePicture(){
    var oImgWin = $("divLargeImgWin");
    var oDivImg = $("divLargeImg");
    var oDivCaption = $("divLargeImgCaption");
    
    oImgWin.hide();
    oDivImg.update();
    oDivCaption.update();
    oImgWin.setStyle({left:"0px", top:"0px"});
}

function openWindowsAndCenter(URL,W,H,scroll){
	var width = W;
	var height = H;
	var winl = (screen.width-width)/2;
	var wint = (screen.height-height)/2 - 50;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	
	if(typeof scroll == 'undefined')
	    scroll = 0;
	
	window.open(URL,"Detail",'top='+wint+',left='+winl+',width='+width+',height='+height+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=0').focus();
}
function rollImg(oImg)
{
	//Settings
	var sNormSuffix = "_norm";
	var sOverSuffix = "_roll";
	var sSelectSuffix = "_slct";
	var iStatusLen = sNormSuffix.length;

	//For manipulation only
	var sCurrentImgName = oImg.src.substring(oImg.src.lastIndexOf('/') + 1,oImg.src.length);
	var sCurrentStatus = sCurrentImgName.substr(sCurrentImgName.lastIndexOf('_'),iStatusLen);
	
    //Cancel if selected
    if(sCurrentStatus == sSelectSuffix) return;
	
	//Will be used for the final string building
	var sPath = oImg.src.substring(0,oImg.src.lastIndexOf('/') + 1);
	var sShortImgName = sCurrentImgName.substr(0,sCurrentImgName.lastIndexOf('_'));
	var sImgExt = sCurrentImgName.substring(sCurrentImgName.lastIndexOf('.'),sCurrentImgName.length);
	
	//New status
	var sNewStatus = ((sCurrentStatus == sNormSuffix) ? sOverSuffix : sNormSuffix);
	
	var sNewImgName = sPath + sShortImgName + sNewStatus + sImgExt;
	
	oImg.src = sNewImgName;
}

function rollMainImageBtn(oImg,action)
{
	//Settings
	var sNormSuffix = "_norm";
	var sOverSuffix = "_roll";
	var sSelectSuffix = "_slct";
	var iStatusLen = sNormSuffix.length;

	//For manipulation only
	var sCurrentImgName = oImg.src.substring(oImg.src.lastIndexOf('/') + 1,oImg.src.length);
	var sCurrentStatus = sCurrentImgName.substr(sCurrentImgName.lastIndexOf('_'),iStatusLen);

    //TODO
	if(action == 'over'){
	    if(currentMediaType != 0){
	        sNewImgPath = 'images/image_roll.gif';
	    }else{
	        if(mainImageWLPath.length > 0 && !showingImageWL)
	            sNewImgPath = 'images/imagewl_roll.gif';
	        else
	            sNewImgPath = 'images/image_roll.gif';
	    }
	}else{
	    if(currentMediaType != 0){
	        sNewImgPath = 'images/image_norm.gif';
	    }else{
	        if(showingImageWL)
	            sNewImgPath = 'images/imagewl_slct.gif';
	        else
	            sNewImgPath = 'images/image_slct.gif';
	    }
	}
	
	oImg.src = sNewImgPath;
}


function menuAction(def)
{
    if(def.id == 5)
    {
        openGlossary();
    }
    else if(def.url != null)
    {
	    switch(def.target)
	    {
	        case "_self" :
	            window.location.replace(def.url);
	            break;
	        case "_blank":
	            window.open(def.url);
	            break;
		        
	    }
	}	
}