window.addEvent('domready', initACIPage);

function initACIPage() {
	initLinks();
	initMainMenu();
	initServices();
	initTeamPage();
}

/**
 *	Links
 */
function initLinks(){
	$$("a").forEach( function(item,ind){
		var rel = item.getProperty("rel");
		var className = item.className;
		if (className == 'close-window'){
			item.addEvent('click', function(e){
				window.close();
				(new Event(e)).stop();
			});
		}
	});
}

/**
 *	Navigation
 */ 
function initMainMenu() {
	var menu = $("menu");
	if(!$defined(menu)) return;
	
	var lis = menu.getElements("li");
	for(var i=0; i < lis.length; i++){
		var li = lis[i];
		var parentID = li.getParent().id;
		var subMenus = li.getElements("ul");
		if(parentID == "menu" && subMenus && subMenus.length > 0){
			li.onmouseover = function(){
				this.getElement("a").addClass('hover');
				this.getElement("ul").setStyle('display', 'block');
			}
			li.onmouseout = function(){
				this.getElement("a").removeClass('hover');
				this.getElement("ul").setStyle('display', 'none');
			}
		}
	}
}

/**
 *	services tabs
 **/ 
var servicesTabs = [];
var activeTab = -1;
var servicesTimer = 'off';
var fadeEffect;
function initServices(){
	var tabs = $("services-tabs");
	var content= $("services-content");
	if(!$defined(tabs) || !$defined(content)) return;
	
	tabs.getElements("li").forEach(function(item,ind){
		var link = item.getElement("a");
		var ID = link.getProperty("rel");
		
		// init content boxes height
		var contentBox = $('scontent'+ID);
		var contentDiv = contentBox.getElement("div");
		contentBox.show();
		contentDiv.setProperty("originHeight", contentDiv.offsetHeight);
		contentDiv.setStyle("height", "0px");
		contentBox.hide();
		
		// prepare tab object
		link.tab = {
			'num':ind, 
			'tabID': 'stab'+ID, 
			'contentID': 'scontent'+ID, 
			'link': link, 
			'content': contentBox,
			'contentDiv': contentDiv
			};
			
		// remember to array
		servicesTabs.push( link.tab );
		
		// hover event: show hovered tab content
		link.addEvent('mouseover', function(e){
			servicesTimer = 'off';
			var active =  servicesTabs[activeTab];
			active.link.removeClass('active');
			active.content.hide();
			
			this.tab.content.show();
			this.tab.contentDiv.show();
			//toogleTabs( this.tab.num );
		});
		
		// over event: enable timer again, show active tab 
		link.addEvent('mouseout', function(e){
			this.tab.content.hide();

			var active =  servicesTabs[activeTab];
			active.link.addClass('active');
			active.content.show();
			
			setTimeout( 'servicesTimer = "off";', 5000 );
		});
		
		// with onclick change active tab
		link.addEvent('click', function(e){
			servicesTimer = "off";
			toggleTabs( this.tab.num, true );
			setTimeout( 'servicesTimer = "off";', "" );
			(new Event(e)).stop();
		});
	});
	toggleTabs(0);
	setTimeout( 'servicesTimer = "on"; toggleTabsTimer();', 5000 );
}

function toggleTabs( next, force ){
	if(activeTab == next) return;
	if(activeTab == -1) {
		// init action
		activeTab = next;
		servicesTabs[activeTab].link.addClass('active');
		servicesTabs[activeTab].content.show();
		servicesTabs[activeTab].contentDiv.show();
	}
	else if(!force){
		if(next >= servicesTabs.length) next = 0;
		
		// unset previous tab
		var active = servicesTabs[activeTab];
		var height = getOriginHeight( active.contentDiv );
		
		// remove active class
		active.link.removeClass('active');
		
		// create effect
		fadeEffect = new Fx.Style(active.contentDiv, 'height', {duration:500,
			// when hide effect is complete show new tab
			onComplete:function(){
				// hide prev tab
				this.element.parentNode.hide();

				activeTab = this.nextTab;
				var active = servicesTabs[activeTab];
				var height = getOriginHeight( active.contentDiv );

				active.link.addClass('active');
				active.contentDiv.setStyle('height', '0px');
				active.content.show();

				fadeEffect = new Fx.Style(active.contentDiv, 'height', {duration:500});
				fadeEffect.nextTab = -1;
				fadeEffect.startPoint = 0;
				fadeEffect.endPoint = height;
				fadeEffect.direction = 'show';
				fadeEffect.start(fadeEffect.startPoint, fadeEffect.endPoint);
			}
		});
			
		fadeEffect.nextTab = next;
		fadeEffect.direction = 'hide';
		fadeEffect.startPoint = height;
		fadeEffect.endPoint = 0;
		fadeEffect.start(fadeEffect.startPoint, fadeEffect.endPoint);
	}
	else {
		var active = servicesTabs[activeTab];
		active.link.removeClass('active');
		active.content.hide();
		
		activeTab = next;
		var active = servicesTabs[activeTab];
		active.link.addClass('active');
		active.content.show();
		active.contentDiv.show();
	}
}

function toggleTabsTimer(){
	if( servicesTimer == 'on' ){
		toggleTabs( activeTab + 1 );
	}
	setTimeout( 'toggleTabsTimer();', 5000 );
}

function getOriginHeight( elem ){
	var height = elem.getProperty('originHeight');
	if(!height){
		elem.setProperty('originHeight', elem.offsetHeight);
		height = elem.offsetHeight;
	}
	return height;
}

/**
 * News page
  
var wndNews;
function initNewsPage(){
	var newsContainer = $("new_and_press");
	if(!$defined(newsContainer)) return;
	
	newsContainer.getElements("a.read_more").forEach( function(item,ind){
		item.addEvent('click', function(e){
			if(wndNews) wndNews.close();
			
			var wndLocation = this.href + "";
			var wndName = "NewsDetails";
			var wndParams = 'scrollbars=yes,resizable,width=700,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no';
			wndNews = window.open(wndLocation,wndName,wndParams);
			(new Event(e)).stop();
		});
	});
}
*/
/**
 * Deals page

var wndDelas;
function initDealsPage(){
	var dealsContainer = $("deals_content");
	if(!$defined(dealsContainer)) return;
	
	dealsContainer.getElements("a.read_more").forEach( function(item,ind){
		item.addEvent('click', function(e){
			if(wndDelas) wndDelas.close();
			
			var wndLocation = this.href + "";
			var wndName = "DealsDetails";
			var wndParams = 'scrollbars=yes,resizable,width=800,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no';
			wndDelas = window.open(wndLocation,wndName,wndParams);
			(new Event(e)).stop();
		});
	});
}
 */ 
/**
 *	Team Page
 */
function initTeamPage(){
	if(!$("team_content") || !$("team_members")) return;
	$("team_members").getElements("a").forEach(function(item,ind){
		item.addEvent('click', function(e){
			var getInfo = new Ajax(this.href, {method:'post', data: 'request=ajax', update: $("team_content"), 
				onComplete: function(){
						$("team_content").addClass('team-single');
					}
				});
			getInfo.request();
			(new Event(e)).stop();
		});
	});
}

/*
 * MooTools addon functions
 */
Element.extend({
	hide: function(){
		this.setStyle('display', 'none');
	},
	show: function(){
		this.setStyle('display', 'block');
		if(this.getProperty('originHeight')){
			this.setStyle('height', this.getProperty('originHeight') + 'px');
		}
	}
});

