var dialog	= window.parent ;
var oEditor = dialog.InnerDialogLoaded() ;

var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;
var FCKRegexLib	= oEditor.FCKRegexLib ;
var FCKTools	= oEditor.FCKTools ;


var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;

var urlField = '';
var oDocument = dialog.Selection.GetSelection().MoveToAncestorNode( 'A' ) ;
if ( oDocument )
	FCK.Selection.SelectNode( oDocument ) ;

/*if ( oDocument )
	FCK.Selection.MoveToNode( oDocument ) ;
*/
var aDocProperties = new Array();
var docImageType = 'none';
var icon_image = '';
var image_tag = '';
var size_value = '';
var fileSize = '';
var file_name = '';
var old_file_name = '';
var oImageOriginal ;
var oImage = FCK.Selection.GetSelectedElement() ;
var oRegex = new Object();
oRegex.ReserveTarget = new RegExp('') ;
oRegex.ReserveTarget.compile( '^_(blank|self|top|parent)$', 'i' ) ;
oRegex.PopupUri = new RegExp('') ;
oRegex.OnClickPopup = /^\s*on[cC]lick="\s*FCKOpenPopUp\(\s*this\s*,\s*event\s*,\s*[0-9]+\s*,\s*[0-9]+\s*\);"$/ ;

if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
	oImage = null ;
function  SetImageType(imageType)
{
	docImageType = imageType;
	GetE(imageType).checked = true;
}
window.onload = function()
{
	oEditor.FCKLanguageManager.TranslatePage(document) ;
	LoadSelection() ;
	window.parent.SetOkButton( true ) ;
}

function show_props(obj, objName) {
	var result = "";
	for (var i in obj) {
	  result = objName + "." + i + " = " + obj[i] + " \n";
		alert(result);
	}
	//return result;
}

function LoadSelection()
{

	if ( oEditor.FCKBrowserInfo.IsIE )
		GetE('txtTitle').value  = FCK.EditorDocument.selection.createRange().text.replace( /(^\s*)|(\s*$)/g, '' );
	else
		GetE('txtTitle').value =  FCK.EditorWindow.getSelection().getRangeAt(0).toString().replace( /(^\s*)|(\s*$)/g, '' );

	if ( ! oDocument ) {
		SetImageType('none');
		return ;
	}
	var id = '';
	id = oDocument.getAttribute( 'id' , 2 );

	if (!id) {
		SetImageType('none');
		id = '';
	}

	if (id.match(/^Doc_|_+/))
	{
		sType = 'Doc';

		aDocProperties = id.split("_|_");
		/*
		[0] - Type
		[1] - file_name
		[2] - Image_tag

		*/
	} else {
		SetImageType('none');
		return ;
	}

	var sHRef = oDocument.getAttribute('href',2) + '' ;
	var oPopupMatch = '';
	var onclick = oDocument.getAttribute( 'onclick_fckprotectedatt' ) ;
	if ( onclick )
	{
		// Decode the protected string
		onclick = decodeURIComponent( onclick ) ;
		oPopupMatch = oRegex.OnClickPopup.exec( onclick ) ;
		var popup_data = oPopupMatch.toString();
		if( oPopupMatch )
		{
			var aPopUp = new Array();
			var aPopUp = popup_data.split(",");
			var width = aPopUp[2];
			var exp = '\);\"$'
			var height = aPopUp[3].replace( /\);\"$/ , '' );
			GetE('txtPopupWidth').value = width;
			GetE('txtPopupHeight').value = height;
			GetE( 'cmbTarget' ).value = 'popup' ;
			SetTarget( 'popup' ) ;
			ShowE('divPopupSize',1);

		}
	}


	GetE('docUrl').value    = sHRef ;
	file_name = GetFilename(sHRef);
	old_file_name = GetFilename(sHRef);

	if(FCK.EditorDocument.getElementById(file_name)) {
		fileSize = FCK.EditorDocument.getElementById(file_name).innerHTML;
		GetE('txtTitle').value = GetE('txtTitle').value.slice(0, GetE('txtTitle').value.length - fileSize.length  ).replace( /(^\s*)|(\s*$)/g, '' );
	}
	fileSize = str_replace(' ', '', fileSize);
	fileSize = str_replace('KB', '', fileSize);
	fileSize = str_replace('(', '', fileSize);
	fileSize = str_replace(')', '', fileSize);
	if (fileSize != '')
		GetE('display_size').checked = true;
	else
		GetE('display_size').disabled = true;

	image_tag = aDocProperties[2]; //GetAttribute( oDocument, 'imagetag', '' );

	if (image_tag && image_tag != 'doc' && image_tag != 'pdf' && image_tag != 'image' && image_tag != 'excel' && image_tag != 'ppt'&& image_tag != 'audio' && image_tag != 'video' && image_tag != 'none')
	{
		SetImageType('other');
		GetE('otherUrl').value = image_tag;
	} else if (image_tag && image_tag != 'none') {
		SetImageType(image_tag);
	} else {
		SetImageType('none');
	}
	if ( !oPopupMatch )
	{
		if (oDocument)
			var sTarget = oDocument.target;
		else if (window.parent.parentData['linkCmbTarget'])
			var sTarget	= window.parent.parentData['linkCmbTarget'];
		if ( sTarget && sTarget.length > 0 )
		{
			if ( oRegex.ReserveTarget.test( sTarget ) )
			{
				sTarget = sTarget.toLowerCase() ;
				GetE('cmbTarget').value = sTarget ;
			}
			else
				GetE('cmbTarget').value = '' ;
		}
	}
}

function GetFilename(file_url)
{
	return file_url.slice(file_url.lastIndexOf('\/')+1);
}

function str_replace(s_search, s_replace, s_subject)
{
	var s = "";
	while(true) {
		i = s_subject.indexOf(s_search);
		if (i >= 0) {
			s = s + s_subject.substr(0, i) + s_replace;
			s_subject = s_subject.substr(i + s_search.length);
		}
		else
			break;
	}
	return s + s_subject;
}

function Ok()
{
	var alert_msg = '';

	if ( GetE('docUrl').value.length == 0 )
	{
		GetE('docUrl').focus() ;
		alert_msg = oEditor.FCKLang.DlgDocAlertUrl;

	}
	if(docImageType == 'other' && GetE('otherUrl').value == '') {
		GetE('otherUrl').focus() ;
		if (alert_msg != '')
			alert_msg = alert_msg+'\n'+oEditor.FCKLang.DlgDocAlertOther;
		else
			alert_msg = oEditor.FCKLang.DlgDocAlertOther;
	}
	if (alert_msg != '') {
		alert(alert_msg);
		return false ;
	}

	sUri = GetE('docUrl').value;
	file_name = GetFilename(sUri);

/*	if( GetE('cmbTarget').value == 'popup' )
		sUri = BuildPopupUri( sUri ) ;
*/
	if (docImageType != 'other')
		imgUri = GetE('otherUrl').value;

		var add_insert = '';
		// if (oDocument)
		// 	oDocument.parentNode.removeChild(oDocument)
		GetIconByType();

		if (GetE('display_size').checked == true)
			add_insert = '<span id="'+file_name+'"> ('+fileSize+' KB)</span>';
		var my_title = '';
		if (GetE('txtTitle').value)
			my_title = GetE('txtTitle').value+' '

		if (GetE('cmbTarget').value == 'popup') {
			var onclick = 'FCKOpenPopUp(this,event,'+ GetE('txtPopupWidth').value +','+ GetE('txtPopupHeight').value +');';
			var insert = '<a href="'+sUri+'" id="Doc_|_' + file_name + '_|_' + image_tag + '" alt="'+GetE('txtTitle').value+'" onclick="'+onclick+'">'+icon_image+my_title+add_insert+'</a> ';
		} else if (GetE('cmbTarget').value != '')
			var insert = '<a href="'+sUri+'" id="Doc_|_' + file_name + '_|_' + image_tag + '" alt="'+GetE('txtTitle').value+'" target="'+GetE('cmbTarget').value+'">'+icon_image+my_title+add_insert+'</a> ';
		else
			var insert = '<a href="'+sUri+'" id="Doc_|_' + file_name + '_|_' + image_tag + '" alt="'+GetE('txtTitle').value+'">'+icon_image+my_title+add_insert+'</a> ';

		oEditor.FCK.InsertHtml(insert);
		return true;

}

function GetIconByType()
{
	if (docImageType != 'none' && docImageType != 'other') {
		icon_image = '<img src="'+FCKConfig.BaseUrl+oEditor.FCKConfig.IconImagesUrl+'/'+docImageType+'.gif" border="0" alt="'+GetE('txtTitle').value+'"> '
		image_tag = docImageType;
	}
	if (docImageType == 'other') {
		image_tag = GetE('otherUrl').value;
		icon_image = '<img src="'+GetE('otherUrl').value+'" border="0" alt="'+GetE('txtTitle').value+'">';
	}
	if (docImageType == 'none') {
		icon_image = '';
		image_tag = docImageType;
	}

}


function BrowseServer(field)
{
	urlField = field; // gobal variable, used in SetUrl function later

	// Open the browser window.
	if (field == 'docUrl') {
		OpenFileBrowser( FCKConfig.DocumentsBrowserURL, FCKConfig.DocumentsBrowserWindowWidth, FCKConfig.DocumentsBrowserWindowHeight ) ;
	}
	else {
		OpenFileBrowser( FCKConfig.ImageBrowserURL, FCKConfig.DocumentsBrowserWindowWidth, FCKConfig.DocumentsBrowserWindowHeight ) ;
	}
}

function SetUrl( url, width, height, alt, fsize )
{
	if (urlField == 'docUrl') {
		if (fsize > 0) {
			GetE('display_size').disabled = false;
		}

		fileSize = fsize;
	}

	GetE(urlField).value = url ;

	if ( alt ) {
		GetE('txtAlt').value = alt;
	}
}

function SetTarget( targetType )
{

	switch ( targetType )
	{
		case "_blank" :
		case "_self" :
		case "_parent" :
		case "_top" :
			GetE('cmbTarget').value = targetType ;
			break ;
		case "" :
			GetE('cmbTarget').value = '' ;
			break ;
	}

	if ( targetType == 'popup' )
	{
		ShowE('divPopupSize',1);
		window.parent.SetAutoSize( true ) ;
		if (!GetE('txtPopupWidth').value)
			GetE('txtPopupWidth').value = 800;
		if (!GetE('txtPopupHeight').value)
			GetE('txtPopupHeight').value = 600;
	} else
		ShowE('divPopupSize',0);
}