/**
 * $Id: catalog.js 8745 2011-04-12 09:35:50Z vincent $
 **/

/**
 * My catalog management
 * Add / Remove notices
 * Add notices individually, by page, all notices founded
 */

// Add to my catalog
function addToMyCatalog(method, index) {
	// Get notices selected
	notices = getSelectedNotices((method == 'selection' || method == 'notice'));
	if (method == 'selection' && notices.length == 0) {
		winMessage('ERROR', '', error_selectOneNoticeAtLeast);
		return false;
	}
	// Prompt for catalog name
	if (!Ext.getDom('catalog_id' + index)) {
		xajax_catalogNoticesAdd(method, form_catalogDefaultName, notices);
	} else if (Ext.getDom('catalog_id' + index).value == 'new') {
		Ext.Msg.prompt('', form_catalogName, function(btn, text){if(text == '') {winMessage('ERROR', '', error_catalogNameRequired);return false;} if (btn == 'ok') {xajax_catalogNoticesAdd(method, text, notices);}}, this, false, form_catalogDefaultName);
	} else {
		xajax_catalogNoticesAdd(method, Ext.getDom('catalog_id' + index).value, notices);
	}
}

// Delete from my catalog
function deleteFromMyCatalog(method, catalog_id) {
	// case delete all
	if (method == 'all') {
		xajax_catalogNoticesRemove(catalog_id, method);
	} else {
	// case delete page or delete selected
		// Get notices selected
		notices = getSelectedNotices((method == 'selection' || method == 'notice'));
		if (method == 'selection' && notices.length == 0) {
			winMessage('ERROR', '', error_selectOneNoticeAtLeast);
			return false;
		}
		xajax_catalogNoticesRemove(catalog_id, notices);
	}
}

// Make selected notices array
function getSelectedNotices(onlySelected) {
	var notices = new Array();
	checkboxes = Ext.query('.noticeCheckbox');
	for (var i=0; i<checkboxes.length; i++) {
		if (!onlySelected || checkboxes[i].checked) {
			notices.push(checkboxes[i].value);
		}
	}
	return notices;
}

// Open new popup to view document
function openfullscreen(name, url) {
	
	//Check browser
	var isNav = (navigator.appName == "Netscape")?1:0;
	var isIE = (navigator.appName.indexOf("Microsoft")!= -1)?1:0;
	//Check Platform
	var isMac=(navigator.platform.indexOf("Mac")>-1)?1:0;
	var isWin=(navigator.platform.indexOf("Win")>-1)?1:0;
	//Set global window options
	var opts = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizeable=yes,copyhistory=no,menubar=no";

	//Set platform and browser specific options
	if (isNav)
	{
		//Navigator windows have outerWidth and outerHeight properties	
		//use these to set window size to the available screen height and width
		opts += ",outerWidth=" + screen.availWidth+",outerHeight="+screen.availHeight;
	}
	else if (isIE) {
		//IE has a "full-screen" option which can be used by placing "fullscreen=yes" here
		opts = opts+",left=0,top=0";
		//To size the window (rather than open in fullscreenmode) we need to know the padding
		//i.e. the space taken by the title bar and windowedges. These values are subtracted
		//from the available screen width and height (differentfor Macs). If the new window
		//is opened with toolbars, status bar, etc. the valueshere should be changed to
		//compensate for the new features.
		if (isMac) {
			//this uses a value of 13 for the extra width and 32 for the extra height
			opts = opts+",width="+(screen.availWidth - 13)+",height="+(screen.availHeight - 32);
		} else if (isWin) {
			//this uses a value of 12 for the extra width and 25 for the extra height
			opts = opts+",width="+(screen.availWidth - 12)+",height="+(screen.availHeight - 25);
		} else {
			opts = opts+",width="+screen.availWidth+",height="+screen.availHeight;
		}
	} else {
		//return;
	}
	opts += ",width=" + screen.availWidth + ",height=" + screen.availHeight;

	//Open a new window
	var newWin = window.open(url,name,opts);
	newWin.focus();
	
	//Move to 0,0 (JavaScript 1.2)
	if (parseInt(navigator.appVersion) >= 4) {
		newWin.moveTo(0,0);
	}
}

// Show all textplain search results for a given notice
function showAllTextplain(notice_id) {
	$('li.textPlain' + notice_id).each(function() {
		$this = $(this);
		if ($this.hasClass('d-none')) {
			$this.slideDown(function () { $(this).removeClass('d-none').removeClass('d-block'); });
		} else {
			$this.slideUp(function  () { $(this).removeClass('d-block').addClass('d-none'); });
		}
	});
}

// Hide all OCR extracts
showExtracts = true;
function hideTextplain() {
	showExtracts = !showExtracts;
	$('.ocrExtract').each(function() {
		$this = $(this);
		if (!showExtracts) {
				$this.slideUp(function() {$(this).addClass('d-none')});
		} else {
			if (!$this.hasClass('otherExtracts')) {
				$this.slideDown(function() {$(this).removeClass('d-none')});
			}
		}
	});
	if (showExtracts) {
		$('#textplainExtractsToogleLink').html(action_hideTexplainExtracts).attr('title', action_hideTexplainExtracts);
	} else {
		$('#textplainExtractsToogleLink').html(action_showTexplainExtracts).attr('title', action_showTexplainExtracts);
	}
}

// Toogle search results in hierarchy mode
function searchToogleNode(notice_id, numberChildsResults) {
	if ($('#childs_' + notice_id).hasClass('d-none')) {
		// Show childs results/tree
		$('#childs_' + notice_id).removeClass('d-none').addClass('d-block');
		$('#imgChilds' + notice_id).attr('src', BASE + '/images/icons/cancel.gif');
		// Call ajax command only one time
		if ($('#childs_' + notice_id).html() == '') {
			$('#childs_' + notice_id).html('<img src="' + BASE + '/images/icons/wait.gif" />');
			xajax_searchHierarchyChildsLoad(notice_id);
		}
		// Update "View childs" button label
		$('#btnChilds' + notice_id).attr('value', action_hideResults);
	
	} else {
		// Hide childs results/tree
		$('#childs_' + notice_id).addClass('d-none');
		$('#imgChilds' + notice_id).attr('src', BASE + '/images/icons/add.gif');
		// Update "View childs" button label
		$('#btnChilds' + notice_id).attr('value', action_showResults_start + ' ' + numberChildsResults + ' ' + action_showResults_end);
	}
}
