﻿

function confirmAndGo(url){
	if(confirm('Etes vous sur?')){
		window.location = url
	}
	else{
		return
	}
}

function popitup(url,name,height,width) {
	newwindow=window.open(url,name,'height='+height+',width='+width+',scrollbars=1');
	if (window.focus) {newwindow.focus()}
	return false;
}

function loadProject(pid){
	window.location = "?page=project&p="+pid
}

function hide_text_edition(){
	obj = _$('texts_table');
	obj.style.display="none";
}

function show_text_edition(){
	obj = _$('texts_table');
	obj.style.display="";
}

//open and show image for lightshow form slideshow
function oifl(obj){
	if(obj.childNodes[1]){
		return obj.childNodes[1].src;
	}
	if(obj.childNodes[0]){
		return obj.childNodes[0].src;
	}
}


function display_floor(floor){
	if(document.getElementById('floor_'+floor).style.display == 'none'){
		document.getElementById('floor_'+floor).style.display = ''
	}
	else{
		document.getElementById('floor_'+floor).style.display = 'none'
	}
}

function display_document(documentName){
	if(document.getElementById('document_'+documentName).style.display == 'none'){
		document.getElementById('document_'+documentName).style.display = ''
		document.getElementById('document_'+documentName).style.backgroundColor = "#F2FCF4";
	}
	else{
		document.getElementById('document_'+documentName).style.display = 'none'
		document.getElementById('document_'+documentName).style.backgroundColor = "";
	}
}

function projects_show(id){
	obj = _$(id);
	if(obj.style.display==""){
		obj.style.display='none';
	}
	else{
		obj.style.display='';
	}
}

function openUnit(uid){
	window.location = "?page=unit&uid="+uid
}

function openEditUnit(uid){
	obj = _$('edit_unit_'+uid);
	if(obj.style.display==""){
		obj.style.display='none';
	}
	else{
		obj.style.display='';
	}
}

function saveTextsForm(){
	form = _$('texts_form');
	form.text_fr.value =  escape(WYSIWYG.getEditorWindow('text_editor_fr').document.body.innerHTML);
	form.text_en.value =  escape(WYSIWYG.getEditorWindow('text_editor_en').document.body.innerHTML);
	form.submit();
}

function translate(text,fromLang,toLang,container){
	if(confirm("Voulez vous vraiment traduire de "+fromLang+" à "+toLang)){
		length = text.length;
		
		end = 0;
		container.innerHTML = '';
		while(end < length){
			//get part of text
			charSub = text.substring(end+400,end+400+50);
			endCharPos =  charSub.search(' ')*1+end+400;
			
			part = text.substring(end,endCharPos);
			//alert(part);
			end = endCharPos;
			google.language.translate(part, fromLang, toLang, function(result) {
				if (!result.error) {
					container.innerHTML = container.innerHTML+' '+result.translation;
				}
				else{
					alert('Une erreur c\'est produite durant la traduction automatisée');
				}
			});
			
			
		}
		
	}
	else{
		return
	}
}





function backPicLeft(picName){
	document.getElementById('intro_left').style.backgroundImage = 'url(images/home/'+picName+')';
}

function backPicRight(picName){
	document.getElementById('intro_right').style.backgroundImage = 'url(images/home/'+picName+')';
}

function restorePicLeft(){
	document.getElementById('intro_left').style.backgroundImage = 'url(images/index/home_intro_buttons_middle.gif)';
}
function restorePicRight(){
	document.getElementById('intro_right').style.backgroundImage = 'url(images/index/home_intro_buttons_middle.gif)';
}

function _$(objId){
	return document.getElementById(objId);
}

function showArtistThumb(picture,obj){
	obj.className = 'artist_cell_over';
	$('showThumb').get('tween', {duration: 200}).start('opacity',0).chain(
		function() {
			if($('showThumb').style.display == 'none'){
				$('showThumb').style.display = 'block';
			}
			_$('showThumbImage').src = "/images/artists/icons/big/"+picture;
			$('showThumb').get('tween', {duration: 200}).start('opacity',1).chain(
				function() {
				}
			);
		}
	);
}

function selectTab(tab_name){
	//close tabs
	var tab = tab_name;
	var panels = $('panels').getChildren();
	panels.each(function(el){
		if(el.style){
			if(el.style.visibility != 'hidden'){
				all_tabs = $('tabs_panel').getElements('a');
				all_tabs.each(function(el){
					if(el.className == 'tabs_selected'){
						el.className = 'tabs';
					}
				});
				$(tab+'_tab').className='tabs_selected';
				el.get('tween', {duration: 'short'}).start('opacity',0).chain(
					function() {
						panels = $('panels').getChildren();
						panels.each(function(el){
							if(el.style.display != 'none'){
								el.style.display = 'none';
							}
						});
						$(tab+'_panel').style.display="block";
						$(tab+'_panel').get('tween', {duration: 'short'}).start('opacity',1).chain();
					}
				);
			}
		}
	});
}

function changeImg(id,img){
	$(id).style.backgroundImage = 'url(images/artists/'+img+')';
}

function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function animate_bck(){
	var max_opacity = 0.3;
	var min_opacity = 0;
	var speed = 2000;
	if($('color_div2').style.backgroundImage == ''){
		$('color_div2').get('tween', {duration: 0}).start('opacity',min_opacity).chain(
			function() {
				changeImg('color_div2','bck'+rand(1,7)+'.jpg');
				animate_bck();
			}
		);
	}
	$('color_div2').get('tween', {duration: speed}).start('opacity',max_opacity).chain(
		function() {
			$('color_div1').get('tween', {duration: speed}).start('opacity',min_opacity).chain(
				function() {
					changeImg('color_div1','bck'+rand(1,7)+'.jpg');
					$('color_div1').get('tween', {duration: speed}).start('opacity',max_opacity).chain(
						function() {						
							$('color_div2').get('tween', {duration: speed}).start('opacity',min_opacity).chain(
								function() {
									changeImg('color_div2','bck'+rand(1,7)+'.jpg');
									animate_bck();
								}
							);
						}
					);
				}
			);
		}
	);
}

function stopAnimate(){
	clearTimeout(artistsTimeout);
	animateArtists(99999999,'');
}

function showArtist(els,number){
	if(els[number]){
		div = $(els[number]).getElements('div');
		div[0].onmouseover();
		numberStart = number+1;
		artistsTimeout = setTimeout('animateArtists(numberStart,div[0])',5000);
	}
}

function animateArtists(start,previousObj){
	if(previousObj!='' && previousObj){
		previousObj.onmouseout();
	}
	var els = $('artists_buttons').getElements('a');
	if(start>els.length){
		return
	}else{
		showArtist(els,start);
	}
}

function changeLanguage(lang){
	//find how many folders
	var foldersNum = window.location.pathname.split('/').length;
	var folders = window.location.pathname.split('/');
	var l = '/';
	
	if(foldersNum == 2){
		var l=('/'+folders[1]+'/'+lang);
		
	}
	if(foldersNum == 3 && (folders[2] == 'fr' || folders[2] == 'en' )){
		var l=('/'+folders[1]+'/'+lang);
		
	}
	else if(foldersNum == 3){
		var l=('/'+folders[1]+'/'+folders[2]+'/'+lang);
	}
	if(foldersNum == 4 && (folders[3] == 'fr' || folders[3] == 'en' )){
		var l=('/'+folders[1]+'/'+folders[2]+'/'+lang);
		
	}
	
	window.location=l;
	
}
