/*-----------------------------------------------------------
	Project: Vagues Oceanes
	Date : 	October 2009
	Last Update : 14/10/09
	Author:	C2iS - AFO
	Summary : JS for all pages	
-----------------------------------------------------------*/

$(document).ready(function() {	
	$('#nav > li:last-child').addClass('lastOfMenu');
	$('#nav > li li:last-child ').addClass('lastOfSubMenu');
	
		
	$('#searchBox .datePick').datepicker({
		changeMonth: true,
		changeYear: true,
		showOn: 'both',
		showAnim: 'slideDown',
		dateFormat: 'dd/mm/yy',
		minDate: 0,
		hideIfNoPrevNext: true,
		buttonImage: 'images/aside/picto_calendar.gif',
		buttonImageOnly: true 
	}); 
	
	$('#ui-datepicker-div').position({
			of: $('#date_arrive'),			
			offset: "50"
	});

		
	skinnedSelect();	
	
	if($('.littlePopinLink').length) {
		initLittlePopin();
	}
			
	if($('.popinLink').length) {
		initPopin();
	}
	
	if($('.bigPopinLink').length) {
		initBigPopin();
	}
	
				
	/*if(!($('#homePage').length || $('#ficheCamping').length )) {
		autoHeightContent();
	}*/
		
	if($('#offers .result_item').length) {
		oddBgSearchResult();
	}
	
	if($('#offers #hebergement').length) {
		$cunfonedHeb = $('#hebergement .leftSide_tabBloc h6');
		Cufon.replace($cunfonedHeb);			
	}
	
	if($('#homePage').length) {
		homeCarousel();		
		topDestBloc();
		lastMinBloc();
		$cufonedTitlesHome = $('#homePage h2');
		Cufon.replace($cufonedTitlesHome);
		initHerbegementPopin();	
	}
	
	if($('#ficheCamping').length) {
		mainTabs();
		initTooltip();	
		$cunfonedFicheCamping = $('#hebergement .leftSide_tabBloc h6 , #hebergement #accomodationType dt em , #region .leftSide_tabBloc dt , #surPlace .leftSide_tabBloc dt , #region .relatedActivities dt em , #region .leftSide_tabBloc .suggest a');
		Cufon.replace($cunfonedFicheCamping);			
	}
	
	if($("#offers .reco_accomodation").length) {
		showAllHeb();
	}
	
	if($("#offers table.espaceReception").length) {
		oddTr();
		initTooltipBourse();	
	}
	
	/* ----------- CUFON FONT REPLACEMENT -----------*/
	$cufonedTitles = $('#aside h2 , #accessLanguage p a');
	$cunfonedMenu = $('#nav > li > a');						
	Cufon.replace($cufonedTitles);	
	Cufon.replace($cunfonedMenu);		  
	
	/*functions for IE 6*/
	if ($.browser.msie && $.browser.version <= 6 ) {
		try {
		document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
		
		if(!($('#homePage').length || $('#ficheCamping').length )) {
			autoHeightContent();
		}
	}	
	/*end functions for IE 6*/
	
	/*functions for IE 6 & 7*/
	if ($.browser.msie && $.browser.version <= 7 ) {		
		simuHoverIE();	
	}	
	/*end functions for IE 6 & 7*/
	
});	

function lastMinBloc() {
	$lastMinBloc = $('#homeBloc_lastMinute .lastMinGroup');
	$lastMinBloc.eq(0).show().addClass('showed');
	var total_lastMinBloc = $lastMinBloc.length;
	$('#total_lastMinBloc').replaceWith(total_lastMinBloc);
	
	$('#next_lastMinBloc').click(function() {
		if($('#homeBloc_lastMinute .showed').next('.lastMinGroup').length) {
			$('#homeBloc_lastMinute .showed').removeClass('showed').hide().next('.lastMinGroup').addClass('showed').customFadeIn();	
			var current_lastMinBloc = $('#current_lastMinBloc').text();
			$('#current_lastMinBloc').text(parseInt(current_lastMinBloc) + 1);
		}
		return false;
	});
	
	$('#prev_lastMinBloc').click(function() {
		if($('#homeBloc_lastMinute .showed').prev('.lastMinGroup').length) {
			$('#homeBloc_lastMinute .showed').removeClass('showed').hide().prev('.lastMinGroup').addClass('showed').customFadeIn();
			var current_lastMinBloc = $('#current_lastMinBloc').text();
			$('#current_lastMinBloc').text(parseInt(current_lastMinBloc) - 1);
		}
		return false;
	});	
}

function topDestBloc() {
	$topDestBloc = $('#homeBloc_topDesti .topDestGroup');
	$topDestBloc.eq(0).show().addClass('showed');
	var total_topDestBloc = $topDestBloc.length;
	$('#total_topDestiBloc').replaceWith(total_topDestBloc);
	
	$('#next_topDestiBloc').click(function() {
		if($('#homeBloc_topDesti .showed').next('.topDestGroup').length) {
			$('#homeBloc_topDesti .showed').removeClass('showed').hide().next('.topDestGroup').addClass('showed').customFadeIn();	
			var current_topDestBloc = $('#current_topDestiBloc').text();
			$('#current_topDestiBloc').text(parseInt(current_topDestBloc) + 1);
		}
		return false;
	});
	
	$('#prev_topDestiBloc').click(function() {
		if($('#homeBloc_topDesti .showed').prev('.topDestGroup').length) {
			$('#homeBloc_topDesti .showed').removeClass('showed').hide().prev('.topDestGroup').addClass('showed').customFadeIn();
			var current_topDestBloc = $('#current_topDestiBloc').text();
			$('#current_topDestiBloc').text(parseInt(current_topDestBloc) - 1);
		}
		return false;
	});	
}

function homeCarousel() {
	 $("#carousel_home .carousel_outer").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		visible: 3 ,
		circular: false 
    });	
	
	$('#carousel_home .prev').addClass('disabled');
} 

function mainTabs() {
	$tabBlocs = $('#ficheCamping .tabBloc');
	if($('#mainTabs > li').hasClass('openDefault')) {
		$openedTab = $('#mainTabs > li.openDefault');
		$openedTab.addClass('active');
		var defaultTabBloc = $openedTab.children('a').attr('href').split('#');
		$("#"+defaultTabBloc[1]).show();		
		var testDefaultPlan = defaultTabBloc[1];
		if(testDefaultPlan == 'plan') {				
			initMap();
		}		
	} else {
		$('#mainTabs > li:first-child').addClass('active');
		$tabBlocs.eq(0).show();
	}
	
	$('#mainTabs > li > a').click(function() {
		//if($(this).parent('li').hasClass('specialTab')) {
			//return true;
		//} else {
			
			$tabBlocs.hide();
			var linkTab = $(this).attr('href').split('#');
			$("#"+linkTab[1]).show();
			
			$('#mainTabs > li').removeClass('active');
			$(this).parent('li').addClass('active');
			
			var testPlanBloc = linkTab[1];	
			
			if(testPlanBloc == 'plan') {				
				initMap();
			}
			
			return false;
		//}
	});
}

function initTooltip() {
	$('img.toolTipped').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "toolTip_services",
		left: -59,
		top: -57
	});	
}

function initTooltipBourse() {
	$('img.toolTipped').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "toolTip_bourse",
		left: -65,
		top: -40
	});	
}

function initLittlePopin() {	
	$('.littlePopinLink').click(function() {
		var poppedBloc = $(this).attr('href').split('#');	
		var titlePopin = $("#"+poppedBloc[1]).children('span.titlePopin').text();	
		$("#"+poppedBloc[1]).dialog({
			bgiframe: true,
			height: 200,
			width:300,
			title: titlePopin,
			modal: true
		});	
		$("#"+poppedBloc[1]).dialog('open');
		return false;
	});
}

function initPopin() {	
	$('.popinLink').click(function() {
		var poppedBloc = $(this).attr('href').split('#');	
		var titlePopin = $("#"+poppedBloc[1]).children('span.titlePopin').text();	
		$("#"+poppedBloc[1]).dialog({
			bgiframe: true,
			height: 350,
			width:550,
			title: titlePopin,
			modal: true
		});	
		$("#"+poppedBloc[1]).dialog('open');
		return false;
	});
}

function initBigPopin() {	
	$('.bigPopinLink').click(function() {
		var poppedBloc = $(this).attr('href').split('#');	
		var titlePopin = $("#"+poppedBloc[1]).children('span.titlePopin').text();	
		$("#"+poppedBloc[1]).dialog({
			bgiframe: true,
			height: 710,
			width:810,
			title: titlePopin,
			modal: true
		});	
		$("#"+poppedBloc[1]).dialog('open');
		return false;
	});
}

function initHerbegementPopin(namePopin) {	
		
	var titlePopin = $("#"+namePopin).children('span.titlePopin').text();	
	$("#"+namePopin).dialog({
		bgiframe: true,
		height: 710,
		width:810,
		title: titlePopin,
		modal: true
	});	
	$("#"+namePopin).dialog('open');
	return false;

}

/*ouverture popin Flash*/
function openPopinFlash(idFlash) {
	
	var titlePopin = $("#"+idFlash).children('span.titlePopin').text();
	$("#"+idFlash).dialog({
			bgiframe: true,
			height: 550 , /*dynamique*/
			width:700, /*dynamique*/
			title: titlePopin,
			modal: true
		});	
		$("#"+idFlash).dialog('open');
		return false;
}

function openIt(idFlash, wImage, hImage) {
	
	var titlePopin = $("#getVisu"+idFlash).children('span.titlePopin').text();
	$("#getVisu"+idFlash).dialog({
			bgiframe: true,
			height: hImage , 
			width: wImage, 
			title: titlePopin,
			modal: true
		});	
		$("#getVisu"+idFlash).dialog('open');
		return false;
}

function oddBgSearchResult() {
	$('#offers .result_item:odd').addClass('oddItem');
}

function oddTr() {
	$('#offers .espaceReception tr:odd').addClass('odd');
}

function showAllHeb() {
	$('table.reco_accomodation').each(function() {
		$(this).find('tr:gt(2)').hide();
	});
	
	$('dl.result_item dd a.showAllHeb').toggle(function() {
		$myRows = $(this).parent('p').parent('dd').next('dd');
		$myRows.find('table.reco_accomodation tr').show();		
		},function() {
		$myRows.find('table.reco_accomodation tr:gt(2)').hide();
	});
}

function autoHeightContent () {
	var asideHeight = $('#aside').height();
	var contentHeight = $('#mainContent').height();
	if (asideHeight > contentHeight) {
		$('#mainContent').height(asideHeight + 30);
	}
} 

function simuHoverIE() {
	$('#nav li').hover(function() {
		$(this).addClass('simuHover');
	}, function() {
		$(this).removeClass('simuHover');
	});
}

function skinnedSelect() {
	$('#search_form_nb_days').selectbox({ containerClass: 'selectbox-wrapper search_form_nb_days'}); 
	$('#nb_adults').selectbox({ containerClass: 'selectbox-wrapper nb_adults'}); 
	$('#destination_pays').selectbox({ containerClass: 'selectbox-wrapper destination_pays'}); 
	$('#destination_region').selectbox({ containerClass: 'selectbox-wrapper destination_region'}); 
	$('#destination_camping').selectbox({ containerClass: 'selectbox-wrapper destination_camping'}); 
	$('#type_hebergement').selectbox({ containerClass: 'selectbox-wrapper type_hebergement'}); 
	$('#campingEnImages').selectbox({ containerClass: 'selectbox-wrapper campingEnImages'}); 
		
}
