/*
	Common methods and variables
*/
/* ========  Core & Window section ======== */
Browser.Engine.ie6 = (function(){
	if( Browser.Engine.trident && Browser.Engine.version<=4)
		return true;
	else
		return false;
})();
var Engine = Browser.Engine;

// popup element object
window.Popup = new Element('div', {
	'class': 'ui-popup',
	'html': '<div class="s t">&nbsp;</div>' + 
			'<div class="s b">&nbsp;</div>' + 
			'<div class="s l">&nbsp;</div>' + 
			'<div class="s r">&nbsp;</div>' + 
			'<div class="s tl">&nbsp;</div>' + 
			'<div class="s tr">&nbsp;</div>' + 
			'<div class="s bl">&nbsp;</div>' + 
			'<div class="s br">&nbsp;</div>' +
			'<a>'+l10n['ui_close']+'</a>'
	}
);

// method shows popup window with aimed image
window.Popup.show = function(elem){
	// Try to show popup with embedded element, if it passed
	if (elem) {
		this.hide();
		this.content = elem;
		this.content
			.set('title', l10n['ui_close'])
			.inject(this, 'bottom');
		this
			.inject(document.body, 'bottom')
			.fade('hide').fade('in')
			.setStyles({
				'margin-left': -this.getSize().x/2,
				'top': document.getScroll().y + 50 + 'px'
			});
		this.getElements('a, img').addEvent('click', this.hide.bind(this));
	}
	// returns Popup object anyway
	return this;
}

// method hides popup window
window.Popup.hide = function(){
	if (this.content) this.content.destroy();
	this.dispose();
	return this;
}




/* ========  Document section ======== */
document.printVersion = false;

// define screen and CSS link objects
document.CSS = {
	screen: $$('link[media=screen]'),
	print: $$('link[media=print]')
}

// define print buttons
document.Buttons = {
	print: new Element('input', {
		'type': 'submit',
		'value': l10n['ui_toprint'],
		events: {
			'click': function(){
				window.print();
				return false;
			}
		}
	}),
	close: new Element('input', {
		'type': 'reset',
		'value': l10n['ui_close'],
		events: {
			'click': function(){
				document.printPreview(false);
				}
			}
	})
}

Document.implement({
	// method switches the document to print version
	_showPrintVersion: function(){
		this.printVersion = true;
		$$('table').setProperty('cellspacing', '2mm');

		if ( !Engine.ie6 ) {
			this.Buttons.print.inject($('wrapper'), 'top');
		}
		this.Buttons.close.inject($('wrapper'), 'top');

		this.CSS.screen.dispose();
		this.CSS.print.set('media', 'all');
		if ( Engine.name === 'gecko' && Engine.version <= 18 ) {
			this.CSS.print.dispose().inject(document.head);
		}

		return this;
	},

	// method switches the document to screen version
	_showScreenVersion: function(){
		this.printVersion = false;
		$$('table').setProperty('cellspacing', '0');

		this.Buttons.close.dispose();
		this.Buttons.print.dispose();

		this.CSS.screen.inject(this.CSS.print[0], 'after');
		this.CSS.print.set('media', 'print');
		if ( Engine.name === 'gecko' && Engine.version <= 18 ) {
			this.CSS.print.dispose().inject(document.head);
		}

		return this;
	},

	// wrapper for print/screen switching methods, wich make shading effect during switching through states
	printPreview: function(show){

		if ( Engine.trident || (Engine.presto && Engine.version <= 925) ) {
			if (show) document._showPrintVersion();
			else document._showScreenVersion();
		} else {
			$$('#wrapper, #footer').fade('out');
			(function(){
				if (show) document._showPrintVersion();
				else document._showScreenVersion();
				$$('#wrapper, #footer').fade('in');
			}).delay(1000);
		}

		return this;
	},
	
	// function creates iframe, to put under submenu
	fixIE6selects: function(action, elem){
		if ( Engine.ie6 ) {
			if (action) {
				// show iframe
				this.menu = elem.getCoordinates();

				this.iframe = new Element('iframe', {
					'src': '/img/design/x.png',
					'frameborder': '0',
					'scrolling': 'no',
					'width': this.menu.width - 6,
					'height': this.menu.height - 5,
					'styles': {
						'display': 'block',
						'position': 'absolute',
						'top': '35px',
						'left': '2px'
						}
					});

				this.iframe.inject(elem, 'before');
			} else {
				// hide iframe
				this.iframe.dispose();
			}
		}
		return this;
	}
});




/* ========  Element section ======== */
Element.implement({

	// hide element with 'display: none;' CSS property
	hide: function(){
		this.setStyle('display', 'none');
		return this;
	},

	// show element with display property, pointed in first argument ('display: block;' as default)
	show: function(display){
		if (display)
			this.setStyle('display', display);
		else
			this.setStyle('display', 'block');
		return this;
	},

	// release tabs
	releaseTabs: function(){
		this.getElements('dt').addEvents({
			'mouseover': function(){
				if(!this.hasClass('this')) this.removeClass('corners-4').addClass('corners-3');
				return true;
			},
			'mouseout': function(){
				if(!this.hasClass('this')) this.removeClass('corners-3').addClass('corners-4');
				return true;
			},
			'click': function(){
				if(!this.hasClass('this')) {
					this.getParent().getElements('dt, dd').removeClass('this').fireEvent('mouseout');
					this.fireEvent('mouseover').addClass('this').getNext().addClass('this');
				}
				return true;
			}
		});
		return this;
	},

	// method for adding corners FOR BOXES
	releaseCorners: function(){
		if ( Engine.trident || Engine.presto) {
			new Element('div', {'class': 'fix-corners fix-corners-tl', 'html': '&nbsp;'}).inject(this, 'top');
			new Element('div', {'class': 'fix-corners fix-corners-tr', 'html': '&nbsp;'}).inject(this, 'top');
			new Element('div', {'class': 'fix-corners fix-corners-bl', 'html': '&nbsp;'}).inject(this, 'top');
			new Element('div', {'class': 'fix-corners fix-corners-br', 'html': '&nbsp;'}).inject(this, 'top');
		}
		return this;
	},

	// method for adding corners FOR TABLES
	releaseTable: function(){
		// get table width
		if ( this.get('width') ) {
			this.width = this.get('width');
		} else {
			this.width = '99%';
		};

		// create wrapper for table
		this.wrapper = new Element('div', {
			'class': 'fix-table',
			'styles': {
				'width': this.width
			}
		}).wraps(this);

		// add top corners
		new Element('div', {
			'class': 'fix-table-top corners-5',
			'html': '<div class="fix-corners fix-corners-tl">&nbsp;</div><div class="fix-corners fix-corners-tr">&nbsp;</div>'
		}).inject(this.wrapper, 'top');

		// add bottom corners
		new Element('div', {
			'class': 'fix-table-bottom corners-5',
			'html': '<div class="fix-corners fix-corners-bl">&nbsp;</div><div class="fix-corners fix-corners-br">&nbsp;</div>'
		}).inject(this.wrapper, 'bottom');

		// check header
		if ( !this.getElement('tr:first-child td') ) {
			this.wrapper.addClass('fix-table-complete');
			this.getElements('tr:even').addClass('odd');
		} else {
			this.getElements('tr:odd').addClass('odd');
		}

		// check browser
		if ( Engine.gecko && Engine.version >= 19 ) {
			this.wrapper.addClass('fix-table-ff3');
		} else {
			this.wrapper.addClass('fix-table-ff2');
		}

		return this;
	},

	// method removes One Pixel bug in IE 6 when you use 'right' and 'bottom' properties
	fixOnePixel: function(){
		if ( this.getCoordinates().width % 2 != 0) {
			this.getChildren('.fix-corners-tr').setStyle('right', '-2px');
			this.getChildren('.fix-corners-br').setStyle('right', '-2px');
		} else {
			this.getChildren('.fix-corners-tr').setStyle('right', '-1px');
			this.getChildren('.fix-corners-br').setStyle('right', '-1px');
		}

		if ( this.getCoordinates().height % 2 != 0) {
			this.getChildren('.fix-corners-bl').setStyle('bottom', '-2px');
			this.getChildren('.fix-corners-br').setStyle('bottom', '-2px');
		} else {
			this.getChildren('.fix-corners-bl').setStyle('bottom', '-1px');
			this.getChildren('.fix-corners-br').setStyle('bottom', '-1px');
		}

		return this;
	},

	// method works with inputs default VALUE
	resetValue: function(){
		this.addEvents({
			'focus': function(){
				if( this.get('value') === this.retrieve('defaultValue'))
					this.set('value', '');
				return false;
			},
			'blur': function(){
				if( this.get('value') === '')
					this.set('value', this.retrieve('defaultValue'));
				return false;
			}
		});
		return this;
	}

});