var timer;
var firstCategoryLoad = true;
var archers_page;

jQuery(document).ready(function() {
	jQuery(".tabset li a").bind("click", onTabChange); // tab change
	jQuery(".choose_destination").bind("click", onDestinationChange); // destination change
	jQuery("#find_year").bind("change", onYearChange); // year change
	
	// useful when browser's back button was used
	jQuery(".choose_destination").removeAttr("checked");
	jQuery("#dest_continents").attr("checked", "checked");	
	loadKeywords('continents');		
	
	// google search
	jQuery("#cse-search-box").bind("submit", onGoogleCustomSearch);
	
	// is it archers page?
	var str = location.hostname;
	if (str.search(/archers/i) > -1) { 
		archers_page = "yes";
	} else if (str.search(/cosmos/i) > -1) { 
		archers_page = "no";
	}
});

function loadMoreOptions(preselect) {
	var year = jQuery("#find_year").val();
	var url = "/content/list-more-options.php?year="+year;
	jQuery(".types ul").hide();
	jQuery(".types ul").empty();
	jQuery(".types").css("height", "auto");
	jQuery(".types").css("overflow-y", "hidden");	
	jQuery("#ajaxLoading").show();
	jQuery("#side_box_moreoptions ul").load(url, function() {
		jQuery("#side_box_moreoptions ul li input").bind("click", onMoreOptionsClick);
		var firstOption = jQuery("#side_box_moreoptions ul li input:first");
		var selectOption = preselect ? jQuery("#" + preselect) : firstOption;
		jQuery(selectOption).attr("checked", "checked");
		loadKeywords(selectOption.val());
	});
}

function loadKeywords(category) {
	
	var year = jQuery("#find_year").val();

	if (firstCategoryLoad) {
		firstCategoryLoad = false;
	} else {
		// sitestats	
		loadSitestatsImage(
			"-search/" + year + "/" + category,
			"&action=search&year=" + year + "&category=" + category,
			null // no redirect
		);
	}
	
	var url = "/content/list-keywords.php?year="+year+"&category="+category;
	jQuery(".types ul").hide();
	jQuery(".types ul").empty();
	jQuery(".types").css("height", "auto");
	jQuery(".types").css("overflow-y", "hidden");	
	jQuery("#ajaxLoading").show();
	
	if (archers_page == "yes")
		refresh_flash_pos();
	
	jQuery("#types_col1").load(url, function() {
		
		var columnCount, columnCapacity;
		//if (category=='lifestyleandinterests' || category=='railoperators' || category=='trains' || category =='airport' ) 
		if (false) {
			columnCount = 2;
		} else {
			columnCount = 3;
		}
		if (jQuery("#tab_moreoptions").hasClass("active")) {
			if (jQuery("#side_box_moreoptions").children("li").length == 10) {
				columnCapacity = 16;	// if Late Availability is displayed
			} else {
				columnCapacity = 14;
			}
		} else {
			columnCapacity = 8;
		}		

		var keywords = jQuery("#types_col1").children("li");
		var currentColumn = 1;
		var currentCount = 0;

		var splitCount = Math.ceil(keywords.length / columnCount);		
		if (splitCount < columnCapacity) { splitCount = columnCapacity; }
		
		for (var i=0; i<keywords.length; i++) {
			currentCount++;
			if (currentCount > splitCount) {
				currentCount = 1;
				currentColumn++;
				jQuery("#types_col" + currentColumn).empty();
			}
			if (currentColumn > 1) {
				jQuery(keywords[i]).remove().appendTo("#types_col" + currentColumn);
			}
		}
		
		var columnHeight = jQuery("#types_col1").height();
		if (columnHeight > 360) {
			jQuery(".types").css("height", 360);
			jQuery(".types").css("overflow-y", "auto");
		} else {
			jQuery(".types").css("height", "auto");
			jQuery(".types").css("overflow-y", "hidden");
		}

		jQuery("#ajaxLoading").fadeOut("fast", function() {
			if (columnCount == 2) {
				jQuery(".types").removeClass("types-3cols").addClass("types-2cols");
				jQuery("#types_col1").fadeIn("slow");
				jQuery("#types_col2").fadeIn("slow");
			} else {
				jQuery(".types").removeClass("types-2cols").addClass("types-3cols");
				jQuery(".types ul").fadeIn("slow");
			}
			
			if (archers_page == "yes")
				refresh_flash_pos();
		});
	});
}

function onTabChange() {
		jQuery(".tab-content").css("display", "block");
		jQuery(".search-block .tabset-holder").removeClass("closed");
		var tabs = jQuery(".tabset li a").removeClass("active");
		
		// hide world map if displayed
		hideWorldMap();
		
		// Select active tab
		jQuery(this).addClass("active");
		var tab = jQuery(this).attr("id").substring(4);
		
		// Show correct side-box
		jQuery(".tab-content-bottom div.side-box").hide();
		jQuery("#side_box_"+tab).show();
		
		// Load keywords
		if ((tab != "destinations") && (tab != "moreoptions")) {
			loadKeywords(tab);
		}
		
		// Show side-box and load keywords for destinations
		if (tab == "destinations") {
			// switch to countries by before loading
			jQuery(".choose_destination").removeAttr("checked");
			jQuery("#dest_continents").attr("checked", "checked");
			loadKeywords('continents');		
		}
		
		// Load side-box and keywords for more options
		if (tab == "moreoptions") {
			loadMoreOptions();
		}
		
		return false;
}

function onDestinationChange() {
	var destination = jQuery(this).val();
	if (destination != "worldmap") {
		hideWorldMap();
		var category = destination;
		loadKeywords(category);
	}
	else {
		jQuery(".types").fadeOut("fast", function() {
				var year = jQuery("#find_year").val();																			
				jQuery("#world_map_" + year).show();
				
				if (archers_page == "yes")
					refresh_flash_pos();
/*				jQuery("#world_map_" + year).fadeIn("slow", function() {
			});
*/
		});
	}
}

function onYearChange() {
	// hide world map if displayed
	hideWorldMap();

	var year = jQuery("#find_year").val();
	var tabs = jQuery(".tabset").children("li").children("a");
	var active_tab;
	
	// Find active tab
	for (var i=0; i<tabs.length; i++) {
		if (jQuery(tabs[i]).hasClass("active")) {
			active_tab = jQuery(tabs[i]).attr("id").substring(4);
		}
	}
	
	// Load keywords
	if ((active_tab != "destinations") && (active_tab != "moreoptions")) {
		loadKeywords(active_tab);
	}
	
	// Load side-box and keywords - destinations
	if (active_tab == "destinations") {
		//var act_destination = jQuery("input[name=radio_destinations]:checked").val();
		//loadKeywords(act_destination);
		jQuery(".choose_destination").removeAttr("checked");
		jQuery("#dest_continents").attr("checked", "checked");
		loadKeywords('continents');
	}
	
	// Load side-box and keywords - more options
	if (active_tab == "moreoptions") {
		var act_option = jQuery("input[name=group_moreoptions]:checked").attr("id");		
		loadMoreOptions(act_option);
	}
}

function onMoreOptionsClick() {
	category = jQuery(this).val();
	loadKeywords(category);
}

function hideWorldMap() {
	jQuery(".world_map").hide();
	jQuery(".types").show();
	
/*	
	if (jQuery(".world_map:visible"))	{
		jQuery(".world_map:visible").fadeOut("slow", function() {
				jQuery(".types").show();
		});
	}
*/
}

function onGoogleCustomSearch(event) {
	var keyword = jQuery("#inp-search").val();
	return (keyword != '');
}
