ACC.Destination = function() {
	if(!(this instanceof ACC.Destination)) {
		return new ACC.Destination();
	}
	
	ACC.Destination.superclass.constructor.call(this);
	
	this.addEvents(['change']);
	//test
	this.addEvents(['change-second']);
	this.initialize();
};
ACC.extend(ACC.Destination, ACC.Observable, {
	initialize: function() {
		var destSideMenu = document.getElementById('destinations-rappel'),			
			destMain = document.getElementById('destinations'),
			iframe = document.getElementById('iframeHotdeals'),
			that = this;	

	// debut test du second destination 
		var destSideMenu_second = document.getElementById('destinations-rappel-second'),				
			destMain_second = document.getElementById('destinations-second'),
			iframe_second = document.getElementById('iframeHotdeals'),
			that = this;
		// fin test du second destination 

			
		
		if(destSideMenu  &&  destMain && iframe) {
			this.iframe = iframe;
			this.mapDest = ['pays', 'europe', 'monde'];
			this.lkSideMenu = destSideMenu.getElementsByTagName('a');
			this.lkMain = destMain.getElementsByTagName('a');
			
			var url = iframe.src;
			if(url.indexOf('-pays-') > -1) {
				this.active = 'pays';
				this.activeIndex = 0;
			} else if(url.indexOf('-europe-') > -1) {
				this.active = 'europe';
				this.activeIndex = 1;
			} else if(url.indexOf('-monde-') > -1) {
				this.active = 'monde';
				this.activeIndex = 2;
			}
			
			if(this.lkSideMenu.length != this.lkMain.length) {return;}
			
			this.lastLink = this.lkSideMenu[this.activeIndex].parentNode;
			this.lastImage = this.lkMain[this.activeIndex].getElementsByTagName('img')[0];
			
			for(var i = 0, len = this.lkMain.length; i < len; i++) {
				(function(c) {
					that.lkMain[c].onclick = function() {return that.change(this, c);};
					that.lkSideMenu[c].onclick = function() {return that.change(this, c);};
				})(i);
			}
		}	
		
		
	

// debut test du second destination
if(destSideMenu_second  &&  destMain_second && iframe_second) {

			this.iframe_second = iframe_second;
			this.mapDest_second = ['pays', 'europe', 'monde'];
			this.lkSideMenu_second = destSideMenu_second.getElementsByTagName('a');
			this.lkMain_second = destMain_second.getElementsByTagName('a');
			
			var url = iframe_second.src;
			if(url.indexOf('-pays-') > -1) {
				this.active = 'pays';
				this.activeIndex = 0;
			} else if(url.indexOf('-europe-') > -1) {
				this.active = 'europe';
				this.activeIndex = 1;
			} else if(url.indexOf('-monde-') > -1) {
				this.active = 'monde';
				this.activeIndex = 2;
			}
			
			if(this.lkSideMenu_second.length != this.lkMain_second.length) {return;}
			
			this.lastLink_second = this.lkSideMenu_second[this.activeIndex].parentNode;
			this.lastImage_second = this.lkMain_second[this.activeIndex].getElementsByTagName('img')[0];
			
			for(var i = 0, len = this.lkMain_second.length; i < len; i++) {
				(function(c) {
					that.lkMain_second[c].onclick = function() {return that.change_second(this, c);};
					that.lkSideMenu_second[c].onclick = function() {return that.change_second(this, c);};
				})(i);
			}
		}		

	// fin test du second destination	
		
	},
	
	change: function(el, i) {	
		if(this.lastLink && this.lastImage) {
			$(this.lastLink).removeClassName('on');
			$(this.lastImage.parentNode.parentNode).removeClassName('on');
			this.lastImage.src = this.lastImage.src.replace('_on.', '_off.');
			this.lastLink = this.lastImage = null;
		}
		var img = el.getElementsByTagName('img')[0];
		if(img) {
			$(el.parentNode).addClassName('on');
			img.src = img.src.replace('_off.', '_on.');
			this.lastImage = img;
			this.lastLink = $(this.lkSideMenu[i].parentNode).addClassName('on');
			
			
		} else {
			this.lastLink = $(el.parentNode).addClassName('on');
			$(this.lkMain[i].parentNode).addClassName('on');
			img = this.lkMain[i].getElementsByTagName('img')[0];		
			
			img.src = img.src.replace('_off.', '_on.');
			this.lastImage = img;
		}
		this.active = this.mapDest[i];		
		this.fireEvent('change');
	},
	
	//fonction test le second
	change_second: function(el, i) {	
		if(this.lastLink_second && this.lastImage_second) {
			$(this.lastLink_second).removeClassName('on');
			$(this.lastImage_second.parentNode.parentNode).removeClassName('on');
			this.lastImage_second.src = this.lastImage_second.src.replace('_on.', '_off.');
			this.lastLink_second = this.lastImage_second = null;
		}
		var img = el.getElementsByTagName('img')[0];
		if(img) {
			$(el.parentNode).addClassName('on');
			img.src = img.src.replace('_off.', '_on.');
			this.lastImage_second = img;
			this.lastLink_second = $(this.lkSideMenu_second[i].parentNode).addClassName('on');
			
			
		} else {
			this.lastLink_second = $(el.parentNode).addClassName('on');
			$(this.lkMain_second[i].parentNode).addClassName('on');
			img = this.lkMain_second[i].getElementsByTagName('img')[0];		
			
			img.src = img.src.replace('_off.', '_on.');
			this.lastImage_second = img;
		}
		this.active = this.mapDest_second[i];		
		this.fireEvent('change_second');
	}
	
	
	
});

ACC.tools.addDomReadyListener(function() {
	$(document.body).addClassName('js');
	
	ACC.promosDest = ACC.Destination();
	if(!ACC.promosDest.active) {return;}
	
	
	var filters = document.getElementById('filters'),
		iframe = document.getElementById('iframeHotdeals');
	if(!iframe) {return;}
	
	function lg() {
		var l = document.getElementsByTagName('html')[0].lang;
		if(!l || l == 'en') {l = 'gb';}
		return l;
	}
	
	
	if(initPage == "earlybooking") var iframeURL = 'http://www.accorhotels.com/hotel-cms/' + lg() + '/promoEB/';
	else if (initPage == "hotdeals") var iframeURL = 'http://www.accorhotels.com/hotel-cms/' + lg() + '/promo/';
	
	
	
	// detect url param to check if a specific tab should be highlighted
	var url = ''+document.location;
	var selectedZone = url.toQueryParams().zone;
	if (selectedZone == 'pays' || selectedZone == 'europe' || selectedZone == 'monde')
	{
		var selectedZoneIndex = (selectedZone == 'pays')?0:((selectedZone == 'europe')?1:2);
		ACC.promosDest.change(document.getElementById('vers-' + selectedZone).getElementsByTagName('a')[0], selectedZoneIndex);
		iframe.src = iframeURL + 'hotel-' + ACC.promosDest.active + '-dest.html';
	}
	
	if(filters) {
		var initFirstRadio = function() {
			if(lastRd) {
				$(lastRd.parentNode).removeClassName('on');
			}
			rd[0].checked = true;
			$(rd[0].parentNode).addClassName('on');
			lastRd = rd[0];
		};
		
		var changeTri = function() {
			var v = this.value;
			if(lastRd) {
				$(lastRd.parentNode).removeClassName('on');
			}
			$(this.parentNode).addClassName('on');
			lastRd = this;
			iframe.src = iframeURL + 'hotel-' + ACC.promosDest.active + '-' + v + '.html';
		};
		
		var inp = filters.getElementsByTagName('input'),
			rd = [],
			lastRd;
		
		for(var i = 0, c = 0, len = inp.length; i < len; i++) {
			if(inp[i].type && inp[i].type == 'radio') {
				rd[rd.length] = inp[i];
				if(c === 0) {
					inp[i].checked = true;
					lastRd = inp[i];
					$(lastRd.parentNode).addClassName('on');
					++c;
				}
				inp[i].onclick = changeTri;
			}
		}
	}
	
	var triBase = document.getElementById('tri-base'),
		triMore = document.getElementById('tri-show-more'),
		triEurope = document.getElementById('tri-more-europe'),
		triEuropeHide = document.getElementById('tri-hide-europe'),
		triMonde = document.getElementById('tri-more-monde'),
		triMondeHide = document.getElementById('tri-hide-monde'),
		triEuropeSelect = document.forms['filter-destinations'].elements.destination[0],
		triMondeSelect = document.forms['filter-destinations'].elements.destination[1];
	
	if(triBase && triMore && triEurope && triEuropeHide && triMonde && triMondeHide && triEuropeSelect && triMondeSelect) {
		var showMore = function(e) {
			if(ACC.promosDest.active == 'europe') {
				$(triEurope).removeClassName('off');
				$(triBase).addClassName('off');
			} else if(ACC.promosDest.active == 'monde') {
				$(triMonde).removeClassName('off');
				$(triBase).addClassName('off');
			}
			$(triMore).addClassName('off');
			return false;
		};
		
		var hideMore = function(e) {
			$(this.parentNode).addClassName('off');
			$(triBase).removeClassName('off');
			$(triMore).removeClassName('off');
			return false;
		};
		
		// var filterByCountry = function() {
		// 	iframe.src = iframeURL + this.value + '.html';
		// };
		
		if(ACC.promosDest.active == 'pays') {
			$(triMore).addClassName('off');
		}
		
		ACC.promosDest.addListener('change', function() {
			$(triBase).removeClassName('off');
			$(triMonde).addClassName('off');
			$(triEurope).addClassName('off');
			$(triMore)[(this.active != 'pays') ? 'removeClassName' : 'addClassName']('off');
			initFirstRadio();
			triEuropeSelect.selectedIndex = triMondeSelect.selectedIndex = 0;
		});
		
		triMore.onclick = showMore;
		triEuropeHide.onclick = hideMore;
		triMondeHide.onclick = hideMore;
		// triEuropeSelect.onchange = filterByCountry;
		// triMondeSelect.onchange = filterByCountry;
	}
});

function changeFrameFiltre(arg) {
	if(arg != 0) {	
		var expression = /\/([a-z]{2}(?:-[a-z]{2})?)\//; // on cherche le code langue
		expression.exec(document.location);
		var lang = RegExp.$1;
		if(initPage == "earlybooking") arg = "http://www.accorhotels.com/hotel-cms/"+lang+"/promoEB/"+arg+".html";
		else if (initPage == "hotdeals") arg = "http://www.accorhotels.com/hotel-cms/"+lang+"/promo/"+arg+".html";
		
		document.getElementById("iframeHotdeals").setAttribute("src", arg);
	}
}
