/* Globals */

var curProfile = -1;
var browserProfiles = '';

/* Init */

$(document).ready(function(){
						   
	// Initialise homepage tabs
	$(".leadhome .tabs").each(hometabs);

	// Homepage regions map
	var currentRegion = $(".region_map option:selected");
	
	// If we're not in the National region, change the map
	var mapSrc = "images/map/map_" + currentRegion.val() + ".jpg";
	$("#imgMap").attr("src",mapSrc);
	
	// The region map on the home page
	$("area").mouseover(function(){
		var regionid = $(this).attr("id").split("_")[1];
		// Change dropdown
		$(".region_map option:selected").attr("selected","");
		$(".region_map option[value='"+regionid+"']").attr("selected","selected");
	}).mouseout(function(){
		// Reset dropdown
		$(".region_map option:selected").attr("selected","");
		$(currentRegion).attr("selected","selected");
	});
	
	// Get another 5 peeps for the Directory Browser
	fetchProfiles();

	// Bind the directory browser buttons
	dirBrowser = $(".directory_panel");
	$(".next",dirBrowser).click(nextProfile);
	$(".prev",dirBrowser).click(prevProfile);

});

/* Directory Browser */

function prevProfile() {
	if (curProfile - 1 <= browserProfiles.length && curProfile - 1 >= 0) {
		curProfile = curProfile - 1;
		swapProfile(browserProfiles[curProfile]);		
	}
	return false;
}

function nextProfile() {
	curProfile = curProfile + 1;
	swapProfile(browserProfiles[curProfile]);
	// If there are less than 5 profiles left in the object, fetch some more
	if (browserProfiles.length - curProfile < 6) {
		fetchProfiles();
	}
	return false;
};

function swapProfile(profile) {
	$(".next",dirBrowser).unbind('click',nextProfile);
	$(".prev",dirBrowser).unbind('click',prevProfile);
	dirBrowser.addClass("loading");
	// Preload the image and update depending on success
	// Events need to be bound before their trigger (in this case setting the attr)
	$("<img />")
		.load(function(e){
			// If the image loads fine, replace the image and the details
			dirBrowser.find(".image img").attr("src",profile.pic);
			dirBrowser.find(".image img").attr("alt","");
			dirBrowser.find(".image").attr('href','/arts_directory_detail.aspx?OrganisationID=' + profile.artistID);
			dirBrowser.find(".title")
				.attr('title',profile.name)
				.text(profile.name)
				.attr('href','/arts_directory_detail.aspx?OrganisationID=' + profile.artistID);
			dirBrowser.find("#tab1").html(profile.summary);
			dirBrowser.find("#tab2").text(profile.artforms);
			dirBrowser.find("#tab3").text(profile.exp);	
			dirBrowser.find(".next").click(nextProfile);
			dirBrowser.find(".prev").click(prevProfile);
			dirBrowser.removeClass("loading");
		})
		.error(function(e){
			// If the image fails to load, replace the alt text
			dirBrowser.find(".image img").attr("src","");
			dirBrowser.find(".image img").attr("alt","No image");
			dirBrowser.find(".title")
				.text(profile.name)
				.attr('title',profile.name)
				.attr('href','/arts_directory_detail.aspx?OrganisationID=' + profile.artistID);
			dirBrowser.find("#tab1").html(profile.summary);
			dirBrowser.find("#tab2").text(profile.artforms);
			dirBrowser.find("#tab3").text(profile.exp);	
			dirBrowser.find(".next").click(nextProfile);
			dirBrowser.find(".prev").click(prevProfile);
			dirBrowser.removeClass("loading");		
		})
		.attr("src",profile.pic);
}

function fetchProfiles() {
	$.ajax({
		type: "GET",
		url: "artsdirectory_json.aspx?" + "%3F" + Math.random(),
		data: "records=5",
		dataType: "json",
		cache: false,
		success: function(data){
			if(browserProfiles == "") {
				browserProfiles = data.artistProfiles;
			} else {
				browserProfiles = browserProfiles.concat(data.artistProfiles);	
			}
		}
	});	
}

/* Misc */

// Resize the tabs on the home pages depending on how many there are
// Add classes for the first and last tabs
function hometabs(i) {
	var tabsNumber = $(this).find("li").length;
	var containerWidth = $(this).width();
	var tabWidth = Math.floor(containerWidth / tabsNumber);
	var remainder = containerWidth % tabsNumber;
	$(this).find("li").width(tabWidth);
	$(this).find("li:last").width(tabWidth + remainder).find('a').addClass('last');
	$(this).find("li:first").find('a').addClass('first');
}
