// Import theme specific language pack 
tinyMCE.importPluginLanguagePack('imgmanager', tinyMCE.settings['lang_list']);

// Returns the HTML contents of the ibrowser control.

function TinyMCE_imgmanager_getControlHTML(control_name) {
	switch (control_name) {
		case "imgmanager":
			return '<img id="{$editor_id}_imgmanager" src="{$pluginurl}/images/imgmanager.gif" title="{$lang_imgmanager_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceImageManager\', true);">';
	}
	return "";
}

// Executes the mceImgManager command.

function TinyMCE_imgmanager_execCommand(editor_id, element, command, user_interface, value) {

    function getAttrib(elm, name) {
		return elm.getAttribute(name) ? elm.getAttribute(name) : "";
	}
    var inst = tinyMCE.getInstanceById(editor_id);
    var focusElm = inst.getFocusElement();
    var imgElm = tinyMCE.getParentElement(focusElm, "img");
    var linkElm = tinyMCE.getParentElement(inst.getFocusElement(), "a");

    // Handle commands
	switch (command) {
		case "mceImageManager":
            var template = new Array();
                if (user_interface){

                    template['file'] = '../../plugins/imgmanager/ImageManager/manager.php'; // Relative to theme location
                    template['width'] = 700;
                    template['height'] = 520;
    			
        			var src = "", alt = "", border = "", hspace = "", vspace = "", width = "", height = "", align = "", title = "", onmouseover = "", onmouseout = "", href = "", caption = false, action = "insert";
                 
                if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "a") {
    				tinyMCE.linkElement = tinyMCE.selectedElement;
                }
                if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "img") {
    				tinyMCE.imgElement = tinyMCE.selectedElement;
                }
                if (tinyMCE.imgElement) {
                        // Is selection a image
                        if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
    				        name = getAttrib(focusElm, 'name');
    				    }
                        // Is it a internal MCE visual aid image, then skip this one.
                        if (name.indexOf('mce_') == 0)
    					return true;

                        src = tinyMCE.imgElement.getAttribute('src');
        				alt = tinyMCE.imgElement.getAttribute('alt');

        				// Try polling out the title
        				if (alt == "")
        					alt = tinyMCE.imgElement.getAttribute('title');
        					
                        // Fix width/height attributes if the styles is specified
        				if (tinyMCE.isGecko) {
        					var w = tinyMCE.imgElement.style.width;
        					if (w != null && w != "")
        						tinyMCE.imgElement.setAttribute("width", w);

        					var h = tinyMCE.imgElement.style.height;
        					if (h != null && h != "")
        						tinyMCE.imgElement.setAttribute("height", h);

        				}
        				// Is realy specified?
        				if (tinyMCE.isMSIE) {
        					width = tinyMCE.imgElement.attributes['width'].specified ? width : "";
        					height = tinyMCE.imgElement.attributes['height'].specified ? height : "";
        				}

        				border = tinyMCE.imgElement.getAttribute('border');
        				hspace = tinyMCE.imgElement.getAttribute('hspace');
        				vspace = tinyMCE.imgElement.getAttribute('vspace');
        				width = tinyMCE.imgElement.getAttribute('width');
        				height = tinyMCE.imgElement.getAttribute('height');
        				align = tinyMCE.imgElement.getAttribute('align');
        				title = tinyMCE.imgElement.getAttribute('title');

                        onmouseover = tinyMCE.imgElement.getAttribute('onmouseover');
                        onmouseout = tinyMCE.imgElement.getAttribute('onmouseout');

                        onmouseover = tinyMCE.cleanupEventStr(onmouseover);
        				onmouseout = tinyMCE.cleanupEventStr(onmouseout);
        				
                        // Fix for drag-drop/copy paste bug in Mozilla
        				mceRealSrc = getAttrib(tinyMCE.imgElement, 'mce_real_src');
        				if (mceRealSrc != "")
        					src = mceRealSrc;

        				src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, tinyMCE.imgElement);");
        				action = "update";
                    }
                    //Handle existing popup link
                    if (tinyMCE.linkElement && tinyMCE.imgElement){

                        if (name.indexOf('file') == 0)
    					return true;

                        href = tinyMCE.linkElement.getAttribute('href');
                        href = eval(tinyMCE.settings['urlconverter_callback'] + "(href, tinyMCE.linkElement);");

                        src = tinyMCE.imgElement.getAttribute('src');
                        alt = tinyMCE.imgElement.getAttribute('alt');
                        title = tinyMCE.imgElement.getAttribute('title');
                        width = tinyMCE.imgElement.getAttribute('width');
                        height = tinyMCE.imgElement.getAttribute('height');
                        border = tinyMCE.imgElement.getAttribute('border');
                        align = tinyMCE.imgElement.getAttribute('align');
                        hspace = tinyMCE.imgElement.getAttribute('hspace');
                        vspace = tinyMCE.imgElement.getAttribute('vspace');
                        onmouseover = tinyMCE.imgElement.getAttribute('onmouseover');
                        onmouseout = tinyMCE.imgElement.getAttribute('onmouseout');

                		onmouseover = tinyMCE.cleanupEventStr(onmouseover);
            			onmouseout = tinyMCE.cleanupEventStr(onmouseout);

                        action = "update";
               }
        }else{//Insert or Update image and/or popup
                phref = value['phref'];
                tcaption = value['tcaption'];
                tpopup = value['tpopup'];
                
                isrc = value['iSrc'];
                iborder = value['iBorder'];
                ialign = value['iAlign'];
                ivspace = value['iVspace'];
                ihspace = value['iHspace'];
                ialt = value['iName'];
                iwidth = value['iWidth'];
                iheight = value['iHeight'];
                ionmouseover = value['ionmouseover'];
                ionmouseout = value['ionmouseout'];

            if (tinyMCE.linkElement)//If there is a popup
            {
                if (tpopup == false) { //If we remove the popup
                    tinyMCE.execCommand("unlink",false,tinyMCE.imgElement);
                    tinyMCE.insertImage(isrc, ialt, iborder, ihspace, ivspace, iwidth, iheight, ialign, ialt, ionmouseover, ionmouseout);
                }
                if (tpopup == true) {//otherwise, update
                tinyMCE.insertLink(phref, target="");
                tinyMCE.insertImage(isrc, ialt, iborder, ihspace, ivspace, iwidth, iheight, ialign, ialt, ionmouseover, ionmouseout);
                }
            }
            if (tinyMCE.imgElement && !tinyMCE.linkElement) {//Just an image
                tinyMCE.insertImage(isrc, ialt, iborder, ihspace, ivspace, iwidth, iheight, ialign, ialt, ionmouseover, ionmouseout);
            }
            if (tinyMCE.imgElement && !tinyMCE.linkElement && tpopup == true)//Just an image, but we add a popup
            {
                tinyMCE.insertLink(phref, target="");
            }
            if ((!tinyMCE.imgElement && !tinyMCE.linkElement) && tpopup == true) {//Add a new popup image
                phref = unescape(phref);
                var html = '';
                if (tcaption == true){
                    html += '<table border="0" align="'+ialign+'" cellspacing="1" cellpadding="1"><tr><td><div align="center">';
                }
                html += '<a href="'+phref+'">';
                html += '<img src="' +isrc+ '" border="'+iborder+'" align="'+ialign+'" vspace="'+ivspace+'" hspace="'+ihspace+'" alt="'+ialt+'" title="'+ialt+'" width="'+iwidth+'" height="'+iheight+'" />';
                html += '</a>';
                if (tcaption == true ){
                    html += '</td></tr><tr><td><div align="center">'+ialt+'</div></td></tr></table>';
                }
                tinyMCE.execCommand("mceInsertContent",false,html);
            }
            if ((!tinyMCE.imgElement && !tinyMCE.linkElement) && tpopup == false){//Add a new plain image
                tinyMCE.insertImage(isrc, ialt, iborder, ihspace, ivspace, iwidth, iheight, ialign, ialt, ionmouseover, ionmouseout);
            }
          return true;
        }
        tinyMCE.openWindow(template, {editor_id : editor_id, src : src, alt : alt, border : border, hspace : hspace, vspace : vspace, width : width, height : height, align : align, onmouseover : onmouseover, onmouseout : onmouseout, href : href, caption : caption, action : action});
        return true;
    }
   // Pass to next handler in chain
   return false;
}
function TinyMCE_imgmanager_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {

	// No node provided
	if (node == null)
		return;
    var inst = tinyMCE.getInstanceById(editor_id);
    var focusElm = inst.getFocusElement();
    var imgElm = tinyMCE.getParentElement(focusElm, "img");
    var linkElm = tinyMCE.getParentElement(inst.getFocusElement(), "a");
    
    function getAttrib(elm, name) {
		return elm.getAttribute(name) ? elm.getAttribute(name) : "";
	}

    if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
        name = getAttrib(focusElm, 'name');
    }
    
    tinyMCE.switchClassSticky(editor_id + '_imgmanager', 'mceButtonNormal');

	do {
		if (name.indexOf('mce_') != 0 && node.nodeName.toLowerCase() == "img" || (name.indexOf('mce_') != 0 && node.nodeName.toLowerCase() == "a" && node.nodeName.toLowerCase() == "img" && linkElm.getAttribute('href') == "#"))
			tinyMCE.switchClassSticky(editor_id + '_imgmanager', 'mceButtonSelected');
	} while ((node = node.parentNode));
	return true;
}
