/* Functions for the imageForm plugin popup */
function log(){

}


function preinit() {
	// Initialize
	tinyMCE.setWindowArg('mce_windowresize', false);

	// Import external list url javascript
	var url = tinyMCE.getParam("external_image_list_url");
	if (url != null) {
		// Fix relative
		if (url.charAt(0) != '/' && url.indexOf('://') == -1)
			url = tinyMCE.documentBasePath + "/" + url;

		document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></sc'+'ript>');
	}
}

function init() {
	tinyMCEPopup.resizeToInnerSize();
	var formObj = document.getElementById('videoForm');

	var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id'));
	var elm = inst.getFocusElement();
	var action = "insert";
	var select_player = formObj.select_player;
	var html = "";

	if(tinyMCE.getAttrib(elm, 'alt')){
		var sel = tinyMCE.getAttrib(elm, 'alt');
		formObj.flvplayer.value = sel;
		var ops = select_player.options;
		for (var i=0; i<ops.length; i++)
			if(ops[i].value == sel)
				ops[i].selected = true;
	} else
		formObj.flvplayer.value = select_player.options[0].value;

	var attr = tinyMCE.getAttrib(elm, 'attr');
	var width = tinyMCE.getAttrib(elm, 'width');
	var height = tinyMCE.getAttrib(elm, 'height');
	formObj.static_width.value = width;
	formObj.static_height.value = height;
	//log(attr);
	var vars = attr.split('&');
	var playlist = false;
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		//log(pair[0]);
		switch(pair[0]){
		case "hideControls":
			formObj.hidecontrols.checked = true;
			break;
		case "autoplay":
			formObj.autoplay.checked = true;
			break;
		case "hitPlay":
			formObj.hitplay.checked = true;
			break;
		case "hitText":
			formObj.hittext.value = pair[1];
			break;
		case "plId":
			//log("plID lšyty: "+pair[1]);
			playlist = true;
			setPlaylist(pair[1]);
			break;
		case "staticSize":
			formObj.static_size.checked = true;
			break;
		}
	}



	formObj.movie_width.value = tinyMCE.getAttrib(elm, 'width');
	formObj.movie_height.value = tinyMCE.getAttrib(elm, 'height');


}

function insertAction() {

	var formObj = document.forms[0];
	var html      = '';
	var attr      = '';
	var file      = formObj.flvplayer.value;
	var width     = formObj.movie_width.value;
	var height    = formObj.movie_height.value;
	var movie_id = formObj.smovie.value;

	if (width == "")
		width = 320;

	if (height == "")
		height = 240;

	if(formObj.static_size.checked){
		width = formObj.static_width.value;
		height = formObj.static_height.value;
	}
	//log("10:43");

	attr = 'videoID='+movie_id;
	attr += '&url='+formObj.baseurl.value;
	if(formObj.hidecontrols.checked)
		attr += "&hideControls=1";
	if(formObj.autoplay.checked)
		attr += "&autoplay=1";
	if(formObj.hitplay.checked && !formObj.autoplay.checked){
		attr += '&hitPlay=1';
		attr += '&hitText='+formObj.hittext.value;
	}
	if(formObj.static_size.checked){
		attr += '&staticSize=1';
	}
	attr += "&cfgFile="+formObj.select_cfg.value;

	attr += "&baseUrl=flvplayers/";

	html += ''
		+ '<img src="'+formObj.baseurl.value+'binary/image/thumbnailer/-/vid/'+movie_id+'/width/' + width + '/height/' + height + '" mce_src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" '
		+ 'width="' + width + '" height="' + height + '" '
		+ 'border="0" alt="' + file + '" title="' + file + '" class="mceItemFlash" attr="' + attr + '"/>';

	tinyMCEPopup.execCommand("mceInsertContent", true, html);
	tinyMCE.selectedInstance.repaint();

	tinyMCEPopup.close();
}

function updateEmbed() {
	var embed = '';
	var formObj = document.forms[0];
	var width     = formObj.movie_width.value;
	var height    = formObj.movie_height.value;
	//var url = document.URL;
	//url = url.substring(0, url.lastIndexOf("?"));
	//url = url.substring(0, url.lastIndexOf("/"));
	var url = formObj.baseurl.value;
	//log(url);
	var file      = url+formObj.flvplayer.value;
	var movie_id = formObj.smovie.value;

	attr = 'videoID='+movie_id;

	if(formObj.hidecontrols.checked)
		attr += "&hideControls=1";
	if(formObj.autoplay.checked)
		attr += "&autoplay=1";
	if(formObj.hitplay.checked && !formObj.autoplay.checked){
		attr += '&hitPlay=1';
		attr += '&hitText='+formObj.hittext.value;
	}
	if(!formObj.no_playlist.checked){
		attr += '&plId='+formObj.select_playlist.value;
		attr += '&getPlUrl='+formObj.getplurl.value;
		//log('use playlist');
	}

	attr += "&cfgFile="+formObj.select_cfg.value;

	attr += "&baseUrl="+url+"/flvplayers/";

	embed += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	embed += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"';
	embed += ' width="' + width + '" height="' + height + '">';
	embed += '<param name="movie" value="' + file + '" />';
	embed += '<param name="quality" value="high" />';
	embed += '<param name="menu" value="false" />';
	embed += '<param name="wmode" value="" />';
	embed += '<param name="salign" value="lt" />';
	embed += '<param name="FlashVars" value="' + attr + '" />';
	embed += '<embed src="' + file  + '" FlashVars="' + attr + '" wmode="" salign="lt" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed></object>';


	formObj.embed_text.value = embed;
}


function cancelAction() {
	tinyMCEPopup.close();
}

// While loading
preinit();
