﻿	/*
	WandlWindow ver 1.0
	Chibisov Gennady 2009
	http://www.wandl.ru
	*/
	var WandlWindow = new Class({
			Extends: Request,
			Implements: [Options, Events],
			options:{
				arrOfImages: new Array('top_left', 'top_center', 'top_right', 'bottom_left', 'bottom_center', 'bottom_right', 'close'),
				url : '',
				holder : '',
				under : 0,
				shown : false,
				imageAutoSize: true,
				underColor: '#000',
				underClose: true,
				title : 'Окно',
				style : 'blue',
				width : 300,
				height : 100,
				contentWidth : 'auto',
				contentHeight : 'auto'
			},
			initialize: function(element, options){
				this.setOptions(options);
					this.element = $(element);
					this.under = $('wandlWindowUnder');
					this.btnClose = this.element.getElement('#top_right');
					this.divTitle = this.element.getElement('#caption');
					this.trTop = this.element.getElement('#top_right').getParent();
					this.trMiddle = this.element.getElement('#middle_right').getParent();
					this.trBottom = this.element.getElement('#bottom_right').getParent();
					this.tdCenter = this.element.getElement('#middle_center');
					this.windowText = this.element.getElement('#middle_center').getElement('#window_text');
					this.frame = this.element.getElement('#frameText');
					this.object = this.element.getElement('#objText');
					this.image = this.element.getElement('#wandlImage');
					this.holder = this.element.getElement('#holder');
					this.loader = this.element.getElement('#wandlWindowLoader');
					this.deltaY = this.trTop.getStyle('height').toInt() + this.trBottom.getStyle('height').toInt();//размер шапки и футера
				if ((this.options.url)||(this.options.holder)) this.setUrl(this.options.url, this.options.holder);
				this.setUnder(this.options.under);
				//this.setSize(this.options.width, this.options.height);
				this.setWidth(this.options.width);
				this.setHeight(this.options.height);
				this.setStyle(this.options.style);
				this.setTitle(this.options.title);
				this.setEvents();
			},
			setPosition: function(what, val){
				//this.setStyle(this.options.style);/*sdfasdfdf*/
				if (what == 'x') {
					leftt = (getScreenSize('width') - val) / 2 - 20;
					if (leftt <= 0) leftt = 10;
					this.element.setStyle('left', leftt);
				}
				if (what == 'y') {
					topp = (getScreenSize('height') - val - this.deltaY) / 2 + window.getScrollTop() - this.deltaY*0.5;
					if (topp <= 0) topp = 10;
					this.element.tween('top', topp);
					//this.element.setStyle('top', topp);
				}
			},
			setWidth: function(thisWidth){
				instance = this;
				this.options.width = thisWidth;
				this.setPosition('x', this.options.width);//установка позиции по X
				var widthTween = new Fx.Tween (this.tdCenter, {property:'width'});
				widthTween.start(this.options.width);
				//this.tdCenter.setStyle('width', this.options.width);
				
				/*if (pngfadefix){// for ie7,8 with iepngfix from www.wandl.ru
					topback = this.element.getElement('#top_center').getElement('.back');
					topback.setStyle('width', 'auto');
					topfix = this.element.getElement('#top_center').getElement('div');
					topfix.setStyle('width', 'auto');
				
					bottomback = this.element.getElement('#bottom_center').getElement('.back');
					bottomback.setStyle('width', 'auto');
					bottomfix = this.element.getElement('#bottom_center').getElement('div');
					bottomfix.setStyle('width', 'auto');
					this.under.fade([this.options.under+0.01], [this.options.under-0.01]);// for opera
				}*/
			},
			setHeight: function(thisHeight){
				this.options.height = thisHeight;
				this.setPosition('y', this.options.height);//установка позиции по Y
				//this.tdCenter.setStyle('height', this.options.height);				
				this.tdCenter.tween('height', this.options.height);
			},
			setStyle: function(thisStyle){
				this.options.style = thisStyle;
				imagee = this.element.getElement('#middle_left').getStyle('background-image');//выбираем картинку бэкграунда, в данном случае гифа левой голонки
				bgImageFolder = getFolder(imagee); // получаем папку, где картинка находится
				lenLines = this.options.arrOfImages.length;
				for ( i = 0; i < lenLines; i++){
					tdName = '#' + this.options.arrOfImages[i];
					td = this.element.getElement(tdName);
					el = td.getElement('.back');
					imagee = this.options.arrOfImages[i];
					newImage = bgImageFolder + '/' + this.options.style + '/' + imagee + '.png)';
					//alert(i);
					el.setStyle('background-image', newImage);//устанавливаем новый бэкграунд
				}
				if (this.under.getStyle('opacity') != 0)
				this.under.fade([this.options.under+0.01], [this.options.under-0.01]);// for opera
				//this.under.setOpacity(this.options.under);// for opera
			},
			setUrl: function(thisUrl, thisHolder){
				if ((this.options.url == thisUrl)&&(this.options.holder == thisHolder)) return;
				this.options.url = thisUrl;
				this.options.holder = thisHolder;
				
				instance = this;
				
				if ((this.options.url == '')||(this.options.holder == '')) {//reset
					this.holder.innerHTML = '';
				}
				if (this.options.holder == 'frame') {	//frame
					this.holder.innerHTML = '<iframe id="frameText" src="' + this.options.url + '" frameborder="no"></iframe>';
				}
				else
				if (this.options.holder == 'object') {	//object
					this.holder.innerHTML = '<object id="objText" name="objText" type="text/html" data="' + this.options.url + '" class="mainContent">Error</object>';
				}
				else
				if (this.options.holder == 'flash') {	//object
					hr = this.options.url;
					var aHref = new Element('a', {'href':hr});
					this.holder.innerHTML = '';
					aHref.inject(this.holder);
					flowplayer(aHref, "scripts/flowplayer-3.1.0.swf");
				}
				else
				if (this.options.holder == 'image') {	//image
					this.loader.setStyle('display', 'block');
					if (this.options.imageAutoSize) {
						//this.setSize(300, 300);
						this.holder.innerHTML = '<img id="wandlImage" src="' + this.options.url + '" onload="instance.imageOnLoad(this.width, this.height);" />'; //реальный размер картинки
					}
					else this.holder.innerHTML = '<img id="wandlImage" src="' + this.options.url + '" onload="$(\'wandlWindowLoader\').setStyle(\'display\', \'none\');" style="width:100%; height:100%;" />';//насильный размер =)
					//this.holder.innerHTML = '<img id="wandlImage" src="' + this.options.url + '" onload="$(\'wandlWindowLoader\').setStyle(\'display\', \'none\');" />';
				}
			},
			setImageAutoSize: function(thisImageAutoSize){
				this.options.imageAutoSize = thisImageAutoSize;
			},
			imageOnLoad: function(imageWidth, imageHeight){
				this.loader.setStyle('display', 'none');
				this.setWidth(imageWidth);
				this.setHeight(imageHeight);
				//this.setSize(imageWidth, imageHeight);
			},
			setTitle: function(thisTitle){
				this.options.title = thisTitle;
				this.divTitle.innerHTML = this.options.title;
			},
			setEvents: function(){	//установка стандартных событий
				instance = this;
				this.btnClose.removeEvents();
				this.btnClose.addEvent('click', function(){ 	//нажатие кнопки закрыть
														 instance.hide();
														 });
				this.under.removeEvents();
				if (this.options.underClose){
					this.under.addEvent('click', function(){ 	//нажатие по фону
															 instance.hide();
															 });
				}
			},
			show: function(){	//вывод окна
				if (this.options.shown) return; //выведено ли уже окно?
				this.element.setStyle('display', 'block');
				this.element.setStyle('visibility', 'visible');
				//this.setAlign(this.options.align);
				//this.setSize(this.options.width, this.options.height);
				
				this.showUnder();
				this.options.shown = true;//окно выведено
			},
			hide: function(){	//скрытие окна
				instance = this;
				this.setUrl('', '');
				this.options.shown = false;//окно закрыто
				this.element.setStyle('visibility', 'hidden');
				this.element.setStyle('display', 'none');
				this.setHeight(100);
				this.setWidth(300);
				this.hideUnder();// обязательно последним, иначе в IE не исчезает слой.
			},
			setUnder: function(thisUnder){
				this.options.under = thisUnder;
				this.under.setOpacity(0);
			},
			setUnderColor: function(thisUnderColor){
				this.options.underColor = thisUnderColor;
			},
			setUnderClose: function(thisUnderClose){
				this.options.underClose = thisUnderClose;
				this.setEvents();
			},
			showUnder: function(){
				this.under.setStyle('background-color', this.options.underColor);
				this.under.setStyle('display', 'block');
				this.under.fade(this.options.under);
			},
			hideUnder: function(){
				this.under.fade(0);
			}
	});
