function fixCollapsableFieldsets(){
	$('fieldset legend').css('cursor','pointer').bind('click',
		function(){
			$('div.container', $(this).parent()).slideToggle(125);
		}
	);
}
/*
 * This function will change, on page load once,
 * and will mimic the settings set on the from to the to droplists
 */
function fixBookingDropLists(){
	$('#form-fromdate-year').one('change',
		function(){
			if($(this).get(0).selectedIndex > $("#form-todate-year").get(0).selectedIndex){
				$("#form-todate-year").get(0).options[$(this).get(0).selectedIndex].selected=true;
			}
		}
	);
	$('#form-fromdate-month').one('change',
		function(){
			if($(this).get(0).selectedIndex > $("#form-todate-month").get(0).selectedIndex){
				$("#form-todate-month").get(0).options[$(this).get(0).selectedIndex].selected=true;
			}
		}
	);
	$('#form-fromdate-day').one('change',
		function(){
			if($(this).get(0).selectedIndex > $("#form-todate-day").get(0).selectedIndex){
				$("#form-todate-day").get(0).options[$(this).get(0).selectedIndex].selected=true;
			}
		}
	);
}

function fixRoomType(){
	var obj=$('#form-bookbed').bind('click',
		function(){
			if(this.checked){
				$('#typavsangar').slideDown();
			}
			else{
				$('#typavsangar').slideUp();
			}
		}
	).get(0);
	if(obj.checked){
		$('#typavsangar').slideDown();
	}
}

function countBeds(obj,type){
	if( !(!$('#form-bookroom').get(0).checked && $('#form-bookbed').get(0).checked ) ){		//this only works if we are ONLY interested in beds, not rooms
		return false;
	}
	numMen=$('#form-nummen').val();
	numWomen=$('#form-numwomen').val();
	total=$('#form-numpersons').val();
	window.status=numMen+'-'+numWomen;
	if(type=='men'){	//we should adjust the number of women....
		numWomen=total-numMen;
		$('#form-numwomen').val(numWomen);
	}
	else{
		numMen=total-numWomen;
		$('#form-nummen').val(numMen);
	}
}

function makeCorrectNumOfPeople(){
	//first lets bind the fields
	$('#form-nummen').bind('keyup',
		function(){
			countBeds(this, 'men');
		}
	);
	$('#form-numwomen').bind('keyup',
		function(){
			countBeds(this, 'women');
		}
	);
}

function showWarningMessages(){
	$('div.meddelande').slideDown(250);
}

function externalLinks(){
	$('a').bind('click',urlChecker);
}

var pageDomain=String(document.location).split('/')[2].split('.').slice(-2).join('.');
function urlChecker(){
	linkDomain=String(this.href).split('/')[2].split('.').slice(-2).join('.');
	if(linkDomain==pageDomain){	//same domain link, we return true
		return true;
	}
	else{
		window.open(this.href, '_blank');
		return false;
	}
}

function fixEqualHeightFieldsets(){
	var fieldset1=$('#form-bokningsuppgifter');
	var fieldset2=$('#form-personuppgifter');
	var txtArea=$('textarea',fieldset1);
	var deltaHeight=fieldset1.height() - fieldset2.height();
	//alert(txtArea.height() +'-'+deltaHeight +'='+(txtArea.height()-deltaHeight));
	newHeight=(txtArea.height()-deltaHeight);
	txtArea.css('height',newHeight+'px');
}

function init(){
	$('body').addClass('js');
	//fixCollapsableFieldsets();
	setTimeout('showWarningMessages()','500');
	fixBookingDropLists();
	fixRoomType();
	makeCorrectNumOfPeople();
	externalLinks();
}
$(document).ready(function(){init()});
