function initTabs() {
	$$("div.listFilms").each(function(aDiv) {	
		var fx = new Fx.Tween(aDiv.getElement("ul"), {
			onComplete: function() {
				this.isTweening = false;
			},
				
			onStart: function() {
				this.isTweening = true;
			}
		});		
		fx.isTweening = false;
		fx.curIndex = 0;
		fx.elements = aDiv.getElement("ul").getElements("a");
		fx.width = 0;
		fx.pos = 0;
		
		fx.elements.each(function(aLink) {
			aLink.addEvent("click", function(evt) {
				new Event(evt).stop();
				if (this.getParent().hasClass("current")) {
					return;
				}
				
				this.getParent("ul").getElement("li.current").removeClass("current");
				this.getParent().addClass("current");
				
				loadContentForItem(this);
			});
			
			fx.width += aLink.getSize().x;
		});		
		aDiv.getElement("ul").setStyle("width", fx.width);
		if (fx.width < aDiv.getSize().x) {
			aDiv.getElements("p.btn").setStyle("display", "none");
		}
		
		aDiv.getElements("p.btn a").each(function(aLink, index) {
			if (index == 0) {
				aLink.setOpacity(0.5);
			}
			
			aLink.addEvent("click", function(evt) {
				new Event(evt).stop();
				if (fx.isTweening) {
					return;
				}
				
				if (index == 0) { // left
					if (fx.pos == 0) {
						return;
					}
					
					fx.curIndex--;
					fx.pos -= fx.elements[fx.curIndex].getSize().x;					
				} else { // right
					if (-fx.pos + fx.width <= aDiv.getSize().x) {
						return;
					}
					
					fx.pos += fx.elements[fx.curIndex].getSize().x;
					fx.curIndex++;
				}
				
				fx.start("margin-left", -fx.pos);
				
				var arr = aDiv.getElements("p.btn a");
				if (fx.pos == 0) {
					arr[0].setOpacity(0.5);
				} else {
					arr[0].setOpacity(1);
				}
				
				if (-fx.pos + fx.width <= aDiv.getSize().x) {
					arr[1].setOpacity(0.5);
				} else {
					arr[1].setOpacity(1);
				}
			});
		});
		
		// load first content
		loadContentForItem(fx.elements[0]);
	});
	
	function loadContentForItem(aLink) {
		if (!aLink.getProperty("longdesc")) {
			return;
		}
		
		var container = aLink.getParent("div.infoType").getElement("div.infoType2");
		container.getChildren("div.infoType3").each(function(aElement) {
			aElement.destroy();
		});
		
		container.getChildren("div.loadingInfo").setStyle("display", "block");
		
		new Request.HTML({
			url: aLink.getProperty("longdesc"), 
				
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var container = aLink.getParent("div.infoType").getElement("div.infoType2");
				var paging = container.getElement("p.paging");
				
				var aDiv = new Element("div");
				aDiv.set("html", responseHTML);				
				
				aDiv.getChildren("div").each(function(aElement, index) {
					if (index != 0) {
						aElement.addClass("hidden");
					}
					aElement.inject(container);
				});
				aDiv.destroy();
				
				container.getChildren("div.loadingInfo").setStyle("display", "none");
				paging.inject(container);
				initPaging(paging);
			}
		}).get();
	}
	
	function initPaging(aPagingNode) {
		var curPage = 0;
		aPagingNode.getChildren("a").each(function(aLink, index) {
			aLink.removeEvents("click");
			aLink.addEvent("click", function(evt) {
				new Event(evt).stop();
				if(this.getStyle('opacity') == 0.5) return;
				var temp = curPage;
				var totalPage = this.getParent().getParent().getChildren("div").length;
				if (index == 0 && temp > 0) {
					temp--;
				} else if (index != 0 && temp < totalPage - 1) {
					temp++;
				}
				
				if (temp != curPage) {
					showPage(this.getParent(), temp);
				}
				
				curPage = temp;
			});
		});
		
		showPage(aPagingNode, curPage);
	}
	
	function showPage(aPagingNode, page) {
		var arrDivs = aPagingNode.getParent().getChildren("div.infoType3");
		arrDivs.each(function(aDiv, index) {
			if (page == index) {
				aDiv.removeClass("hidden");
				
				aDiv.getChildren("div").each(function(aChildDiv) {
					aChildDiv.getElements("a").each(function(aLink, index) {
						if (index >= 2) {
							return;
						}
						
						aLink.removeEvents("mouseover");
						aLink.removeEvents("mousemove");
						aLink.removeEvents("mouseout");
						aLink.addEvents({
							mouseover: function(evt) {
								new Event(evt).stop();
								showTooltip(this.getParent("div.details2").getElement("a"));
								updateTooltipPosition(evt.page.x, evt.page.y);
							},
							
							mousemove: function(evt) {
								new Event(evt).stop();
								updateTooltipPosition(evt.page.x, evt.page.y);
							},
								
							mouseout: function(evt) {
								new Event(evt).stop();
								hideTooltip();
							}
						});
					});
				});
			} else {
				aDiv.addClass("hidden");
			}
		});
		
		aPagingNode.getElement("span").set("text", page + 1 + "/" + arrDivs.length);
		
		var arrLinks = aPagingNode.getElements("a");
		if (page == 0) {
			arrLinks[0].setOpacity(0.5);
		} else {
			arrLinks[0].setOpacity(1);
		}
		
		if (page == arrDivs.length - 1) {
			arrLinks[1].setOpacity(0.5);
		} else {
			arrLinks[1].setOpacity(1);
		}
	}
	
	function showTooltip(aLinkNode) {
		if (!aLinkNode.getProperty("longdesc")) {
			return;
		}
		
		$("tooltipLoading").setStyle("display", "block");
		
		new Request.HTML({
			url: aLinkNode.getProperty("longdesc"), 
			update: $("tooltip"),
			
			onComplete: function() {
				if ($("tooltipLoading").getStyle("display") == "block") {
					$("tooltip").setStyle("display", "block");
					$("tooltipLoading").setStyle("display", "none");
				}
			}
		}).get();
	}
	
	function updateTooltipPosition(x, y) {
		$("tooltip").setStyles({
			top: y,
			left: x + 10
		});
		
		$("tooltipLoading").setStyles({
			top: y,
			left: x + 10
		});
	}
	
	function hideTooltip() {
		$("tooltip").setStyle("display", "none");
		$("tooltipLoading").setStyle("display", "none");
	}
}
/////////////
/////////////tabs /////////////
var ShowHideTab = new Class({
	
	initialize: function(container, options){
		this.container = container;
		this.setOptions(options);
		this.initTab();
	},
	
	initTab: function(){
		var that = this;
		if(!$(that.container)) return;
		var tabsContainer = $(that.container);
		var items = tabsContainer.getElements('li');
		var contents = tabsContainer.getElements('div.infoType4');			
		var activeItem = null;
		var activeContent = null;		
		items.each(function(item, index){
			if(item.hasClass('current')) {
				activeItem = items[index];
				activeContent = contents[index];
			}		
		});		
		items.each(function(item, index){		
			item.addEvent('click', function(evt){
				new Event(evt).stop();
				if(activeItem != this){
					// remove current active item
					if(activeItem){								
						activeItem.removeClass('current');
						activeContent.addClass('hidden');	
					};
					// set active					
					this.addClass('current');
					activeItem = this;
					activeContent = contents[index];
					activeContent.removeClass('hidden');
				}			
			});
		});	
	}	
});
ShowHideTab.implement(new Chain, new Events, new Options);

function togglePCN(){
	var filmType = $('filmType');
	if(!filmType) return;
	var divToggle = filmType.getElement('div.contextType');
	var aToggle = filmType.getElement('a.toggle');
	var myFx = new Fx.Slide(divToggle, {});		
	if(!aToggle.hasClass('open'))
		myFx.hide();
	aToggle.addEvent('click', function(e){
		e.stop();
		myFx.toggle();
	});
}

function voteFunc(){
	var voteForm = $('voteForm');
	if(!voteForm) return;
	var voteBtn = voteForm.getElement('a.vote');
	if(voteBtn){
		voteBtn.addEvent('click', function(e){
			e.stop();			
			new Request.HTML({
				url: '/poll.php',
				onComplete: function(){
					if(arguments[2] == '1'){
						showMessage('C&#7843;m &#417;n b&#7841;n &#273;&#227; g&#7917;i tr&#7843; l&#7901;i cho c&#226;u h&#7887;i n&#224;y.');
					}
					else{
						showMessage('B&#7841;n &#273;&#227; g&#7917;i tr&#7843; l&#7901;i cho c&#226;u h&#7887;i n&#224;y r&#7891;i.');
					}
				}
			}).post(voteForm);
		});
	}
}

function voteResult(){
	var voteResult = $('voteResult');
	if(!voteResult) return;
	var voteResultForm = $('voteResultForm');
	if(!voteResultForm) return;
	voteResultForm.getElements('input').each(function(input, index){		
		voteResult.getElements('ul')[index].getElements('li')[1].setStyle('width', input.value * 2 + 'px');		
		voteResult.getElements('ul')[index].getElements('li')[2].set('html', input.value + '%');		
	});
}

function voteAFilm(){
	var voteFilmLayer = $('voteAFilmLayer');
	if(!voteFilmLayer) return;
	var voteFilmLayerFx = new Fx.Slide(voteFilmLayer);
	voteFilmLayerFx.hide();
	voteFilmLayer.removeClass('hidden');
	var voteAFilm = $('voteAFilm');
	if(voteAFilm){
		voteAFilm.addEvent('click', function(e){
			e.stop();
			voteFilmLayerFx.toggle();
			var btnClose = voteFilmLayer.getElement('a.btnClose');
			if(btnClose){
				btnClose.addEvent('click', function(e2){
					e2.stop();
					voteFilmLayerFx.toggle();
				});
			}
			voteFilmLayer.getElement('input[type=submit]').addEvent('click', function(e3){
				e3.stop();
				new Request.HTML({
					url: '/vote.php',
					method: 'post',
					onComplete: function(){
						if(arguments[2] == '1'){
							showMessage('C&#7843;m &#417;n b&#7841;n &#273;&#227; b&#236;nh ch&#7885;n cho phim n&#224;y.');
						}
						else{
							showMessage('B&#7841;n &#273;&#227; b&#236;nh ch&#7885;n cho phim n&#224;y r&#7891;i.');
						}
						voteFilmLayerFx.toggle();
					}
				}).post($('voteAFilmForm'));
			});			
		});
	}
}

function autoUpdateStatistic(intervals){
	if($('flashVideo')){
		var uid = $('uid').value;
		var mid = $('mid').value;
		var sid = $('sid').value;
		(function(){
			new Request.HTML({
				url: '/update.php?mid='+mid+'&uid='+uid+'&sid='+sid+'&rand='+Math.random(),
				method: 'get'//,
				//onComplete: function() {
					//alert(arguments[2]);
				//}
		   }).get();
		}).periodical(intervals);
	}
}

function actorFade(){
	var actorContent = $('actorContent');
	var myFx = new Fx.Tween(actorContent);

	if(!actorContent) return;
	var actorContentHolder = $('actorContentHolder');
	if(!actorContentHolder) return;
	$("actorContent1").setStyle('opacity', 1);
	$("actorContent2").setStyle('opacity', 0);
	var imgs = actorContentHolder.getElements('img');
	if(imgs.length <= 1) return;
	var index = 1;
	var timer = 5000;
	(function(){
		(function(){		
			toggle(imgs[index].src);		
			index++;
			if(index == imgs.length) index = 0;		
		}).periodical(timer);		
		toggle(imgs[index].src);		
		index++;
		if(index == imgs.length) index = 0;		
	}).delay(Math.round(timer/2));	
	var visibleDiv = $("actorContent1");	
	$("actorContent1").fx = new Fx.Tween($("actorContent1"), {property: 'opacity', duration: 2000});
	$("actorContent2").fx = new Fx.Tween($("actorContent2"), {property: 'opacity', duration: 2000});
	function toggle(newSrc){
		//if($("actorContent1").fx){$("actorContent1").fx.cancel();}
		//if($("actorContent2").fx){$("actorContent2").fx.cancel();}
		if(visibleDiv == $("actorContent1")){
			$("actorContent2").getElement('img').src = newSrc;
			$("actorContent1").fx.cancel().start(0);
			$("actorContent2").fx.cancel().start(1);
			visibleDiv = $("actorContent2");
		}else{
			$("actorContent1").getElement('img').src = newSrc;
			$("actorContent1").fx.cancel().start(1);
			$("actorContent2").fx.cancel().start(0);
			visibleDiv = $("actorContent1");
		} 
	}
}

function showMessage(message){
	var showMessage = $('showMessage');
	if(!showMessage) return;
	showMessage.getElement('p').set('html', message);
	var top = -51;
	var _top = 0;
	if(Browser.Engine.trident4){
		top += window.getScrollTop();		
		_top += window.getScrollTop();
		window.addEvent('scroll', function(){
			showMessage.fx.set(window.getScrollTop());
		});
	}
	else{
		showMessage.setStyle('position', 'fixed');
	}
	showMessage.setStyle('left', window.getWidth()/2 - showMessage.getCoordinates().width / 2);
	showMessage.setStyle('top', window.getHeight()/2 - showMessage.getCoordinates().height / 2);
	showMessage.fx = new Fx.Tween(showMessage, {
		property: 'top'
	});
	showMessage.fx.set(top).start(_top).chain(function(){
		setTimeout(function(){
			showMessage.fx.start(-10000);
		}, 3000);
	});
}

function showAlert(container){
	var container = $(container);
	if(!container) return;
	container.setStyle('opacity', 0);
	
	container.fx = new Fx.Tween(container, {
		duration: 1000		
	}).set('opacity', 0);
	container.getElement('p.btnClose1 a').addEvent('click', function(e){
		new Event(e).stop();
		container.fx.start('opacity', 0);
	});
	container.fx.start('opacity', 1);
}

function autoCheckValidLoggedIn(interval1, interval2){
	if($('loggedIn')){
		var uid = $('loggedIn').get('text').trim();		
		var intervalFn = (function(){		
			new Request.HTML({
				url: '/checkonlineuser.php?uid='+ uid + '&rand='+Math.random()*100,
				method: 'get',
				onComplete: function() {
					if(arguments[2] == 1){
						showLoggedInMessage(interval2);
						$clear(intervalFn);
					}					
				}
		   }).get();
		}).periodical(interval1);
	}
}

function showLoggedInMessage(interval2){
	var showMessage = $('checkOnline');
	if(!showMessage) return;
	maskLayer().set('styles', {
		'display': 'block',
		'z-index': 19999
	}); 
	var top = -51;
	var _top = 0;
	if(Browser.Engine.trident4){
		top += window.getScrollTop();		
		_top += window.getScrollTop();
		window.addEvent('scroll', function(){
			showMessage.fx.set(window.getScrollTop());
		});
	}
	else{
		showMessage.setStyle('position', 'fixed');
	}
	showMessage.setStyle('left', window.getWidth()/2 - showMessage.getCoordinates().width / 2);
	showMessage.setStyle('top', window.getHeight()/2 - showMessage.getCoordinates().height / 2);
	showMessage.fx = new Fx.Tween(showMessage, {
		property: 'top'
	});
	showMessage.fx.set(top).start(_top);
	showMessage.getElement('a').addEvent('click', function(e){
		new Event(e).stop();
		document.location.reload();
	});
	setTimeout(function(){
		document.location.reload();
	}, interval2);
}

function maskLayer(){
    var opacity = 0.6;
    var frameColor = "#ffffff";
	var maskIframe = $("maskIframe");
	var docBody = document.body;
	if (!maskIframe) {
        maskIframe = new IFrame({
			"id": "maskIframe",
			"src": "javascript:false;",
			"frameBorder": 0,
			"scrolling": "no",
			"styles": {
				"width": Math.max(window.getWidth(), docBody.offsetWidth) + "px",
				"height": Math.max(window.getHeight(), docBody.offsetHeight) + "px",
				"z-index": 990,
				"display": "none",
				"position": "absolute",
				"margin": 0,
				"padding": 0,
				"top": 0,
				"left": 0,
				"opacity": opacity
			}
		}).inject(docBody);
		var doc = maskIframe.contentDocument;
		if (doc == undefined || doc == null) {
			doc = maskIframe.contentWindow.document;
		}
		doc.open();
		doc.write("<html><body bgColor='" + frameColor + "'></body></html>");
		doc.close();
    }	
	return maskIframe;
};

window.addEvent('domready', function(){
	new Navigator('lev');
	initTabs();
	togglePCN();
	voteFunc();
	voteAFilm();
	voteResult();
	//actorFade();
	autoUpdateStatistic(1000*60*1); // 1 minutes
	autoCheckValidLoggedIn(1000*60*1, 1000*30);
	new ShowHideTab('contTab');
	if($('messageType')){
		showAlert($('messageType'));
	}
});

