Lib.Utils.EventUtils.addEvent(window, 'load', function() { new PrinterMain() });

PrinterMain = function() {
    this._printerFriendlyElement = document.getElementById('printerFriendlyLink');
    this._sendToPrinterElement = document.getElementById('sendToPrinterLink');
    this._contentElement = document.getElementById('content');
    this._attachPrinterFriendlyEvent();
    this._attachSendToPrinterEvent();
    this._hideBoxes();
}

PrinterMain.prototype._attachPrinterFriendlyEvent = function() {
    if (Lib.Utils.ObjectUtils.isDefined(this._printerFriendlyElement)) {
        Lib.Utils.EventUtils.addEvent(this._printerFriendlyElement, 'click', this._openPrinterFriendlyWindow, this);
    }
}

PrinterMain.prototype._openPrinterFriendlyWindow = function(evt) {
	Lib.Utils.EventUtils.cancelDefaultAction(evt);
	var additionalParams = '';
	if (cssQuery('.currentTab', this._contentElement).length > 0) {
		var currentTabId = cssQuery('.currentTab', this._contentElement)[0].firstChild.id;
		additionalParams = '&tabId=' + currentTabId;
	}
    window.open(this._printerFriendlyElement.href + additionalParams, 
                'printerFriendly', 
                'width=760, height=580, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=no');
}

PrinterMain.prototype._attachSendToPrinterEvent = function() {
	if (Lib.Utils.ObjectUtils.isDefined(this._sendToPrinterElement)) {
		if (Lib.Utils.ObjectUtils.isDefined(document.getElementById('ulTabs'))) {
			var newsReleases = new NewsReleases();
			newsReleases._changeTab(document.getElementById(this._getTabId()), this._contentElement)();
		}
		Lib.Utils.EventUtils.addEvent(this._sendToPrinterElement, 'click', this._sendToPrinter, this);
	}
}

PrinterMain.prototype._sendToPrinter = function(evt) {
	Lib.Utils.EventUtils.cancelDefaultAction(evt);
	window.print();
}

PrinterMain.prototype._hideBoxes = function() {
	if (Lib.Utils.ObjectUtils.isDefined(this._sendToPrinterElement)) {
		//Hide the related resources box for print friendly
		var hideBoxes = document.getElementsByTagName('div');
		for (var i = 0; i < hideBoxes.length; i++) {
			if (hideBoxes[i].id == "box_related" || hideBoxes[i].id == "box_otp") {
				hideBoxes[i].style.display = "none";
			}
		}
	}
}

PrinterMain.prototype._getTabId = function() {
	if (location.href.indexOf('tabId') > -1) {
		var tabIdStart = location.href.indexOf('tabId');
		tabIdStart = location.href.substring(tabIdStart).indexOf('=') + tabIdStart + 1;
		var tabIdEnd = location.href.substring(tabIdStart).indexOf('&') + tabIdStart + 1;
		if (tabIdStart != tabIdEnd) {
			return location.href.substring(tabIdStart, tabIdEnd - 1);
		}
		return location.href.substring(tabIdStart);
	}
}