
function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator:first').css({opacity: 1.0});	
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',10000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('div.rotator.show') ? $('div.rotator.show') : $('div.rotator:first'));

    if ( current.length == 0 ) current = $('div.rotator:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator:first') :current.next()) : $('div.rotator:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
        //var rndNum = Math.floor(Math.random() * sibs.length );
        //var next = $( sibs[ rndNum ] );
			

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
	
};

$(document).ready(function(){
		//homepage slideshow
		$('div.rotator').hide();					   
		theRotator();
		$('div.rotator').fadeIn(2000);
		
		checkIndexLinks();
		getProgram();
		destinationMap();	
		$( "#tabs" ).tabs();
		$('#imageGallery a').lightBox();
		
		

		// event listener for country selection
		$('dd.country').click(function(e)	{
			var countryID = this.id
			var r = $(this).parent().parent().parent().attr("id");
			var regionID = r.split('_')[1];
			$('input[name="countryID"]').val(countryID);
			$('input[name="regionID"]').val(regionID);
			getProgram(countryID,regionID,0);
		});
		
		//selectbox listener for subtypes
		$('#subSelector').change(function(e)	{	
			var t =  $('.subtypeID:selected').attr("id");
			var contentID = t.split('_')[1];
			var opt = $('.subtypeID:selected').val();
			if(opt != '')
			{
				getProgram(0,0,opt)
				getSubType(contentID);
			}
		});
		
		// Loads ShareThis asyncronously
			if($('.st_sharethis').length != 0)
			{
				$.getScript('http://w.sharethis.com/button/buttons.js',function(){
					stLight.options({publisher:'1e50165b-e549-49db-82e2-749eab578e25'});															})
			}
		});
	
function destinationMap()	{
		$('<div class="mapRegion1"></div>').appendTo('#destinationMap');
		$('<div class="mapRegion2"></div>').appendTo('#destinationMap');
		$('<div class="mapRegion3"></div>').appendTo('#destinationMap');
		$('<div class="mapRegion4"></div>').appendTo('#destinationMap');
		$('<div class="mapRegion5"></div>').appendTo('#destinationMap');
		$('<div class="mapRegion7"></div>').appendTo('#destinationMap');

		$('.mapRegion1').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_1').show();});
			$('.selectionBox#region_1').mouseenter(function(){$(this).show();});
			$(mapID).mouseleave(function(){$('.selectionBox#region_1').hide();});
			$('.selectionBox#region_1').mouseleave(function(){$(this).hide();});
			});
		$('.mapRegion2').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_2').show();});
			$('.selectionBox#region_2').mouseenter(function(){$(this).show();});
			$(mapID).mouseleave(function(){$('.selectionBox#region_2').hide();});
			$('.selectionBox#region_2').mouseleave(function(){$(this).hide();});
			});
		$('.mapRegion3').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_3').show();}); 
			$('.selectionBox#region_3').mouseenter(function(){$(this).show();});
			$(mapID).mouseleave(function(){$('.selectionBox#region_3').hide();});
			$('.selectionBox#region_3').mouseleave(function(){$(this).hide();});
			});
		$('.mapRegion4').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_4').show();});
			$('.selectionBox#region_4').mouseenter(function(){$(this).show();}); 
			$(mapID).mouseleave(function(){$('.selectionBox#region_4').hide();});
			$('.selectionBox#region_4').mouseleave(function(){$(this).hide();});
			});
		$('.mapRegion5').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_5').show();});
			$('.selectionBox#region_5').mouseenter(function(){$(this).show();});
			$(mapID).mouseleave(function(){$('.selectionBox#region_5').hide();});
			$('.selectionBox#region_5').mouseleave(function(){$(this).hide();});
			});
		$('.mapRegion7').each(function(i){
			mapID = $(this).addClass('region');
			$(mapID).mouseenter(function(){$('.selectionBox#region_7').show();});
			$('.selectionBox#region_7').mouseenter(function(){$(this).show();});
			$(mapID).mouseleave(function(){$('.selectionBox#region_7').hide();});
			$('.selectionBox#region_7').mouseleave(function(){$(this).hide();});
			});

		
}

// shows index links on homepage if href exists
function checkIndexLinks()	{
	$('.index').each(function(i){
		var linkVal = $(this).attr('href');
		if (linkVal) {$(this).css({'display':'inline-block'});}	
	});	
}

// posts countryID and retrieves program data
function getProgram(c,r,s)	{
	var str = window.location.pathname
	var p
	var programList = ['/pages/5_destinations.cfm','/pages/2_volunteer_abroad.cfm','/pages/3_work_teach_abroad.cfm'];
	if ($.inArray(str,programList) != -1){
		if (str.search('destinations') != -1 && typeof(c) === 'undefined'){var c = 0;}
		else if (str.search('volunteer') != -1){var p = 1;}
		else if (str.search('work_teach') != -1){var p = 2;}
		$.post('/programs/inc_programs_list.cfm', {'countryID':c,'regionID':r, 'subTypeID':s, 'programTypeID':p}, function(data){
			$('.programData').html(data);
			Cufon.refresh();
		});
	}
}



// retrieves subType content
function getSubType(k)	{
		$.post('/programs/inc_programs_subtypes.cfm', {'contentID':k}, function(data){
		$('.programTypeDesc').html(data);
		Cufon.refresh();
		});
}





function tabbar(action,hl) {
document.form.jsaction.value = action;
document.form.newhl.value = hl;
document.form.submit();
}

// Aktion �ber URL-Variable verschicken.
	function ActionSubmit(action) {
		document.forms[0].jsaction.value = action;
		document.forms[0].submit();
	}
	
function formchanged ()
	{
	document.form.form_changed.value = 'yes';
	}


// Set Focus on the first field that contains an error
function errfocus (fieldname)
		{
		document.forms[0].elements[fieldname].focus();
		}

// R�ckfrage (Ja,Nein) ausl�sen, Aktion zwischenspeichern




function formsubmit(action,message) {
	if (message != '')
		{if (confirm (message) == true)
			{
			document.forms[0].action = document.forms[0].action + "?action=" + action;
			document.forms[0].submit();
			}
		}
	else
		{
		document.forms[0].action = document.forms[0].action + "?action=" + action;
		document.forms[0].submit();
		}
}

	<!-- begin to hide script contents from old browsers
	function checksearch()
	  {
		var searchlen = document.forms[0].searchterm.value;
		if(searchlen == "")
			{
			alert("Please type in keyword(s) to search!");
			}
		else {
				ActionSubmit('act_content_search');
			 }
	  }
	// end hiding script from old browsers -->



	function popitup(destination,win_name,win_dim) 
	{
			window.open (destination ,win_name,win_dim + ',' + 'resizable=no,scrollbars=no,toolbar=no,Left=250,Top=250,status=no,directories=no,menubar=no,location=tabelle');
 	}

	function newwindow(destination,win_name,win_dim) 
	{
			window.open (destination ,win_name,win_dim + ',' + 'resizable=yes,scrollbars=yes,toolbar=no,Left=250,Top=250,status=no,directories=no,menubar=no,location=tabelle');
 	}

	if (document.layers) document.captureEvents(Event.KEYPRESS); 
	// needed if you wish to cancel the key

	document.onkeypress = keyhandler;
	
	function keyhandler(e) {
	    if (document.layers){
	        Key = e.which;
			ename = e.target.name;}
	    else{
	        Key = window.event.keyCode;
			ename = window.event.srcElement.name;}

//        alert("Key pressed! ASCII-value: " + Key + " - " + event.srcElement.name);

	    if (Key == 13 && (ename == "searchterm" || ename == "searchterm")){
			var searchlen = document.forms[0].searchterm.value;
			if(searchlen == "")
				{
				alert("Please type in keyword(s) to search!");
			    if (document.layers)
					return false;
		    	else
					window.event.returnValue = false;
				}
			else {
					for(var iii=0; iii < document.forms[0].elements.length; iii++){
						if(document.forms[0].elements[iii].type == "submit"){
							document.forms[0].elements[iii].name = "act_content_search";
							}
						}	
					ActionSubmit('act_content_search');
				 }
			}
	}

function noSpamMailLink(user,domain,tld,param) {
	locationstring = "mailto:" + user + "@" + domain + "." + tld + param;
	window.location = locationstring;
}

