/*-----------------------------------------------------------
	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 , #nav > li 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($('#diaporama').length) {
		initDiapo();
	}

	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();*/
		
		$('#sliderHome').jcarousel({
			auto: 5,   
			vertical: true,
			scroll : 1,	
			visible: 1,
			animation: 450,		
			wrap : "last" ,
			initCallback: sliderHome_initCallback ,
			itemFirstOutCallback: {onBeforeAnimation: autoHighlight } ,
			 // This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
		});		
		
		$cufonedTitlesHome = $('#homePage h2 , #sliderHome_controls li h3');
		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 , #btn_specialOffers');
		Cufon.replace($cunfonedFicheCamping);			
		crossSelling();	
	}
	
	if($("#offers .reco_accomodation").length) {
		showAllHeb();
	}
	
	if($("#offers table.espaceReception").length) {
		oddTr();
		initTooltipBourse();	
	}
	
	/* ----------- CUFON FONT REPLACEMENT -----------*/
	$cufonedTitles = $('#aside h2 , #accessLanguage p a , #numContact span');
	$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();
		}
		
		simuSubMenuIE();	
	}	
	/*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;
	});	
}

/*Slider Home*/
function autoHighlight(index) {	
	$currentIndex = $('#sliderHome_controls li.active');
		
	if($currentIndex.next('li').length) {
		$currentIndex.removeClass('active').next('li').addClass('active');
	} else {
		$currentIndex.removeClass('active');
		$('#sliderHome_controls li').eq(0).addClass('active');
	}		
}

function sliderHome_initCallback(carousel) {
	$('#sliderHome_controls li').eq(0).addClass('active');
	
	$('#sliderHome_controls li').bind('click', function() {
		var sliderNum = $(this).attr('id').split('goTo_');
		carousel.scroll(jQuery.jcarousel.intval(sliderNum[1]));
		
		$('#sliderHome_controls li').removeClass('active');
		$(this).addClass('active');		
    });	
	
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
	$('#sliderHome_controls').hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });	
} 
/*fin Slider Home*/


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();
			}
			
			if(testPlanBloc == 'crossSelling') {				
				$('#mainTabs').css('border-color','#FC7D00');		
			} else {
				$('#mainTabs').css('border-color','#45BFD9');
			}
			
			return false;
		//}
	});
	
	$('#btn_specialOffers').click(function() {	
		$tabBlocs.hide();
		$('#offres').show();
		$('#mainTabs').css('border-color','#45BFD9');
		$('#mainTabs > li').removeClass('active');
	});
	
}

function crossSelling() {
	$('#crossSelling .crossItems > li').hover(function() {
		$(this).children('div').stop().animate({top:"-50px"},600);
	}, function() {
		$(this).children('div').stop().animate({top:"0"},600);
	});
	
	$('#crossSelling .crossItems > li').click(function() {
		window.location= $(this).find("a").attr("href");
	});
	
}

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 + 2, 
			width: wImage + 2, 
			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 simuSubMenuIE() {
	$('#nav > li li.hasMenu').hover(function() {
		$(this).addClass('simuSubMenuIE');
	}, function() {
		$(this).removeClass('simuSubMenuIE');
	});
}

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'}); 
}
		
function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
}			

function initDiapo() {
	$diapo = $('#diaporama')
	$diapoItem = $('#diaporama').find('li');				
	
	$diapo.find('ul').jcarousel({
		//auto: 4,
		scroll:1,
		visible:4,
		wrap: 'circular',
		initCallback: mycarousel_initCallback
	});
		
	if($diapoItem.length <= 4) {
		$diapo.find('.jcarousel-prev').css('display','none').end().find('.jcarousel-next').css('display','none');
}
	var firstItemSrc = $diapoItem.eq(0).children('img').attr('src');	
	$('#placeholder').attr('src', firstItemSrc);	
	$diapoItem.eq(0).addClass('active')
	
	$diapoItem.live('click', function() {
		$diapo.find('li').removeClass('active');
		$(this).addClass('active');
		var src = $(this).children('img').attr('src');				
		$('#placeholder').attr('src', src);				
	});	
}
			
			
	
