var FLASH_IMAGE = "++resource++flash.png";

function URLDecode(url) //function decode URL
{
    // Replace + with ' '
    // Replace %xx with equivalent character
    // Put [ERROR] in output if %xx is invalid.
    
    if(url.indexOf("?") > -1){
        url = url.split("?")[1];
    }
    
    var HEXCHARS = "0123456789ABCDEFabcdef";
    var encoded = url;
    var plaintext = "";
    var i = 0;
    while (i < encoded.length) {
        var ch = encoded.charAt(i);
        if (ch == "+") {
            plaintext += " ";
            i++;
        } else if (ch == "%") {
            if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                plaintext += unescape( encoded.substr(i,3) );
                i += 3;
            } else {
                //alert( 'Bad escape combination near ...' + encoded.substr(i) );
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
            plaintext += ch;
            i++;
        }
    }
    try{
        var res = {};
        var items = plaintext.split("&");
    
        for(var i = 0; i < items.length; i++){
            var temp = items[i].split("=");
            var key = temp[0];
            var value = temp[1];
            res[key] = value;
        }
    
        return res;
    }catch(err){
        return {};
    }
};


jq(document).ready(function(){
    
    var image_button = jq("#toolbar #kupu-tb-buttons .kupu-tb-buttongroup #kupu-imagelibdrawer-button");
    
    if(image_button.size() > 0){
        image_button.attr('title', 'Insert an image or flash content (flv or swf)');
    }
    
    jq('img.flashElement').each(function(){
        var imgele = jq(this);
        
        var flash_image = imgele.attr('src');
        var flash_url = flash_image.substring(0, flash_image.indexOf(FLASH_IMAGE));
        var options = URLDecode(flash_image);
        var width = imgele.width();
        var height = imgele.height();
        
        var classes = imgele[0].className;
        var class_to_remove = 'flashElement';
        var extra_classes = classes.substring(0, classes.indexOf(class_to_remove)) + 
                            classes.substring(classes.indexOf(class_to_remove) + class_to_remove.length, classes.length);
        
        var new_id = "flash" + options.id.replace(".", "-");
        
        //check if it is a streaming flv
        if(options.flv_url != undefined && options.flv_url != '0'){
            var flv = jq("<a class='player " + extra_classes + "' id='" + new_id +
                         "' style='width:" + width + "px; height:" + height + "px' ></a>");
            imgele.after(flv);
            imgele.remove();
            
            flowplayer(new_id, {src:'flowplayer.swf', width: width + "px", height:height + "px"}, {
                clip: { 
                    url: options.flv_url, 
                    autoPlay: false,
                    provider: 'influxis'
                },
                plugins: { 
                    influxis: {  
                      url: 'flowplayer.rtmp.swf',
              		  netConnectionUrl: options.streaming_url
                    } 
                }
            });
            
        //insert flash element
        //If problems with this continue, just do it manually....
        }else if(options.filename != undefined && options.filename.indexOf(".swf") > -1){
            imgele.wrap("<div class='" + extra_classes + "' id='" + new_id + 
                        "' style='width:" + width + "px;height:" + height + "px' ></div>");
            imgele = imgele.parent();

            var swf_full_url = flash_url + (flash_url.substring(flash_url.length-1) == "/" ? "download.swf" : "/download.swf");
            
            var html = 
            	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' + 
					'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" ' +
					'width="' + width + '" height="' + height + '" ' + 
 					'id="' + options.id + '"> ' +
					'<param name="movie" value="' + swf_full_url + '" /> ' +
					'<param name="quality" value="high" /> ' +
					'<param name="bgcolor" value="#ffffff" /> ' +
					'<param name="wmode" value="opaque" />' +
					'<embed src="' + swf_full_url + '" quality="high" bgcolor="#ffffff" ' + 
						'width="' + width + '" height="' + height + '" wmode="opaque" ' +
						'name="' + options.id + '" align="" type="application/x-shockwave-flash" ' + 
						'pluginspage="http://www.macromedia.com/go/getflashplayer"> ' +
					'</embed> ' +
				'</object> ';
            
        	imgele.html(html);
        //add flv normal way without streaming....    
        //XXX should we have plone stream it since we can?
        }else if(options.filename != undefined && options.filename.indexOf(".flv") > -1){
            var flv = jq("<a class='player " + extra_classes + "' id='" + 
                        new_id + "' style='width:" + width + "px; height:" + height + "px' ></a>");
            imgele.after(flv);
            imgele.remove();
            
            var swf_full_url = flash_url + (flash_url.substring(flash_url.length-1) == "/" ? "download.swf" : "/download.swf");
            
            flowplayer(new_id, 'flowplayer.swf', swf_full_url);
        }
    });
});
