var Movies = {
	form          : null,
	parent        : null,
	iterator      : 0,
	limit         : 2,
	id            : null,
	oncomplete    : null,
	dAlignX       : ((document.documentElement.offsetWidth/2) - (200/2)),
	dAlignY       : (((document.documentElement.offsetHeight+document.documentElement.scrollTop)/2)-100),
	movieslist    : new Array(),
	listpath      : "",
	httphost      : "",
	pagination    : null,
	viewmovie     : null,
	categoryCombo : null,
	catloaded     : false,
	usecategory   : false,
	linksloaded   : 0,
	category      : "",
	site          : 0,
	main          : false,
	info          : true,
	XMLfile       : "",
	vWidth        : 200,
	vHeight       : 200,
	
	request : function () {
		if(!Movies.checkExists()) {
			if(Movies.oncomplete != null)
				eval(Movies.oncomplete);
				
			return;
		}
		
		if(Movies.id != null)
			insertHTML(Movies.form, "beforeEnd", '<input type="hidden" name="id" id="id" value="'+Movies.id+'"/>');
		
		Movies.waiting();
		Movies.form.submit();
	},
	
	waiting : function (state) {
		Loading.show("Enviando Vídeos", "Aguarde enquanto o sistema grava os vídeos.", Movies.dAlignX, Movies.dAlignY, true);
	},
	
	parseURL : function (url) {
		return url.split("&")[0];
	},
	
	requestGalery : function (id, flag) {
		Ajax.loadXMLDoc('engine/movie.galery.control.php', "id="+id+"&flag="+flag, 'POST', {fn : 'Movies.responseGalery'}, true, {fn : 'Movies.waitingGalery'});
	},
	
	waitingGalery : function (state) {
		if(state < 4)
			Loading.show("Ajustando galeria de Vídeos", "Aguarde enquanto o sistema ajusta a geleria de vídeos.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseGalery : function (xmldoc, textdoc, status) {
		if(status != 200)
			return;
			
		this.iterator = 0;
		Movies.list(this.category);
	},
	
	requestXML : function (id) {
		var params = "";
		
		var file = this.listpath+"/"+id+".flv";
		Ajax.loadXMLDoc('engine/movie.xml.add.php', "path="+file+"&id="+id+"&xml=../"+this.XMLfile, 'POST', {fn : 'Movies.responseXML'}, true, {fn : 'Movies.waitingXML'});
	},
	
	waitingXML : function (state) {
		if(state < 4)
			Loading.show("Adicionando Vídeo", "Aguarde enquanto o sistema adiciona o vídeo.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseXML : function (xmldoc, textdoc, status) {
		if(status != 200)
			return;
			
		Movies.sendClearXML();
	},
	
	response : function (reason, useXML) {
		Loading.hide();
		
		var phrases = {0 : { icon : 'success', title : 'Sucesso', text : 'Vídeos gravados com sucesso !'}, 
					   1 : { icon : 'error', title : 'Problemas na gravação dos vídeos.', text : 'Ocorreu um erro ao gravar os vídeos.' }};
		
		if(reason > 0)
			reason = 1;
		
		createDialog('joinerror', document.body, false);
		joinerror.setTitleText(phrases[reason].title);
		preloader('images/icons/'+phrases[reason].icon+'.gif');
		joinerror.setTitleIcon('images/icons/'+phrases[reason].icon+'.gif');
		joinerror.setX((document.documentElement.offsetWidth/2) - (300/2));
		joinerror.setY(document.documentElement.scrollTop+100);
		
		joinerror.setW(300);
		joinerror.setH(10);
		joinerror.setContent('<div class="NewsAdd"><div class="titleerror">'+phrases[reason].text+'</div></div>');
		joinerror.setCloseButton(true);
		joinerror.setButtonsBar('<center><input type="button" onclick="joinerror.hide(); Movies.sendClear()" value="OK" /></center>');
		joinerror.setCloseByESC(true);
		
		if(Movies.oncomplete != null)
			joinerror.setActionOnHide(Movies.oncomplete);
		
		joinerror.setCloseAndDestroy(true);
		joinerror.setTransparency(0.95);
		joinerror.setMovable(true);
		joinerror.setModal(true);
		joinerror.show();	
	},
	
	requestRemoveCategory : function () {
		var category = Movies.categoryCombo.value;
		if(category == "" || category == 0)
			return;
		
		if(!confirm("Confirma a exclusão desta categoria?\nATENÇÃO: Serão apagadas todas as fotos que estão dentro desta categoria."))
			return;
		
		Movies.categoryCombo.disabled = true;
		var params = "category="+encodeURIComponent(category);
		Ajax.loadXMLDoc('engine/movie.category.remove.php', params, 'POST', {fn : 'Movies.responseRemoveCategory', params : category}, true, {fn : 'Movies.waitingResponseRemoveCategory'});
	},
	
	waitingResponseRemoveCategory : function (state) {
		obj = Movies.categoryCombo;		
		obj.options[0].text = 'Removendo Categoria. Aguarde...';
		obj.options[0].value = 0;
		if(state == 4) {
			obj.disabled = false;			
			obj.options[0].text = ':: Selecione';
		}
	},
	
	responseRemoveCategory : function  (xmldoc, textdoc, status, category) {
		if(status != 200) {
			alert("Erro ao remover categoria.")
			return;
		}
		
		for(var i=0; i<Movies.categoryCombo.options.length; i++) {
			if(Movies.categoryCombo.options[i].value == category)
				Movies.categoryCombo.options[i] = null;
		}
		
		Movies.categoryCombo.disabled = false;
	},	
	
	requestCotegoriesList : function (path, pValue) {
		var value = (typeof pValue != "undefined" ? pValue : "");
		this.clearCombo();
		Ajax.loadXMLDoc('engine/movie.category.list.php', "path="+escape(path), 'POST', {fn : 'Movies.responseCotegoriesList', params : value}, true, {fn : 'Movies.waitingResponseCotegoriesList'});
	},
	
	waitingResponseCotegoriesList : function (state) {
		obj = Movies.categoryCombo;		
		obj.options[0].text = 'Carregando Categorias. Aguarde...';
		obj.options[0].value = 0;
		if(state == 4) {
			obj.disabled = false;			
			obj.options[0].text = ':: Selecione';
		}
	},
	
	responseCotegoriesList : function (xmldoc, textdoc, status, value) {
		if(status != 200)
			return;
		
		if(!xmldoc)
			return;
		
		var child;
		var obj = Movies.categoryCombo;
		
		obj.disabled = false;
		var y=1;
		for(var i=0; i<xmldoc.childNodes.length; i++) {
			child = xmldoc.childNodes[i];
			if(child.nodeType != 1)
				continue;
				
			var name = child.getAttribute('name');
			name = name.split("[")[0];

			obj.options[y] = new Option(name);
			obj.options[y].value = child.getAttribute('name');
			if(value == child.getAttribute('name'))
				obj.options[y].selected = true;
				
			y++;
		}
		
		Movies.catloaded = true;
		if(Movies.toAdd)
			Movies.add();
	},	
	
	sendClear : function () {
		if(this.parent)
			this.parent.innerHTML = "";
		
		this.iterator = 0;
		this.add();
	},
	
	sendClearXML : function () {
		if(this.parent)
			this.parent.innerHTML = "";
		
		this.iterator = 0;
		Movies.list();
	},
	
	requestCategory : function () {
		if(!Movies.checkCategoryExists()) {
			if(Movies.oncomplete != null)
				eval(Movies.oncomplete);
				
			return;
		}
		
		Movies.waitingCategory();
		Movies.form.submit();
	},
	
	waitingCategory : function (state) {
		Loading.show("Criando Categorias", "Aguarde enquanto o sistema cria as categorias.", Movies.dAlignX, Movies.dAlignY, true);
	},
	
	responseCategory : function (reason) {
		Loading.hide();
		
		var phrases = {0 : { icon : 'success', title : 'Sucesso', text : 'Categorias criadas com sucesso !'}, 
					   1 : { icon : 'error', title : 'Problemas na criação das categorias.', text : 'Ocorreu um erro ao criar as categorias.' }};
		
		if(reason > 0)
			reason = 1;
		
		createDialog('joinerror', document.body, false);
		joinerror.setTitleText(phrases[reason].title);
		preloader('images/icons/'+phrases[reason].icon+'.gif');
		joinerror.setTitleIcon('images/icons/'+phrases[reason].icon+'.gif');
		joinerror.setX((document.documentElement.offsetWidth/2) - (300/2));
		joinerror.setY(document.documentElement.scrollTop+100);
		
		joinerror.setW(300);
		joinerror.setH(10);
		joinerror.setContent('<div class="NewsAdd"><div class="titleerror">'+phrases[reason].text+'</div></div>');
		joinerror.setCloseButton(true);
		joinerror.setButtonsBar('<center><input type="button" onclick="joinerror.hide(); Movies.sendCategoryClear()" value="OK" /></center>');
		joinerror.setCloseByESC(true);
		
		if(Movies.oncomplete != null)
			joinerror.setActionOnHide(Movies.oncomplete);
		
		joinerror.setCloseAndDestroy(true);
		joinerror.setTransparency(0.95);
		joinerror.setMovable(true);
		joinerror.setModal(true);
		joinerror.show();
	},
	
	clearCombo : function () {
		obj = Movies.categoryCombo;
		obj.disabled = true;
		var count = obj.options.length;
		for(var i=count-1; i>0; i--) {
			obj.options[i] = null;
		}
		obj.options[0].selected = true;
	},	
	
	requestRemove : function (id) {
		if(!confirm("Confirma a exclusão deste vídeo ?"))
			return;
		
		var params = "id="+id;
		Ajax.loadXMLDoc('engine/movie.remove.php', params, 'POST', {fn : 'Movies.responseRemove', params : id}, true, {fn : 'Movies.waitingResponseRemove'});
	},
	
	waitingResponseRemove : function (state) {
		if(state < 4)
			Loading.show("Removendo Vídeo", "Aguarde enquanto o sistema remove o vídeo.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseRemove : function  (xmldoc, textdoc, status, id) {
		if(status != 200) {
			alert("Erro ao remover vídeo.")
			return;
		}
		
		var element = document.getElementById("movie("+id+")");
		try {
			element.parentNode.removeChild(element);
			for(var i=0; i<Movies.movieslist.length; i++) {
				if(Movies.movieslist[i].id == id) {
					Movies.movieslist.splice(i, 1);
					break;
				}
			}
			
			Movies.requestRemoveXML(id);
			
			if(Movies.movieslist.length == 0)
				Movies.showEmptyPhrase();
		}
		catch(o){}
	},
	
	showEmptyPhrase : function () {
		if(!Movies.parent)
			return;
		Movies.parent.innerHTML = "Não exitem vídeos cadastrados.";			
	},	
	
	list : function (category, path, engine) {
		if(typeof category != "undefined" && typeof path != "undefined") {
			if(category == 0)
				return;
			
			this.category = category;
			this.listpath = path+category;
		}		
		
		this.requestMovies(this.listpath, (typeof engine != "undefined" ? engine : null));
	},
	
	requestMovies : function (path, engine) {
		this.usecategory = false;
		Movies.movieslist = new Array();
		this.clearArea();
		var params = "path="+escape(path);
		Ajax.loadXMLDoc((engine != null ? engine :'engine/movie.list.php'), params, 'POST', {fn : 'Movies.responseMovies'}, true, {fn : 'Movies.waitingResponseMovies'});
	},
	
	galeryList : function (path) {
		this.requestGaleryMovies(path);
	},
	
	requestGaleryMovies : function (path) {
		this.usecategory = true;
		Ajax.loadXMLDoc('../intranet/engine/movie.galery.list.php', 'site='+this.site, 'POST', {fn : 'Movies.responseMovies'}, true, {fn : 'Movies.waitingResponseMovies'});
	},	
	
	waitingResponseMovies : function (state) {
		if(typeof Loading != "undefined") {
			if(state < 4)
				Loading.show("Carregando Vídeos", "Aguarde enquanto o sistema carrega os vídeos.", Movies.dAlignX, Movies.dAlignY, true);
			else
				Loading.hide();
		}
	},
	
	responseMovies : function  (xmldoc, textdoc, status) {
		if(status == 404) {
			Movies.showEmptyPhrase();
			return;
		}
		
		if(status != 200 || !xmldoc) {
			alert("Erro ao carregar vídeos.")
			return;
		}

		var childs = xmldoc.childNodes;
		for(var i=0; i<childs.length; i++) {
			var child = childs[i];
			if(child.nodeType != 1)
				continue;
			
			Movies.movieslist.push({id : child.getAttribute("id"),
									pic : child.getAttribute("psrc"),
									main : child.getAttribute("main"),
									caption : child.getAttribute("caption"),
									size : child.getAttribute("size"),
									galery : child.getAttribute("galery"),
									category : child.getAttribute("category"),
									date : child.getAttribute("date")});
		}
		
		if(!Movies.pagination) {
			createPagination('Movies.pagination', document.getElementById('moviespgarea'));
			Movies.pagination.setStack(Movies.movieslist);
			Movies.pagination.setUpdateAction('Movies.update');
			Movies.pagination.setNumberOfLinks(5);
			Movies.pagination.setNumRegsByPage(16);
			Movies.pagination.paint();
			Movies.update();
		}
		else
			Movies.paintMovies();
	},
	
	viewMovie : function (src) {
		createDialog('Movies.viewmovie', document.body, false);
		Movies.viewmovies.setTitleText("Exibindo Vídeo");
		Movies.viewmovies.setX((document.documentElement.offsetWidth/2) - (300/2));
		Movies.viewmovies.setY(document.documentElement.scrollTop+100);
		
		var html = "<embed type='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='../imagens/videos/"+src+"' width='356' height='310' displaysize='4' name='MediaPlayer1' autosize='true' showcontrols='true' showstatusbar='1' id='movieviwed'  enablecontextmenu='0' invokeurls='false' autostart='True' ></embed>";
		
		Movies.viewmovies.setW(356);
		Movies.viewmovies.setH(310);
		Movies.viewmovies.setContent(html);
		Movies.viewmovies.setCloseButton(true);
		Movies.viewmovies.setCloseByESC(true);
		if(isBrowserVersion("msie"))
			Movies.viewmovies.setActionOnHide("document.getElementById(\'movieviwed\').stop()");
		Movies.viewmovies.setCloseAndDestroy(true);
		Movies.viewmovies.setTransparency(0.95);
		Movies.viewmovies.setModal(true);
		Movies.viewmovies.show();			
	},
	
	requestRemoveXML : function (id) {	
		var file = this.listpath+"/"+id+".flv";
		var params = "file="+file+"&xml=../"+this.XMLfile+"&id="+id;
		Ajax.loadXMLDoc('engine/movie.xml.remove.php', params, 'POST', {fn : 'Movies.responseRemoveXML', params : id}, true, {fn : 'Movies.waitingResponseRemoveXML'});
	},
	
	waitingResponseRemoveXML : function (state) {
		if(state < 4)
			Loading.show("Removendo Vídeo", "Aguarde enquanto o sistema remove o vídeo.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseRemoveXML : function  (xmldoc, textdoc, status, id) {
		if(status != 200) {
			alert("Erro ao remover vídeo.")
			return;
		}
		
		Movies.list();
	},	
	
	
	/*listXML : function (path) {
		this.XMLURL = path;
		this.requestMoviesXML(path);
	},
	
	requestMoviesXML : function (path) {
		Movies.linksloaded = 0;
		Movies.movieslist = new Array();
		this.clearArea();
		Ajax.loadXMLDoc(path, '', 'GET', {fn : 'Movies.responseMoviesXML'}, true, {fn : 'Movies.waitingResponseMoviesXML'});
	},
	
	waitingResponseMoviesXML : function (state) {
		if(state < 4)
			Loading.show("Carregando Vídeos", "Aguarde enquanto o sistema carrega os vídeos.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseMoviesXML : function  (xmldoc, textdoc, status) {
		if(status == 404) {
			Movies.showEmptyPhrase();
			return;
		}
		
		if(status != 200 || !xmldoc) {
			alert("Erro ao carregar vídeos.")
			return;
		}	
		
		var tracklist = xmldoc.getElementsByTagName('trackList')[0];
		var tracks = tracklist.getElementsByTagName('track');
		for(var i=0; i<tracks.length; i++) {
			var track = tracks[i];
			if(track.nodeType > 1)
				continue;
				
			var title    = track.getElementsByTagName('title')[0];
			var creator  = track.getElementsByTagName('creator')[0];
			var location = track.getElementsByTagName('location')[0];
			var info     = track.getElementsByTagName('info')[0];
			var image    = track.getElementsByTagName('image')[0];

			Movies.movieslist.push({title    : (title.firstChild ? title.firstChild.data : ""),
								    creator  : (creator.firstChild ? creator.firstChild.data : ""),
									location : (location.firstChild ? location.firstChild.data : ""),
									info     : (info.firstChild ? info.firstChild.data : ""),
									image    : (image.firstChild ? image.firstChild.data : "")});
		}
		
		Movies.requestEmbedUrl(Movies.linksloaded);
	},
	
	requestEmbedUrl : function (i) {
		Ajax.loadXMLDoc('engine/movie.getembed.php', 'url='+encodeURIComponent(Movies.movieslist[i].location), 'POST', {fn : 'Movies.responseEmbedUrl'}, true, {fn : 'Movies.waitingResponseEmbedUrl'});
	},
	
	waitingResponseEmbedUrl : function (state) {
		if(state < 4)
			Loading.show("Carregando Links", "Aguarde enquanto o sistema carrega os links dos vídeos.", Movies.dAlignX, Movies.dAlignY, true);
		else
			Loading.hide();
	},
	
	responseEmbedUrl : function  (xmldoc, textdoc, status) {
		if(status != 200 || !xmldoc) {
			alert("Erro ao carregar vídeos.")
			return;
		}
		
		var vlink = xmldoc.getAttribute("value");
		Movies.movieslist[Movies.linksloaded].link = vlink;
		
		if(Movies.linksloaded == Movies.movieslist.length - 1) {
			Movies.linksloaded = 0;
			if(!Movies.pagination) {
				createPagination('Movies.pagination', document.getElementById('moviespgarea'));
				Movies.pagination.setStack(Movies.movieslist);
				Movies.pagination.setUpdateAction('Movies.updateXML');
				Movies.pagination.setNumberOfLinks(5);
				Movies.pagination.setNumRegsByPage(16);
				Movies.pagination.paint();
				Movies.updateXML();
			}
			else
				Movies.paintMoviesXML();
		}
		else 
			Movies.requestEmbedUrl(++Movies.linksloaded);
	},		*/
	
	update : function () {
		Movies.paintMovies();
		Movies.pagination.updateNavigation();
	},	
	
	/*updateXML : function () {
		Movies.parent.innerHTML = Movies.listXMLToHTML();
		Movies.pagination.updateNavigation();
	},*/
	
	sendCategoryClear : function () {
		if(this.parent)
			this.parent.innerHTML = "";
		
		this.iterator = 0;
		this.addCategory();
	},
	
	removeCategory : function () {
		if(Movies.iterator <= 0)
			return;
		
		Movies.iterator--;
		
		var element = document.getElementById('category('+Movies.iterator+')');
		if(!element)
			return;
			
		element.parentNode.removeChild(element);
		
		Movies.updateButtons();
	},
	
	addCategory : function () {
		if(!Movies.parent)
			return;
			
		if(Movies.iterator >= Movies.limit)
			return;

		insertHTML(Movies.parent, "beforeEnd", Movies.categoryToHTML(Movies.iterator));
		Movies.iterator++;
		
		Movies.updateButtons();
	},	
	
	paintMovies : function () {
		if(!Movies.parent)
			return 0;

		Movies.parent.innerHTML = this.listToHTML();
		for(var i=(Movies.pagination && typeof Movies.pagination.getStart != 'undefined' ? Movies.pagination.getStart() : 0); i<=(Movies.pagination && typeof Movies.pagination.getEnd != 'undefined' ? Movies.pagination.getEnd() : Movies.movieslist.length); i++) {	
		//for(var i=0; i<Movies.movieslist.length; i++) {
			var movie = Movies.movieslist[i];
			if(!movie)
				continue;
			if(!document.getElementById('movieplayer('+movie.id+')'))
				return 0;
			
			this.loadMovie(movie.id, '../arquivos/videos/'+(this.usecategory ? movie.category : this.category)+'/'+movie.id+'.flv', this.vWidth, this.vHeight);
		}
		
		return 0;
	},
	
	paintMoviesXML : function () {
		if(!Movies.parent)
			return;
		
		insertHTML(Movies.parent, "beforeEnd", this.listXMLToHTML());
	},
	
	categoryToHTML : function (pIndex) {
		var html
		= '<div id="category('+pIndex+')" style="border-bottom:1px solid #CCC; padding-bottom:10px; margin-right: 10px;">'
		  + 'Nome:<br />'
		  + '<input name="category[]" type="text" size="80" maxlength="50" />'
		  + '<input name="site" type="hidden" value="'+this.site+'" />'
		+ '</div>';
	
		return html;
	},	
	
	/*makeEmbed : function (url) {
		var html = '';
		html += '<object width="200" height="175>';
		html += '  <param name="movie" value="'+url+'"></param>';
		html += '  <param name="allowFullScreen" value="true"></param>';
		html += '  <embed src="'+url+'" type="application/x-shockwave-flash" allowfullscreen="true" width="200" height="175"></embed>';
		html += '</object>';
		
		return html;
	},*/
	
	listXMLToHTML : function () {
		var control = 0;
		var html = "";
		for(var i=(Movies.pagination && typeof Movies.pagination.getStart != 'undefined' ? Movies.pagination.getStart() : 0); i<=(Movies.pagination && typeof Movies.pagination.getEnd != 'undefined' ? Movies.pagination.getEnd() : Movies.stack.length); i++) {	
			var movie = Movies.movieslist[i];
			if(!movie)
				continue;
			
			control++;
			html += '<div style="margin:5px 0px 10px 30px; float:left" id="movie('+Movies.location+')">';
			html += '  <div>';
			html += 	 this.makeEmbed(Movies.link);
			html += '  </div>';
			html += '  <div>';
			html += 	 Movies.title;
			html += '  </div>';
			html += '  <div>';
			html += '    | <a href="javascript:Movies.requestRemoveXML(\''+Movies.location+'\')">Excluir</a> |';
			html += '  </div>';
			html += '</div>';

			if(control == 2) {
				html += '<br clear="all" />';
				control = 0;
			}
		}
		html += '<br clear="all" /><br />';
		return html;
	},	
	
	mainControl : function (pObj, id) {
		if(pObj.checked)
			this.requestXML(id);
		else {
			if(!confirm("Confirma a remoção deste vídeo da página principal?")) {
				pObj.checked = true;
				return false;
			}
			
			this.requestRemoveXML(id);
		}
	},
	
	galeryControl : function (pObj, id) {
		if(pObj.checked)
			this.requestGalery(id, 1);
		else {
			if(!confirm("Confirma a remoção deste vídeo da galeria?")) {
				pObj.checked = true;
				return false;
			}
			
			this.requestGalery(id, 0);
		}
	},	
	
	listToHTML : function () {
		var control = 0;
		var html = "";
		//for(var i=0; i<Movies.movieslist.length; i++) {
		for(var i=(Movies.pagination && typeof Movies.pagination.getStart != 'undefined' ? Movies.pagination.getStart() : 0); i<=(Movies.pagination && typeof Movies.pagination.getEnd != 'undefined' ? Movies.pagination.getEnd() : Movies.stack.length); i++) {	
			var movie = Movies.movieslist[i];
			if(!movie)
				continue;
			
			control++;

			html += '<div style="margin:5px 0px 0px 5px; float:left" id="movie('+movie.id+')">';
			if(this.main) {
				html += '  <div>';
				html +=      '<input type="checkbox" id="main('+movie.id+')" onchange="Movies.mainControl(this, '+movie.id+')" '+(Number(movie.main) == 1 ? "checked" : "")+'> <label for="main('+movie.id+')" style="font-weight:bold">Página Inicial</label>';
				html += '  </div>';				
			}
			else if(this.site == 0 && this.info) {
				html += '  <div>';
				html +=      '<input type="checkbox" id="galery('+movie.id+')" onchange="Movies.galeryControl(this, '+movie.id+')" '+(Number(movie.galery) == 1 ? "checked" : "")+'> <label for="galery('+movie.id+')" style="font-weight:bold">Galeria</label>';
				html += '  </div>';
			}
			html += '  <div id="movieplayer('+movie.id+')"></div>';
			html += '  <div><b>';
			html +=      movie.caption;
			html += '  </div></b>';
			html += '  <div>';
			html += '    <b>Tamanho:</b> '+(((Number(movie.size)/1024)/1024).toPrecision(3))+" MB";
			html += '  </div>';
			html += '  <div>';
			html += '    <b>Data:</b> '+movie.date;
			html += '  </div>';
			if(this.info) {	
				html += '  <div>';
				html += '    | <a href="javascript:Movies.requestRemove(\''+movie.id+'\')">Excluir</a> |';
				html += '  </div>';
			}
			html += '</div>';

			if(control == 2) {
				html += '<br clear="all" />';
				control = 0;
			}
		}
		html += '<br clear="all" /><br />';
		return html;
	},
	
	loadMovie : function (id, movie, width, height) {
		var moviearea = document.getElementById('movie('+id+')');
		moviearea.style.width = width+'px';
		
		var s1 = new SWFObject("../videos/mediaplayer.swf","flvplayer",width,height,"0");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("file",movie);
		s1.addVariable("width",width);
		s1.addVariable("height",height);
		s1.addVariable("displayheight","280");
		s1.addVariable("autostart","false");
		s1.addVariable("wmode","transparent");
		s1.addVariable("scale","noscale");
		s1.addVariable("shuffle","false");
		s1.addVariable("thumbsinplaylist","false");		
		s1.addVariable("linkfromdisplay","false");
		s1.write('movieplayer('+id+')');
	},
	
	clearArea : function () {
		if(!Movies.parent)
			return;
		
		Movies.parent.innerHTML = "";
	},
	
	setVWidth : function (p) {
		this.vWidth = p;
	},
	
	setVHeight : function (p) {
		this.vHeight = p;
	},	
	
	setIterator : function (pInt) {
		Movies.iterator = pInt;
		Movies.updateButtons();
	},
	
	setLimit : function (pInt) {
		Movies.limit = pInt;
		Movies.updateButtons();
	},
	
	setInfo : function (p) {
		this.info = p;
	},
	
	setID : function (pId) {
		Movies.id = pId;
	},
	
	setSite : function (p) {
		this.site = p;
	},
	
	setXMLfile : function (p) {
		this.XMLfile = p;
	},	
	
	setMain : function (p) {
		this.main = p;
	},	
	
	setForm : function (pForm) {
		if(typeof pForm != "object")
			pForm = document.getElementById(pForm);
		
		Movies.form = pForm;
	},
	
	setParent : function (pParam) {
		if(typeof pParam != "object")
			pParam = document.getElementById(pParam);
		
		Movies.parent = pParam;
	},
	
	setCategoryCombo : function (pCombo) {
		if(typeof pCombo != "object")
			pCombo = document.getElementById(pCombo);
		
		this.categoryCombo = pCombo;
	},	
	
	checkExists : function () {
		var fields = document.getElementsByName("pictures[]");
		var found = false;
		for(var i=0; i<fields.length; i++) {
			if(fields[i].value.trim().length > 0) {
				found = true;
				break;
			}
		}
		
		if(found = true) {
			fields = document.getElementsByName("movies[]");
			found = false;
			for(var i=0; i<fields.length; i++) {
				if(fields[i].value.trim().length > 0) {
					found = true;
					break;
				}
			}
		}
		
		
		return found;
	},
	
	checkCategoryExists : function () {
		var fields = document.getElementsByName("category[]");
		var found = false;
		for(var i=0; i<fields.length; i++) {
			if(fields[i].value.trim().length > 0) {
				found = true;
				break;
			}
		}
		
		return found;
	},	
	
	updateButtons : function () {
		var btnadd = document.getElementById("btnaddmovie");
		var btnremove = document.getElementById("btnremovemovie");
		
		if(btnadd) {
			if(Movies.iterator < Movies.limit)
				btnadd.style.display = "";
			else
				btnadd.style.display = "none";
		}
		
		if(btnremove) {
			if(Movies.iterator > 1)
				btnremove.style.display = "";
			else
				btnremove.style.display = "none";
		}
	},
	
	remove : function () {
		if(Movies.iterator <= 0)
			return;
		
		Movies.iterator--;
		
		var element = document.getElementById('movie('+Movies.iterator+')');
		if(!element)
			return;
			
		element.parentNode.removeChild(element);
		
		Movies.updateButtons();
	},
	
	add : function () {
		if(!Movies.parent)
			return;
			
		if(Movies.iterator >= Movies.limit)
			return;

		insertHTML(Movies.parent, "beforeEnd", Movies.toHTML(Movies.iterator));
		Movies.iterator++;
		
		Movies.updateButtons();
	},
	
	addXML : function () {
		if(!Movies.parent)
			return;
			
		if(Movies.iterator >= Movies.limit)
			return;

		insertHTML(Movies.parent, "beforeEnd", Movies.xmlToHTML (Movies.iterator));
		Movies.iterator++;
		
		Movies.updateButtons();
	},
		
	xmlToHTML : function (pIndex) {
		var html
		= '<div id="movie('+pIndex+')" style="border-bottom:1px solid #CCC; padding-bottom:10px; margin-right: 10px;">'
		  + 'Vídeo '+(pIndex+1)+':<br />'
		  + '<input name="url" type="text" size="80" maxlength="255" /><br />'		  
		  /*+ '<input name="movies[]" nc="yes" type="file" size="50" /><br />'
		  + 'Imagem:<br />'
		  + '<input name="pictures[]" nc="yes" type="file" size="50" /><br />'	*/	  
		  + 'Título:<br />'
		  + '<input name="title" type="text" size="80" maxlength="255" /><br />'	  
		+ '</div>';
	
		return html;
	},	
	
		
	toHTML : function (pIndex) {
		var html
		= '<div id="movie('+pIndex+')" style="border-bottom:1px solid #CCC; padding-bottom:10px; margin-right: 10px;">'
		  + 'Vídeo '+(pIndex+1)+':<br />'
		  + '<input name="movies[]" nc="yes" type="file" size="50" /><br />'
		  + 'Imagem:<br />'
		  + '<input name="pictures[]" nc="yes" type="file" size="50" /><br />'		  
		  + 'Título:<br />'
		  + '<input name="captions[]" type="text" size="80" maxlength="255" /><br />'
		  + 'Data:<br />'
		  + '<input name="date[]" type="text" size="11" maxlength="10" />';
		  if(this.site == 0) {
		  	  html += '<br />'
		           +  '<input name="galery[]" type="checkbox" value="1" checked /> Galeria';
		  }
		
		html += '</div>';
	
		return html;
	},
	
	init : function () {
		insertHTML(document.body, "beforeEnd", '<iframe name="movieUpload" id="movieUpload" src="dummy.html" style="display:none; width:300px; height:300px;"></iframe>');
	}
}