/**
 * Shows a loading.gif when an ajax loading process is going on
 *
 * @argument object jQueryObj A valid jQueryObject within which to show the animation
 */
function showLoadingAnimation(jq){
	jq.html("<div class=\"loading\" />");
};

/*Callback function before a form is submitted*/
function beforeFormSubmit (jFormData, jForm, jOptions){
	showLoadingAnimation($(jOptions.target));
	return true;
};

function onAjaxFormSubmit (linkForm){
	if (typeof $(linkForm).ajaxSubmit != "function") {
		alert("You need to include jquery.form.js to submit a form via ajax");
	} else {
		$(linkForm).ajaxSubmit({target:$(linkForm).parent(), beforeSubmit:beforeFormSubmit});
	}
	return false;
};

function remoteLoad (el, url){

	var jel; //jquery element

	if (typeof el == "string") {
		jel = $(el);
	} else {
		jel = el;
	}

	if (typeof jel.load == "function") {
		showLoadingAnimation(jel);
		jel.load(url);
	} else {
		alert("The element " + el + " doesn't seem to have a load method.\nEither jquery has not loaded up or your element "+ el +" is invalid");
	}
};

function extLinkCallback(responseText, textStatus, request){
	window.location = $(this).attr("href");
};

function makeScrollable (navigationDiv, slidesDiv, width, height, controller, slideWrapper, selectedWrapper, prevClass, nextClass){
	navigationDiv = $(navigationDiv);
	if (typeof navigationDiv.jFlow == "function") {
		navigationDiv.jFlow({
			slides: slidesDiv,
			controller: controller == null ? ".jFlowControl" : controller, // must be class, use . sign
			slideWrapper : slideWrapper == null ? "#jFlowSlide" : slideWrapper, // must be id, use # sign
			selectedWrapper: selectedWrapper == null ? "jFlowSelected" : selectedWrapper,  // just pure text, no sign
			width: width,
			height:height,
			duration: 400,
			prev: prevClass == null ? ".jFlowPrev" : prevClass, // must be class, use . sign
			next: nextClass == null ? ".jFlowNext" : nextClass // must be class, use . sign
		});
	}
};

function activateFlowEmbed(){
	if (typeof $("a.flowplayer").flowplayer == "function") {
		$("a.flowplayer").flowplayer("/flowplayers/flowplayer-3.1.4.swf");
	}
};

function embedFlowPlayer(elementId, flvPath, w, h, playerSwf){
	if (typeof flashembed == "function") {
		var playerConfig = {
			videoFile:flvPath,
			autoPlay:false,
			autoBuffering:false,
			protected:true,
			controlBarBackgroundColor:0,
			showMuteVolumeButton: false,
			showMenu:false,
			controlBarGloss:'medium'
		};
		if (playerSwf == null) playerSwf = "/flowplayers/flowplayer-3.1.4.swf";
		flashembed(elementId, {src:playerSwf, width:w, height:h}, {config:playerConfig});
	}
};

function embedPodcast(embedDiv, audioFile){
	if (typeof flashembed == "function") {

		var podcastConfig = {
			playerID:"podcast"+Math.round(Math.random()*100),
			bg:"0xf8f8f8",
			leftbg:"0xeeeeee",
			lefticon:"0x666666",
			rightbg:"0xcccccc",
			rightbghover:"0x999999",
			righticon:"0x666666",
			righticonhover:"0xFFFFFF",
			text:"0x666666",
			slider:"0x666666",
			track:"0xFFFFFF",
			border:"0x666666",
			loader:"0x9FFFB8",
			soundFile:escape(audioFile)
		};

		var flashvars="";
		for(var s in podcastConfig) flashvars += s + "=" + podcastConfig[s] + "&";

		var params = {
			src:"/flowplayers/onePixelPodcast.swf",
			width:290,
			height:24,
			version:[8,0],
			expressInstall:"/expressInstall.swf",
			seamlesstabbing:"true",
			wmode:"transparent",
			allowfullscreen:"false",
			allowscriptaccess:"always",
			allownetworking:"all",
			flashvars:flashvars
		};

		$("#"+embedDiv).flashembed(params);
	} else {
		alert("flashembed is not defined. Unable to embed podcast in " + embedDiv);
	}
};

function disableElement (el, onDocReady){
	if (onDocReady) {
		$(document).ready(function(){
			$(el).attr("disabled", true);
		});
	} else {
		$(el).attr("disabled", true);
	}
	return false;
}

$(document).ready(function() {
	activateFlowEmbed();
});
