var cropToolWnd = top;

// settings
var c_width = 795;
var c_height = 540;

var cropToolDiv = cropToolWnd.document.getElementById('croptool');
var cropToolDivWidth = 795;
var cropToolDivHeight = 540;

function initCropToolDiv()
{
	cropToolDiv.style.display = 'none';
	cropToolDiv.style.position = 'absolute';
	cropToolDiv.style.width = cropToolDivWidth;
	cropToolDiv.style.height = cropToolDivHeight;
	cropToolDiv.style.top = (screen.height - cropToolDivHeight - 130) / 2;
	cropToolDiv.style.left = (screen.width - cropToolDivWidth) / 2;
	cropToolDiv.style.border = '0px';
	cropToolDiv.style.backgroundColor = '#FFFFFF';
}
initCropToolDiv();

function URLEncode( plaintext )
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789 " +                  // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "+";
            } else {
			if(ch!='/')
			{
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
			}
			else
				encoded += "/";
            }
        }
    } // for

    return encoded;
}

function ShowCropTool(params)
{
	initCropToolDiv();
	var url = params["baseUrl"] + 'croptool/index.html?s=1';
	var queryStart = true;
	for(name in params)
	{
		if(name == 'sizesString')
        {
			url += params[name];
		}
        else
        {
			url += "&" + name + "=" + URLEncode(params[name]);
        }
	}
	cropToolDiv.targetWindow = window;
	cropToolDiv.innerHTML = '<iframe src="' + url + '" marginwidth="0" marginheight="0" frameborder="0" width=' + c_width + ' height=' + c_height + '/>';
	cropToolDiv.style.display = 'block';
}

function ShowUploadTool(params)
{
	var width = 540;
	var height = 400;
	
	cropToolDiv.style.width = width;
	cropToolDiv.style.height = height;
	cropToolDiv.style.top = (screen.height - height - 130) / 2;
	cropToolDiv.style.left = (screen.width - width) / 2;
	cropToolDiv.style.border = '2px solid #AAAAAA';
	cropToolDiv.style.backgroundColor = '#FFFFFF';
	cropToolDiv.targetWindow = window;
	var url = params["baseUrl"] + 'croptool/uploadtool/index.html?uploadUrl=' + URLEncode(params["uploadUrl"]) + '&onCompleteUrl=' + URLEncode(params["completeUrl"]);
	cropToolDiv.innerHTML = '<iframe src="' + url + '" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" width="' + width + '" height="' + height + '" />';
	cropToolDiv.style.display = 'block';
}
