/**
 * @version $Id: functions.js 25220 2012-01-20 10:08:58Z rudi $
 */

/** Google Map objects */
var map = false, marker = false;

function formatAsMoney(mnt) {
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
}

window.___gcfg = {lang: 'en-GB'};
(function() {
  var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  po.src = 'https://apis.google.com/js/plusone.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

function getId(id, pos)
{		
	var id_array = id.split('_');

	return id_array[id_array.length - pos];
}

$(document).ready(function() {
	
	$('.datepicker').datepicker({
		dateFormat: 'dd/mm/yy',
		showWeek: true,
		firstDay: 1,
		minDate: '+1d',
		changeMonth: true,
		changeYear: true,
		navigationAsDateFormat: true,
		gotoCurrent: true,
		showOtherMonths: true,
		selectOtherMonths: true
	});
	
	$('.datepickerDOB').datepicker({
		dateFormat: 'dd/mm/yy',
		showWeek: true,
		firstDay: 1,
		defaultDate: '-20y',
		maxDate: '-18y',
		changeMonth: true,
		changeYear: true,
		navigationAsDateFormat: true,
		gotoCurrent: true,
		showOtherMonths: true,
		selectOtherMonths: true,
		yearRange: '-100:-18'
	});
	
	$('.lightbox').lightBox();
	
	$(".tweet").tweet({
		username : "ski_world",
		join_text : "auto",
		count : 1,
		auto_join_text_default : "",
		auto_join_text_ed : "",
		auto_join_text_ing : "",
		auto_join_text_reply : "",
		auto_join_text_url : "",
		loading_text : "loading tweets..."
	});
	
	$('.toolTip[title]').qtip({style:{name:'dark',tip: 'topLeft',border:{radius:4}}});

	/*
	 * Gallery / Thumb carousel
	 */
	$(".carousel").jCarouselLite({
		btnNext : ".next",
		btnPrev : ".prev",
		vertical : true,
		visible : 5,
		circular : false
	});
	
	$('#dealSlider').nivoSlider({
		effect: 'fade',
		pauseTime: 5000,
		slices: 1
	});

	// what happens when we click on a thumbnail
	$('.pb-gallery .thumb-list ul li.active a').live('click', function(e) {
		return false;
	});

	$('.pb-gallery .thumb-list ul li:not(.active) a').live('click', function(e) {
		e.preventDefault();
		// highlight only the clicked thumb
		$(this).parents('.pb-gallery').find('.thumb-list ul li').removeClass('active');
		$(this).parent().addClass('active');
		// find our target and show it
		var target = this.hash.slice(1);
		$(this).parents('.pb-gallery').find('.photos img').fadeOut(1000);
		$(this).parents('.pb-gallery').find('.photos img[id="' + target + '"]').fadeIn(550);
	});

	/*
	 * Automatic Tabs
	 */
	$('.tabs.auto > div').each(function() {
		var tabTitle = $(this).attr('title');
		var tabID = $(this).attr('id');
		$(this).parent().find('ul.tabNavigation').append('<li><h3><a href="#' + tabID + '">' + tabTitle + '</a></h3></li>');
		$(this).attr('title', '');
	});

	/*
	 * Tabs
	 */
	$('.tabs').each(function() {
		var tabContainers = $(this).find('div.tabCont');

		$(this).find('ul.tabNavigation a').click(function() {
			tabContainers.hide().filter(this.hash).show();

			$(this).parents('ul').find('li').removeClass('active');
			$(this).parents('li').addClass('active');

			if (map) {
				google.maps.event.trigger(map, 'resize');
				map.setCenter(marker.getPosition());
			}

			return false;
		});

		var to_match = this.hash;

		var found = false;
		tabContainers.each(function() {
			if ($(this).hasClass('active')) {
				$('.tabs ul.tabNavigation a[href$=#' + $(this).attr('id') + ']').click();
				found = true;
			}
		});

		if (!found)
			$(this).find('ul.tabNavigation a').filter(':first').click();
	});

	/*
	 * Breadcrumbs 
	 */
	$("#breadcrumbs ul.top-lvl li").mouseover(function() {
		var sub = $(this).find('ul');
		sub.show();
	}).mouseout(function() {
		var sub = $(this).find('ul');
		sub.hide();
	});

	/*
	 * Form Validation
	 */
	$('form').submit(function() {
		var valid = true;
		
		$(this).find('.invalid').removeClass('invalid');
		
		$(this).find('.required').each(function() {
			if ($(this).is(':checkbox')) {
				if (!$(this).is(':checked')) {
					valid = false;
					$(this).parent('label').addClass('invalid');
				}
			} else if ($(this).val() == '') {
				valid = false;
				$(this).addClass('invalid');
			}
		});
		
		if (!valid) {
			$('.invalid:first').focus();
			alert('Please complete all required fields.');
		}
		
		return valid;
	});
	
	$('.side-content').has('.ccheck').each(function() {
		var sc = $(this);
		if ($(this).find('.ccheck').empty()){
			sc.hide();
		}
	});
	
	$('.pe-content img[style*=float\\:\\ left]').css({marginRight: '10px'});
	$('.pe-content img[style*=float\\:\\ right]').css({marginLeft: '10px'});
	
	$('#skiSearchLink').click(function() { $('#skiSearch').submit() } );
	
	$('#brochureRequestBtn').click(function(event){
		event.preventDefault();
		$('#offersAlert').get(0).setAttribute('action', '/brochure-request');
		$('#offersAlert').submit();
	});
	
	$('.offer-type-tab').click(function(){
		var id_offer_type = getId($(this).attr('id'), 1);
		if(!$(this).hasClass('active'))
		{
			var last_tab_id = $('.offer-type-tab-wrap').find('.active').attr('id');
			var last_container_id = getId(last_tab_id, 1);			
			$('#' + last_tab_id).removeClass('active');
			$('#' + last_tab_id).find('#active').remove();
			$('#offer_type_' + last_container_id).hide();
						
			$(this).addClass('active');
			$(this).append('<b id="active"></b>');
			$('#offer_type_' + id_offer_type).fadeIn(800);
		}
	});
	
});
