// JavaScript Document
function img_plus_textEditHook(idNum)
{
	readyOpts();
	var imgID = '#onewp_img_' + idNum;
	var curH = $(imgID).height();
	var curW = $(imgID).width();
	if(curH && curH > 0) $("#imgH").val(curH);
	if(curW && curW > 0) $("#imgW").val(curW);
	$("#srcURL").val($(imgID).attr('src'));
	
	if(parseInt(idNum) > 0)
	{
		$("#rteContent").replaceWith('<textarea id="rteContent" class="tinymce excludeOption" name="rteContent" ></textarea>');
		var orig = $("#onewp_item_" + idNum).find('.onewp_content').html();
		$("#onewp_item_" + idNum).find('.imgPlusTextBox, .onewp_clearDiv').remove();
		var rteContent = $("#onewp_item_" + idNum).find('.onewp_content').html();
		$('#rteContent').text(rteContent);
		$("#onewp_item_" + idNum).find('.onewp_content').html(orig);
	}
	
	toggleEditor('rteContent');
	if($.browser.msie)
		window.onbeforeunload = null;
}

function img_plus_textOkHook(idNum)
{
	//var rteContent = $("#rteContent").html();
	//$("#rteContent").replaceWith('<textarea id="rteContent" class="tinymce excludeOption" name="rteContent" ></textarea>');
	//$("#rteContent").val(rteContent);
	/*
	if(isNaN(idNum))
		var imgID = '#' + idNum;
	else
		var imgID = '#onewp_img_' + idNum;

	if(isNaN(idNum))
	{
		var imgID = '#' + idNum;
		var hdr = new Array();
		hdr['srcURL'] = $("#srcURL").val();
		hdr['imgH'] = $("#imgH").val();
		hdr['imgW'] = $("#imgW").val();
		hdr['templateImage'] = 1;
		hdr['idNum'] = idNum;
		hdr['globalCopy'] = $('#globalCopy').is(':checked');
		var hdrArr = serialize(hdr);
		$(imgID).attr('src', hdr['srcURL']).css('height', hdr['imgH'] + 'px').css('width', hdr['imgW'] + 'px');
		$.get("/pages/saveImage", { imgData: hdrArr}, function (data)
		{
		});
	}
	else
	{
		*/
		if (tinyMCE.get('rteContent'))
			tinyMCE.get('rteContent').remove();
		var imgID = '#onewp_img_' + idNum;
		savePage();
	//}
}

function img_plus_textPreviewHook() 
{
	$("#image_preview").attr('src', $("#srcURL").val());
	$("#image_preview").css('height', $("#imgH").val() + 'px');
	$("#image_preview").css('width', $("#imgW").val() + 'px');
	$("#image_preview_link").attr('href', $("#link").val());
	$("#imgPreviewContent").replaceWith($("#rteContent").html());
}

function img_plus_textFinishedHook(idNum)
{

	img_plus_textInitPageItem(idNum);
	if($.browser.msie)
		window.onbeforeunload = doBeforeUnload;
}

function img_plus_textInitPageItem(idNum)
{
	var imgID = '#onewp_img_' + idNum;
	var imgW = $(imgID).width();
	var imgH = $(imgID).height();
	
	var offset = $(imgID).position();
	$(imgID).after('<div style="position: absolute; top: ' + offset.top + 'px; left: ' + offset.left + 'px; width: 45px; height: 45px;" class="imageResizer" id="resize_onewp_img_' + idNum + '"><a href="onewp_img_' + idNum + '" class="onewp_imgResize"><img src="/img/resize-image-icon.gif"></a></div>');
	setupImageResizing();

}