function initSidebar() {
	
	//attach events
	var sidebarTabs = $("#mbSidebar div.sidebarTab");
	
	$.each(sidebarTabs, function(index, value) {
		var tabID = $(value).attr("id");
		$("#" + tabID + " > h1").click(function() {
			openSidebarTab(tabID);
		});
	});
	
	
	$("#videosTab .filters").tabs();
	$("#audioTab .filters").tabs();
	$("#albumsTab .filters").tabs();
	$("#socialTab .filters").tabs();
	$("#reportsTab .filters").tabs();
	
	initSearchBox();
}




function initSearchBox() {
	var query = $("#query");
	
	var defaultVal = "search...";
	
	if($.query.get("query")) {
		defaultVal = $.query.get("query");
	}
	
	query.val(defaultVal);
	query.addClass("inactiveQuery");
	
	query.focusin(function() {
		if(query.val() == defaultVal) {
			query.val(""); //should be "null" but IE doesn't like that.
			query.removeClass("inactiveQuery");
		}
	});
	
	query.focusout(function() {
		if(query.val() == defaultVal || query.val().length == 0) {
			query.val(defaultVal);
			query.addClass("inactiveQuery");
		}
	});
}




function openSidebarTab(tabID) {
	var currentActiveTabID;
	
	//close the others
	$("#mbSidebar .open").each(function(index, currentActiveTab) {
		currentActiveTabID = $(currentActiveTab).attr("id");
		$(currentActiveTab).removeClass("open");
	});

	//if already open, just close it and move on
	if(currentActiveTabID == tabID && tabID != "favoritesTab") {
		return;
	}
	
	if(tabID == "favoritesTab")
		window.location = theRoot + "/index.php?module=favorites";

	if(tabID == "homeTab")
		goHome();
		//window.location = "index.php?module=home";

	//open the chosen one
	$("#" + tabID).addClass("open");
}





function showDirectLinkAsset() {
	var assetID = $.query.get("assetID");
	
	switch($.query.get("module")) {
		case "photos":
			//console.log("photos!");
			//theDetail = theRoot + "/views/photo-file.php?assetID=" + assetID + "&extention=jpg"; //not working
			break;
		case "audio":
		case "homeA":
			theDetail = theRoot + "/views/audio-detail.php?assetID=" + assetID;
			break;
		case "video":
		case "homeV":
		default:
			theDetail = theRoot + "/views/video-detail.php?assetID=" + assetID;
			break;
	}

	$.fancybox({
		type:"ajax",
		href:theDetail,
		scrolling:"no",
		//onComplete:initVideoPlayer,
		//afterShow:initVideoPlayer,
		padding:0,
		overlayOpacity:0.8,
		overlayColor:"#000000"
	});
}




function goHome() {
	window.location = theRoot + "/index.php";
}





function initVideoPosters() {
	$(".videoListing .title, .videoListing .videoThumb, #mostPopularVideos li a, #newVideoReleases li a, .editorForm .videoThumb img").each(function(index, current) {
		
		el = $(current);
		var assetID = el.parent().attr("asset");
		
		var tagID = $.query.get("tagID");
		var filter = $.query.get("filter");
		var module = "video"; //$.query.get("module");
		
		//in case they get the link from somewhere else, like favorites or search results
		if(!tagID)
			module = "homeV";

		$(el).fancybox({
			type:"ajax",
			href:"/mediabox/views/video-detail.php?module=" + module + "&tagID=" + tagID + "&filter=" + filter + "&assetID=" + assetID,
			scrolling:"no",
			//onComplete:initVideoPlayer,
			//afterShow:initVideoPlayer,
			padding:0,
			overlayOpacity:0.8,
			overlayColor:"#000000"
		});
	});
}




function initAudioPosters() {
	$(".audioListing .title, .audioListing .audioThumb, #mostPopularAudio li a, #newAudioReleases li a").each(function(index, current) {
		
		el = $(current);
		var assetID = el.parent().attr("asset");
		
		var tagID = $.query.get("tagID");
		var filter = $.query.get("filter");
		var module = "audio"; //$.query.get("module");

		//in case they get the link from somewhere else, like favorites or search results
		if(!tagID)
			module = "homeA";

		$(el).fancybox({
			type:"ajax",
			href:"/mediabox/views/audio-detail.php?module=" + module + "&tagID=" + tagID + "&filter=" + filter + "&assetID=" + assetID,
			scrolling:"no",
			padding:0,
			overlayOpacity:0.8,
			overlayColor:"#000000",
			onCleanup:function() {
				$("#jPlayer").jPlayer("stop"); //forces the player to stop...thanks, IE
			}
		});
	});
}





function initLightbox() {
	$(".fancybox").fancybox({
		overlayOpacity:0.8,
		overlayColor:"#000000",
		transitionIn:"elastic",
		transitionOut:"elastic"
	});
}


function initMemberInfo() {

	$(".memberInfoButton").each(function(index, current) {
		el = $(current);
		//var cID = el.attr("cID");
		//var year = el.attr("year");
		var postID = el.attr("postID");
		
		$(el).fancybox({
			//href:"views/user-info.php?year=" + year + "&cID=" + cID,
			type:"ajax",
			href:"views/user-info.php?postID=" + postID,
			overlayOpacity:0.8,
			overlayColor:"#000000",
			transitionIn:"elastic",
			transitionOut:"elastic"
		});
	});
	
}





function initVideoPlayer() {
	//runMobileCompatibilityScript("myExperience");
	brightcove.createExperiences(); //force brightcove to init player
}













var bcExp;
var modVP;
var modExp;
var modCon;

// called when template loads, this function stores a reference to the player and modules.
//http://support.brightcove.com/en/docs/using-javascript-player-api
function onTemplateLoaded(experienceID) {

    bcExp = brightcove.getExperience(experienceID);
 
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modCon = bcExp.getModule(APIModules.CONTENT);
    
    modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
    modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad); 
}


function onTemplateReady(evt) {
	//alert("template ready");
	//modVP.play(); //autoplay
}

function onContentLoad(evt) {
	//var currentVideo = modVP.getCurrentVideo();
	//alert("INFO: Currently Loaded videoID: " + currentVideo.id);
}

function onVideoLoad(evt) {
}












function toggleFavorite(assetID) {
	$.post("/mediabox/actions/process-favorite.php", "assetID=" + assetID, function(data) {
		data = $.parseJSON(data);
		if(data.response.action == 'added') {
			$("#toggleFavorite").addClass("favoriteActive");
		}
		if(data.response.action == 'deleted') {
			$("#toggleFavorite").removeClass("favoriteActive");
		}
	});
}




function shareToggle(url) {

	var shareUtil = $("#shareUtil");
	var box = $("#shareUtil input");
	var state = shareUtil.css("display");
	
	
	if(state == "none")
		shareUtil.css("display", "inline");
	else
		shareUtil.css("display", "none");
		
		
	if(box.val() == "getting code...") {
		//get bit.ly
		$.post("/mediabox/utils/shorten-url.php", "url=" + escape(url), function(data) {
			box.val(data);
			box.focus();
			box.select();
		});
	}
	
}




function addToCart(assetID, el) {
	$.post("/mediabox/actions/process-cart.php", "assetID=" + assetID + "&action=add", function(data) {
		var button = $(el);
		button.val("Added");
		button.attr("disabled", "disabled");
	});
}




function removeFromCart(assetID, el) {
	$.post("actions/process-cart.php", "assetID=" + assetID + "&action=removeItem", function(data) {
		var li = $(el).parent().parent();
		
		data = $.parseJSON(data);
		if(!data.response.total)
			data.response.total = 0;
		
		//fade out
		li.css({
			overflow:"hidden"
		}).animate({
			opacity:0,
			height:0,
			paddingTop:0,
			paddingBottom:0
		}, "", function() {
			$("#miniCartBody").load("views/mini-cart.php");
			$("#checkoutPage #total").html("$" + data.response.total);
			$("#checkoutPage #totalField").val(data.response.total);
		});
	});
}




function initMiniCart() {

	var miniCart = $("#miniCartContainer");
	var miniCartBody = $("#miniCartBody");
	var trigger = $("#miniCartTrigger");
	var visible = false;
	var hideDelay = 300;
	var hideDelayTimer = null;

	trigger.click(function() {

		//show it
		if(isMSIE())
			miniCart.show();
		else
			miniCart.fadeIn();
		
		miniCartBody.html('<div class="spinner"></div>');
		
		//load up the minicart
		miniCartBody.load("views/mini-cart.php", function() {
			//console.log("loaded");
		});
	});
	
	$([trigger, miniCart]).each(function() {
		this.mouseover(function() {
			visible = true;
			clearTimeout(hideDelayTimer);
		});
	});
	
	$([trigger, miniCart]).each(function() {
		//return;
		this.mouseout(function() {
			visible = false;
			
			hideDelayTimer = setTimeout(function() {
				if(!visible) {
					if(isMSIE())
						miniCart.hide();
					else
						miniCart.fadeOut("fast");
				}
			}, hideDelay);
		});
	});
	
}




function showLoginBox() {
	$.fancybox({
		type:"ajax",
		href:"views/login.php",
		scrolling:"no",
		overlayOpacity:0.8,
		overlayColor:"#000000"
	});
}



function logout() {
	$.post("actions/process-logout.php", null, function() {
		alert("You have been logged out.");
		goHome();
	});
}


