// JavaScript Document
/*
$("#basicWidgetConfig input,#basicWidgetConfig select,#basicWidgetConfig textarea").live('change keypress focusout',function()
{
	imageUpdatePreview();
});
*/

$("#updateImagePreview").live('click', function()
{
	imageUpdatePreview();
});

function imageUpdatePreview()
{
	//$.post('/image/Image/preview', { srcURL: $("#srcURL").val(), imgH: $("#imgH").val(), imgW: $("#imgW").val(), link: $("#imgLink").val(), pos: $("#pos").val(), borderSize: $("#borderSize").val() }, function(data)
	//{
	//	$("#previewWidgetConfig").html(data);
	//});	
	$("#imagePreview").width($("#imgW").val() + 'px').height($("#imgH").val() + 'px').attr('src', $("#srcURL").val());
	$("#imgPosPreviewContainer").css('text-align', $("#pos").val());
	$("#imgLinkPreview").attr('href', $("#imgLink").val());
	var bSize = $("#borderSize").val();
	if(bSize > 0)
		$("#imagePreview").css('border-style', 'solid').css('border-width', bSize + "px");
}

function imageEditHook(idNum)
{
	readyImgOpts();
	//alert(idNum);
	if(isNaN(idNum))
		var imgID = '#' + idNum;
	else
		var imgID = '#onewp_img_' + idNum;
	//$(imgID).resizable('destroy');
	if(idNum != 'none')
	{
		var curH = $(imgID).height();
		var curW = $(imgID).width();
		if(curH && curH > 0) $("#imgH").val(curH);
		if(curW && curW > 0) $("#imgW").val(curW);
		if(isNaN(idNum))
		{
			var src = ieCleanURL($(imgID).attr('src'));
			$("#srcURL").val(src);
			$("#imagePreview").attr('src', src).height($("#imgH").val()).width($("#imgW").val());		
		}
	}
}

function imageOkHook(idNum)
{
	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');
		
		//alert('imgH: ' + hdr['imgH']);
		//if(hdr['imgH'] > 0)
			//$(imgID).height(hdr['imgH']);
		//if(hdr['imgW'] > 0)
			//$(imgID).height(hdr['imgW']);
	
		if($("#resetImage").is(':checked'))
		{
			resetHeaderImage();
		}
	}
	else
	{
		//var imgID = '#onewp_img_' + idNum;
		//savePage();
	}	
}

function imagePreviewHook() 
{
	$("#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());
}

function imageFinishedHook(idNum)
{
	imageInitPageItem(idNum);
}

function imageInitPageItem(idNum)
{
	if(isNaN(idNum))
	{
		var imgID = '#' + idNum;
		var oID = idNum;
		//alert($(imgID).parent().attr('class'));
		if($(imgID).parent().hasClass('onewp_tplImageResizecontainer'))
		{
			//var content = $(this).parent().html();
			//$(this).parent().replaceWith(content);
			$(imgID).unwrap();
			
		}
		$(imgID).wrap('<div class="onewp_tplImageResizecontainer" style="position: absolute;"></div>');
	}
	else
	{
		var imgID = '#onewp_img_' + idNum;
		var oID = 'onewp_img_' + idNum;
	}
	var imgW = $(imgID).width();
	var imgH = $(imgID).height();

	if(imgID.length > 0)
	{	
		var offset = $(imgID).position();
		if(offset)
		{
			if(isNaN(idNum))
				$(imgID).parent('.onewp_tplImageResizecontainer').append('<div style="position: absolute; top: ' + offset.top + 'px; left: ' + offset.left + 'px; width: 45px; height: 45px;" class="imageResizer" id="resize_' + oID + '"><a href="' + oID + '" class="onewp_imgResize"><img src="/img/resize-image-icon.gif"></a></div>');
			else
				$(imgID).offsetParent().append('<div style="position: absolute; top: ' + offset.top + 'px; left: ' + offset.left + 'px; width: 45px; height: 45px;" class="imageResizer" id="resize_' + oID + '"><a href="' + oID + '" class="onewp_imgResize"><img src="/img/resize-image-icon.gif"></a></div>');
		}
	}
	setupImageResizing();
}

function setupImageResizing()
{
	$(".onewp_imgResize").die('click');
	$(".onewp_imgResize").live('click', function()
	{
		var cID = ieCleanURL($(this).attr('href'));
		var idRE = /onewp_img_(.*)/;
		cID = cID.replace(idRE,"$1");
		
		if(isNaN(cID))
		{
			var cName = '#' + cID;
			var rName = '#resize_' + cID;
			var ctn = $(cName).offsetParent();
		}
		else
		{
			var cName = '#onewp_img_' + cID;
			var rName = '#resize_onewp_img_' + cID;
			var ctn = 'document';
		}
		
		$(cName).addClass('curResize');
		$(cName).resizable(
		{
			stop: function(ev, ui)
			{
				$(cName).removeClass('curResize');
				$(this).resizable("destroy");
				var hdr = new Array();
				hdr['srcURL'] = ieCleanURL($(cName).attr('src'));
				hdr['imgH'] = $(cName).height();
				hdr['imgW'] = $(cName).width();
				var hdrArr = serialize(hdr);
				$.post("/widgets/saveWidgetOptions", { wID: cID, opts: hdrArr }, function (data)
				{
				});
				$(cName).css('position', '').css('display', '');
				
			},
			handles: 's,e,se'
			//containment: ctn
		});
		
		var oset = $(".ui-resizable-se").offset();
		$('body').append('<img class="resizeHelpBubble" src="/img/skin/resize-help-bubble.png" style="position: absolute; top:' + (oset.top - 125) + 'px; left:' + (oset.left - 50) + 'px; z-index: 1200;">');
		$(".resizeHelpBubble").animate({opacity: 0}, 5000, function() { $(".resizeHelpBubble").remove(); });
		
		return false;
	});

	$(".imageResizer").hide();

	$(".onewp_content").mouseenter(
	function()
	{
		$(".imageResizer").hide();
		$(this).find('.imageResizer').show();
	});
	
	$('.onewp_content').mouseleave(
	function()
	{
		$(".imageResizer").hide();
	});
	
	$(".onewp_tplImageResizecontainer").mouseenter(
	function()
	{
		$(".imageResizer").hide();
		$(this).find(".imageResizer").show();
	});

	$(".onewp_tplImageResizecontainer").mouseleave(
	function()
	{
		$(".imageResizer").hide();
	});	
}
