// send html to the post editor
function send_to_editor(h) {

	/*var w = jQuery(h).find("img").width;
	var hi = jQuery(h).find("img").height;

	var newW = 212;
	if(w > newW)
	{
		var rel = newW/w;
		w = newW;
		hi = hi*rel;
	}*/

	var k = h.match(/src=(["\'])(.*?)\1/);
	
	var ext = k[2].substr(k[2].length - 3)
	if(ext == 'swf')
	{
		//var w = h.match(/width=(["\'])(.*?)\1/);
		//var k = h.match(/width=(["\'])(.*?)\1/);
		jQuery("#spot_image_holder_" + current_id).html("Flash tillagd, går ej att förhandsvisa i nuvarande version");
		jQuery("#widget_spot-"+current_id+"-img").val("__swf__"+h);
	}
	else
	{
		var alt = h.match(/alt=(["\'])(.*?)\1/);
		jQuery("#spot_image_holder_" + current_id).html("<img width=\"250\" alt=\""+alt[2]+"\" src=\""+ k[2] + "\" /><span>(visas i riktigt storlek på framsidan)</span>");
		jQuery("#widget_spot-"+current_id+"-img").val(k[2]);		
		jQuery("#widget_spot-"+current_id+"-alt").val(escape(alt[2]));				
	}
	//var o = h.replace(/width=".*?"/gi, 'width="'+w+'"');
	//var o = o.replace(/height=".*?"/gi, 'height="'+hi+'"');
	//jQuery("<img src=\""+ k[2] + "\" alt='bild' />")

	tb_remove();
}
function re_init_tb()
{
	//tb_init('a.spotlink');
	jQuery('a.spotlink').bind("click", function(){	
		current_id = this.id.substring(5);
	});
}
var current_id;
// thickbox settings
jQuery(function($) {
	tb_position = function() {
		var tbWindow = $('#TB_window');
		var width = $(window).width();
		var H = $(window).height();
		var W = ( 720 < width ) ? 720 : width;

		if ( tbWindow.size() ) {
			tbWindow.width( W - 50 ).height( H - 45 );
			$('#TB_iframeContent').width( W - 50 ).height( H - 75 );
			tbWindow.css({'margin-left': '-' + parseInt((( W - 50 ) / 2),10) + 'px'});
			if ( typeof document.body.style.maxWidth != 'undefined' )
				tbWindow.css({'top':'20px','margin-top':'0'});
			$('#TB_title').css({'background-color':'#222','color':'#cfcfcf'});
		};
		
		return $('a.thickbox').each( function() {
			var href = $(this).attr('href');
			if ( ! href ) return;
			href = href.replace(/&width=[0-9]+/g, '');
			href = href.replace(/&height=[0-9]+/g, '');
			$(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) );
		});
	};

	jQuery('a.thickbox').click(function(){

		if ( typeof tinyMCE != 'undefined' &&  tinyMCE.activeEditor ) {
			tinyMCE.get('content').focus();
			tinyMCE.activeEditor.windowManager.bookmark = tinyMCE.activeEditor.selection.getBookmark('simple');
		}
	});
	jQuery('a.spotlink').bind("click", function(){
		
		current_id = this.id.substring(5);
		jQuery("#TB_title").remove();
		
	});

	$(window).resize( function() { tb_position() } );
});

