window.addEvent('load', function(){
	//initImages();
	new gallery();
	new ShowHideMenuTab('linkTabs');
});

//////////////////////////////////////////////////////
// ****DEBUT SCRIPT OUVERTURE POPUP****

function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}


/////////////////////////////////
//hover for Main Menu Tab
/////////////////////////////////
var ShowHideMenuTab = new Class({
	Implements: [Options],
	
	options: {
		
	},
	initialize: function(element, options){
		this.element = $(element);
		this.setOptions(options);
		this.intScrollContent();		
	},
	intScrollContent: function(){
		if (!this.element) {
			return;
		}
			
		this.element.getElements('li').each(function(item, index){			
			var that = item;
			var maskID = $('mask');
			if (!maskID) {
				return;
			}			
			var mask = maskID.getElements('span');
			item.store('myFx', new Fx.Tween(item, {
				link:'ignore',
				transition: Fx.Transitions.Quad.easeOut
			}));
			item.store('myFx02', new Fx.Tween(mask[index], {
				link:'ignore',
				duration: 'long',
				transition: Fx.Transitions.Bounce.easeOut
			}));
			if(!item.hasClass('last'))
				item.addEvents({
					mouseenter: function(evt){
						new Event(evt).stop();						
						this.retrieve('myFx').cancel().start('margin-top', -12).chain(function(){
							this.retrieve('myFx02').cancel().set('opacity', 0);	
							this.retrieve('myFx').cancel().start('margin-top', 0);
						}.bind(this));
					},
						
					mouseleave: function(evt){
						new Event(evt).stop();			
						this.retrieve('myFx02').cancel().set('opacity', 1);							
						this.retrieve('myFx').cancel().start('margin-top', 53);
					}
				});
		});		
	}	
});
///////////////////////////////////////////////////////////////////////
//Images Slider
///////////////////////////////////////////////////////////////////////
var gallery = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container: 'imagesSlider',		
		direction: 'LTR',
		counter: '{x}/{y}',
		itemWidth: 377,
		fx: null,
		fx2: null,
		opacity: {
			scroll: 0.3,
			info: 0.6
		},
		totalImages: 0,
		currentIndex: 0,
		scrollTimeout: 2000, // 5s
		clickTimeout: 2000,
		duration: 1000,
		timerScroll: null,
		timerClick: null,
		ulTags: null,
		liTags: null,
		scrollContainer: null,
		btnPrevious: null,
		btnNext: null,

		counterContainer: null
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.options.container = $(this.options.container);
		if(!this.options.container){
			return;
		}
		this.initGallery();
		this.setAutoRun();
	},	

	precheck: function(){
		var that = this;
		that.options.scrollContainer = that.options.container.getElement('div.viewPhotoWrapper');
		if(!that.options.scrollContainer){
			return;
		}
		that.options.btnPrevious = that.options.container.getElement('a.btnPre');
		if(!that.options.btnPrevious){
			return;
		}
		that.options.btnNext = that.options.container.getElement('a.btnNext');
		if(!that.options.btnNext){
			return;
		}
		that.options.ulTags = that.options.scrollContainer.getElement('ul');
		if(!that.options.ulTags){
			return;
		}
		that.options.liTags = that.options.ulTags.getElements('li');
		if(that.options.liTags.length == 0 ){
			return;
		}
		return true;
	},
	
	initGallery: function(){
	
		var that = this;
		
		if(!that.precheck()){			
			return;
		}
		that.options.totalImages = that.options.liTags.length;
		if (that.options.totalImages == 1) {
			that.options.btnPrevious.setStyles({
				opacity: 0.3,
				cursor: 'default'
			});
			that.options.btnPrevious.enabled = false;

			that.options.btnNext.setStyles({
				opacity: 0.3,
				cursor: 'default'
			});
			that.options.btnNext.enabled = false;
			return;
		}
		
		if (that.options.currentIndex == 0) {
			that.options.btnPrevious.setStyles({
				opacity: 0.3,
				cursor: 'default'
			});
			that.options.btnPrevious.enabled = false;
		}
		
		that.options.btnPrevious.enabled = true;
		that.options.btnPrevious.addEvent('click', function(e){
			if (e) {
				new Event(e).stop();
			}
			if (!that.options.btnPrevious.enabled) {
				return;
			}
				
			window.clearInterval(that.options.timerScroll);
			window.clearTimeout(that.options.timerClick);
			that.options.direction = 'RTL';			
			that.toLi(--that.options.currentIndex);
			that.options.timerClick = window.setTimeout(function(){
				that.setAutoRun();
			}, that.options.clickTimeout);
		});
		
		that.options.btnNext.enabled = true;
		that.options.btnNext.addEvent('click', function(e){
			if(e){
				new Event(e).stop();
			}
			if(!that.options.btnNext.enabled){
				return;
			}
			if (that.options.currentIndex != 0) {
				that.options.btnPrevious.setStyles({
					opacity: 1,
					cursor: 'pointer'
				});
				that.options.btnPrevious.enabled = true;
			}
			window.clearInterval(that.options.timerScroll);
			window.clearTimeout(that.options.timerClick);
			that.options.direction = 'LTR';		
			that.toLi(++that.options.currentIndex);
			that.options.timerClick = window.setTimeout(function(){
				that.setAutoRun();
			}, that.options.clickTimeout);
		});

		that.options.fx = new Fx.Scroll(that.options.scrollContainer, {
			link: 'ignore',
			duration: that.options.duration,
			transition: Fx.Transitions.Quad.easeOut,
			onStart: function(){				
			},			
			onComplete: function(){	
				if(that.options.currentIndex <= 1){					
					that.options.currentIndex = 2 + that.options.totalImages - 1;
					that.options.fx.set(that.options.itemWidth * that.options.currentIndex, 0);		
				}
				if(that.options.currentIndex > that.options.totalImages + 1){
					that.options.currentIndex = 2;
					that.options.fx.set(that.options.itemWidth * that.options.currentIndex ,0);	
				}					
			}
		});	
		that.swapItem();
		that.options.ulTags = that.options.scrollContainer.getElement('ul');
		that.options.liTags = that.options.ulTags.getElements('li');
		that.options.currentIndex = that.options.totalImages;
		that.options.fx.set(that.options.itemWidth * that.options.currentIndex, 0);
		that.toLi(that.options.currentIndex);
		
	},
	
	swapItem: function(){
		var that = this;
		if(that.options.liTags.length > 0 ){
			that.options.liTags.each(function(li){
				li.clone().inject(that.options.ulTags, 'bottom');
			});
			for(var j = 1; j <= that.options.totalImages; j++){				
				that.options.liTags[that.options.totalImages - j].clone().inject(that.options.ulTags, 'top');
			}
		}
	},
	
	toLi: function(index){
		var that = this;		
		that.options.fx.toElement(that.options.liTags[index]);
		
	},
		
	setAutoRun: function(){
		var that = this;
		if (!that.precheck()) {
			return;
		}
		if (that.options.totalImages <= 0) {
			return;
		}
		else {
			var fn = $empty;
			if(that.options.direction == 'LTR'){
				fn = that.options.btnNext;
			}
			else{
				fn = that.options.btnPrevious;
			}
			that.options.timerScroll = window.setInterval(function(){
				fn.fireEvent('click');
			}, that.options.scrollTimeout);
		}
	}
});


/////////////////////////////////////////////////////
function initImages() {	
	document.imageOut = new Object();
	document.imageOver = new Object();
	var pngRegExp = new RegExp("\\.png$", "i")
	var f = "DXImageTransform.Microsoft.AlphaImageLoader";	
	var imageArray = $$("img", "input");
	imageArray.each(function(item){
		var image = item.src.substr(item.src.lastIndexOf("/")+1);
		var id = item.id || image.replace("_n.", "").replace("_N.", "");
		
		var hover = (image.toLowerCase().lastIndexOf("_n.") !=-1);
		//fix hover for png @len.nguyenvan
		if(!hover){
			if (Browser.Engine.trident4 && !Browser.Engine.trident5) {					
				if (item.filters[f] && item.filters[f].src.test(pngRegExp) && item.filters[f].src.lastIndexOf("_n.") != -1) {											
					hover = true;
				}
			}
		}
		//
		if (hover && document.imageOut && document.imageOver) {			
			document.imageOut[id] = new Image();
			document.imageOut[id].src = item.src;
			document.imageOver[id] = new Image();
			document.imageOver[id].src = item.src.substr(0, item.src.lastIndexOf("/")+1)+image.replace("_n.", "_o.").replace("_N.", "_O.");
		}
		if (hover && document.imageOut && document.imageOver) {
			
			item.onmouseover = function(){				
				if (document.imageOver && document.imageOver[this.id]) setImage(this, document.imageOver[this.id].src, 'over');
			}			
			item.onmouseout = function(){
				
				if (document.imageOut && document.imageOut[this.id]) setImage(this, document.imageOut[this.id].src, 'out');
			}			
			item.id = id;
			
			function setImage(imageObject, src, status) {				
				if (Browser.Engine.trident4 && !Browser.Engine.trident5) {					
					if (imageObject.filters[f] && imageObject.filters[f].src.test(pngRegExp)) {											
						if(status == 'over')
							imageObject.filters[f].src = imageObject.filters[f].src.replace('_n.png', '_o.png'); 
						else{
							imageObject.filters[f].src = imageObject.filters[f].src.replace('_o.png', '_n.png');
						}
					} else {
						imageObject.src = src;
					}
				} else {
					imageObject.src = src;
				}
			}
		}
	});
}
