/* 
 * Fairtrade Resources by Web Molecule
 * Version 0.1
 * September 2010 
 * 
 * Notes:
 *  Required jQuery >= 1.3.2 (probably would would with earlier versions)
 *  Use jQuery() instead of shortcut $() to preserve funcitonality of other library
 * 
 * Contents:
 *  Simple jQuery content loader (click the blocks around the main content block)
 * 
 */

jQuery(document).ready(function()
{
	
	/*
	 * Load content into main div based on linkto id
	 */
	function loadFairtradeContent(blockId) {
		//default value
		var default_blockId = "linkto-fairtrade-home";
		if ( ! blockId || blockId == null) {
			blockId = default_blockId;
		}
		
		//remove 'linkto' portion of id
		blockId = blockId.replace("linkto-", "");
		
		//hide current one
		jQuery(".fairtrade-main-content:visible")
			.removeClass("selected")
			.fadeOut();
		//select and show appropriate one
		jQuery("#"+blockId)
			.addClass("selected")
			.fadeIn();
	}
	
	/*
	 * Left click mouse event for clickable blocks
	 */
	jQuery(".fairtrade-block").mousedown(function(event) {
	    switch (event.which) {
	        case 1:
	        	//alert('Left mouse button pressed');
	        	//get blockId
	        	var blockId = null;
	        	if (event.currentTarget) {
	        		blockId = event.currentTarget.id;
	        	}
	        	//if no idea set, probably just a link
	        	if (blockId.length > 0) {
		        	//load content
		        	loadFairtradeContent(blockId);
	        	}
	        	break;
	        case 2:
	        	//alert('Middle mouse button pressed');
	        	break;
	        case 3:
	        	//alert('Right mouse button pressed');
	        	break;
	        default:
	        	alert("You have a strange mouse");
	    }
	});
	
	/*
	 * History slider
	 */
	if (jQuery("#fairtrade-history-timeline").length > 0) {
		// get the 'years'
		var years = jQuery("#fairtrade-history-timeline > ol > li > a");
		function selectHistoryYear(yearId) {
			//remove 'selected' class from other years
			jQuery("#fairtrade-history-timeline > ol > li > a").removeClass("selected");
			//add 'selected' class
			jQuery(years.get(yearId)).addClass("selected");
	
			if (jQuery(years.get(yearId)).attr("href")) {
				var yearName = jQuery(years.get(yearId)).attr("href").replace("#", "");
				//console.log(yearName);
				//reset opacity for images
				jQuery("#fairtrade-history-images-show > img:visible").hide();
				//IE can't fade images nicely
				if (jQuery.browser.msie) {
					jQuery("#fairtrade-history-images-show > .year-"+yearName).show();
				} else {
					jQuery("#fairtrade-history-images-show > .year-"+yearName).fadeIn();
				}
				//show content depending on selected year
				jQuery("#fairtrade-history-text > div:visible").hide();
				if (jQuery.browser.msie) {
					jQuery("#fairtrade-history-text-"+yearName).show();
				} else {
					jQuery("#fairtrade-history-text-"+yearName).fadeIn();
				}

				//show bubble caption
				jQuery("#fairtrade-history-bubbles > div:visible").hide();
				jQuery("#fairtrade-history-bubbles").removeClass().addClass("year-"+yearName);
				var bubSelection = "#fairtrade-history-bubble-left, #fairtrade-history-bubble-right, #fairtrade-history-bubble-bottom, #fairtrade-history-bubble-"+yearName;
				//choose left or right botton
				if (years.length - yearId <= 2) {
					jQuery("#fairtrade-history-bubble-bottom").removeClass("bubble-left").addClass("bubble-right");
				} else {
					jQuery("#fairtrade-history-bubble-bottom").removeClass("bubble-right").addClass("bubble-left");
				}
				jQuery(bubSelection).show();
			}
		}
			
		// slider
		jQuery("#fairtrade-history-slider").slider({
			value:0,
			min: 0,
			max: 12,
			step: 1,
			slide: function(event, ui) {
				if (ui.value >= years.length) {
					return false;
				}
				selectHistoryYear(ui.value);
			}
		});
		// click on year
		jQuery("#fairtrade-history-timeline > ol > li > a").mousedown(function(event) {
			// left click only
		    if (event.which == 1) {
	        	jQuery(years).each(function(item) {
	        		//console.log(event.currentTarget.hash, this.hash);
	        		if (event.currentTarget.hash == this.hash) {
	        			jQuery("#fairtrade-history-slider").slider("value", item);
	    				selectHistoryYear(item);
	        			return false;
	        		}
	        	});
	
	    	    return false;
		    }
		});
		
		//Select the first year of timeline
		jQuery("#fairtrade-history-slider").slider("value", 0);
		selectHistoryYear(0);
	}
});

