var videos = {
	id: "video_leaf_set",
	currentlySelectedLeaf: "guided_tour"
};

// Global variables for the screenshots
var iphone = { 
	leaf: "iphone",
	tab: "iphone_tab"
};
var android = { 
	leaf: "android",
	tab: "android_tab"
};
var browsers = { 
	leaf: "browsers",
	tab: "browsers_tab"
};
var screenshotLeaves = [iphone, android, browsers];
var screenshots = {
	id: "screenshot_leaf_set",
	leaves: screenshotLeaves,
	currentlySelectedLeaf: iphone
};

var leafSets = [videos, screenshots];

var tab = {
	normal: "media_tab",
	selected: "media_tab_selected"
};

var fadeSpeed = 300;

function switchOff(leafSet, callback) {
	if (leafSet == null)
		return;
	
	var leafObject = $("#" + leafSet.id + "> #" + leafSet.currentlySelectedLeaf.leaf);
	var tabObject = $("#" + leafSet.id + " #" + leafSet.currentlySelectedLeaf.tab);

	$(leafObject).fadeOut(fadeSpeed);
	$(tabObject).removeClass(tab.selected);
	$(tabObject).addClass(tab.normal);
	leafSet.currentlySelectedLeaf = null;
}

function switchOn(leafSet, leaf, callback) {
	if (leafSet == null || leaf == null)
		return;
	
	leafObject = $("#" + leafSet.id + "> #" + leaf.leaf);
	tabObject = $("#" + leafSet.id + " #" + leaf.tab);
	
	$(leafObject).fadeIn(fadeSpeed, callback);
	$(tabObject).removeClass(tab.normal);
	$(tabObject).addClass(tab.selected);
	leafSet.currentlySelectedLeaf = leaf;
}

function switchTo(leafSet, leaf) {
	var matchingSet;
	
	for (i = 0; i < leafSets.length; i++) {
		if (leafSets[i].id == leafSet) {
			matchingSet = leafSets[i];
			break;
		}
	}
	
	var matchingLeaf;
	
	for (i = 0; i < matchingSet.leaves.length; i++) {
		if (matchingSet.leaves[i].leaf == leaf) {
			matchingLeaf = matchingSet.leaves[i];
			break;
		}
	}
	
	switchOff(matchingSet, null);
	setTimeout(function() {switchOn(matchingSet, matchingLeaf, null)}, fadeSpeed + 100);
}
