/**
 * @author Thomas Collet
 * Bibliothèque javascript pour le site www.rhonetourisme.com
 */



/* Fonction permettant d'ouvrir une url
 * Ex : onclick="MM_goToURL('parent','url');return document.MM_returnValue"
 * 
 * Non utilisé car provoque bug sur IE (texte partiellement affichÃ©)
 */
function MM_goToURL() { //v3.0
	var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
	for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function insertStartLinkJs(target,url) {
 	document.write('<a href="javascript:void(0)" onclick="MM_goToURL(\''+target+'\',\''+url+'\');return document.MM_returnValue">');
} 
function insertEndLinkJs() {
 	document.write('</a>');
}

/* Fonction d'initialisation du menu
 */
function initMainMenu() {
	var rootNodeId = 'main_menu'
	if(document.getElementById(rootNodeId)){
		var rootNode = document.getElementById(rootNodeId)
		var sfEls = rootNode.getElementsByTagName('li');
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=' sfhover';
				this.getElementsByTagName('a')[0].className+=' lighton';
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(/sfhover/,'');
				this.getElementsByTagName('a')[0].className = this.getElementsByTagName('a')[0].className.replace(/lighton/,'');
			}
		}
	}
}


/* Fonction du menu carto
 */
function initCartoMenu() {
	var rootNodeId = 'carte_menu'
	if(document.getElementById(rootNodeId)){
		var rootNode = document.getElementById(rootNodeId)
		var sfEls = rootNode.getElementsByTagName('ul');
		for (var i=0; i<sfEls.length; i++) {
			if(sfEls[i].className!='activated'){
				sfEls[i].style.display = 'none';
			}
		}
		
	}
}
function toogleMenuCarto(div) {
	var rootNodeId = 'carte_menu'
	if(document.getElementById(rootNodeId)){
		var rootNode = document.getElementById(rootNodeId)
		var sfEls = rootNode.getElementsByTagName('ul');
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].style.display = 'none';
		}
	}
	Element.toggle(div);
}

function updateOptionListold() {
	
	SelectedTypeIndex = document.getElementById('Type').selectedIndex;
	CommuneSelectNode = document.getElementById('Commune');
	if ( CommuneSelectNode.hasChildNodes() )
	{
	    while ( CommuneSelectNode.childNodes.length >= 1 )
	    {
	        CommuneSelectNode.removeChild( CommuneSelectNode.firstChild );       
	    } 
	}
	for (var i = 0; i < 5; i++) {
		var NewOption = document.createElement('option');
		NewOption.text = 'Indifférent';
		NewOption.value = '0';
		CommuneSelectNode.appendChild(NewOption)
		
	}

} 

function makeHash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
}

function initSitraSearchBoxOld() {
	var searchBoxIndex = 2;
	var accordion = new Accordion(	'a.toggler',
									'div.form_box',
									{opacity: false, show:searchBoxIndex},
									$('sitra_search_box')
								  );
}


function startGallery() {
	var myGallery = new gallery($('myGallery'), {
		timed: true,
		showArrows: false,
		showCarousel: false,
		useThumbGenerator: false,
		embedLinks: false,
		delay: 6000
	});
}

function resizeElements() {
   
   var center_height = parseInt( $('center').getStyle('height') );
   var center_padding = parseInt( $('center').getStyle('padding-bottom') );
   var lefter_height = parseInt( $('lefter').getStyle('height') );
   var body_width = parseInt( $('body').getStyle('width') );
   var container_width = parseInt( $('main_container').getStyle('width') ); 
   //Menu height
   if ( lefter_height > center_height + center_padding )  {
       $('center').setStyle('height', lefter_height - center_padding + 'px')
   }else{
        $('lefter').setStyle('height', center_height + center_padding + 'px')
   };
   
   //Right menu 
   if ( $('right_menu') && body_width > container_width )  {
       $('right_menu').setStyle('right', (body_width - container_width)/2 - 10 + 'px')
   }
   
}

function initSite(modules,options) {
	if (window.addEvent) {
		window.addEvent('domready', function() {
			//initialize Sitra Search Box
			if(modules.contains('sitra_search_box') && document.getElementById('sitra_search_box')) {
			//alert('Sitra Search Box');
				/*if(options.has('ssb_toggle')){
					var myAccordion = new Accordion($('sitra_search_box'), 'h3.toggler', 'div.element', { opacity: false, show: options.get('ssb_toggle') });
				}
				else {
					//create our Accordion instance
					var myAccordion = new Accordion($('sitra_search_box'), 'h3.toggler', 'div.element', { opacity: false, show: 3 });
				}*/

				//create calendar 
				myCal = new Calendar({ 
					date1: { date1: 'd/m/Y' }, 
					date2: { date2: 'd/m/Y' },
					TS_date1: { TS_date1: 'd/m/Y' },
					TS_date2: { TS_date2: 'd/m/Y' }
				}, { pad: 0, classes: ['dashboard'], direction: 0, days: ['dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi'], months: ['janvier','f\u00e9vrier','mars','avril','mai','juin','juillet','ao\u00FBt','septembre','octobre','novembre','d\u00e9cembre'], offset: 1, tweak: { x: -168, y: 20 }, titleformat: 'D j M Y' });
				//document.getElementsByName('DateDebut')[0].value = today;
				//document.getElementsByName('DateFin')[0].value = today;
			}

			/*if(document.getElementById('main_container')
			   && document.getElementById('main_container').className == 'homepage'
			   && document.getElementById('theme_search')) {
			//alert('Sitra Search Box');
				if(options.has('ssb_toggle')){
					var myAccordion = new Accordion($('theme_search'), 'strong.title', '#theme_search form', { opacity: false, show: options.get('ssb_toggle') });
				}
				else {
					//create our Accordion instance
					var myAccordion = new Accordion($('theme_search'), 'strong.title', '#theme_search form', { opacity: false, show: 3 });
				}

				//create calendar 
				myCal = new Calendar({ 
					TS_date1: { TS_date1: 'd/m/Y' },
					TS_date2: { TS_date2: 'd/m/Y' }
				}, { pad: 0, classes: ['dashboard'], direction: 0, days: ['dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi'], months: ['janvier','f\u00e9vrier','mars','avril','mai','juin','juillet','ao\u00FBt','septembre','octobre','novembre','d\u00e9cembre'], offset: 1, tweak: { x: -168, y: 20 }, titleformat: 'D j M Y' });
				//document.getElementsByName('DateDebut')[0].value = today;
				//document.getElementsByName('DateFin')[0].value = today;
			}*/

			//initialize MainMenu
			if(modules.contains('main_menu')) {
				initMainMenu();
			}
			//initialize CartoMenu
			if(modules.contains('carto_menu')) {
			//alert('Carto Menu');
				initCartoMenu();
			}
			//initialize myGallery
			if(modules.contains('my_gallery') && document.getElementById('myGallery')) {
			//alert('Gallery');
				document.getElementById('myGallery').style.height='250px';
				document.getElementById('myGallery').style.width='520px';
				startGallery();
			}
            //resize elements
            resizeElements();
		});
        window.addEvent('resize', function() {
            //resize elements
            resizeElements();
        })
	}
	//if(modules.contains('calendar')) {alert('ok');}
}

/*
 * Source : http://simonwillison.net/2004/May/26/addLoadEvent/
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function () {
  var inputs = document.getElementsByTagName('input');
  for (i = 0 ; i < inputs.length ; i++) {
    if (inputs[i].className.match(/\s*hoverable\s*/)) {
      inputs[i].onmouseover = function () {
        this.className = this.className + ' hover';
      }
      inputs[i].onmouseout = function () {
        this.className = this.className.replace(/\s*hover\s*/, '');
      }
      inputs[i].className = inputs[i].className.replace(/\s*hoverable\s*/, '');
    }
  }

/*  if (document.getElementById('sitra_search_box')) {
    document.getElementById('TypeHeb').onchange = function() {
      var o = document.getElementById('CommuneHeb').getElementsByTagName('option');
      for(i = 1 ; i < o.length ; i++) {
        if (nbHebParCommunes[o[i].value][this.value] == 0)
          o[i].style.display = 'none';
        else
          o[i].style.display = 'block';
      }
    }
  
    document.getElementById('TypeRes').onchange = function() {
      var o = document.getElementById('CommuneRes').getElementsByTagName('option');
      for(i = 1 ; i < o.length ; i++) {
        if (nbResParCommunes[o[i].value][this.value] == 0)
          o[i].style.display = 'none';
        else
          o[i].style.display = 'block';
      }
    }
  }
*/
  ts = document.getElementById('theme_search');
  if (ts) {
    if (document.getElementById('theme_search').className == '') {
      document.getElementById('TSh_type').onchange = function() {
        var o = document.getElementById('TSh_commune').getElementsByTagName('option');
        for(i = 1 ; i < o.length ; i++) {
          if (nbHebParCommunes[o[i].value][this.value] == 0)
            o[i].style.display = 'none';
          else
            o[i].style.display = 'block';
        }
      }
      document.getElementById('TSr_type').onchange = function() {
        var o = document.getElementById('TSr_commune').getElementsByTagName('option');
        for(i = 1 ; i < o.length ; i++) {
          if (nbResParCommunes[o[i].value][this.value] == 0)
            o[i].style.display = 'none';
          else
            o[i].style.display = 'block';
        }
      }
    } else if (document.getElementById('theme_search').className == 'heb') {
      document.getElementById('TS_type').onchange = function() {
        var o = document.getElementById('TS_commune').getElementsByTagName('option');
        for(i = 1 ; i < o.length ; i++) {
          if (nbHebParCommunes[o[i].value][this.value] == 0)
            o[i].style.display = 'none';
          else
            o[i].style.display = 'block';
        }
      }
    } else if (document.getElementById('theme_search').className == 'res') {
      document.getElementById('TS_type').onchange = function() {
        var o = document.getElementById('TS_commune').getElementsByTagName('option');
        for(i = 1 ; i < o.length ; i++) {
          if (nbResParCommunes[o[i].value][this.value] == 0)
            o[i].style.display = 'none';
          else
            o[i].style.display = 'block';
        }
      }
    }
    if (document.getElementById('TS_type')
        && document.getElementById('TS_type').onchange) {
      document.getElementById('TS_type').onchange();
    }
/*
    ts2 = document.getElementById('form_'+ ts.className+ '_search');
    inputs = ts2.getElementsByTagName('input');
    for (i = 0 ; i < inputs.length ; i++) {
      if (inputs[i].type == 'checkbox'
          || inputs[i].type == 'text') {
        switch (inputs[i].name) {
        case 'handi':
        case 'anim':
        case 'pisc':
        case 'Classement-0':
        case 'Classement-1':
        case 'Classement-2':
        case 'Classement-3':
        case 'Classement-4':
        case 'Classement-5':
          inputs[i].checked = document.getElementById('TS_'+ inputs[i].name).checked;
        break;
        case 'DateDebut':
        case 'DateFin':
          inputs[i].value = document.getElementById('TS_'+ inputs[i].id).value;
        break;
        }
      }
    }
    selects = ts2.getElementsByTagName('select');
    j = null;
    for (i = 0 ; i < selects.length ; i++) {
      switch (selects[i].name) {
      case 'TypeHeb':
      case 'TypeRes':
      case 'TypeEve':
        selects[i].selectedIndex = document.getElementById('TS_type').selectedIndex;
        if (selects[i].onchange) j = i;
      break;
      case 'Commune':
        selects[i].selectedIndex = document.getElementById('TS_commune').selectedIndex;
      break;
      case 'MenuAdulte':
        selects[i].selectedIndex = document.getElementById('TS_menu_adulte').selectedIndex;
      break;
      }
    }
    if (j != null) selects[j].onchange();
    */
  }

});
