var Style = {
	assign: function(element,style) {
		element = _e(element);
		if(typeof (style) == 'string') {
			if( typeof( element.style.cssText ) != 'undefined' ) element.style.cssText = style;
			else element.setAttribute('style',style);
		}
		else {
			styleStr = '';
			for(x in style)
				switch(x) {
				case 'zIndex':
					styleStr += 'z-index: '+style[x]+';';
					break;
				case 'marginTop':
					styleStr += 'margin-top: '+style[x]+';';
					break;
				case 'marginLeft':
					styleStr += 'margin-left: '+style[x]+';';
					break;
				default:
					styleStr += x+': '+style[x]+';';
					break;
				}
			if( typeof( element.style.cssText ) != 'undefined' ) element.style.cssText = styleStr;
			else element.setAttribute('style',styleStr);
		}
		return element;
	}
};

function centerElementEx(element,style)
{
	element = _e(element);
	var hasElement = document.documentElement && document.documentElement.clientWidth;
	var w = browser.isIE ? (hasElement ? document.documentElement.clientWidth : document.body.clientWidth) : window.innerWidth;
	var h = browser.isIE ? (hasElement ? document.documentElement.clientHeight : document.body.clientHeight) : window.innerHeight;
	var offsetTop = _int((browser.isIE || browser.isOpera ? (hasElement ? document.documentElement.scrollTop : document.body.scrollTop) : window.scrollY),0);
	var offsetLeft = _int((browser.isIE || browser.isOpera ? (hasElement ? document.documentElement.scrollLeft : document.body.scrollLeft) : window.scrollX),0);

	element.style.position = 'absolute';
	element.style.left = '0px';
	element.style.top = '0px';

	var eo = _offset(element);
	offsetLeft -= eo[0];
	offsetTop -= eo[1];

	var x = offsetLeft+(w - style.width.replace('px',''))/2;
	var y = offsetTop+(h - style.height.replace('px',''))/2;

	style.left = _int(x,0)+'px';
	style.top = _int(y,0)+'px';
/*
	style.position = 'absolute';
	var e = element.offsetParent;
	while(e)
	{
		offsetTop -= e.offsetTop;
		offsetLeft -= e.offsetLeft;
		e = e.offsetParent;
	}

	style.left = (offsetLeft+Math.floor((w-parseInt(style.width.replace('px',''), 10))/2))+"px";
	style.top = (offsetTop+Math.floor((h-parseInt(style.height.replace('px',''), 10))/2))+"px";*/
}

var Preview = {
	element: null,
	elementStyle: null,
	imageElement: null,
	imageStyle: null,
	promoLabelElement: null,
	promoLabelStyle: null,
	closeButton: null,
	closeButtonStyle: null,
	iefixElement: null,
	iefixStyle: null,

	width: null,
	height: null,
	imgWidth: null,
	imgHeight: null,
	opacity: 0,
	speed:  1,
	handle: null,

	create: function() {
		this.element = document.createElement('div');
		this.elementStyle = {
			position: 'absolute',
			overflow: "hidden",
			visibility: "hidden",
			background: "url('img/bg.png') repeat",
			width: '300px',
			height: '300px',
			left: '0px',
			top: '0px',
			zIndex: '1300'
		};
		Style.assign(this.element,this.elementStyle);

		//------- zamykanie na kliknięcie
		this.element.onclick = this.close.bind(this);

		document.body.appendChild(this.element);
		if (browser.isIE6) {
			this.iefixElement = document.createElement('iframe');
			this.iefixStyle = {
				position:'absolute',
				top:'0',
				left:'0',
				zIndex:'-1',
				filter:'mask()'
			};
			Style.assign(this.iefixElement, this.iefixStyle);
			this.element.appendChild(this.iefixElement);
		}
	},
	fitToWindow: function(scale) {
		var has_element = document.documentElement && document.documentElement.clientWidth;
		w = browser.isIE ? (has_element ? document.documentElement.clientWidth : document.body.clientWidth) : window.innerWidth;
		h = browser.isIE ? (has_element ? document.documentElement.clientHeight : document.body.clientHeight) : window.innerHeight;

		aw = (Math.round(scale*w));
		ah = (Math.round(scale*h));

		if(this.imgWidth-40 > aw-100) {
			this.imgHeight = Math.round(((aw-100)/(this.imgWidth-40))*(this.imgHeight-40));
			this.imgWidth = (aw-100);
		}
		if(this.imgHeight-40 > ah-100) {
			this.imgWidth = Math.round(((ah-100)/(this.imgHeight-40))*(this.imgWidth-40));
			this.imgHeight = (ah-100);
		}

		this.elementStyle.width = w+'px';
		this.elementStyle.height = h+'px';
		this.imageStyle.width = this.imgWidth +'px';
		this.imageStyle.height = this.imgHeight +'px';

		centerElementEx(this.element,this.elementStyle);
		centerElementEx(this.imageElement,this.imageStyle);
		this.imageStyle.left = (_int(this.imageStyle.left,0)-25)+'px';
		this.imageStyle.top = (_int(this.imageStyle.top,0)-25)+'px';
	},
	show: function(img) {
		if(!this.element) this.create();

		if(!this.imageElement) {
			this.imageElement = document.createElement('img');
			//this.element.appendChild(this.imageElement);
			this.imageElement.onclick = this.close.bind(this);
			document.body.appendChild(this.imageElement);

			var has_element = document.documentElement && document.documentElement.clientWidth;
			var w = browser.isIE ? (has_element ? document.documentElement.clientWidth : document.body.clientWidth) : window.innerWidth;

			this.closeButton = document.createElement('img')
			this.closeButton.src = 'img/zamknij.gif';
			this.closeButtonStyle = {
				visibility: "hidden",
				position: 'absolute',
				left: (w-this.closeButton.width-20)+'px',
				top: '5px'
			};
			this.closeButton.onclick = this.close.bind(this);
			Style.assign(this.closeButton,this.closeButtonStyle);
			this.element.appendChild(this.closeButton);
		}

		this.imageStyle = {
			visibility: "hidden",
			border: "#FFFFFF solid 20px",
			overflow: "hidden",
			position: 'absolute',
			zIndex: '1301'
		};
		Style.assign(this.imageElement,this.imageStyle);

		this.imageElement.src = img.src;

		this.imgWidth = img.width;
		this.imgHeight = img.height;

		this.fitToWindow(0.98);

		this.elementStyle.visibility = 'visible';
		this.imageStyle.visibility = 'visible';
		this.closeButtonStyle.visibility = 'visible';
		if(browser.isIE && (typeof document.body.style.maxHeight == "undefined" )) {
				this.elementStyle.background = "transparent";
				this.elementStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/bg.png', sizingMethod='scale')";
		}
		else this.elementStyle.background = "url('img/bg.png') repeat";

		Style.assign(this.element,this.elementStyle);
		Style.assign(this.imageElement,this.imageStyle);
		Style.assign(this.closeButton,this.closeButtonStyle);
		/*
		this.opacity = 20;
		this.frame();
		this.handle = window.setInterval(this.frame.bind(this),this.speed);
		*/

		if (browser.isIE6) {
			this.iefixStyle.width = this.elementStyle.width;
			this.iefixStyle.height = this.elementStyle.height;
			Style.assign(this.iefixElement, this.iefixStyle);
		}
	},
	frame: function() {
		if(browser.isIE)
			this.elementStyle.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+this.opacity+")";
		else
			this.elementStyle.opacity = (this.opacity/100);

		if(this.opacity>=100) {
			window.clearInterval(this.handle);
			this.handle = null;
			if(browser.isIE)
				this.elementStyle.filter = '';
			else
				this.elementStyle.opacity = '1.0';

			if(browser.isIE && (typeof document.body.style.maxHeight == "undefined" )) {
				this.elementStyle.background = "transparent";
				this.elementStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/bg.png', sizingMethod='scale')";
			}

		}
		this.opacity += 30;

		Style.assign(this.element,this.elementStyle);
	},
	close: function() {
		if(!this.element || !this.imageElement) return;

		this.imageStyle.visibility = "hidden";
		this.elementStyle.visibility = "hidden";
		this.closeButtonStyle.visibility = 'hidden';

		Style.assign(this.element,this.elementStyle);
		Style.assign(this.imageElement,this.imageStyle);
		Style.assign(this.closeButton,this.closeButtonStyle);

		if(this.promoLabelElement) {
			this.promoLabelStyle.visibility = 'hidden';
			Style.assign(this.promoLabelElement,this.promoLabelStyle);
		}
	},
	showLabel: function(link) {
		if(!this.promoLabelElement) {
			this.promoLabelElement = document.createElement('div');
			this.element.appendChild(this.promoLabelElement);
			this.promoLabelElement.onclick = this.close.bind(this);
		}
		this.promoLabelStyle = {
			visibility: "hidden",
			width: "200px",
			height: "200px",
			position: 'absolute'
		};
		Style.assign(this.promoLabelElement,this.promoLabelStyle);
		img = new Image();
		img.src = link;
		this.promoLabelStyle.left = (_int(this.imageStyle.left,0)-10)+'px';
		this.promoLabelStyle.top = (_int(this.imageStyle.top,0)+this.imgHeight-img.height+40)+'px';
		this.promoLabelStyle.visibility = "visible";
		if(browser.isIE && (typeof document.body.style.maxHeight == "undefined" )) {
			this.promoLabelStyle.background = 'transparent';
			this.promoLabelStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+link+"', sizingMethod='noscale')";
		}
		else
			this.promoLabelStyle.background = 'url('+link+') no-repeat 0px 0px';

		Style.assign(this.promoLabelElement,this.promoLabelStyle);
	}
};

var imagePreloader = null;
function openPreview(duzy,maly,promoLabel) {
	var image = duzy && duzy.length > 0 ? duzy : maly;
	imagePreloader = new Classes.ImagePreloader({
		imagesArray: [image,'img/bg.png','img/zamknij.gif'],
		data: null,
		onImageError: function(img,index) {
			alert(dict('Błąd podczas ładowania obrazka: @link@', {'link' : img.src}));
		},
		onLoad: function() {
			Preview.image = new Image;
			Preview.image.src = image;
			Preview.show(Preview.image);
			if(promoLabel && promoLabel.length >0) Preview.showLabel(promoLabel);
		}
	});
	imagePreloader.preload();
}


