$(document).ready(function(){
		
	// For collapsing menus
	$(".slider").hide();
	$(".collapse span").click(function() {
		$(this).toggleClass('active');
		$(this).next().slideToggle(300);
	});
	
	$("div.link-it").click(function(){
		var id = $(this).attr('id');
		if ($('#'+id+'-url').val()) {
			document.location = $('#'+id+'-url').val();
		}
		else {
			showBookingForm($('#'+id+'-country').val(), $('#'+id+'-station').val(), $('#'+id+'-promotion_code').val(), $('#'+id+'-custom_query').val());
		}
	});
	
	// 
	// For tab navigation > tabs2.js
	$(".idTabs").parents(".shadowbox-wrapper").addClass("marginfortabs");
	
	// For "lightbox" flash
	$("#launch-flash").click(function(){
		showMap();
		
		$('#lightbox').click(function(){
			closeMap();
		});
		
	});
	
	/* check submit after each change of country station */
	$('#country > select').change(function () {		
		changeCountry(this);				
		
		var a = new Date();
		a = new Date(a.getTime() +1000*180);
		document.cookie = 'sixtHCCountry='+this.value+'; expires='+a.toGMTString()+';'; 
		
		
//		checkSubmit();
	});
	
	/* check submit after each change of pickup station */
	$('#pickup > select').change(function () {
		changePickupStation(this);
//		checkSubmit();
	});
	
	/* check submit after each change of drop station */
	$('#drop > select').change(function () {
		changeDropStation(this);
//		checkSubmit();
	});
	
	/* hides the last ruler of a tab element */
	$('div.tabcontainers > div > div.hr:last-child').hide();
	
	//
	// check all teaser output whether it might be too large
	$('span.teaser-title').each(function () {
		if ($(this).width() > 140) {
//			$.consoleInfo($(this).css("font-size"));
			$(this).addClass('teaser-title-small');
		}
	});
	$('span.teaser-subtitle').each(function () {
		if ($(this).width() > 128) {
//			$.consoleInfo($(this).css("font-size"));
			$(this).addClass('teaser-subtitle-small');
		}
	});
	$('span.teaser-price').each(function () {
		if ($(this).width() > 128) {
//			$.consoleInfo($(this).css("font-size"));
			$(this).addClass('teaser-price-small');
		}
	});
	
	
   $('.long_wrapper').each(function () {
		var previousHeight = 0;
		$(this).prevAll().each(function () {
			previousHeight += $(this).height();			
		});

		var rightHeight = 0;
		var rightHeight2 = 0;
		var count = 0;

                var imgHeight = 0;
		$('div.pageheader-transparent > img').each(function () {
			imgHeight += $(this).height();     
		});

		$('div.right > div').each(function () {
                        rightHeight2 = rightHeight;
			rightHeight += $(this).height();
			count++;					
			if (count == 2) {	
				var margin = previousHeight - rightHeight2 + 150;								
				if (rightHeight2 != imgHeight) {
                                    margin = previousHeight - rightHeight2 + 143;	
                                };
							
				$(this).css('margin-top', margin);
			};
		});
	});
		
	$("#content .form-body").show();		
	
	if (($("#country > select").val() > 0) && ($("#pickup > select").val() == -1)) {
		getStations($("#country > select").val());		
	}		

	if (($("#country > select").val() == -1) && (getCookie('sixtHCCountry') > 0)) {	
		$("#country > select").val(getCookie('sixtHCCountry'));
		getStations($("#country > select").val());		
	}	
	
});

function getCookieVal (offset) { 
	var endstr = document.cookie.indexOf (";", offset); 
	if (endstr == -1) 
	endstr = document.cookie.length; 
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) { 
	var arg = name + "="; 
	var alen = arg.length; 
	var clen = document.cookie.length; 
	var i = 0; 
	while (i < clen) { 
		var j = i + alen; 
		if (document.cookie.substring(i, j) == arg) 
			return getCookieVal (j); 
		i = document.cookie.indexOf(" ", i) + 1; 
		if (i == 0) break; 
	} 
	return null;
}
 
function showMap () {
 	$("#lightbox").css({height: $('#wrapper').height()+'px'});
	$('#lightbox').show();
	$('#centerflash').show();
	$('select').hide();
}

function closeMap() {
			
 	$('#lightbox').hide();
	$('#centerflash').hide();
	$('select').show();
}
 
 function openAllElements () {
 	$(".collapse span").addClass('active');
	$(".slider").fadeIn('slow');
 }
 
 function closeAllElements () {
 	$(".collapse span").removeClass('active');
	$(".slider").fadeOut('slow');
 }



/*

This function is called from the IBE in order to set the height of the iFrame
on basis of the length of the loaded iFrame-Content

*/
function setIBEiframeHeight (height) {
	$('#holiday-cars-ibe').attr('height', height+30);
}


function changeCountry (e) {
	var country_id 		= e.options[e.selectedIndex].value;
	if (country_id != -1) {
		$('#country select').removeClass('warning');
	}	
	getStations(country_id);		
}


function changePickupStation (e) {
	var station_id 		= e.options[e.selectedIndex].value;
	if (station_id != -1) {
		$('#pickup select').removeClass('warning');
		getStations($('#country select option:selected').val(), station_id);
	}
}


function changeDropStation (e) {
	var station_id 		= e.options[e.selectedIndex].value;
	if (station_id != -1) {
		$('#drop select').removeClass('warning');
	}
}

function getStations (country_id, station_id, dropoff_id) {
	var lang_id 		= $('#lang-id').attr('value');
	var op_id 			= $('#op-id').attr('value');
	var base_url 		= $('#base-url').attr('value');

	if (country_id > 0 && lang_id && base_url) {
		$('#pickup > select').load(base_url + '?eID=holidaycarsfe-async&lang_id=' + lang_id + '&op_id=' + op_id + '&country_id=' + country_id + '&station_id=' + station_id,'',function () {
			getStationsLoad('#pickup', station_id);
		});
		
		if (station_id > 0) {
			$('#drop > select').load(base_url + '?eID=holidaycarsfe-async&dropoff=1&lang_id=' + lang_id + '&op_id=' + op_id + '&country_id=' + country_id + '&station_id=' + station_id + '&dropoff_id=' + dropoff_id,'',function () {
				getStationsLoad('#drop');
			});
		}
		else {
			$('#drop > select').html('');
			getStationsLoad('#drop');
			$('#drop > select').attr({disabled: 'disabled'});
		}
	}
}

function getStationsLoad (select, station_id) {
	var title 	= $(select +'-title').attr('value');
		
	$(select + ' > select').removeAttr('disabled');

	$(select + ' > select').prepend('<option value="-1">' + title + '</option>');

	if (station_id > 0) {
		$(select + ' > select > *[value=' + station_id + ']').attr('selected','selected');
	}
	else {
		$(select + ' > select > option:first-child').attr('selected','selected');
	}

//	checkSubmit();
}

function showBookingForm (country_id, station_id, promotion_code, custom_query) {

	if (IBEisContentElement == false) {

		//
		// make sure to hide the selects as are visible through the layer
		$('.elements > select').hide();
		
		var height_left = $('.left').height();
		var height_right = $('.right').height();
		if (height_left >= height_right) {
			var set_height = height_left;
		}
		else {
			var set_height = height_right;
		}
		
		$("#opacity-layer").css({height: set_height+'px'});
		
		$("#opacity-layer").show();
		$("#booking-form").show();
		
		$('#close-form-button').show();
	
	}
	
	if (country_id > 0) {
		$('#country > select > *[value=' + country_id + ']').attr('selected','selected');
	}
	
	if (promotion_code) {
		$('#promotion').attr({value: promotion_code});
	}
	
	if (custom_query) {
		$('#custom-query').attr({value: custom_query});
	}
	
	getStations(country_id, station_id);

//	checkSubmit();
}

function hideBookingForm () {
	if (IBEisContentElement == false) {
		$('.elements > select').show();
		$('#country > select > option:selected').attr('selected','');
		
		$('#promotion').attr({value: ''});
		
		$('#pickup > select').html('');
		$('#drop > select').html('');
		getStationsLoad('#pickup');
		getStationsLoad('#drop');
		
		
		$("#booking-form").hide();
		$("#opacity-layer").hide();
	}
}

function checkSubmit () {

	var country_val = $('#country select').val();
	var pickup_val 	= $('#pickup select').val();
	var drop_val 	= $('#drop select').val();
	
	var submit = true;

	if (country_val < 0) {
		$('#country select').addClass('warning');
		submit = false;
	}
	else {
		$('#country select').removeClass('warning');
	}
	
	if (pickup_val < 0) {
		$('#pickup select').addClass('warning');
		submit = false;
	}
	else {
		$('#pickup select').removeClass('warning');
	}
	
	if (submit == true) {
		return true;
	}
	else {
		return false;
	}
}

function submitForm () {
	if (checkSubmit() == true) {
		document.holidaycarssearch.submit();
	}
	else {
		alert($('#required-warning').attr('value'));
		return false;
	}
}




(function($) {

    /* jQuery class extension methods */
    $.extend({

        /* method for logging an object or message */
        consoleInfo: function (message) {
             
             if ($.browser.mozilla && !(typeof(window["console"]) == "undefined")) {             
             	console.info(message);
             }
             
        },
        
        /* method for logging an object or message */
        consoleWarn: function (message) {
             
             if ($.browser.mozilla && !(typeof(window["console"]) == "undefined")) {
             	console.warn(message);
             }
             
        }        
    }); 

})(jQuery);
