/**
 * An Application
 *
 * @author    Octadyne Systems
 * @copyright (c) 2008, by Octadyne Systems
 * @date      8. August 2008
 * @version   jQueryIdjQuery
 *
 */
function updateDiv(id,url,param) {
	// short handed call directly from the element object
	AJAX_get(id,url,'get',param)
	
}

function AJAX_get(div,file,method,params_string) {
	showAjaxLoading();
	jQuery.ajax({
		type: method,
		url: file,
		data: params_string,
		timeout: 20000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			AJAX_error(XMLHttpRequest, textStatus, errorThrown);
		},
		success: function(html){
			jQuery("#" + div).html(html);
			AJAX_success();
		}
	});
}

function showAjaxLoading() {
	var winWidth=jQuery(window).width();
	var scrollTop;
	if(self.pageYOffset) {
		scrollTop = self.pageYOffset;
	}
	else if(document.documentElemet && document.documentElement.scrollTop) {
		scrollTop = document.documentElement.scrolTop;
	}
	else if(document.body) {
		scrollTop = document.body.scrollTop;
	}
	jQuery('#ajax-loading').css('position', 'absolute');
	jQuery('#ajax-loading').css('top', scrollTop+'px');
	jQuery('#ajax-loading').css('left', parseInt((winWidth-150)/2)+"px");
	jQuery('#ajax-loading').show();
}

function hideAjaxLoading() {
	jQuery('#ajax-loading').hide();
}


function AJAX_success() {
	document.body.style.cursor = "auto";
	hideAjaxLoading();
}

function AJAX_error(XMLHttpRequest, textStatus, errorThrown) {
	alert("I'm sorry there has been an error.\n\nPlease try again later.");
}

function ajaxFileUpload(strAsset,strFieldName,remove_i18n,confirm_i18n)
{
	var strURL='/doajaxfileupload.cfm?asset=' + strAsset;
	var strName = ("afp" + (new Date()).getTime());
	jQuery("#ajax-fileloading").show();
	jQuery.ajaxFileUpload ( {
			url:strURL,
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			beforeSend:function()
			{
				jQuery("#ajax-fileloading").show();
			},
			complete:function()
			{
				jQuery("#ajax-fileloading").hide();
			},				
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						var strFile="<li id='" + strName + "'>";
							strFile +="<a href=\"" + data.path + "/" + data.filename + "\" target=\"_blank\"><img src='" + data.path + "/th_" + data.filename + "' /></a>";
							strFile += "<a href=\"javascript:removeFile('" + strName + "');\" class=\"action\" onclick=\"return confirm('" + confirm_i18n + "');\">" + remove_i18n + "</a>";
							strFile += "<input type=\"hidden\" name=\"" + strFieldName + "\" value=\"" + data.filename + "\">";
							strFile += "</li>";
						jQuery("#form-files").append(strFile);
					}
					prepZooms();
				}
				jQuery("#fileToUpload").val('');
				jQuery("#ajax-fileloading").hide();
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	
	return false;
} 
var cropperTool = null; 
var cropperWidth;
var cropperHeight;
function ajaxBannerUpload(strAsset,strFieldName,width,height,autoresizeOptions,mediaTypeID)
{
	cropperWidth=width;
	cropperHeight=height;
	var strURL='/doajaxfileupload.cfm?asset=' + strAsset +'&fileElementName=bannerFileToUpload' + '&mediaTypeId=' +mediaTypeID;
	var strName = ("afp" + (new Date()).getTime());
	jQuery("#ajax-BannerFileLoading").show();
	jQuery.ajaxFileUpload ( {
			url:strURL,
			secureuri:false,
			fileElementId:'bannerFileToUpload',
			dataType: 'json',
			beforeSend:function()
			{
				jQuery("#ajax-BannerFileLoading").show();
			},
			complete:function()
			{
				jQuery("#ajax-BannerFileLoading").hide();
			},				
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						
						jQuery("#bannerFileName").val(data.filename);
						jQuery("#listingCropperSourceImage").attr('src',data.path +'/' + data.filename);
						
						var strFile2 = '<img src="' + data.path +'/th_' + data.filename +'" >';
						
						jQuery("#div-imageReise-auto").html(strFile2);
						
						jQuery("#div-imageResize-options").show();
						if (autoresizeOptions == 'auto')
						{
							jQuery("#div-imageReise-auto").show();
						}
						else
						{
							
								attachCropper(width,height);
						}
						
					}
					prepZooms();
				}
				jQuery("#bannerFileToUpload").val('');
				jQuery("#ajax-BannerFileLoading").hide();
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	return false;
} 
function attachCropper(width,height)
{
	if( cropperTool != null ) {
		cropperTool.remove();
		cropperTool=null;
	}
	cropperTool=new Cropper.ImgWithPreview(
				'listingCropperSourceImage',
				{
					previewWrap: 'previewWrap',
					minWidth: cropperWidth,
					minHeight: cropperHeight,
					maxWidth: cropperWidth,
					maxHeight: cropperHeight,
					ratioDim: { x: 50, y: 50 },
					onEndCrop: function (coords, dimensions) {
						$('bannerStartX').value = coords.x1;
						$('bannerStartY').value = coords.y1;
					 }
					
				});
	
}
function clearCropper()
{
	if( cropperTool != null ) {
		cropperTool.remove();
		cropperTool=null;
	}
	jQuery("#cropper").hide();

	jQuery("#div-imageReise-auto").show();
}
function resetCropper()
{
	jQuery('#cropper').show();
	jQuery('#div-imageReise-auto').hide();
	attachCropper(cropperWidth,cropperHeight);
}

function removeFile(id) {
	jQuery("#" + id).remove();
}

