var playlist = {}
var cPlaylistItem = 0;
var titlestr = "";
var linkstr = "";
var embedstr = "";
var started = false;
var domain = "http://www.democratica.tv";
var streamtest = false;


function thisMovie(swf) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[swf];
	} else {
		return document[swf];
	}
};

function onStreamError(state,pr) {
	if (!streamtest) {
		if ((state == "load") && (pr == 0)) {
			alert("Stream Error!")
		}
	}
}

function getUpdate(typ,pr1,pr2,swf) {
	if((typ == "state")&&(pr1 != undefined)) {
		currentState = pr1;
		if (!started) {
			if ((pr1 == 2) && (cPlaylistItem==0)) {
				playlist_setcurrent("#videos", 0);
				playlist_currentinfo(0);
				started = true;
			}
		}
		if(currentState=="3") {
			cPlaylistItem += 1;
			playItem(cPlaylistItem);
			setTimeout("playpause()", 300)
		}
	}
};

function playItem(item_num) {
	if (playlist.length == 0) {return false}
	if ((item_num == null)||(item_num==playlist.length)){
		item_num = 0;
	}
	cPlaylistItem = item_num;
	if (playlist.length > 0) {
		thisMovie("mp").sendEvent("playitem", item_num);
		//plugin.content.findName("menuTitle").text = playlist[item_num].TITLE;
	} else {}
	playlist_setcurrent("#videos", item_num);
	playlist_currentinfo(item_num)
}

function prev() {
	thisMovie("mp").sendEvent("prev");
}

function next() {
	thisMovie("mp").sendEvent("next");
}

function playpause() {
	thisMovie("mp").sendEvent("playpause");
}

function stop() {
	thisMovie("mp").sendEvent("stop");
}

function volume(percent) {
	thisMovie("mp").sendEvent("volume", percent);
}


function playlist_setcurrent(el, item) {
	if (homepd) {
		$(".video").show("slow");
		$("#video_"+ item).hide("fast");
	} else {
		var element = el + " .video"
		var pl = $(element);
		if (playlist.length>1) {
			$(pl).removeClass("current_video");
			$($(pl)[item]).addClass("current_video");
		} else {
			$(pl).removeClass("current_video");
			$(pl).addClass("current_video");
		}
	}
}

function playlist_currentinfo(item) {
	if (homepd) {
		$("#currentvideo_title").hide("fast");
		$("#currentvideo_abstract").hide("fast");
		//$("#currentvideo_url").hide("fast");
		if ((playlist.length) && (playlist.length > 1)) {
			$("#currentvideo_title").html(playlist[item].TITLE);
			$("#currentvideo_abstract").html(playlist[item].ABSTRACT);
			//$("#currentvideo_url").html("<a href='" + playlist[item].URL + "'>Link diretto a questo video</a>")
		} else {
			$("#currentvideo_title").html(playlist.TITLE);
			$("#currentvideo_abstract").html(playlist.ABSTRACT);
			//$("#currentvideo_url").html("<a href='" + playlist.URL + "'>Link diretto a questo video</a>")
		}
		$("#currentvideo_title").show("slow");
		$("#currentvideo_abstract").show("slow");
		//$("#currentvideo_url").show("slow");
	} else {
		$("#clip_format").hide();
		$("#clip_title").hide();
		$("#clip_abstract").hide();
		$("#clip_note").hide();
		if ((playlist.length) && (playlist.length > 0)) {
			$("#clip_format").html(playlist[item].FORMAT_TITLE);
			$("#clip_title").html(playlist[item].TITLE);
			$("#clip_abstract").html(playlist[item].DATE_TEXT + " - " + playlist[item].ABSTRACT);
			$("#clip_note").html(playlist[item].NOTE);
		} else {
			$("#clip_format").html(playlist.FORMAT_TITLE);
			$("#clip_title").html(playlist.TITLE);
			$("#clip_abstract").html(playlist.DATE_TEXT + " - " + playlist.ABSTRACT);
		}
		$("#clip_format").show("slow");
		$("#clip_title").show("slow");
		$("#clip_abstract").show("slow");
		$("#clip_note").show("slow");
		titlestr = playlist[item].TITLE;
		linkstr = domain + "/video/" + playlist[item].ID;
		embedstr = '<object width="400" height="320">\n<param name="movie" value="' + domain + '/js/mediaplayer.swf"/>\n<param name="allowfullscreen" value="true">\n<param name="flashvars" value="config=' + domain + '/playerconfig.xml&file=' + domain + '/xml/embed/' + playlist[item].ID + '"/>\n<embed src="' + domain + '/js/mediaplayer.swf" type="application/x-shockwave-flash" flashvars="config=' + domain + '/playerconfig.xml&file=' + domain + '/xml/embed/' + playlist[item].ID + '" allowfullscreen="true" width="400" height="320"/>\n</object>';
		$("#menu_title").html(titlestr);
		$("#menu_link").val(linkstr);
		$("#menu_embed").val(embedstr);
	}
}

function media_menuon() {
	$("#iebug").css("display","block");
	$("#mediaplayer_menu").css("display","block");
}

function media_menuoff() {
	$("#iebug").css("display","none");
	$("#mediaplayer_menu").css("display","none");
}



$(function() {
if (!is_homepd) {
	position = playlist_position;
	playlistmode = true;
	autoplay = true;
	homepd = is_homepd;
	$.getJSON(playlist_json, function(data) {
		p = data["VIDEOS"];
		playlist = p;
	})
	var flashvars = {
		'width':'400',
		'height':'320',
		'displaywidth':'400',
		'displayheight':'300',
		'enablejs':'true',
		'bufferlength':'3',
		'shuffle':'false',
		'autostart':'true',
		'backcolor':'0x000000',
		'frontcolor':'0x888888',
		'lightcolor':'0x006600',
		'linktarget':'_blank',
		'file': playlist_xml
	};
	var params = {
		/*wmode: 'transparent',*/
		bgcolor: '0x000000',
		allowfullscreen: 'true',
		allowscriptaccess: 'always',
		menu: "false"
	};
	if (!($.browser.msie)) {
		params.wmode = "transparent";
	}
	var attributes = {
		id: 'mp'
	};
	swfobject.embedSWF("/js/mediaplayer.swf", "mplayer", "400", "320", "8.0.0","", flashvars, params, attributes);
	
	if (playlist_mode) {
		$('#videos').jScrollPane({showArrows:true});
	}
	
	$("#embedvideo").click(function() {
		media_menuon();
	});
	
	$("#mediaplayer_menu textarea").click(function() {
		this.focus();
		this.select();
	});
	$("#menu_close a").click(function() {
		media_menuoff()
	});
}
});

