var Mylightwindow = Class.create(
{
	overlayElement: null,
	overlayPopup: null,
	
	initialize: function() {
		
		this.path = this;
		this.effecting = false;
		this.showing = false;
		
		// build elements
		this.overlayElement = Builder.node('div', { id:'mylightwindow_overlay'});
		this.overlayPopup = Builder.node('div', { id:'mylightwindow_popup', style:'overflow:hidden; width:690px; height:475px; background:#2a2a22;' }, [
			Builder.node('div', { id:'mylightwindow_closebtn', style:'text-align:right; padding:15px; cursor:pointer;', className:'text07'}, 'x'),
			Builder.node('div', { id:'mylightwindow_prevbtn', style:'float:left; margin:170px 0px 0px 40px; cursor:pointer;' }, [
				Builder.node('img', { src:'images/arrow-left-l.gif'})
			]),
			Builder.node('div', { id:'mylightwindow_nextbtn', style:'float:right; margin:170px 40px 0px 0px; cursor:pointer;' }, [
				Builder.node('img', { src:'images/arrow-right-l.gif'})
			]),
			Builder.node('div', { id:'mylightwindow_content', style:'overflow:hidden; width:500px; _width:494px; margin:0px 95px 0px 95px;' }, [
				Builder.node('div', { id:'mylightwindow_media', style:'overflow:hidden; margin-bottom:10px; width:500px; height:350px;' }),
				Builder.node('div', { id:'mylightwindow_name', style:'float:left; text-align:left; width:60%;', className:'text04' }),
				Builder.node('div', { id:'mylightwindow_more', style:'float:right; text-align:right; width:35%', className:'text04b' }, [
					Builder.node('img', { src:'images/images/icon-arrow-right-s.gif', style:'margin-right:5px' }),
					Builder.node('a', { style:'cursor:pointer;', className:'text04b' }, 'see more applications')
				])
			])
		]); 
		
		$$('body').first().appendChild(this.overlayElement);
		$$('body').first().appendChild(this.overlayPopup);
		
		$('mylightwindow_overlay').hide();
		$('mylightwindow_popup').hide();
		
		this.key_id = '';
		this.records = Array;
		this.index = 0;
		
		// active links
		this.addClickEvent();
	},
	
	addClickEvent: function() {
		
		var thisObj = this;
		
		$$('a.mylightwindow').each(function(e) { 
			Event.observe(e, 'click', function() { thisObj.onClickOpen(e); });
		}.bind(this));
		
		[$('mylightwindow_overlay'), $('mylightwindow_closebtn')].each(function(e) {
			Event.observe(e, 'click', function() { thisObj.onClickClose(e); });
		}.bind(this));
		
		[$('mylightwindow_prevbtn')].each(function(e) {
			Event.observe(e, 'click', function() { thisObj.onClickPrev(e); });
		}.bind(this));
		
		[$('mylightwindow_nextbtn')].each(function(e) {
			Event.observe(e, 'click', function() { thisObj.onClickNext(e); });
		}.bind(this));
		
	},
	
	onClickPrev: function(e) {
		if (this.index>0) {
			this.index--;
			this.checkArrowBtn();
			this.loadPopup();
		}
	},
	
	onClickNext: function(e) {
		if (this.index<this.records.length-1) {
			this.index++;
			this.checkArrowBtn();
			this.loadPopup();
		}
	},
	
	checkArrowBtn: function(e) {
		this.index>0 ? $('mylightwindow_prevbtn').show() : $('mylightwindow_prevbtn').hide();
		this.index<this.records.length-1 ? $('mylightwindow_nextbtn').show() : $('mylightwindow_nextbtn').hide();
	},
	
	onClickOpen: function(key_id) {
		
		if (this.effecting) return;
		
		var thisObj = this;
		this.effecting = true;
		this.target = key_id.down();
		this.index = key_id.up().title;
		
		if (key_id==this.key_id) {
			this.showOverlay();
			this.showPopup();
		} else if (this.index=='image') {
			this.index = 0;
			this.records[0] = {'file':key_id, 'name':''};
			$('mylightwindow_more').down(0).hide();
			$('mylightwindow_more').down(1).hide();
			this.showOverlay();
			this.showPopup();
		} else {
			new Ajax.Request('_ajax.php',
								{	method: 'post', 
									parameters: {
										ajax:true,
										action:'lightwindow',
										key:key_id
									},
									onSuccess: function(transport, json) {
										if (json.status) {
											thisObj.records = json.records;
											thisObj.showOverlay();
											thisObj.showPopup();
											$('mylightwindow_more').down(0).show();
											$('mylightwindow_more').down(1).show();
											$('mylightwindow_more').down('a').href = json.more;
										}
									} 
								}
							);
		}
		
	},
		
	showOverlay: function() {
		
		$('mylightwindow_overlay').setStyle({	width: '100%', 
									 	height: this.getPageDimensions().height+'px', 
									 	top: '0px',
									 	left: '0px',
									 	background: '#333329',
										position:	'absolute',
										zIndex:		'1000'		 });
		
		new Effect.Appear($('mylightwindow_overlay'),{duration: .8, to:.75});
		
	},
	
	showPopup: function() {
		
		var thisObj = this;
		
		this.getBroswerDimensions();
		this.w = this.target.getDimensions().width;
		this.h = this.target.getDimensions().height;
		this.x = this.target.cumulativeOffset().left;
		this.y = this.target.cumulativeOffset().top;
		this.ow = 690;
		this.oh = 475;
		this.ox = (this.broswerWidth-this.ow)/2;
		this.oy = (this.broswerHeight-this.oh)/2;
		
		$('mylightwindow_popup').setStyle({	width:		this.w+'px',
										height:		this.h+'px',
										top:		this.y+'px',
										left:		this.x+'px',
										position:	'absolute',
										zIndex:		'1100'		});
		
		this.hideElements();
		$('mylightwindow_popup').show();
		
		new Effect.Transform([
			{ '#mylightwindow_popup' : 
				'width: 690px;' +
				'height: 475px;' +
				'left:'+this.getScrollOffsets()[0]+'px;' +
				'top:'+this.getScrollOffsets()[1]+'px;' }
		], { duration: .8 , afterFinish:this.loadPopup.bind(this) }).play();
		
	},
	
	hideElements: function() {
		
		$('mylightwindow_prevbtn').hide();
		$('mylightwindow_nextbtn').hide();
		$('mylightwindow_content').hide();
		$('mylightwindow_media').hide();
		$('mylightwindow_name').hide();
		$('mylightwindow_more').hide();
		
	},
	
	loadPopup: function() {
		
		if (this.records.length<1) return;
		
		this.record = this.records[this.index];
		$('mylightwindow_media').innerHTML = '<img src="images/loading.gif" style="margin-top:150px" />';
		$('mylightwindow_media').show();
		$('mylightwindow_content').show();
		
		if (this.record['type']=='flv') {
			var so = new SWFObject('swf/flvplayer_lightwindow.swf?a='+this.record['file'], 'mylightwindow_flash', '500', '350', '7.0.29', '#FFFFFF', true);
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("allowFullScreen", "true");
			so.addParam("quality", "high");
			so.addParam("scale", "noscale");
			so.addParam("menu", "false");
			so.addParam("wmode", "transparent");
			var flash = so.write('mylightwindow_media');
			if (!flash) {
				var mplayer = '<object id="MPlayer_mylightwindow" classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="500" height="350">'+
								'	<param name="FileName" value="'+this.record['backup']+'" />'+
								'	<param name="showControls" value="false">'+
								'	<param name="autoStart" value="true">'+
								'</object>';
				$('mylightwindow_media').innerHTML = mplayer;
			}
			this.loadContent();
		} else {
			var imgPreloader = new Image();
			imgPreloader.onload = function(){
				$('mylightwindow_media').innerHTML = '<img src="'+this.record['file']+'" width="500" height="350" />';
				this.loadContent();
			}.bind(this);
			imgPreloader.src = this.record['file'];
		}
		
	},
	
	loadContent: function() {
		
		$('mylightwindow_name').innerHTML = this.record['name'];
		$('mylightwindow_name').show();
		$('mylightwindow_more').show();
		this.checkArrowBtn();
		if (!this.showing) this.afterPop();
		
	},
	
	onClickClose: function() {
		
		if (this.effecting) return;
		
		this.effecting = true;
		this.showing = false;
		
		this.hidePopup();
		this.hideOverlay();
		
	},
	
	afterPop: function() {
		
		var thisObj = this;
		this.effecting = false;
		this.showing = true;
		
		this.PE = new PeriodicalExecuter(function() {
			var x = thisObj.getScrollOffsets()[0];
			var y = thisObj.getScrollOffsets()[1];
			$('mylightwindow_popup').setStyle({left:x+'px',top:y+'px'});
		}.bind(this), .1);
		
	},
	
	hideOverlay: function() {
		
		new Effect.Fade($('mylightwindow_overlay'))
		
	},
	
	hidePopup: function() {
		
		this.PE.stop();
		
		this.hideElements();
		if ($('mylightvideo_flash')) $('mylightvideo_flash').remove();
		if ($('MPlayer_mylightwindow')) $('MPlayer_mylightwindow').stop();
				
		new Effect.Transform([
			{ '#mylightwindow_popup' : 
				'width:'+this.w+'px;' +
				'height:'+this.h+'px;' +
				'left:'+this.x+'px;' +
				'top:'+this.y+'px;' }
		], { duration: .8 , afterFinish:this.afterClose.bind(this) }).play();
		
	},
		
	afterClose: function() {
		
		this.effecting = false;
		$('mylightwindow_overlay').hide();
		$('mylightwindow_popup').hide();
		
	},
	
	// tools function
	
	getStyle: function(obj, styleProp) {
		
		if (obj.currentStyle) return obj.currentStyle[styleProp];
		else if (window.getComputedStyle) return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
		
	},
	
	getScrollOffsets: function() {
		
		this.getBroswerDimensions();
		var ox = document.viewport.getScrollOffsets()[0]+((this.broswerWidth-this.ow)/2);
		var oy = document.viewport.getScrollOffsets()[1]+((this.broswerHeight-this.oh)/2);
		return [ox, oy];
		
	},

	getBroswerDimensions: function() {
		var broswerWidth = 0, broswerHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			broswerWidth = window.innerWidth;
			broswerHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			broswerWidth = document.documentElement.clientWidth;
			broswerHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			broswerWidth = document.body.clientWidth;
			broswerHeight = document.body.clientHeight;
		}
		this.broswerWidth = broswerWidth;
		this.broswerHeight = broswerHeight;
	},
	
	getPageDimensions: function() {
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		pageDimensions = { height:pageHeight };
		return pageDimensions;	
	}
	
}
);

function initMylightwindow() { mylightwindow = new Mylightwindow(); }
Event.observe(window, 'load', initMylightwindow, false);
//Component.init('.mylightwindow','Mylightwindow');
