function BxBlgEntry () {
	this._form = 'new_entry_form';
}

/**
 * show login form
 */
BxBlgEntry.prototype.showWnd = function(sName, sTitle, aParams) {
	var sXsl = null;
	switch(sName) {
		case 'license':
			sTitle = 'Licenses:';
			aParams = {license: aParams};
			sXsl = 'wnd_license.xsl';
			break;
		case 'featured':
			sXsl = 'wnd_featured.xsl';
			break;
		case 'trusted':
			sXsl = 'wnd_trusted.xsl';
			break;
		case 'certified':
			sXsl = 'wnd_certified.xsl';
			break;
		case 'not-certified':
			sXsl = 'wnd_not_certified.xsl';
			break;
	}

	if(!sTitle)
		sTitle = 'Please, note...';

	if(!aParams) 
		aParams = {};

    var w = new BxWnd(null, aBxConfig['urlModuleXsl'] + sXsl);
    w.setParams(aParams);
    w.show(sTitle, 1);
    return false;
};

//--- EXT - BEGIN - Specific methods ---//
BxBlgEntry.prototype.addPpVersion = function (sName) {	
	var oProduct = $('ppversion_product');	
	var oVersion = $('ppversion_version');
	var oSubVersion = $('ppversion_subversion');
	var oSelected = $('ppvSelected');
	
	//--- Get Selected version ---//
	var sProduct = "";
	for(var iKey=0; iKey<oProduct.options.length; iKey++)
		if(parseInt(oProduct.options[iKey].value) == parseInt(oProduct.value)) {
			sProduct = oProduct.options[iKey].innerHTML;
			break;
		}	
	var ppVersion = oVersion.value + '.' + oSubVersion.value;
	
	//--- Check for existance ---//	
	if($(oProduct.value + '#' + ppVersion) != null) {
		alert('You already added this version');
		return;
	}	
		
	//--- Visualize results ---//
	var oHidden = getBrowserType() == 'ie' ? document.createElement('<input name="' + sName + '[]">') : document.createElement('input');
	oHidden.name = sName + '[]';
	oHidden.value = oProduct.value + '#' + ppVersion;	
	oHidden.id = oHidden.value;
	oHidden.type = 'hidden';
	
	var oAnchor = document.createElement('a');
	oAnchor.className = 'common';
	oAnchor.href = 'javascript:void(0);';
	oAnchor.innerHTML = 'delete';
	oAnchor.onclick = function() {
		this.parentNode.parentNode.removeChild(this.parentNode);
	};
	
	var oDiv = document.createElement('div');	
	oDiv.innerHTML += '<b>' + sProduct + '</b> v.' + ppVersion + '&nbsp;&nbsp;&nbsp;&nbsp;';
	oDiv.appendChild(oAnchor);
	oDiv.appendChild(oHidden);
	
	oSelected.appendChild(oDiv);	
}
BxBlgEntry.prototype.stopProduct = function(iProductId) {
	if(!iProductId) return false;
			
	var loadComplete = function(oResult) {		
		var oRequest = new BxXmlRequest('', '', '');
		var iResultCode = parseInt(oRequest.getRetNodeValue(oResult, 'resultCode'));
		switch(iResultCode) {
			case 0:
				alert('Product was successfully removed from selling area');
				jQuery('.ext-selling-lnk').unbind('click').bind('click', function() {
					gBxBlgEntry.startProduct(iProductId);
				}).find('i').html('Resume selling');
				break;

			default:
				alert('An error occured. Please report.');			
		}		
	};	
	oRequest = new BxXmlRequest(aBxConfig['urlRoot'] + "xcontent/market/xml_stop/" + iProductId, loadComplete, true);		
};
BxBlgEntry.prototype.startProduct = function (iProductId) {
	if(!iProductId) return false;
		
	var oRequest = null;	
	var loadComplete = function(oResult) {
		var oRequest = new BxXmlRequest('', '', '');
		var iResultCode = parseInt(oRequest.getRetNodeValue(oResult, 'resultCode'));
		switch(iResultCode) {
			case 0:
				alert('Product was successfully resumed to selling area');
				jQuery('.ext-selling-lnk').unbind('click').bind('click', function() {
					gBxBlgEntry.stopProduct(iProductId);
				}).find('i').html('Stop selling');
				break;

			default:
				alert('An error occured. Please report.');			
		}		
	};
	oRequest = new BxXmlRequest(aBxConfig['urlRoot'] + "xcontent/market/xml_start/" + iProductId, loadComplete, true);		
}
//--- EXT - END - Specific methods ---//

BxBlgEntry.prototype.markTrusted = function(iId) {
	this.changeTrustCertifStatus(iId, 'mark_trusted');
}
BxBlgEntry.prototype.unmarkTrusted = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_trusted');
}
BxBlgEntry.prototype.markCertified = function(iId) {
	this.changeTrustCertifStatus(iId, 'mark_certified');
}
BxBlgEntry.prototype.unmarkCertified = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_certified');
}
BxBlgEntry.prototype.markFeatured = function(iId) {
	this.changeTrustCertifStatus(iId, 'mark_featured');
}
BxBlgEntry.prototype.unmarkFeatured = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_featured');
}
BxBlgEntry.prototype.markReviewed = function(iId) {
	this.changeTrustCertifStatus(iId, 'mark_reviewed');
}
BxBlgEntry.prototype.unmarkReviewed = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_reviewed');
}
BxBlgEntry.prototype.markHotNews = function(iId) {
	this.changeTrustCertifStatus(iId, 'mark_hot_news');
};
BxBlgEntry.prototype.unmarkHotNews = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_hot_news');
};
BxBlgEntry.prototype.changeTrustCertifStatus = function(iId, sType) {	
	var loadComplete = function(oResult) {
		var oRequest = new BxXmlRequest('','','');
		iResult = parseInt(oRequest.getRetNodeValue(oResult, 'resultCode'));								
		switch(iResult) {
			case 0:
				alert('Extension was ' + (sType.substr(0, 2) == 'un' ? 'un' : '') + 'marked as ' + sType.replace(/(?:un)?(?:mark_)/, '').replace(/featured/, '"Staff Pick"').replace(/hot_news/, '"Hot News"').replace(/under_dispute/, '"Under Dispute"'));
				document.location = document.location;	
				break;
			case 1:
				alert('Incorrect input data. Please report.');
				break;
			case 2:
				alert('Cannot save information in database');
				break;				
		}		
	};
	new BxXmlRequest(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_" + sType + "/" + iId, loadComplete, true);	
}
BxBlgEntry.prototype.approve = function (id)
{		
	//--- EXT - BEGIN - Need the vote ---//
	var iVote = 0;
	var sResult = prompt("How many points you would add to this extension now.\n- 0-5 for commercial,\n- 0-10 for free");
		
	if((new RegExp("^[0-9]+$")).exec(sResult) != null)	
		iVote = parseInt(sResult);
	else return;	
	//--- EXT - END - Need the vote ---//
	
    var oContent = new BxContent ();
    oContent.onLoadComplete = function (ret)
    {
		switch (ret)        
        {
        case 'a':
            alert('entry approved');
            document.location = document.location;
            break;
        case 'p':
            alert('entry disapproved');
            document.location = document.location;
            break;        
        default:
		    alert('approving failed');
        }
    }
    oContent.getXmlNodeValue (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_approve/" + id + "/" + iVote, 'ret');
    return false;
}
BxBlgEntry.prototype.freeze = function (id)
{				
    var oContent = new BxContent ();
    oContent.onLoadComplete = function (ret) {
		switch (ret) {
	        case '1':
	            alert('entry froze');
	            document.location = document.location;
	            break;
	        default:
			    alert('freezing failed');
        }
    }
    oContent.getXmlNodeValue (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_freeze/" + id, 'ret');
    return false;
}

BxBlgEntry.prototype.ping = function (id)
{
    var e = $('blg_trackbacks_ping');

    if (e) e.innerHTML = 'pinging...';

    var oContent = new BxContent ();
    oContent.onLoadComplete = function (ret) {
		if ('1' == ret)
            e.innerHTML = 'successfully pinged';
        else
		    e.innerHTML = 'ping failed';
    };
    oContent.getXmlNodeValue (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_trackback_ping_send/" + id, 'ret');

    return false;
}

BxBlgEntry.prototype.del = function (id)
{
	if (!this._confirm()) return false;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('1' == ret)
		{
			alert ('Entry deleted with its comments.');			
            $('msg_info').parentNode.innerHTML = 'Deleted Entry';
			return;
		}
		alert ('Delete failed');
	}	
	
	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_delete/?id=" + id, h, true);
  
    return false;
}

BxBlgEntry.prototype.report = function (id)
{
	if (!this._confirm()) return false;
		
    this._sendReport (id, 'report');
  
    return false;
}

BxBlgEntry.prototype.unreport = function (id)
{
	if (!this._confirm()) return false;
		
    this._sendReport (id, 'unreport');
  
    return false;
}

BxBlgEntry.prototype._sendReport = function (ids, uri)
{
	var $this = this;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('1' == ret)
		{
			alert ('Thank you! Entry ' + uri + 'ed.');
			$this.reload ();
			return;
		}
		alert (uri + ' error');
	}	
	
	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_" + uri + "/" + ids, h, true);

	return false;
}

BxBlgEntry.prototype.send = function (sDefText)
{			
	var f = document.forms[this._form].elements;
	sText = f['entry_text'].value;
	if (sText && sText.length && sText != sDefText)	
		return true;
		
	return false;
}

BxBlgEntry.prototype._confirm = function ()
{
	return window.confirm ('Are you sure?');
}

BxBlgEntry.prototype.insertSmile = function (title)
{
/*
    title = title.replace(/&/g, "&amp;");
    title = title.replace(/\"/g, "&quot;");
    title = title.replace(/</g, "&lt;");
    title = title.replace(/>/g, "&gt;");
*/
    var html = "<img class=\"sys_smile\" src=\"" + aBxConfig['urlModuleImg'] + "smiles/smile-" + title + ".png\" border=\"0\" alt=\"" + title + "\" title=\"" + title + "\" />";
    tinyMCE.execCommand("mceInsertContent", false, html);
}

BxBlgEntry.prototype.insertImage = function (sImageUrl)
{
    var html = "<img src=\"" + sImageUrl + "\" border=\"0\" />";
    tinyMCE.execCommand("mceInsertContent", false, html);
}

BxBlgEntry.prototype.filesReload = function (iEntryId)
{
    this._reloadList (iEntryId, 'blg_files_list', 'files_list/', 'attach_files.xsl');
}

BxBlgEntry.prototype.imagesReload = function (iEntryId)
{
    this._reloadList (iEntryId, 'blg_images_list', 'images_list/', 'attach_images.xsl');
        }

BxBlgEntry.prototype.videosReload = function (iEntryId)
        {
    this._reloadList (iEntryId, 'blg_videos_list', 'videos_list/', 'attach_videos.xsl');
        }
		    
BxBlgEntry.prototype._reloadList = function (iEntryId, sDivId, sUrl, sXsl)
{
    $(sDivId).innerHTML = 'loading...';
	var o = new BxContent ();
    o.setForceServerXsl(1);    
    o.replace (sDivId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + sUrl + iEntryId + '/?date=' + (new Date()), aBxConfig['urlModuleXsl'] + sXsl);
}

BxBlgEntry.prototype.toggleUploadButton = function (sBtnId, bActivate)
{
    var e = $(sBtnId); 
    if (!e) return;
    e.disabled = bActivate ? false : true;
    e.value = bActivate ? 'UPLOAD' : 'UPLOADING...';
}

BxBlgEntry.prototype.insertVideo = function (sId)
{
    var html = '<img class="mceItemFlash" src="' + aBxConfig['urlRoot'] + '3rdparty/tiny_mce/plugins/media/img/trans.gif" width="350" height="330" border="0" title="id:\'ray_player_object\',align:\'middle\',allowScriptAccess:\'always\',quality:\'high\',allowFullScreen:\'true\',base:\'' + aBxConfig['urlRoot'] + 'ray_suit_blog/modules/movie/\',FlashVars:\'module=movie&app=player&id=&file=' + sId + '&url=' + aBxConfig['urlRoot'] + 'ray_suit_blog/XML.php\',src:\'' + aBxConfig['urlRoot'] + 'ray_suit_blog/modules/global/app/holder.swf\'"  />';

    tinyMCE.execCommand("mceInsertContent", false, html);
}

BxBlgEntry.prototype.insertEmbed = function (sSrc)
{

    var html = '<img class="mceItemFlash" src="' + aBxConfig['urlRoot'] + '3rdparty/tiny_mce/plugins/media/img/trans.gif" width="425" height="350" border="0" title="allowFullScreen:\'true\',src:\'' + sSrc + '\'"  />';
    tinyMCE.execCommand("mceInsertContent", false, html);

}

BxBlgEntry.prototype.insertFile =  function (sFileName, sFileUrl)
{
    var html = '<a href="' + sFileUrl + '" title="' + sFileName + '">' + sFileName + '</a>';
    tinyMCE.execCommand("mceInsertContent", false, html);
}

BxBlgEntry.prototype.setVideoListToReload = function (iEntryId)
{
    $('blg_videos_list').innerHTML = '<a href="javascript:gBxBlgEntry.videosReload(\'' + iEntryId + '\');void(0);">refresh</a>';
}

BxBlgEntry.prototype.embedRemove = function (iEntryId, iId)
{
    var $this = this;
    this._removeFromList (iEntryId, iId, 'xml_embed_remove/', function () { $this.videosReload (iEntryId); } );
}

BxBlgEntry.prototype.imageRemove = function (iEntryId, iId)
{
    var $this = this;
    this._removeFromList (iEntryId, iId, 'xml_image_remove/', function () { $this.imagesReload (iEntryId); } );
}

BxBlgEntry.prototype.fileRemove = function (iEntryId, iId)
{
    var $this = this;
    this._removeFromList (iEntryId, iId, 'xml_file_remove/', function () { $this.filesReload (iEntryId); } );
}

BxBlgEntry.prototype._removeFromList = function (iEntryId, iId, sUrl, callbackSuccess)
{
    var $this = this;
    var oContent = new BxContent ();
    oContent.onLoadComplete = function (ret)
    {
		if ('1' == ret)
        {
            alert('successfully removed');
            callbackSuccess ();
            return;
        }
            alert('remove failed');   
        }
    oContent.getXmlNodeValue (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + sUrl + "?id=" + iId, 'ret');
}
		    

BxBlgEntry.prototype.makeEntryThumb = function (iEntryId, iId)
{
    var $this = this;
    var oContent = new BxContent ();
    oContent.onLoadComplete = function (ret)
    {
		if ('1' == ret)
        {
            $this.imagesReload (iEntryId);
            return;
        }
        alert('operation failed');   
    };
    oContent.getXmlNodeValue (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_make_thumb/?img_id=" + iId + "&entry_id=" + iEntryId, 'ret');
};


/************************************************
 *				Hide/Unhide functions			*
 ************************************************/
BxBlgEntry.prototype.hideWnd = function(iId) {	
	var oWindow = new BxWnd(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'xml_hide_wnd/' + iId, aBxConfig['urlModuleXsl'] + 'hide_wnd.xsl');

	oWindow.setWidth(380);
	oWindow.show('Hide', 1);
	
	return false; 		
};
BxBlgEntry.prototype.hide = function(iId, sType) {	
	var loadComplete = function(oResult) {
		var oRequest = new BxXmlRequest('','','');
		iResult = parseInt(oRequest.getRetNodeValue(oResult, 'result'));								
		switch(iResult) {
			case 0:
				alert('Entry was hidden successfully');
				document.location = aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'home';	
				break;
			case 1:
				alert('Incorrect input data. Please report.');
				break;
			case 2:
				alert('Cannot save information in database');
				break;				
		}		
	};
	new BxXmlRequest(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_hide/" + iId + "/" + sType, loadComplete, true);	
};
BxBlgEntry.prototype.unhide = function(iId) {	
	var loadComplete = function(oResult) {
		var oRequest = new BxXmlRequest('','','');
		iResult = parseInt(oRequest.getRetNodeValue(oResult, 'result'));								
		switch(iResult) {
			case 0:
				alert('Entry was activated successfully');
				document.location = document.location;	
				break;
			case 1:
				alert('Incorrect input data. Please report.');
				break;
			case 2:
				alert('Cannot save information in database');
				break;				
		}		
	};	
	new BxXmlRequest(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_unhide/" + iId, loadComplete, true);	
};
BxBlgEntry.prototype.hideHistory = function(iId) {
	var oWindow = new BxWnd(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'xml_hide_history_wnd/' + iId, aBxConfig['urlModuleXsl'] + 'hide_history_wnd.xsl');

	oWindow.setWidth(380);
	oWindow.show ('Hide History', 1);
	
	return false;	
};


BxBlgEntry.prototype.approveHistory = function(iId) {
	var oWindow = new BxWnd(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'xml_approve_history/' + iId, aBxConfig['urlModuleXsl'] + 'approve_history_wnd.xsl');

	oWindow.setWidth(380);
	oWindow.show ('Approving History', 1);
	
	return false;	
}
BxBlgEntry.prototype.postWarning = function() {
	var oWindow = new BxWnd(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'xml_post_warning', aBxConfig['urlModuleXsl'] + 'post_warning_wnd.xsl');

	oWindow.setWidth(380);
	oWindow.show('Please, note...', 1);
	
	return false;	
}

/************************************************
 *			"Under Dispute" functions			*
 ************************************************/
BxBlgEntry.prototype.markUnderDispute = function(iId) {
	var oWindow = new BxWnd(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + 'xml_mark_under_dispute_wnd/' + iId, aBxConfig['urlModuleXsl'] + 'wnd_under_dispute_form.xsl');

	oWindow.setWidth(380);
	oWindow.show('Mark as "Under Dispute"', 1);
	
	return false;
};
BxBlgEntry.prototype.unmarkUnderDispute = function(iId) {
	this.changeTrustCertifStatus(iId, 'unmark_under_dispute');
};
