window.addEvent('domready', function(){

	$$('#menu a').each(function(el,i){
		el.getElement('.hov').setStyles({
			'display' : 'inline-block',
			'opacity' : 0
		});
		el.addEvent('mouseenter', function(){
			el.getElement('.hov').set('morph', {
				duration: 250
			}).morph({
				'opacity' : 1
			})
		});
		el.addEvent('mouseleave', function(){
			el.getElement('.hov').set('morph', {
				duration: 250
			}).morph({
				'opacity' : 0
			});
		});
	});

	$$('#dmenu a').each(function(el,i){
		
		if (!el.hasClass('a')) {
			el.getElement('.hov').setStyles({
				'display' : 'inline-block',
				'opacity' : 0
			});
			el.addEvent('mouseenter', function(){
				el.getElement('.hov').set('morph', {
					duration: 250
				}).morph({
					'opacity' : 1
				})
			});
			el.addEvent('mouseleave', function(){
				el.getElement('.hov').set('morph', {
					duration: 250
				}).morph({
					'opacity' : 0
				});
			});
		}
	});


	$$('.dip a').each(function(el,i){
		if (el.getElement('.hov')) {
			el.getElement('.hov').setStyles({
				'display' : 'inline-block',
				'opacity' : 0
			});
			el.addEvent('mouseenter', function(){
				el.getElement('.hov').set('morph', {
					duration: 250
				}).morph({
					'opacity' : 1
				})
			});
			el.addEvent('mouseleave', function(){
				el.getElement('.hov').set('morph', {
					duration: 250
				}).morph({
					'opacity' : 0
				});
			});
		}
	});
	
	$$('#portfolioList a').each(function(el,i){
		if (el.getElement('.hov')) {
			el.getElement('.hov').setStyles({
				'display' : 'inline-block',
				'opacity' : 0
			});
			el.addEvent('mouseenter', function(){
				
					el.getElement('.hov').set('morph', {
						duration: 250
					}).morph({
						'opacity' : 1
					});
					el.set('morph', {
						duration: 250
					}).morph({
						'color' : '#fa47b1'
					});
			});
			el.addEvent('mouseleave', function(){
				el.getElement('.hov').set('morph', {
					duration: 250
				}).morph({
					'opacity' : 0
				});
				el.set('morph', {
					duration: 250
				}).morph({
					'color' : '#828486'
				});
			});
		}
	});	
});





var curPIm = null;
var vPImg = new Image();

function setDipActive(el) {
	el.getElement('.hov').setStyles({
		'display' : 'inline-block',
		'opacity' : 0
	}).set('morph', {
		duration: 250
	}).morph({
		'opacity' : 1
	})
}
	
function setDipNActive(el) {
	el.getElement('.hov').set('morph', {
		duration: 250
	}).morph({
		'opacity' : 0
	});	
}

function setPImage(im, start) {
	
	if (im !== curPIm) {
		if (curVideo) {
			curVideo.dispose();
			$('PImageRotator').setStyle('display', 'inline-block');
			setDipNActive(curVideoClicker.getParent('.dipBox'));		
		}
		if (curPIm) {
			setDipNActive(curPIm.getParent('.dipBox'));
		}
		if (start) {
			$('PImageRotator').setStyles({'background-image':"url("+im.href+")"});
			setDipActive($(im).getParent('.dipBox'));
		} else {
		
			$('PImageRotator').set('morph',{
				duration: 200,
				onComplete: function(){
					$(vPImg).removeEvents();
					vPImg.addEvent('load', function() {
						$('PImageRotator').setStyles({'background-image':"url("+vPImg.src+")"});
						$('PImageRotator').morph({
							'opacity':1
						})
					})
					vPImg.src = im.href;
				}
			}).morph({
				'opacity':0
			});
			setDipActive($(im).getParent('.dipBox'));
		}
		curPIm = im;
		curVideo = null;
		curVideoClicker = null;
	}
	return false;
}

var vDiv = null;
var vcDiv = null;
var curVideo = null;
var curVideoClicker = null;
function openPVideo(clicker) {
	
		var el = clicker.getParent('.dipBox');
		var code = el.getElement('.vc');
			
		vDiv = new Element('div',{
			'styles':{
				'position':'absolute',
				'width':'100%',
				'height':$(document.body).getScrollSize().y,
				'background':'#fff',
				'opacity':.6,
				'z-index' : 1000,
				'top':0,
				'left':0
			}
		});
		vcDiv = new Element('div', {
			'styles':{
				'position':'absolute',
				'width': ((Browser.Engine.trident)?100:'auto'),
				'z-index' : 1010
			},
			'html' : '<div class="globalVideoBox"><div class="r-star-shape"><div class="cn tl"></div><div class="cn tr"></div><div class="content" align="center"><div align="right"><span class="popupClose" onclick="return disposePVideo()">X</span></div><div style="padding:10px 34px 28px 34px" align="center"><div style="width:380px;height:320px" id="mediaspace"></div></div><div class="cn bl"></div><div class="cn br"></div></div></div>'
		});
				
		$(vDiv).inject(document.body);
		$(vcDiv).inject(document.body);		
		
		//alert(code.innerHTML);
		var s1 = new SWFObject("/sharedfiles/player/player.swf","ply","380","320","9","#FFFFFF");
		s1.addParam("allowfullscreen","true");
		s1.addParam("allowscriptaccess","always");
		s1.addParam("autostart","true");
		s1.addParam("flashvars","backcolor=faa5b5&frontcolor=ffffff&file="+code.innerHTML);
		s1.write("mediaspace");
		
		var bordersBlock = vcDiv.getElement('.r-star-shape');
		
		var nw = vcDiv.getElement('.globalVideoBox').getWidth();
		var nh = vcDiv.getElement('.content').getHeight();
		
		bordersBlock.setStyles({
			'width' : nw
		})
		
		var top = Math.round($(document.body).scrollTop + $(document.body).getSize().y/2 - nh/2);
		if (top < $(document.body).scrollTop) top = $(document.body).scrollTop + 10;		
		vcDiv.setStyles({
			'top': top,
			'left': Math.round(window.getSize().x/2 - nw/2)
		})
	
	return false;
}

function disposePVideo() {
	vDiv.dispose();
	vcDiv.dispose();
	
	return false;
}

function disposePVideoImage() {
	
	vDiv.set('morph', {
		duration: 200,
		onComplete: function(){
			vDiv.dispose();
		}
	}).morph({
		'opacity' : 0
	})
	vcDiv.set('morph', {
		duration: 200,
		onComplete: function(){
			vcDiv.dispose();
		}
	}).morph({
		'opacity' : 0
	})
	loaderImgClone.dispose();
	
	return false;

}

var curGIm = null;
var vImgFirst = new Image();
var vImg = new Image();

var GIterator = 0;
var GInterval = null;
var GLoadNext = null;
var timeCounter = 0;
function initG(images) {
	
	$('GImageRotatorSub').setStyles({
		'opacity':0
	});
	var loaded = true;
	$(vImg).addEvent('load', function(){
		
		$(vImg).removeEvents('load');
	
		GLoadNext = function(){
			
			loaded = false;
			$('GImageRotatorSub').setStyle('background-image', 'url('+curGIm+')');
			$('GImageRotatorSub').setStyles({
				'opacity' : 1
			})
			GSetBg($('GImageRotator'), vImg.src);
			$('GImageRotatorSub').set('morph',{
				onComplete: function(){
					$(vImg).removeEvents('load');
					timeCounter = 0;
					var tcId = (function(){
						timeCounter+=500;
					}).periodical(500);
					
					$(vImg).addEvent('load', function(){
						$clear(tcId);
						if (timeCounter > 4000) GLoadNext();
						else GLoadNext.delay(4000 - timeCounter);
						loaded = true;
						
					});
					if (images[GIterator + 1]) {
						vImg.src = images[GIterator + 1];
						GIterator++;
					} else {
						vImg.src = images[0];
						GIterator = 0;
					}
				}
			}).morph({
				'opacity' : 0
			})
		}
		GLoadNext.delay(4000);
		//GInterval = GLoadNext.periodical(5000);
	})
	
	GIterator++;
	vImgFirst.src = images[0];
	GSetBg($('GImageRotator'), images[0]);
	vImg.src = images[1];
}

function GSetBg(el, url) {
	el.setStyles({
		'background-image':'url('+url+')'
	});
	curGIm = url;
}

loaderImg = new Image();
loaderImg.src = "/images/loading.gif";

window.addEvent('domready', function(){	
	
	if (window.GImages) initG(GImages);
	
	$$('a[rel=lightbox]').each(function(clicker) {
		
		clicker.addEvent('click', function(e){
			
			e.stop();
			
			var src = clicker.getAttribute('href');
			
			var code = "<img style='cursor:pointer' src='"+src+"' />";
				
			vDiv = new Element('div',{
				'styles':{
					'position':'absolute',
					'width':'100%',
					'height':$(document.body).getScrollSize().y,
					'background':'#fff',
					'opacity':0,
					'z-index' : 1000,
					'top':0,
					'left':0
				}
			}).set('morph',{
				duration: 300
			}).morph({
				'opacity' : .6
			});
			
			loaderImgClone = $(loaderImg).clone();
			loaderImgClone.setStyles({
				'position' : 'absolute',
				'top' : Math.round($(document.body).scrollTop + $(document.body).getSize().y/2),
				'left' : '50%',
				'z-index' : 1005
			})
			
			$(vDiv).inject(document.body);
			$(loaderImgClone).inject(document.body);
			
			
			vcDiv = new Element('div', {
				'styles':{
					'position':'absolute',
					'width': ((Browser.Engine.trident)?100:'auto'),
					'z-index' : 1010,
					'visibility' : 'hidden',
					'opacity' : 0
				},
				'html' : '<div class="globalVideoBox" onclick="return disposePVideoImage()"><div class="r-star-shape"><div class="cn tl"></div><div class="cn tr"></div><div class="content" align="center"><div align="right"><span class="popupClose">X</span></div><div style="padding:0 24px 24px 24px" align="center">'+code+'</div></div><div class="cn bl"></div><div class="cn br"></div></div></div>'
			});

			var img = new Image();
			$(img).addEvent('load',function(){
				$(vcDiv).inject(document.body);

				var bordersBlock = vcDiv.getElement('.r-star-shape');
				
				var nw = vcDiv.getElement('.globalVideoBox').getWidth();
				var nh = vcDiv.getElement('.content').getHeight();
				
				bordersBlock.setStyles({
					'width' : nw
				})
				
				
				var top = Math.round($(document.body).scrollTop + $(document.body).getSize().y/2 - nh/2);
				if (top < $(document.body).scrollTop) top = $(document.body).scrollTop + 10;
				vcDiv.setStyles({
					'top': top,
					'left': Math.round(window.getSize().x/2 - nw/2)
				})

				vcDiv.setStyles({
					'visibility' : 'visible'
				}).set('morph',{
					duration: 250
				}).morph({
					'opacity' : 1
				});
			})
			img.src = src;
		
			return false;
		});
	});
});
