var display='';

var typeTextArr = new Array();
typeTextArr['hebergement'] = 'H&eacute;bergements &agrave; proximit&eacute;';
typeTextArr['randonnees'] = 'Chemins de randonn&eacute;e &agrave; proximit&eacute;';
typeTextArr['restauration'] = 'Se restaurer &agrave; proximit&eacute;';

function main_core() {	

	function mkSortSelect() {
	
		var val = $('#typeChoix').val();
		val = (!val) ? $('#type').val() : val;
		
		switch(val) {			
			case 'CVV' :
			case 'GSH' :			
						
			case 'VPT' :
			case 'VIS' :
			case 'CES' :	
			
			case 'ATS' :
			case 'ACR' :
			case 'DEB' :
			case 'ANG' :
			case 'VDS' :		
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom'}
			break;
			case 'TFM' :
			case 'BIL' :
			case 'PEA' :
			case 'FES' :
			case 'SPE' :
			case 'EXP' :
			case 'EVG' :
			case 'BFP' :
			case 'FOS' :
			case 'FTR' :
			case 'SEL' :		
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom','categorie':'Catégorie'}	
			break;
			case 'HOT' :
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom','etoile':'Etoile','price':'Prix'}	
			break;
			case 'GIM' :
			case 'CHO' :
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom','etoile':'Etoile / épis','capacity':'Capacité','price':'Prix'}	
			break;
			case 'CAM' :
			case 'RTH' :
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom','etoile': 'Etoile'}	
			break;
			case 'RES' :
				obj = {'zone':'Territoire','lieu':'Commune','titre':'Nom','price':'Prix'}	
			break;
			
		}

		if(typeof obj == 'object') {
			
			$('.sorting .sort select').empty().append('<option value="0">...</option>');
			for(var x in obj) {
				$('.sorting .sort select').append('<option value="'+x+'">'+obj[x]+'</option>');
			}		
		}
	}

	function disabledField(val) {
	
		$('#display_form ul li, #display_form ul li > div').hide();
		$('#display_form input, #display_form select').attr('disabled',true);
							
		$('.default, .'+val).show();
		$('.'+val).parent('li').show();
		$('.default input[type=hidden],.default input, .'+val+' input:visible, .default select, .'+val+' select:visible').removeAttr('disabled');	
	}
	
	function OStoday() {
	
		var today = new Date();
		$('#search_form .day option[value='+parseInt(today.getDate())+']').attr('selected',true);
		$('#search_form .month option[value='+(today.getMonth()+1)+']').attr('selected',true);
		$('#search_form .year option[value='+(today.getYear() + 1900)+']').attr('selected',true);
	
	}
	
	function OSresaButton(act) {
		var el = $('#resa').parent().parent();
		
		if(act=='hide') {
			el.addClass('hide').removeClass('default');
		}
		else {
			el.removeClass('hide').addClass('default');
		}
	}
	
	function OSformResa(val) {	
		var stat = $('#resa').attr('checked');		

		if(stat == true) {
			$('#search_form .reservation.r'+val).show();
			$('#search_form .reservation.r'+val+' input, #search_form .reservation.r'+val+' select').removeAttr('disabled');
			$('form#search_form').attr({
				'action' : 'http://ain.for-system.com/index.aspx',
				'target' : '_self'
			}).data('ajax',false);
		}
		else {
			$('#search_form .reservation').hide();
			$('#search_form .reservation input, #search_form .reservation select').attr('disabled',true);
			$('form#search_form').attr({
				'action' : '/module/carto/index.php',
				'target' : '_self'
			}).data('ajax',true);
		}
	
	}
	
	function OScriteres(keys) {
	
		if(typeof keys == 'object') {
		
			var key='';
			var val = '';
			for(var x in keys) {
			
				key = keys[x];
				switch(key) {
					case 'piscine':
						if($('#'+key).attr('checked')){
							val += '#19,198,199';
						}						
					break;
					case 'animaux':
						if($('#'+key).attr('checked')){
							val += '#67';
						}						
					break;
				}
			}	
			val = val.substring(1);
			$('input[name=Globales/ListeIdCaract]').val(val);	
		}	
	}
	
	function OSchildren(){
		
		var val = $('#renfant').val();
		var val2='';
		var xml= '<?xml version="1.0" encoding="utf-16" ?><xmlAges>';
		if(val>0) {			
			for(var x=1; x <= val; x++) {				
				val2 = $('#AgeEnfant'+x).val();
				xml += '<XmlAgeEnfant>'+val2+'</XmlAgeEnfant>';
			}			
		}	
		xml += '</xmlAges>';	
		$('input[name=Globales/XmlAges]').val(xml);
	}
	
	function OSnbChildren() {
	
		$('#renfant').change(function(){
			var val = $(this).val();
			$('#AgeEnfant1,#AgeEnfant2,#AgeEnfant3,#AgeEnfant4,#AgeEnfant5').hide();
			for(var x=1;x <= val;x++) {
				$('#AgeEnfant'+x).show().css('display','inline');
			}
		});	
	}
	
	function OStypeSejour() {
	
		$('input[name=typeSejour]').click(function(e){
			var val = $(this).val();
			if(val==2) {
				$('select[name=Globales/DureeSemaine]').hide();
				$('select[name=Globales/DureeNuit]').show();
			}
			else {
				$('select[name=Globales/DureeNuit]').hide();
				$('select[name=Globales/DureeSemaine]').show();
			}
			$('input[name=Globales/TypeSejour]').val(val);			
		});
		
		
		$('select[name=Globales/DureeNuit]').unbind('change').change(function(){
			var val = $(this).val();
			$('input[name=Globales/DureeSejour]').val(val);	
		});
		
		$('select[name=Globales/DureeSemaine]').unbind('change').change(function(){
			var val = $(this).val();
			$('input[name=Globales/DureeSejour]').val(val * 7);	
		});						
	}
	
	function OSclassement() {
	
		var val = val2 = '';
		$('div.etoile1 input:checked, div.etoile2 input:checked, div.etoile3 input:checked, div.etoile4 input:checked, div.etoile5 input:checked').each(function(){
							
			val2 = $(this).val();
			switch(val2) {
				case '1':
					val += ',1,11,35';
				break;
				case '2':
					val += ',2,12,36';
				break;
				case '3':
					val += ',3,13,37';
				break;
				case '4':
					val += ',4,5,14,38';
				break;
				case '5':
					val += ',15,39';
				break;
			}	
							
		});							
						
		val = (val!='') ? val.substring(1) : '*';
			
		val2 = $('select[name=classement]:visible').val();			
		switch(val2) {							
			case '1':
				val ='1,11,35';
			break;
			case '2':
				val ='2,12,36';
			break;	
			case '3':
				val ='3,13,37';
			break;
			case '4':
				val ='4,5,14,38';
			break;	
			case '5':
				val ='15,39';
			break;
		}
		val = (val!='') ? val : '*';
					
		if(val) {
			$('input[name=Globales/ListeIdClassement]').val(val);
		}
	
	}	
	
	function OScapacite() {
	
		var val = $('select[name=capacite]').val();
			
		switch(val) {							
			case '-1':
				val ='0,0';
			break;
			case '9+':
				val ='0,100';
			break;
			default:
				val = val.replace(/\-/g,',');
			break;	
		}
										
		$('input[name=Globales/Capacite]').val(val);	
	}	
		
	function OSgim() {	
					
		OScapacite();
		OSclassement();	
		OStypeSejour();
		OStoday();
		OScriteres(['piscine','animaux']);
		
		$('select[name=classement]:visible').unbind('change').change(function(){
			OSclassement();
		});
		
		$('select[name=capacite]').unbind('change').change(function(){				
			OScapacite();				
		});
		
	}
	
	function OShot() {	
					
		OSchildren();
		OSclassement();
		OStoday();
		OScriteres(['piscine','animaux']);
		
		$('input[name=Globales/DureeSejour]').val($('input[name=Globales/DureeNuit]').val());
		$('select[name=Globales/DureeNuit]').change(function(){
			var val = $(this).val();
			$('input[name=Globales/DureeSejour]').val(val);	
		});
		
		$('select[name=capacite]').unbind('change').change(function(){				
			OSgim();				
		});
		
		$('div.etoile1 input, div.etoile2 input, div.etoile3 input, div.etoile4 input, div.etoile5 input').click(function(){
			OSclassement();
		});	
		
		OSnbChildren();
		$('#AgeEnfant1,#AgeEnfant2,#AgeEnfant3,#AgeEnfant4,#AgeEnfant5').change(function(){
			OSchildren();
		});		
		
		$('input[name=Globales/TypeSejour]').val(2);
	}
	
	function OScho() {
	
		OScapacite();
		OStypeSejour();
		OStoday();
		OScriteres(['piscine','animaux']);
		
		$('select[name=capacite]').unbind('change').change(function(){				
			OScapacite();				
		});
	
	}
	
	function initResa(callback) {
	
		var val = $('#type').val();
		var val2 = $('#typeChoix').val();
		val = (val2) ? val2 : val;
		var code = show = '';
		
		OStoday();
		switch(val) {
			case 'HOT':
				code = '878';
				show = 'rhot';	
				OSchildren();
				OSclassement();				
				OScriteres(['piscine','animaux']);
				
				$('input[name=Globales/DureeSejour]').val($('input[name=Globales/DureeNuit]').val());
				
				$('div.etoile1 input, div.etoile2 input, div.etoile3 input, div.etoile4 input, div.etoile5 input').click(function(){
					OSclassement();
				});	
				
				OSnbChildren();
				$('#AgeEnfant1,#AgeEnfant2,#AgeEnfant3,#AgeEnfant4,#AgeEnfant5').change(function(){
					OSchildren();
				});		
				
				$('select[name=Globales/DureeNuit]').unbind('change').change(function(){
					var val = $(this).val();
					$('input[name=Globales/DureeSejour]').val(val);	
				});
				
				$('input[name=Globales/TypeSejour]').val(2);								
			break;
			case 'GIM':
				code = '877'; //gite et meublés
				show = 'rgim';				
				OScapacite();
				OSclassement();	
				OStypeSejour();
				OScriteres(['piscine','animaux']);
				
				$('select[name=classement]:visible').unbind('change').change(function(){
					OSclassement();
				});
				
				$('select[name=capacite]').unbind('change').change(function(){				
					OScapacite();				
				});		
			break;
			case 'CHO':
				code = '879';
				show = 'rcho';
				OScapacite();
				OStypeSejour();
				OScriteres(['piscine','animaux']);
				
				$('select[name=capacite]').unbind('change').change(function(){				
					OScapacite();				
				});
			break;
			case 'RTH':
				code = '1464';
				show = 'rrth';
				OSchildren();
				
				$('input[name=Globales/DureeSejour]').val($('input[name=Globales/DureeNuit]').val());
				
				OSnbChildren();
				$('#AgeEnfant1,#AgeEnfant2,#AgeEnfant3,#AgeEnfant4,#AgeEnfant5').change(function(){
					OSchildren();
				});		
				
				$('select[name=Globales/DureeNuit]').unbind('change').change(function(){
					var val = $(this).val();
					$('input[name=Globales/DureeSejour]').val(val);	
				});
				
				$('input[name=Globales/TypeSejour]').val(2);
			break;
			
			
			
			case 'ATS':
			case 'VDS':
				
				OSresaButton('hide');
				var typ = $('#type').val(); 
				code = (typ=='CAN') ? '882' : '881';
				
				//echo(code+' * '+typ);
				$('input[name=Globales/TypeCourse]').addClass('hide');
				switch(typ) {
					case 'TOF':
					case 'CAN':					
					case 'VIA':
					case 'VIE':
						show = 'rtof';
						OSresaButton('show');
						
						if(typ=='TOF') {
							var course = 75;
						}
						else if(typ=='CAN') {
							var course = 6;
						}
						else if(typ=='VIA') {
							var course = 53;
						}
						else if(typ=='VIE') {
							var course = 92;
						}						
						
						$('input[name=Globales/TypeCourse]').removeClass('hide').val(course);
						
						var val = $('select[name=day]').val()+'/'+$('select[name=month]').val()+'/'+$('select[name=year]').val();
						$('input[name=Globales/DateDeb]').val(val);						
						$('select[name=day],select[name=month],select[name=year]').unbind('change').change(function(){
							var val = $('select[name=day]').val()+'/'+$('select[name=month]').val()+'/'+$('select[name=year]').val();
							$('input[name=Globales/DateDeb]').val(val);
						});
						
					break;
				}
			break;
			case 'ACR':
				OSresaButton('hide');
			break;
			case 'DEB':
				OSresaButton('hide');
			break;
			case 'ANG':
				OSresaButton('hide');
			break;
			case 'VDS':
				OSresaButton('hide');
				if($('#type').val()=='MUT') {
					OSresaButton('show');
					$('form#search_form').attr({
						'action' : 'http://ain.for-system.com/z880e1_fr-Musees-expositions-et-sites-touristiques-Ain-Activites-et-loisirs-Ain-reservation-en-ligne.aspx',
						'target' : '_self'
					}).data('ajax',false);
				}
			break;
			
			
			case 'BIL':
				$('form#search_form').attr({
					'action' : 'http://ain.for-system.com/z2564e1_fr-Spectacle-Ain-billetterie-reservation-en-ligne-spectacle-Ain.aspx',
					'target' : '_self'
				}).data('ajax',false);
				$('#resa:not(:checked)').each(function(){
					$(this).parent().trigger('click');
				});
			break;	
		
		}		

		if(code) {
			$('input[type=hidden]:not(#sector)').attr('disabled',true).hide();
			$('input[type=hidden].'+show+', input[type=hidden].default').removeAttr('disabled');
			$('input[name=Globales/RZ]').val(code);
		}
		
		if(typeof callback == 'function') {		
			callback(show);		
		}	
	}
	
	function OSzone(code){
	
		var communesCode = { 
			'liste1' : {
				'bresse' : '169653,170057,170135,170803,170804,171547,171917,171942,172279,172705,172792,173265,173473,174063,175188,175232,176293,176294,176558,176776,177018,177265,177431,177435,177498,177762,177827,177955,178173,178212,178309,178327,178862,178926,179100,179120,180017,180319,180679,181748,182100,182272,182465,182991,183797,183829,183936,185890,187618,188085,189418,189599,189614,189644,190031,190417,190844,191423,191606,192074,192131,193029,194066,194529,194550,194879,195265,195353,195380,195381,195588,195826,195865,196312,196656,196721,196732,199441,199439,199491,199724,200126,200178,200207,200332,200359,200551,201196,201198,201327,201343,201483,201940,201979,202301,202836,203131,203238,197728,198690,198740,198750,198895,204622,185473,204851,205509,205552,206051,206128,206229,206233,206773,206898,207241,207413',
				'bugey'  : '184180,169217,169236,169259,169263,169375,169439,169622,169636,169639,169649,169652,169804,169853,169967,171768,171803,171805,171807,171838,171909,171920,172208,172413,172675,172833,173470,173623,173655,173660,173665,173865,173869,184430,175064,175129,175233,175326,175340,175479,175520,175543,175638,175797,175969,176121,176314,176336,176337,176351,176533,176912,177098,177186,177209,177332,177338,177371,177376,177411,177413,178037,178283,178383,178981,179041,179237,180067,180559,181722,181723,181922,186894,182511,182527,183018,183374,183376,183602,183604,183715,183716,183721,184000,185577,185819,185919,186204,186566,188204,188211,188239,188243,188553,188639,188640,188941,189255,189304,189669,189843,190053,190145,190161,190617,191430,191485,192080,192646,192772,192794,187951,193161,193489,193688,193762,194033,194060,194321,187172,194673,194675,194914,187253,195266,195325,195499,195791,195796,195985,197281,197526,199353,199731,199773,199903,200170,203617,200669,200746,201168,201837,201935,202064,202826,203115,203393,197847,198119,198450,198724,198726,198815,199065,199074,199152,203859,203885,203960,204127,204297,204589,205756,206409,206410,206647,207025,207244,207245,207247,207411,179402',
				'dombes' : '184178,169203,169219,169949,170907,170980,171560,171726,172289,172461,172510,184371,173177,173278,173679,175308,175321,175653,175673,184556,175852,176091,176125,176133,176306,176767,177242,177941,178395,178904,178911,180188,180193,181143,181539,181706,182606,183558,183795,183965,186251,189073,189887,190142,190755,190811,190963,191007,191016,191062,191597,187122,191827,191971,191996,193014,193087,193140,194219,194534,194701,194906,187211,196339,196564,196731,196833,197118,199446,199480,199751,200210,200223,203509,203549,200265,203706,200358,200636,200764,201103,201116,201329,201706,202057,202059,202302,202461,203239,197895,198268,203828,204341,204393,204757,204798,204988,205377,206178,206584,206793,207049,207124',
				'gex'    : '175219,175343,176562,176578,177041,177263,178191,178776,179253,180195,180370,181815,182483,186024,187519,187577,190909,193850,194528,195584,195854,200550,201076,198192,198415,198652,204391,206188,206224,190910'
			},
			'liste2' : {
				'bresse' : "'010016','010023','010024','010025','010026','010029','010038','010040','010042','010245','010050','010053','010057','010065','010069','010072','010094','010095','010102','010106','010108','010115','010123','010124','010125','010127','010128','010130','010134','010136','010139','010140','010144','010147','010150','010151','010154','010159','010163','010172','010175','010177','010179','010184','010195','010196','010197','010203','010211','010212','010229','010230','010231','010232','010236','010241','010246','010254','010259','010264','010266','010273','010284','010289','010291','010296','010301','010304','010305','010306','010309','010312','010314','010317','010320','010321','010323','010332','010334','010336','010337','010343','010344','010346','010350','010352','010355','010364','010365','010367','010369','010370','010374','010375','010380','010385','010387','010388','010391','010402','010405','010406','010408','010422','010425','010426','010429','010430','010432','010433' ,'010437','010439','010445','010447','010451','010457'",
				'bugey'  : "'010002','010004','010006','010007','010008','010009','010010','010011','010012','010013','010014','010015','010017','010019','010022','010033','010034','010035','010031','010036','010037','010039','010041','010044','010047','010051','010056','010058','010059','010060','010061','010063','010064','010066','010067','010068','010073','010076','010077','010079','010080','010081','010082','010087','010089','010091','010097','010098','010099','010100','010101','010107','010110','010111','010112','010116','010117','010118','010119','010121','010122','010133','010138','010141','010148','010149','010152','010155','010162','010171','010170','010174','010176','010181','010182','010185','010186','010187','010189','010190','010191','010192','010193','010199','010200','010202','010204','010206','010208','010213','010214','010215','010216','010218','010219','010221','010224','010227','010228','010233','010234','010237','010239','010240','010242','010255','010257','010265','010268','010269' ,'010271','010274','010277','010267','010279','010280','010282','010283','010286','010292','010293','010294','010298','010300','010302','010303','010307','010310','010311','010316','010329','010330','010331','010338','010340','010341','010345','010366','010357','010358','010363','010372','010373','010379','010384','010386','010390','010392','010396','010400','010403','010404','010407','010409','010410','010411','010413','010414','010415','010416','010417','010421','010431','010442','010441','010444','010448','010452','010453','010454','010456','740109'",
				'dombes' : "'010001','010165','010005','010021','010027','010028','010030','010032','010043','010045','010046','010049','010052','010054','010062','010074','010075','010083','010084','010085','010088','010090','010092','010093','010096','010105','010113','010129','010142','010146','010145','010156','010157','010166','010167','010169','010183','010188','010194','010198','010207','010225','010235','010238','010243','010244','010248','010249','010250','010252','010258','010260','010261','010262','010263','010272','010275','010276','010285','010290','010295','010297','010299','010318','010319','010322','010325','010328','010333','010335','010339','010347','010348','010342','010353','010349','010382','010351','010356','010359','010361','010362','010368','010371','010376','010378','010381','010383','010389','010393','010398','010412','010418','010420','010423','010424','010427','010428','010434','010443','010446','010449','010450'",
				'gex'    : "'010071','010078','010103','010104','010109','010114','010135','010143','010153','010158','010160','010173','010180','010205','010209','010210','010247','010281','010288','010308','010313','010354','010360','010397','010399','010401','010419','010435','010436','010247'"
			}
		};
			
		var val = $('#sector').val();
		var input='';
		code = (code) ? code : 1;
		
		if(val.search(/;1/g)!='-1') {
			input += ','+communesCode['liste'+code].bresse; 
		}
		if(val.search(/;2/g)!='-1') {
			input += ','+communesCode['liste'+code].dombes; 
		}
		if(val.search(/;3/g)!='-1') {
			input += ','+communesCode['liste'+code].bugey; 
		}
		if(val.search(/;4/g)!='-1') {
			input += ','+communesCode['liste'+code].gex; 
		}
		
		if(input=='') {
			input = '*';
		}
		else {
			input = input.substring(1);
		}
						
		$('input[name=Globales/ListeIdCommune]').val(input);
			
		}	

	return {
		
		/*#####################################################
		# var init
		#####################################################*/
		version : '1.0',
		sortField : '',
		
		/*#####################################################
		# title changer
		#####################################################*/
		changeTitle: function(title){
	
			var origin = $('body').data('title');
			title = title.replace(/&amp;/g,'&');		
			$(document)[0].title = origin+' '+title;

		},
		
		/*#####################################################
		# follow tag link
		#####################################################*/
		cloudInit: function() {
			url = $(window)[0].location.hash;
			if(url) {
				var spl = url.split(/\-/g);
				switch(spl[2]) {
					case '1':
						$('div.hebergement').trigger('click');
					break
					case '2':
						$('div.gastronomie').trigger('click');
					break;
					case '3':
						$('div.avoir_afaire').trigger('click');
					break;
					case '4':
						$('div.fete_manif').trigger('click');
					break;
				}
				
				var root = $('input:[name=category]:checked').val();
				var search = {
					'category' : root,
					'type' : spl[1]
				}
				
				idfr.map.search(search,'',function(){
					idfr.main.initListing();
					var arian = $('.navigation').data('arian');
					$('.navigation p').html(arian+' &gt; Listing');	
					idfr.main.changeTitle('Recherche cartographique');																															
				});			
			}
		},
		
		/*#####################################################
		# wysistats
		#####################################################*/
		wysi : function() {
			$('.wysi').click(function(e){
				  
				//MAIL;HOTNOR027FS0003A|site
				//URL;HOTNOR027FS0003A|site
				var sp = $(this).attr('rel').split(/\|/g);
				if (valeur==1){
					stat('cdt01','','',sp[0],'','','',sp[1]);
				}			
			});
		},

		/*#####################################################
		# sorting
		#####################################################*/
		sortingInit: function() {
		
			$('#listing .sort select').bind('change',function(e){
			
				
				$('#listing .sort select option[value=0]').remove();
				var val = $('option:selected',this).val();
				idfr.main.sortField = val;
				var type='';
				var dir = '';

				switch(val) {
					case 'price':
						type = 'float';
					break;
					case 'etoile':
						type = 'int';
						dir = 'desc'
					break;
					case 'capacity':
						type = 'int';
						dir = 'desc'
					break;
					}
				
				idfr.map.sorting(val, type, dir, function(){			
					idfr.main.selectSortField();				
				});					
			});			
		},
		
		selectSortField: function(){
			mkSortSelect();		
			$('#listing .sort select option[value='+idfr.main.sortField+']').attr('selected',true);		
		},
		
		/*#####################################################
		# Init method
		#####################################################*/
		init : function(){
		
			var _this = this;
			//init zoom
			$('#zoom #plus').click(function(){
				idfr.map.zoom('+1',function(old,level){
				
					$('#zoom #level').removeClass('level'+old).addClass('level'+level);
				});
			});
			
			$('#zoom #moins').click(function(){
				idfr.map.zoom('-1',function(old,level){
				
					$('#zoom #level').removeClass('level'+old).addClass('level'+level);
				});
			});
			
			//init all form
			$('form#search_form').submit(function(e){
			
				var data = $(this).data('ajax');
				if(typeof data == 'undefined' || data==true) {
			
					$('#listing,#map_workspace,#carto_footer').show();
					$('#search').removeClass('hide');
					$('#detail').hide();
					var test = $('#map_workspace');
					if(test.length>0) {
						e.preventDefault();				
						
						idfr.map.search('#'+$(this).attr('id'),'',function(){
							_this.initListing();
							var arian = $('.navigation').data('arian');
							$('.navigation p').html(arian+' &gt; Listing');	
							_this.changeTitle('Recherche cartographique');						
							
							mkSortSelect();																															
						});					
						
						return false;
					}
					else {
						return true;
					}
				}
				else {					
					return true;
				}
			});
		},
		
		initListing : function() {
		
			$('.idfrmap_flag').trigger('mouseleave');
			var bulletimer = 0;
			$('.block').mouseenter(function(){
				clearTimeout(bulletimer);
					
				var id = $(this).attr('id').replace(/block/g,'');
				$(this).addClass('block_selected');
				$('#picto_'+id).addClass('selected');
				$('.idfrmap_flag[id=picto_'+id+']').trigger('mouseover');
			
			}).mouseleave(function(){
				
				var id = $(this).attr('id').replace(/block/g,'');
				$(this).removeClass('block_selected');
				$('#picto_'+id).removeClass('selected');
				bulletimer = setTimeout(function(){
					$('.idfrmap_flag[id=picto_'+id+']').trigger('mouseleave');
					clearTimeout(bulletimer);
				},1000);
				$('#infobulle').mouseenter(function(e){clearTimeout(bulletimer);});
			});
		},
		
		initMask : function(what){
		
			switch(what) {
				case('bresse') :
					var map = new Array({y:20,x:0}, {y:14,x:75}, {y:0,x:140}, {y:24,x:195},	{y:60,x:195}, {y:60,x:8}, {y:54,x:15}, {y:40,x:2}, {y:35,x:8}, {y:25,x:0});
				break;
			};
			
			if(typeof map == 'object') {
				idfr.ui.makeMask('mask1',{	
					'width': '150px',
					'height': '200px',
					'top':'12px',
					'left':'84px',
					'draw': map,
					'color': 'blue'	
				});
			}		
		}, 
		
		infobulle: function() {

			
			idfr.ui.infoBulle('.idfrmap_flag',function(id){
					
				idfr.map.tplLoader('infobulle',function() {					
					
					var html = idfr.map.setTooltip(id,'infobulle');
					$('#infobulle div.warp').html(html);
					
					});
				},{
					'vertical':'none',
					'horizontal':'left',
					'decal':[-39,14,0,4],
					'in':function(id){
						//$('#block'+id).addClass('block_selected'); 
						//$('#picto_'+id).addClass('selected');
						idfr.ui.tooltips();						
					},
					'out':function(id){
						//$('#block'+id).removeClass('block_selected');
						//$('#picto_'+id).removeClass('selected');
					}
					
				});
				
			
		},
				
		initForm : function(what,query) {
		
			var _this = this;	
			switch(what) {
				
				case('heb') :
					this.tplLoader('form_hebergement',function() {							
					
						_this.wysi();
						$('#display_form').html(_this.templates.form_hebergement);
						$('#search .tagcloudCarto').html(_this.templates.cloud_hebergement);		
									
						idfr.ui.mkArea('.departement','#sector', [	
							{
								'id':'bresse',
								'action':function() {
									//echo('bresse');
									OSzone();
									},
								'title':'Bresse',
								'value':1
							},{
								'id':'dombes',
								'action':function() {
									//echo('dombes');
									OSzone();
								},
								'title':'Dombes',
								'value':2
							},{
								'id':'bugey',
								'action':function() {
									//echo('bugey');
									OSzone();
								},
								'title':'Bugey',
								'value':3
							},{
								'id':'gex',
								'title':'Pays de Gex',
								'action':function() {
									//echo('gex');
									OSzone();
								},
								'value':4
							}],
							{'type':'radio'}	
						);
						
						idfr.ui.mkDefault('input.default');	
						idfr.ui.mkButton([
							'etoile',
							'internet',
							'restaurant',
							'parking',
							'animaux',
							'bienetre',
							'piscine',
							'logisFrance',
							'baignade',
							'enfant',
							'chequeVac',
							'tableHot',
							'resa',
							'locahll'
						]);					
						
						
						
						$('#type').change(function(){
	
							var val = $(this).val();
							disabledField(val);
							initResa();
							$('.resa.checked').trigger('click');
	
						});	
						disabledField('HOT');					
						
						$("#ville").autocomplete("/module/carto/commune.php");
						
						if(query){
							idfr.ui.initForm(query,'#search_form');
						}
						
						idfr.ui.tooltips();
						
						$('#resa').click(function(){							
							var val = $('select[name=type]').val();
							val = val.toLowerCase();
							OSformResa(val);							
							
							initResa();
							
						});	
						
						
											
												
					});
					
				break;
				case('gas') :
					this.tplLoader('form_gastronomie',function() {	
						
						_this.wysi();
						$('#display_form').html(_this.templates.form_gastronomie);	
						$('#search .tagcloudCarto').html(_this.templates.cloud_gastronomie);					
									
							idfr.ui.mkArea('.departement','#sector', [	
							{
								'id':'bresse',
								'action':function() {
									//echo('bresse');
									},
								'title':'Bresse',
								'value':1
							},{
								'id':'dombes',
								'action':function() {
									//echo('dombes');
								},
								'title':'Dombes',
								'value':2
							},{
								'id':'bugey',
								'action':function() {
									//echo('bugey');
								},
								'title':'Bugey',
								'value':3
							},{
								'id':'gex',
								'title':'Pays de Gex',
								'action':function() {
									//echo('gex');
								},
								'value':4
							}],
							{'type':'radio'}	
						);
						
						idfr.ui.mkDefault('input.default');	
						idfr.ui.mkButton([
							'terrasse',
							'bordeau',
							'gastronomique',
							'traditionnelle',
							'francaise',
							'cuisineautres',
							'logipicto',
							'cuisinier',
							'prodvins',
							'prodfrom',
							'autreprod',
							'rdpev',
							'resa'
						]);					
						
						$('#type').change(function(){
	
							var val = $(this).val();
							disabledField(val);
						
						});	
						disabledField('RES')					
						
						
						$("#ville").autocomplete("/module/carto/commune.php");
						
						if(query){
							idfr.ui.initForm(query,'#search_form');
						}
						
						idfr.ui.tooltips();
			
						
					});
					
	
				break;
				case('avf') :
					this.tplLoader('form_avoir_afaire',function() {	
						
						_this.wysi();
						$('#display_form').html(_this.templates.form_avoir_afaire);
						$('#search .tagcloudCarto').html(_this.templates.cloud_avoir_afaire);				
									
							idfr.ui.mkArea('.departement','#sector', [	
							{
								'id':'bresse',
								'action':function() {
									//echo('bresse');
									},
								'title':'Bresse',
								'value':1
							},{
								'id':'dombes',
								'action':function() {
									//echo('dombes');
								},
								'title':'Dombes',
								'value':2
							},{
								'id':'bugey',
								'action':function() {
									//echo('bugey');
								},
								'title':'Bugey',
								'value':3
							},{
								'id':'gex',
								'title':'Pays de Gex',
								'action':function() {
									//echo('gex');
								},
								'value':4
							}],
							{'type':'radio'}	
						);
						
						idfr.ui.mkDefault('input.default');	
						idfr.ui.mkButton([
							'terrasse',
							'bordeau',
							'gastronomique',
							'traditionnelle',
							'francaise',
							'cuisineautres',
							'logipicto',
							'cuisinier',
							'prodvins',
							'prodfrom',
							'autreprod',
							'rdpev',
							'resa'
						]);							
						
						$('#typeChoix').change(function(){
	
							var val = $(this).val();
							initResa();							
							disabledField(val);
							$('#resa:checked').trigger('click');
						
						});	
						
						$('#type').change(function(){
							var val = $(this).val();
							initResa();							
							disabledField(val);
							$('#resa:checked').trigger('click');
						
						});	
						
						$('#resa').click(function(){							
							var val = $('select[name=type]').val();
							val = val.toLowerCase();
							OSformResa(val);							
							initResa();						
						});	
						OSresaButton('hide');						
						
						$("#ville").autocomplete("/module/carto/commune.php");
						
						if(query){
							idfr.ui.initForm(query,'#search_form');
						}
						
						idfr.ui.tooltips();					
						
						var fields = new Array();
						
						fields[0] = {
							'ATS' : {
								'CAK' : 'Cano?kayak',
								'CAN' : 'Canyoning',
								'PEC' : 'Pêche',
								'BEL' : 'Baignade et bases de loisirs',
								'TOF' : 'Tourisme fluvial'
							},
							'ACR' : {
								'PAA' : 'Parcours aventure et accrobranche&reg;',
								'ASV' : 'Escalade et via ferrata',
								'PAS' : 'Parapente et sports aériens',
								'MON' : 'Montgolfière',
								'RAN' : 'Randonnées',
								'RAA' : 'Randonnée avec âne',
								'VEV' : 'Vélo / VTT',
								'GOL' : 'Golf',
								'EQU' : 'Equitation',
								'BIA' : 'Biathlon',
								'SES' : 'Séjours sportifs',
								'SPL' : 'Spéléologie',
								'SPM' : 'Sports mécanique',
								'IPL' : 'Insolites et parcs de loisirs'
							},
							'ANG' : {
								'SIS' : 'Sites et stations',
								'LOM' : 'Location de matériel',
								'ACH' : 'Activités hiver'
							},
							'VDS' : {
								'VIA' : 'Visite accompagnée',
								'MUT' : 'Musées et sites touristiques',
								'DEC' : 'Sites naturels et sentiers de découverte',
								'VIE' : 'Visites d\'entreprises et savoir faire'
							}
						}
						//				<!--<option value="FOI">Forfait ski</option>-->
						
						fields[1] = {
							'CAN':{
								3455 : 'Sites de canyoning',
								3460 : 'Sortie accompagnées'	
							},
							'PEC':{
								3456 : 'Sites et étangs de pêche',
								3461 : 'Sorties accompagnés, stages'
							},
							'TOF': {
								3462 : 'Bateaux promenades',
								3458 : 'Haltes nautiques et ports fluviaux'							
							},
							'ASV': {
								3464 : 'Escalade et via ferrata',
								3465 : 'Sorties accompagnées',
								3495 : 'Location de matériel'
							},
							'PAS': {
								3458 : 'Sites d\'envol et d\'atterrissage',
								3494 : 'Baptêmes, vols, stages'
							},
							'RAN' : {
								3492 : 'Balades régulières',
								3406 : 'Accompagnateurs en montagne',
								3405 : 'Grand itinéraires'
							},
							'VEV' : {
								3485 : 'Sites VTT FFC',
								3489 : 'Initiation, sorties accompagnées',
								3490 : 'Location'
							},
							'SPL' : {
								3481 : 'Sites de spéléologie, grottes',
								3482 : 'Découverte, sorties accompagnées'
							},
							'MUT' : {
								3470 : 'Musée et centres d\'interprétation',
								3471 : 'Apothicaireries',
								3472 : 'Châteaux',
								3473 : 'Sites et monuments classés ou inscrits aux Monuments historiques',
								3475 : 'Aventure jeu Fifrelin',
								3474 : 'Incontournables'
							},
							'SPL' : {
								3481 : 'Sites de spéléologie grottes',
								3482 : 'Découverte, sorties accompagnées'
							},
							'DEC' : {
								3468 : 'Sentiers d\'interprétation et thématique',
								3467 : 'Patrimoine naturel',
								3407 : 'Parcs et jardins'
							},
							'VIE' : {
								3466 : 'Métiers d\'art',
								3445 : 'Métiers de bouche'
							}
						}
						
						$('select[name=typeChoix]').change(function(){						
							var val = $(this).val();	
							
							$('select[name=type]').html('');	
							$('select[name=sousType]').html('');					
							if(typeof fields[0][val] != 'undefined') {							
								var sel = $('select[name=type]');
								var opt = fields[0][val];
								sel.append('<option value="-1">---Choix---</option>');
								for(var x in opt) {
									sel.append('<option value="'+x+'">'+opt[x]+'</option>');
								}
								sel.show();
								$('li.act').addClass('default').show();							
							}
							else {
								$('li.act,li.crit').removeClass('default');
								$('li.act,li.crit,select[name=type],select[name=sousType]').hide();
							}						
						});
						
						$('select[name=type]').change(function(){						
							var val = $(this).val();	
							
							$('select[name=sousType]').html('');													
							if(typeof fields[1][val] != 'undefined') {							
								var sel = $('select[name=sousType]');
								var opt = fields[1][val];
								sel.append('<option value="-1">---Choix---</option>');
								for(var x in opt) {
									sel.append('<option value="'+x+'">'+opt[x]+'</option>');
								}
								sel.show();
								$('li.crit').show();							
							}
							else {
								$('li.crit').removeClass('default');
								$('li.crit,select[name=sousType]').hide();
							}						
						});
			
					});	
					
				break;
				case('fma') :
					this.tplLoader('form_fete_manif',function() {	
						
						_this.wysi();
						$('#display_form').html(_this.templates.form_fete_manif);	
						$('#search .tagcloudCarto').html(_this.templates.cloud_fete_manif);					
									
							idfr.ui.mkArea('.departement','#sector', [	
							{
								'id':'bresse',
								'action':function() {
									$('input[name=Globales/ListeIdCommune]').val(bresse);
									},
								'title':'Bresse',
								'value':1
							},{
								'id':'dombes',
								'action':function() {
									$('input[name=Globales/ListeIdCommune]').val(dombes);
								},
								'title':'Dombes',
								'value':2
							},{
								'id':'bugey',
								'action':function() {
									$('input[name=Globales/ListeIdCommune]').val(bugey);
								},
								'title':'Bugey',
								'value':3
							},{
								'id':'gex',
								'title':'Pays de Gex',
								'action':function() {
									$('input[name=Globales/ListeIdCommune]').val(gey);
								},
								'value':4
							}],
							{'type':'radio'}	
						);
						
						idfr.ui.mkDefault('input.default');	
						idfr.ui.mkButton([
							'resa'
						]);					
						
						$('#type').change(function(){	
							var val = $(this).val();
							initResa();
							disabledField(val);						
						});							
						
						$("#ville").autocomplete("/module/carto/commune.php");
						
						if(query){
							idfr.ui.initForm(query,'#search_form');
						}
						disabledField('PEA');
						
						idfr.ui.tooltips();
						
						$.datepicker.setDefaults({
		   					changeMonth: true,
		   					changeYear: true,
		   					dateFormat:'dd/mm/yy',
		   					currentText: 'Now',
		   					minDate: 0,
		   					yearRange: '-109:+109'
		   					});
		
						$("input#start").datepicker();						
						$("input#end").datepicker();				
			
					});
					
				break;
			}		
			
		},
		viewDetail: function(id_offre,type,objettour_code,id_block) {
			$('#detail').html('')
			var position = $('#workspace div.divcartocontent').position();
			var loading = $('<img src="/module/carto/img/loading.gif">').appendTo('body')
			.css({
				position:'absolute',
				top:position.top + 50,
				left:position.left + $('#workspace div.divcartocontent').width()/2
			});
			$('.idfrmap_flag').trigger('mouseleave');
			
			$('.block').unbind('mouseenter');
			$('.block').unbind('mouseleave');
			
			$('#listing,#carnet_listing,#map_workspace,#carto_footer,#heb_type_select').hide();
			$('#detail').show();
			
			$('#detail').load('/module/carto/index.php?a=ajax&objettourcode='+objettour_code+'&type='+type+'&offre='+id_offre+' .detail',function(html){
			
				var offer_name = $("#offer_name",html).html();
				$("td>h1").html('<!--TITRE-->'+offer_name+'<!--\/TITRE-->');
				
				var latitude = $("#map_latitude",html).html();
				var longitude = $("#map_longitude",html).html();
				
				if (GBrowserIsCompatible()) {
					var map = new GMap2(document.getElementById("map_canvas"));
					var marker = new GMarker(new GLatLng(latitude,longitude));
					map.setCenter(new GLatLng(latitude,longitude), 12);
					map.addControl(new GSmallMapControl());
					map.addOverlay(marker);
				}
				
				$('a.retour').bind('click',function(e){	 
 	
					idfr.main.viewMap();
					$("td>h1").html('<!--TITRE-->'+typeTextArr[type]+'<!--\/TITRE-->');
					idfr.main.initListing();
				});
				loading.remove();
			});
		},
		
		viewMap: function() {
			
			$('#listing,#map_workspace,#carto_footer,#heb_type_select').show();
			$('#search').removeClass('hide');
			$('#carnet_listing,#detail').hide();
		},
		
		viewListing: function(id) {
			
			$('#listing,#map_workspace,#carto_footer,#heb_type_select').show();
			$('#search').removeClass('hide');
			$('#carnet_listing,#detail').hide();		
		}
		
	}
}
/*#####################################################
# init all when dom is ready
#####################################################*/
$(document).ready(function() {

	var url = $(window)[0].location.pathname;
	var $_query_string = $(window)[0].location.search;
	if(url.search(/module\/carto/g)!='-1' && $_query_string.search(/offre/g)=='-1') {
		$('#search').removeClass('hide');		
	}
	if(typeof jQuery.fn.datepicker != 'function') {
		$.getScript("/admin/lib/jquery-ui-1.7.1/ui/ui.datepicker.js",function(){
	
	
		$(function($){
			$.datepicker.regional['fr'] = {
				closeText: 'Fermer',
				prevText: '&#x3c;Pr&eacute;c',
				nextText: 'Suiv&#x3e;',
				currentText: 'Courant',
				monthNames: ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin',
				'Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'],
				monthNamesShort: ['Jan','F&eacute;v','Mar','Avr','Mai','Jun',
				'Jul','Ao&ucirc;','Sep','Oct','Nov','D&eacute;c'],
				dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
				dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
				dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
				dateFormat: 'dd/mm/yy', firstDay: 1,
				isRTL: false};
			$.datepicker.setDefaults($.datepicker.regional['fr']);
		});
	
	});
	}
	
	//factory loader object
	idfr = new idfr_core({
		'map':{
			'detail':'#detail',
			'listing':'#listing',
			'carnet':'#carnet_listing',
			'tip':'#infobulle',
			'layer':{
				'legend':{'elem':'legend','classe':'level'}
			},
			'zoom' : {
				'level':{
					1: {'x':1,'y':1,'factor':1},
					2: {'x':2,'y':2,'factor':2},
					3: {'x':4,'y':4,'factor':4}
				},
				'init':1
			},
			'result_by_page':6,
			'path' : {
				'image' : '/module/carto/img/map/'				
			},
			'nav' : {
				'classPrev' : 'arriere',
				'classNext' : 'avant',
				'previous' : 'Précédente <img src="/module/carto/img/empty.gif" class="sprite" />',
				'next' : '<img src="/module/carto/img/empty.gif" class="sprite" /> Suivante',
				'first' : '',
				'last' :'',
				'type' : 'no-linear',
				'view' : 'class'
			},
			'message': {
				'listing_empty':'Il n\'y a pas de résultat pour cette recherche.<br />Veuillez modifier vos critères de recherche...',
				'carnet_empty':'Il n\'y a pas de résultat pour cette carnet de route.'
			}
		},
		'main':''
		},{
			'proxy' :'/module/carto/'
		}
	);
	
	idfr.ui = new map_ui();
	
	idfr.ui.mkButton(['category']);	
	idfr.ui.apng('.apng');
	idfr.ui.multiImg('#detail .photo');
	idfr.main.sortingInit();
	
	idfr.map.registerEvent('pictoReady',idfr.main.infobulle);
	/*	
	$('#search_form div.hebergement,#search_form div.gastronomie,#search_form div.avoir_afaire,#search_form div.fete_manif').click(function(){

		var _this = $(this);
		if(_this.hasClass('hebergement')){
			idfr.main.initForm('heb');
		}
		else if(_this.hasClass('gastronomie')){
			idfr.main.initForm('gas');
		}
		else if(_this.hasClass('avoir_afaire')){
			idfr.main.initForm('avf');
		}
		else if(_this.hasClass('fete_manif')){
			idfr.main.initForm('fma');
		}
		
	});
	
	$().ajaxStart(function(evt, request, settings){
   		$('#loader_bg,#loader').show();
 	}).ajaxComplete(function(evt, request, settings){
   		$('#loader_bg,#loader').hide();
 	}).ajaxError(function(evt, request, settings){
   		$('#loader_bg,#loader').hide();
 	});
	
 	$('a.retour').bind('click',function(e){	 
 	
		e.preventDefault(e);
		idfr.main.viewMap();
		var arian = $('.navigation').data('arian');
		$('.navigation p').html(arian+' &gt; Listing');
		//idfr.main.changeTitle('Recherche cartographique');
	});
	
	$('.showcarnetdiv').show();
	
	$('a.showcarnet').bind('click',function(e){
		idfr.map.getCarnet();
	});
	*/
	$('#listing').bind('changepage',function(){
		idfr.main.initListing();
		idfr.main.selectSortField()		
	});

	//this.initForm = (typeof initForm =='undefined' || initForm =='undefined') ? false : initForm;
	//this.cat = (typeof initForm =='undefined' || initForm == false) ? 'heb' : initForm.category;	
	
	//idfr.main.initForm(this.cat, this.initForm);
	
	//take the default arian value
	/*
	$('.navigation').data('arian',$('.navigation p').html());
	$('body').data('title',$(document)[0].title);
	
	$('#search .tagcloud_word').bind('click',function(e){
		
		if($('#listing').size()>0) {
			var carret = $(this).attr('href');	
			carret = carret.replace(new RegExp('http://'+$(window)[0].location.host,'g'),'');		
			var spl = carret.split(/\-/g);

			e.preventDefault();						
			$('#listing,#map_workspace,#carto_footer').show();
			$('#search').removeClass('hide');
			$('#detail').hide();
			
			var root = $('input:[name=category]:checked').val();
			var search = {
				'category' : root,
				'type' : spl[1]
			}
			
			idfr.map.search(search,'',function(){
				idfr.main.initListing();
				var arian = $('.navigation').data('arian');
				$('.navigation p').html(arian+' &gt; Listing');	
				idfr.main.changeTitle('Recherche cartographique');						
				//mkSortSelect();																															
			});
		}
	});
	
	idfr.main.cloudInit();
	
	if(display && display == 'carnet'){
		idfr.map.getCarnet();
	}
	*/
	/*
	idfr.map.search('','',function(){
		idfr.main.initListing();
		var arian = $('.navigation').data('arian');
		$('.navigation p').html(arian+' &gt; Listing');	
		idfr.main.changeTitle('Recherche cartographique');						
		mkSortSelect();																															
	});
	*/
	//idfr.map.helper=function(){alert('help');} // public function implementation live	
	//console.log(idfr);
});
