/**
 * AUDI JavaScript library: Set print styles [func_print]
 * 
 * @projectDescription	Print related functions
 * @namespace			print
 *
 * @author 				$Author: mguderley $
 * @version				$Revision: 10521 $
 * @copyright			NEUE DIGITALE GmbH, Berlin
 * 
 * @jslint: 2008-10-31
 * 
 * @file:				audi_ngw.print.js
 */

/* create namespace */
audi_ngw.namespace(audi_ngw,'print');

/**
 * Opens popup
 * @param {String} sUrl
 * @return {Void}
 */
audi_ngw.print.open = function (sUrl) {
	return window.open(sUrl,'funcPrint');
};

/**
 * Removes inline styles on specified elments
 * @return {Void}
 */
audi_ngw.print.unsetInlineCss = function(){
	// reset inline styles if necessary
	if (jQuery('.hasModal').size()){
		jQuery('html').css({'overflow': ''});
		jQuery('#audi_modal_super_wrapper').css({'position': '','left':'','top':''});
		jQuery('#audi_modal_wrapper').css({'width': 'auto'});
		jQuery('#audi_modal_container').css({'position': '','left':'','top':'','width':''});
		jQuery('#audi_modal_overlay').css({'display':''});
	}
	jQuery('#audi_navigation_breadcrumb').css({'visibility':'','height':'','line-height':'','font-size':'','text-indent':'','position':'','left':'','top':'','width':''});
	jQuery('ul.func_pagelinks, ul.toolbox').css({'display':''});
	jQuery('#audi_gallery_fullscreen').css({'display':''});
	
	//RESET VDT BACKGROUND FOR PRINT-LAYOUT
	jQuery('html.audi_vdt').css({'background':'none'});
	jQuery('html.audi_vdt div.redClip_S').css({'background':'none'});
	jQuery('html.audi_vdt div.redClip_M').css({'background':'none'});
	jQuery('html.audi_vdt div.redClip_L').css({'background':'none'});
};

/**
 * Removes all external stylesheets except media==='print' and sets the remaining ones as media==='screen,print'
 * @return {Void}
 */
audi_ngw.print.setCss = function () {
	// ff 2 doesn't repaint if we're switching the media type, so we're going to change the href of the first stylesheet[media=screen] and remove all others
	// different approach would be to remove everything and add the print styles by adding a new element, similiar to the "print preview css" but this would cause another request

//	if (jQuery.browser.mozilla && jQuery.browser.version < "2") {
		// get all media screen
		jQuery("link[media*='screen']")
			// set first match's href to print.css 
			.eq(0).attr('href',jQuery("link[media*='print']").eq(0).attr('href'))
			// revert selection to all matches
			.end()
			// remove first element
			.slice(1)
			// trash media attribute of the remaining link elements
			.attr('media','tty')
			// remove remaining stylesheets
			.remove();
//	} 
//	// default: switch media type on print css and remove the original screen stylesheets
//	else {
//		alert('else')
//		// get all screen styles 
//		jQuery("link[media*='screen']")
//			// trash media attribute 
//			.attr('media','tty')
//			// disable
//			.attr('disabled','true')
//			// remove
//			.remove();
//		// get all print styles
//		jQuery("link[media*='print']")
//			// set media to screen and print
//			.attr('media','screen,projection,tv,print')
//			// remove diabled
//			.attr('disabled','false')
//			// set rel to stylesheet
//			.attr('rel','stylesheet');
//	}
	
	// add print preview.css
	jQuery(audi_ngw.print._oOptions.sHTMLPrintPreviewLink)
		.appendTo(jQuery('head').get(0));
};

/**
 * Removes all script-elements
 * @return {Void}
 */
audi_ngw.print.removeScripts = function () {
	// unbind all eventhandlers
	jQuery('*').unbind();
	// remove all scripts except this one 
//	jQuery("script")
//		.filter(function() {
//			if (this.src.indexOf('audi.print') === -1 || this.src.indexOf('jquery') === -1 ) {
//				return this;
//			}
//		})
//		.remove();
	// remove remaining script
//	jQuery("script").remove();
};

/**
 * Adds print-preview specific HTMLelements
 * @return {Void}
 */
audi_ngw.print.addPreviewElements = function () {
	// add print preview elements
	jQuery(audi_ngw.print._oOptions.sIdAddPrintPreviewLabel)
		.prepend(audi_ngw.print._oOptions.sHTMLPrintPreviewLabel);
	jQuery(audi_ngw.print._oOptions.sIdAddPrintPreviewElement)
		.before(audi_ngw.print._oOptions.sHTMLPrintPreviewElement);
	jQuery('body')
		.append(audi_ngw.print._oOptions.sHTMLPrintPreviewElement);


	// add click event on anchors inside .audi_print_bar
	jQuery('.audi_print_bar a')
		.unbind('click.print')
		.bind('click.print',audi_ngw.print.clickPrint);
};

/**
 * Adds print-preview specific HTMLelements
 * @return {Void}
 */
audi_ngw.print.clickPrint = function () {
	window.print();
};

/**
 * Sets current html to print preview
 * @return {Void}
 */
audi_ngw.print.setPreview = function () {

	if (jQuery('.audi_print_bar').size()) { return; }

	// disable flash 
	audi_ngw.flash.disableFlash = true;
 
	audi_ngw.print.removeScripts();
	audi_ngw.print.unsetInlineCss();
	audi_ngw.print.addPreviewElements();
	audi_ngw.print.setCss();
	
	return false;
};

/**
 * Initialize
 * @constructor
 * @return {Void}
 */
audi_ngw.print.initiate = function () {
	
	this.__className = 'audi_ngw.print';
	this.__classVersion= '1.2';
	
	this._oOptions = {};
	this._oOptions.sHTMLPrintPreviewLink = '<link charset="utf-8" media="screen" type="text/css" href="'+audi_ngw.config.cssPrintPreview+'" rel="stylesheet"/>';
	this._oOptions.sIdAddPrintPreviewLabel = '#audi_container_header';
	this._oOptions.sHTMLPrintPreviewLabel = '<p id="audi_print_label">'+audi_ngw.dictionary.PRINTLABEL+'</p>';
	this._oOptions.sIdAddPrintPreviewElement = '#audi_container_page';
	this._oOptions.sHTMLPrintPreviewElement = '<div class="audi_print_bar"><a href="#"><img alt="Drucken" src="'+audi_ngw.docroot+'img/audi_sprite_icon_print.gif"/>'+audi_ngw.dictionary.PRINT+'</a></div>';

	// set load event
	jQuery(window).bind('load.print', function (){
		// clear memory 
		jQuery(window).unbind('load.print');
		// check window name to check for "print preview"
		if (window.name == 'funcPrint') {
			// set preview
			audi_ngw.print.setPreview();
			window.name = '';
			window.setTimeout(function () {
				window.print();
			}, 500);
		}
	});
	
};

/*
// initialize
jQuery(function(){
	audi_ngw.print.initiate();
});
*/
