$(function() {

	$.getJSON('?page=info&action=GetHostelsInfo&country=' + $('#country_code').val(), 
	function(travel_info) {
		//Show Hostels only if <li> found in the summary of the hostels
		//The hostels are from page = info , method GetHostelsInfo .
		if (travel_info.summary.indexOf('<li>') === -1) {
			//Remove the hostels...
			$('span.travel_hostels').parents('li:first').fadeOut();
		}
	});

	$('li', $('ul.info')).bind('click', changeTabs); //fires wheneve we click an li item within the info list
	$('#country').bind('change',changeCountryCodeName);	//fires when we change the 'Where would you like totravel' value
	$('div.travelTips ul').attr('direction','up').marquee();
	
	
	
});

//temp
var travel_mates_string = '';
var init = 1;

function changeTabs()
{
	//first remove the old class for all the tabs
	$('li', $('ul.info')).each(function() {
		$(this).removeAttr('class');
	});
	
	//now set the selected class for the wanted tab
	$(this).attr('class','selected');
	
	//now get the tab name
	var tab_name = $('span', $(this)).attr('class');
	
	//get the info div
	var info_div = $('#info_content');

	//save the travel mates string that was already loaded (temp)
	if (init)
	{
		travel_mates_string = info_div.html();
		init = 0;
	}
	
	var country_code = $('#country_code').val();
	var country_name = $('#country option[value='+country_code+']').text();

	switch(tab_name)
	{
		case 'travel_mates':
			//info_div.html(travel_mates_string); //restore the saved travel mates div (temp)
			
			//Fix for : 
			//בשינוי המיקום, הטאב travel Info לא משתנה - אמור להציג מידע על המדינה בה אני נמצא
			
			setMatesInfo(info_div, country_name);
			break;
			
		case 'travel_info':
			setTravelInfo(info_div, country_code);
			break;
			
		case 'travel_festivals':
			setFestInfo(info_div, country_code);
			break;	
			
		case 'travel_hostels' :
			setHostelsInfo(info_div, country_code);
			break;
		
	}
	
}

function setTravelInfo(info_div, country_code)
{
	$.getJSON('?page=info&action=GetTravelInfo&country=' + country_code, 
	function(travel_info) {
		if (travel_info == false)
			travel_info = {summary : 'want to write information about this Location? contact us to <a href="mailto:support@trekCafe.com">support@TrekCafe.com</a>'};
		
		var travel_info = '<div class="cont">' + travel_info.summary + '</div>';
		
		travel_info += '<a href="Travel-Destinations/?action=' + country_code + '" class="more_people">More Info...</a>';
		
		info_div.html(travel_info);
	});
}

function setHostelsInfo(info_div, country_code)
{
	
	$.getJSON('?page=info&action=GetHostelsInfo&country=' + country_code, 
	function(travel_info) {
		
		if (travel_info == false)
			travel_info = {summary : 'No hostels found for this location.'};
		
		var travel_info = '<div class="cont">' + travel_info.summary + '</div>';
				
		info_div.html(travel_info);
	});
}

function setMatesInfo(info_div, country_name)
{
	
	$.getJSON('?page=info&action=GetTravelersNearBy&country=' + country_name, 
	function(travel_info) {

		var travel_info = '<div class="cont">' + travel_info + '</div>';
		
		info_div.html(travel_info);
	});
}


function setFestInfo(info_div, country_code)
{
	$.getJSON('?page=info&action=GetFestInfo&country=' + country_code, 
	function(travel_info) {
		var max_items		= 6;
		var travel_info_div = '<div class="cont">';
		
		if (travel_info.length == 0)
			travel_info_div += 'No festivals to show';
		else {
			travel_info_div += '<ul class="fest">';
			var i = 0;
			for (pageName in travel_info) {
				
				i++;
				var	event_start_date	= travel_info[pageName].event_start_date;
				var	event_end_date		= travel_info[pageName].event_end_date;
				
			
				travel_info_div += '<li>';
				travel_info_div += '<strong><a href="travel/?action=' + pageName + '">' + travel_info[pageName].event_name + '</a></strong>, ' + event_start_date + ' - ' + event_end_date
				travel_info_div += '</li>';
				
				if (i>=max_items)
					break;
				
			}
			travel_info_div += '</ul>';
		}
		travel_info_div += '</div>';
		travel_info_div += '<a href="?page=event" class="more_people">More Info...</a>';
		
		info_div.html(travel_info_div);
	});
}

function setToolbox(obj)
	{
	obj.blur();
	
	var	_toolbox 	= document.getElementById('toolbox');

	var _ul = _toolbox.getElementsByTagName('li');
	var _ulnum = _ul.length;

	for (var i=0;i<_ulnum;i++)
		_ul[i].className = '';
	
	obj.parentNode.className = 'selected';
	
	return true;
	}
	
function changeCountryCodeName()
	{
	if ($(this).val() == '')
		return ;
	
	$('#country_code').val( $(this).val() );		//Change the country code
	
	$.getJSON('?page=info&action=GetHostelsInfo&country=' + $('#country_code').val(), 
	function(travel_info) {
		
		//Check the currentTab
		var currentTab = $('ul.info li.selected');
		var currentIsHostels = $.trim(currentTab.text()) == 'Hostels';
		
		//Show Hostels only if <li> found in the summary of the hostels
		//The hostels are from page = info , method GetHostelsInfo .
		
		
		if (travel_info.summary.indexOf('<li>') === -1) {
			//Remove the hostels...
			$('span.travel_hostels').parents('li:first').fadeOut();
			if (currentIsHostels) {
				currentTab = currentTab.parent().find('li:first');
			}
		} else {
			//Restore the hostels
			$('span.travel_hostels').parents('li:first').fadeIn();
		}
		
		currentTab.click();				//Click on the current tab
	
	});
	}
