var dom = "http://www.centralh.fr/";
//var dom = "http://localhost/85-clients/central-hotel/www/";
var currentLocationURL;
var askedURL;
var tabId;
var swfisReady = false;

var imgInit = ['img/accueil/img-1.jpg','img/accueil/img-2.jpg']
var imgHotel = ['img/hotel/img-1.jpg','img/hotel/img-2.jpg','img/hotel/img-3.jpg','img/hotel/img-4.jpg']
var imgChambrePrivilege = ['img/chambres/privilege/img-1.jpg','img/chambres/privilege/img-2.jpg','img/chambres/privilege/img-3.jpg']
var imgChambreClassique = ['img/chambres/classique/img-1.jpg','img/chambres/classique/img-2.jpg','img/chambres/classique/img-3.jpg']
var imgChambreHorsNormes = ['img/chambres/hors-normes/img-1.jpg','img/chambres/hors-normes/img-2.jpg','img/chambres/hors-normes/img-3.jpg']
var imgBar =['img/bar/img-1.jpg','img/bar/img-2.jpg','img/bar/img-3.jpg']
var imgSpa =['img/spa/img-1.jpg','img/spa/img-2.jpg','img/spa/img-3.jpg']
var imgServices =['img/services/img-1.jpg','img/services/img-2.jpg','img/services/img-3.jpg','img/services/img-4.jpg']
var imgTourisme =['img/tourisme/img-1.jpg','img/tourisme/img-2.jpg','img/tourisme/img-3.jpg']
var imgTarif =['img/tarif/img-1.jpg','img/tarif/img-2.jpg','img/tarif/img-3.jpg']
var imgAcces =['img/acces/img-1.jpg']
var imgTab = [imgInit,imgHotel,imgChambrePrivilege,imgChambreClassique,imgChambreHorsNormes,imgBar,imgSpa,imgServices,imgTourisme,imgTarif,imgAcces]

/*/=====================================================================================================================================*/
$(function(){
	getIndexLocation()
	melto()
	});
	
function getIndexLocation(){
	currentLocationURL = String(window.location)
	if(currentLocationURL != dom){
		askedURL = currentLocationURL.substring(currentLocationURL.lastIndexOf(dom) + dom.length,currentLocationURL.length)
		if(askedURL.indexOf('#') != -1){
			askedURL = $.address.path()
			initPage()
			return;
		}
		$.cookie('askedURL', askedURL, { path: '/'});
		window.location.replace(dom);
		}
	else{
		askedURL =  $.cookie('askedURL')
		if(askedURL != null) setAddressValue(askedURL)
		initPage()
		}
	}
	
function initPage(){
	extLnk()
	setAddressNav()
	var $grid = '<div id="grid"></div>'
	$($grid).appendTo('body')
}
	
function setAddressNav(){
	$.address.init(function(event) {
			bindAjaxNav()
			}).change(function(event) {
				var _addressURL =  event.value
				if(_addressURL == '/'){
					tabId = 0
					initTab(0);
					return false;
				}
				
				var url = _addressURL.substring(1,_addressURL.length);
				var pageTitle = url.replace('-',' ');
				getPage(url);
				})
}


function bindAjaxNav(){
	var elNav = $('#nav').find('a:not(:last)')
	elNav.each(function(i){
		$(this).bind('click',function(){
			setAddressValue($(this).attr('href'))
			return false;
			});
		$(this).parent(':not(.s1)').append('<span class="liserai"></span>')
	})
	
	$('#top a').bind('click',function(){
			setAddressValue($(this).attr('href'))
			return false;
	});
		
		
	var $holder = $('#content')
	if($holder.length < 1) $('#absWrap').append('<div id="content"></div>')
}

function setAddressValue(url){
	$.address.value(url)
	$.address.tracker(url)
	}

function getPage(url){
	var $holder = $('#content')
	
	$.ajax({
	  url: url,
	  success: function(data) {
			var $data = $(data).find('#content').html()
			var $pageTitleMatch = data.match(/<title>(.*?)<\/title>/);
	       	var $pageTitle = $pageTitleMatch[1];
			$.address.title($pageTitle)
			$holder.slideUp('fast',function(){
				$holder.empty()
				$holder.append($data)
				if(url != "index.php")	setTimeout(function(){$holder.slideDown();},300)
				extLnk()
				})
			}
		});
	
	setCurrentNav(url)
}

function setCurrentNav(url){
	
	if(url == "index.php"){
		var otherLine = $('#nav').find('span')
		otherLine.stop().animate({width:"0%"},'fast')
		subShowed = false;
		$('.hasSub').children('ul').css('display','none');
		tabId=0
		initTab(0)
	}else{
	
	var elId
	var elNav = $('#nav').find('a')
	$('#nav').find('li').removeClass('current')
	$('#nav').find('li').removeClass('subcurrent')
	elNav.each(function(i){
		var elURL = $(this).attr('href')
		if(elURL == url){
			setCurrent($(this))
			elId =  $('#nav').find('a').index($(this))
		}
	})
	setImg(elId)
	}
}

function setImg(i){
	if(i == 2 )tabId = i
	else if(i >= 3 ) tabId = i 
	else tabId = i + 1
	initTab(tabId)
}

	
function initTab(id){
	if(swfisReady){
		var swf = swfobject.getObjectById("fsViewer");
		swf.setViewer(imgTab[id]);
	}else{checkifSwfReady()}
	
	if(id == 0) $('#intro').fadeIn('slow')
	else $('#intro').fadeOut('slow')

}

function swfReady(){swfisReady = true}
function checkifSwfReady(){
	setTimeout(function(){initTab(tabId);},200)
	}

function setCurrent(el){
	
	var subShowed;
	
	var elId =  $('#nav').find('a').index(el)
	line = el.next('span')
	
	if(elId == 1 || elId == 2 || elId==3 || elId == 4){
		 $('.hasSub').children('ul').css('display','block');
		 line = $('.hasSub').children('span');
		 subShowed = true
	}else{
		$('.hasSub').children('ul').css('display','none');
		subShowed = false
		
	}
	
	var otherLine = $('#nav').find('span')
	otherLine.each(function(i){
		var parentLine = $('.hasSub').children('span');
		if($(this).prev().is('ul')){
			if(subShowed == false)  $(this).stop().animate({width:"0px"},"fast")
		}else{
			 $(this).stop().animate({width:"0px"},"fast")
		}
	})
		
	if(subShowed){
		$('.hasSub li').css('border-left','none')
		var parentLine = $('.hasSub').children('span');
		if(elId == 1 || elId == 2){
			parentLine.stop().animate({width:"100%"},'slow')
		}else if (elId == 3 || elId == 4){
			if(parentLine.width()!= 185) parentLine.stop().animate({width:185},'slow')
			
			el.parent('li').css('border-left','1px solid #fff')//borderLEft
			var subLine = el.next('span')
		 	subLine.stop().animate({width:"100%"},'slow')
		}
	}else{
		line.stop().animate({width:"100%"},'slow')
	}
}


function extLnk(){
	$('a[href^="http://"]').addClass('external').attr('target', '_blank');
	$('a[href^="'+dom+'"]').removeClass('external').removeAttr('target', '_blank');
	$('a[href$=".pdf"]').addClass('external').attr('target', '_blank');
}

function melto(){
	var lnkMail = $('a.melto')
	$(lnkMail).each(function (i){
			dom = $(this).attr("href")
			domUrl = dom.substring(11,dom.length);
			lnkMelto = "mailto:" + $(this).text().toLowerCase() +'@'+ domUrl;				  
			$(this).attr('href',lnkMelto);
			
							  })
}
