// Import theme specific language pack 
tinyMCE.importPluginLanguagePack('caption', 'en');

function TinyMCE_caption_initInstance(inst) {
		tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/caption/caption.css");
}

// Returns the HTML contents of the caption control.

function TinyMCE_caption_getControlHTML(control_name) {
	switch (control_name) {
		case "caption":
			return '<img id="{$editor_id}_caption" src="{$pluginurl}/images/caption.gif" title="{$lang_caption_desc}" width="20" height="20" class="mceButtonDisabled" lass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" onclick="tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceCaption\', true);">';
	}
	return "";
}
// Executes the mceCaption command.

function TinyMCE_caption_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 capElm = tinyMCE.getParentElement(focusElm, "div");

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

                    template['file'] = tinyMCE.baseURL + '/plugins/caption/caption.php'; // Relative to theme location
                    template['width'] = 350;
                    template['height'] = 350;
    			
        			var caption_text = "", caption_align = "", caption_bgcolour = "", caption_bordercolour = "", caption_colour = "", caption_border_width = "", caption_border_style = "", caption_action = "", caption_style = "", caption_width = "", caption_height = "", action = "insert";

                if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "img") {
    				tinyMCE.imgElement = tinyMCE.selectedElement;
                }
                if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "a") {
    				tinyMCE.linkElement = tinyMCE.selectedElement;
                }

                if(tinyMCE.linkElement) {
                    tinyMCE.capElement = tinyMCE.getParentElement(tinyMCE.linkElement, "div");
                }else{
                    tinyMCE.capElement = tinyMCE.getParentElement(tinyMCE.imgElement, "div");
                }
                
               if(!tinyMCE.imgElement)
                    return true;

                if (tinyMCE.imgElement) {
                        // Is selection a image
                        if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
    				        name = getAttrib(focusElm, 'name');
    				    }
    					var caption_text = getAttrib(tinyMCE.imgElement, 'alt');
                        if (caption_text == '')
                            caption_text = getAttrib(tinyMCE.imgElement, 'title');
                            
                        if (name.indexOf('mce_media') == 0)
                            caption_text = 'streaming_media';

        				action = "insert";
                    }
                        
                    if (tinyMCE.capElement){
                        if(tinyMCE.capElement.getAttribute('id') == 'caption')
                        {
                            tinyMCE.captextElement = tinyMCE.capElement.lastChild;
                        }
                            if(tinyMCE.captextElement && tinyMCE.captextElement.getAttribute('id') == 'caption_text'){
                                caption_text = tinyMCE.captextElement.lastChild.data;
                            }

                        caption_align = tinyMCE.captextElement.style.textAlign;
                        caption_style = tinyMCE.capElement.getAttribute('class');
                        caption_bgcolour = tinyMCE.capElement.style.backgroundColor;
                        caption_bordercolour = tinyMCE.capElement.style.borderLeftColor;
                        caption_colour = tinyMCE.captextElement.style.color;
                        caption_border_width = tinyMCE.regexpReplace(tinyMCE.capElement.style.borderLeftWidth, '[^0-9]', '', 'gi');
                        caption_border_style = tinyMCE.capElement.style.borderLeftStyle;
                        caption_width = tinyMCE.capElement.style.width;
                        caption_width = caption_width.replace('px', '', 'gi');
                        caption_height = tinyMCE.capElement.style.height;
                        caption_height = caption_height.replace('px', '', 'gi');

                        action = "update";
                    }

        }else{//Insert / Update Caption
        
                caption_text = value['caption_text'];
                caption_style = value['caption_style'];
                caption_align = value['caption_align'];
                caption_padding = value['caption_padding'];
                caption_border_width = value['caption_border_width'];
                caption_border_colour = value['caption_bordercolour'];
                caption_border_style = value['caption_border_style'];
                caption_bgcolour = value['caption_bgcolour'];
                caption_colour = value['caption_colour'];
                caption_width = value['caption_width'];
                caption_height = value['caption_height'];
                
                caption_action = value['caption_action'];

                var ialign = getAttrib(tinyMCE.imgElement, 'align');
                var iborder = getAttrib(tinyMCE.imgElement, 'border');
                var ivspace = getAttrib(tinyMCE.imgElement, 'vspace');
                var ihspace = getAttrib(tinyMCE.imgElement, 'hspace');
                var iname = getAttrib(tinyMCE.imgElement, 'name');
                var iwidth = getAttrib(tinyMCE.imgElement, 'width');
                var iheight = getAttrib(tinyMCE.imgElement, 'height');
                
                var media_type = 'img';
                if (iname.indexOf('mce_media') == 0) {
                    media_type = 'sm';
                }
                
                if(caption_action == 'remove')
                {
                    removeCaption(tinyMCE.capElement);
                }
                if(tinyMCE.capElement && caption_action == 'insert')
                {
                    createCaption(tinyMCE.imgElement, true, caption_width, caption_height, ialign, iborder, ivspace, ihspace, iwidth, iheight, caption_text, caption_align, caption_border_width, caption_border_colour, caption_border_style, caption_bgcolour, caption_colour, caption_style, media_type);
                }

                if(tinyMCE.imgElement && !tinyMCE.linkElement && !tinyMCE.capElement && caption_action == 'insert')
                {
                    createCaption(tinyMCE.imgElement, false, caption_width, caption_height, ialign, iborder, ivspace, ihspace, iwidth, iheight, caption_text, caption_align, caption_border_width, caption_border_colour, caption_border_style, caption_bgcolour, caption_colour, caption_style, media_type);
                        
                }
                if(tinyMCE.linkElement && !tinyMCE.capElement && caption_action == 'insert') {
                    createCaption(tinyMCE.linkElement, true, caption_width, caption_height, ialign, iborder, ivspace, ihspace, iwidth, iheight,  caption_text, caption_align, caption_border_width, caption_border_colour, caption_border_style, caption_bgcolour, caption_colour, caption_style, media_type);
                }
                return true;
                }
        tinyMCE.openWindow(template, {editor_id : editor_id, caption_text : caption_text, caption_align : caption_align, caption_bgcolour : caption_bgcolour, caption_bordercolour : caption_bordercolour, caption_colour : caption_colour, caption_border_width : caption_border_width, caption_border_style : caption_border_style, caption_action : caption_action, caption_style : caption_style, caption_width : caption_width, caption_height : caption_height, action : action});
        return true;
    }
   // Pass to next handler in chain
   return false;
}
function removeCaption(elm)
{
    if(elm) {
        capText = elm.lastChild;

        elm.removeChild(elm.lastChild);
        tinyMCE.execCommand("mceRemoveNode", false, elm);
        
        return;
    }
}
function createCaption(elm, state, caption_width, caption_height, ialign, iborder, ivspace, ihspace, iwidth, iheight, caption_text, caption_align, caption_border_width, caption_border_colour, caption_border_style, caption_bgcolour, caption_colour, caption_style, media_type)
{
    var doc = tinyMCE.selectedInstance.getDoc();
    var capElement = tinyMCE.getParentElement(elm, "div");

    if(capElement)
        capTextElement = capElement.lastChild;
    
    var newCaption = false;
    
    newCaption = (state == false) ? true : false;

    if(!capElement)
    {
        capElement = doc.createElement("div");
        capTextElement = doc.createElement("div");
        newCaption = true;
    }

    if (newCaption) {
			capElement.appendChild(elm.cloneNode(true));
			elm.parentNode.replaceChild(capElement, elm);
			
			capElement.appendChild(capTextElement);
            capTextElement.appendChild(doc.createTextNode(caption_text));
    }else{
            capTextElement.removeChild(capTextElement.lastChild);
            capTextElement.appendChild(doc.createTextNode(caption_text));
    }
    var cwidth = parseInt(caption_width);
    var cheight = parseInt(caption_height);

    if(caption_width == '') {
        cwidth = parseInt(iwidth);
        if(iborder != '')
            cwidth = cwidth+(parseInt(iborder)*2);

        if(media_type == 'sm') {
            cwidth = cwidth + (parseInt(ihspace)*4);
        }else{
            cwidth = cwidth + (parseInt(ihspace)*2);
        }
    }
    if(caption_height == '') {
        cheight = parseInt(iheight);
        cheight = (caption_text != '') ? parseInt(cheight)+30 : (parseInt(cheight) + (parseInt(ivspace)*2));
        if(iborder != ''){
            cheight = cheight+(parseInt(iborder)*2);
        }
    }
        
    if (tinyMCE.isMSIE)
        capElement.style.styleFloat = ialign;
    else
        capElement.style.cssFloat = ialign;
        
    capElement.style.width = cwidth;
    capElement.style.height = cheight;
    capElement.setAttribute('id', 'caption');
    capElement.setAttribute('class', caption_style);
    capElement.style.marginLeft = ihspace;
    capElement.style.marginRight = ihspace;
    capElement.style.marginTop = ivspace;
    capElement.style.marginBottom = ivspace;
    capElement.style.borderWidth = (caption_border_width != '') ? caption_border_width : '';
    capElement.style.borderStyle = (caption_border_style != '') ? caption_border_style : '';
    capElement.style.borderColor = (caption_border_colour != '') ? caption_border_colour : '';

    if(caption_bgcolour != '')
        capElement.style.backgroundColor = caption_bgcolour;

    capTextElement.setAttribute('id', 'caption_text');
    capTextElement.style.clear = 'both';

    capTextElement.style.textAlign = caption_align;
    if(caption_colour != '')
        capTextElement.style.color = caption_colour;
}
function TinyMCE_caption_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");
    var divElm = tinyMCE.getParentElement(inst.getFocusElement(), "div");
    
    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 + '_caption', 'mceButtonDisabled', true);

	do {
		if ((name.indexOf('mce_media') == 0 && node.nodeName.toLowerCase() == "img") || (node.nodeName.toLowerCase() == "a" && node.nodeName.toLowerCase() == "img" && linkElm.getAttribute('href') == "#") || (node.nodeName.toLowerCase() == "div" && divElm.getAttribute('id') == "caption") || node.nodeName.toLowerCase() == "img")
			tinyMCE.switchClassSticky(editor_id + '_caption', 'mceButtonSelected', false);
	} while ((node = node.parentNode));
	return true;
}
