var _duration = 400;
var nodes = [];
function detectSafari(){ 
if (navigator.appVersion.indexOf("Safari") != -1) 
   document.body.className += " safari"; 
} 

if (window.addEventListener) 
   window.addEventListener("load", detectSafari, false);
   
function initMenu()
{
	var nav = document.getElementById("main-nav");
	var _li_tmp_height = -18;
	if (nav)
	{
		nodes = nav.getElementsByTagName("li");				
		for (var i=0; i<nodes.length; i++)
		{
			if(nodes[i].parentNode.id == 'main-nav')
			{
				_li_tmp_height += 18;
			}
			var _uls = nodes[i].getElementsByTagName("ul");
			if(_uls.length > 0)
			{
				nodes[i]._i = i;
				nodes[i]._div = _uls[0].parentNode;
				nodes[i]._div._i = i;
				nodes[i]._ul = _uls[0];
				nodes[i]._div.id = "div-" + i;
				nodes[i]._div.style.top = _li_tmp_height + "px";

				nodes[i]._div.mooeffect = new Fx.Styles( nodes[i]._div.id, {wait: false, duration: _duration,					
						onStart: function(){
							//resetAll(this.element._i);
							},
						onComplete: function(){moveLIs(nodes[this.element._i]);}					
					});

				nodes[i]._ul_height = _uls[0].offsetHeight;				
				nodes[i]._ul_width = _uls[0].offsetWidth;				
				nodes[i]._ul.style.position = "relative";
				nodes[i]._lis = _uls[0].getElementsByTagName("li");
				
				var _tmp_height = 0;
				
				for(j=0; j < nodes[i]._lis.length; j++)
				{
					nodes[i]._lis[j].style.position = "absolute";
					nodes[i]._lis[j].style.left = "-200px";
					nodes[i]._lis[j].style.top = _tmp_height + "px";
					nodes[i]._lis[j].id = "li-" + i + "-" + j;
					nodes[i]._lis[j].mooeffect = new Fx.Styles( nodes[i]._lis[j].id, {duration: _duration,					
						onStart: function(){},
						onComplete: function(){}					
					});
					_tmp_height += nodes[i]._lis[j].offsetHeight;					
				}					
								
				nodes[i].onmouseover = function()
				{
					resetAll(this._i);
					if(this._timeout)
					{
						clearTimeout(this._timeout);
					}
					this._div.style.display = 'block';
					if(this._div.offsetHeight == 0)
					{
						resetLIs(this);
						this._div.mooeffect.set({'height':0});
						this._div.mooeffect.custom({'height':[0,this._ul_height]});
					}
				}
				nodes[i].onmouseout = function()
				{
					this._timeout = setTimeout('resetSUB(' + this._i + ')',150);
				}
			}
			else
			{
/*
				nodes[i].onmouseover = function()
				{
					resetAll(-1);					
				}
*/				
			}
		}	
	}
}

function resetAll(_i)
{
	for (var i=0; i<nodes.length; i++)
	{
		if(i != _i && nodes[i]._i)
		{
			resetSUB(nodes[i]._i);
		}
	}
}

function resetSUB(_i)
{
	nodes[_i]._div.style.display = 'none';
	nodes[_i]._div.style.height = '0';
	nodes[_i]._div.style.overflow = 'hidden';
	nodes[_i]._div._isopen = false;
	resetLIs(nodes[_i]);
}

function moveLIs(_this)
{
	for(j=0; j < _this._lis.length; j++)
	{
		_this._lis[j]._timeout = setTimeout('moveLI(' + _this._i +',' + j +',' + -1*_this._ul_width +',0)',_duration * j /2);
	}
}

function resetLIs(_this)
{
	for(j=0; j < _this._lis.length; j++)
	{
		if(_this._lis[j]._timeout)
		{
			clearTimeout(_this._lis[j]._timeout);
		}
		_this._lis[j].mooeffect.set({'left':-200});
	}
}

function moveLI(_i,_j,_from,_to)
{
	nodes[_i]._lis[_j].mooeffect.custom({'left':[_from,_to]});
}

var _testimonials = [];
var curr_t = 0;
var _tdiv = false;

function initTestimonials()
{
	_tdiv = document.getElementById('testimonials');
	if(_tdiv)
	{
		_tdiv.mooeffect = new Fx.Styles( _tdiv.id, {duration: 1000,					
						onStart: function(){},
						onComplete: function(){}					
					});
		var _spans = _tdiv.getElementsByTagName('span');
		for(var i=0; i < _spans.length; i++)
		{
			_testimonials[i] = _spans[i].innerHTML.replace(/\n/," ");
		}
		showTestimonial();		
	}	
}

function showTestimonial()
{
	_tdiv.innerHTML = "";		

	_tdiv.mooeffect.set({'top':0});

	var _text_parts = _testimonials[curr_t].split(" ");
	
	for(var i=0; i < _text_parts.length; i++)
	{
		setTimeout("addText('"+ _text_parts[i] +"')",250*i);
	}
	setTimeout("scrollText()",_text_parts.length*250);
}

function addText(_text){

	_tdiv.innerHTML = _tdiv.innerHTML + (" " + _text );

}

function scrollText(){

	_step_ = 100; /* was 83 */
	if(_tdiv.offsetHeight + _tdiv.offsetTop - _step_ > 10)
	{
		if(_tdiv.offsetHeight + _tdiv.offsetTop - 2*_step_ > 0)
		{
			var _step = _step_;
		}
		else
		{
			var _step = _tdiv.offsetHeight + _tdiv.offsetTop - _step_;
		}
		_tdiv.mooeffect.custom({'top':[_tdiv.offsetTop,_tdiv.offsetTop - _step]});
		setTimeout("scrollText()",1000 + 1000);		
	}
	else
	{
		curr_t++;
		if(curr_t >= _testimonials.length )
		{
			curr_t = 0;
		}		
		setTimeout("showTestimonial()",1000 + 1000);
	}
}

var _imgs = false;

function initImgGall()
{
	_imgs = document.getElementById('gallery-images');
	if(_imgs)
	{
		_imgs.parentNode.id = "div-p";
		_imgs.parentNode.parentNode.id = "div-pp";

		_imgs.pmooeffect = new Fx.Styles(_imgs.parentNode.id, {duration: 700,					
						onStart: function(){},
						onComplete: function(){}					
					});
		
		_imgs.ppmooeffect = new Fx.Styles(_imgs.parentNode.parentNode.id, {duration: 700,					
						onStart: function(){},
						onComplete: function(){}					
					});		
		
		_links = _imgs.getElementsByTagName('a');
		for(i=0; i < _links.length; i++)
		{
			_links[i].onclick = function()
			{
					var _href = this.href.substr(this.href.indexOf("#"));
 					var ow = window.open('/gallery.html'+_href,'ggallery','height=650,width=800');
 					ow.focus();
			}
		}
	
	}
	if ($('wrap-gallery-box')){
		var hs1 = new noobSlide({
			box: $('wrap-gallery-box'),
			items: [1,2,3,4],
			size: 402,
			autoPlay: true,
			interval:6000
		});
	}
}

function showImgGall()
{
	if(_imgs)
	{
		_imgs.pmooeffect.custom({'top':[-96,0]});
		_imgs.ppmooeffect.custom({'height':[23,117]});
	}
}


var as = [];
var curr_index = 0;

var duration = 2000;
var stop_duration = 3000;

var start_o = 0;
var end_o = 1;

function initSlideShow()
{
	if(_imgs)
	{	
		var _as = _imgs.getElementsByTagName('ul');
		var cnt = 0;
		var total_cnt = 0;

		for(i = 0; i < _as.length; i++)
		{
			if(_as[i].parentNode.id == "gallery-images")
			{
				total_cnt++;
			}
		}
		
		for(i = 0; i < _as.length; i++)
		{
			if(_as[i].parentNode.id == "gallery-images")
			{
				as[cnt] = _as[i];
				as[cnt]._index = cnt;
				
				if(cnt == 0)
				{
					as[cnt].style.display = 'block';
					as[cnt].style.visibility = 'visible';
				}
				else
				{
					as[cnt].style.display = 'none';					
					as[cnt].style.visibility = 'hidden';
				}
				cnt++;
			}
		}

		curr_index = 0;		
		_switch(true);
		
	}
}


function _switch(_start,_next_index)
{
	var as_cnt = as.length;
	if(as_cnt > 1)
	{
		if(!_start)
		{

			if(_next_index)
			{
				var next_index = _next_index - 1;
			}
			else
			{			
				var next_index = curr_index + 1;
				if(next_index == as_cnt) next_index = 0;
			}
	
			if(next_index != curr_index)
			{

				var _opacity1 = new Fx.Styles( as[curr_index], {duration: duration,
					
					onStart: function(){
						this.element.style.display = 'block';
					},
					onComplete: function(){this.element.style.display = 'none';}	
					
					});
			
				_opacity1.custom({'opacity': [end_o,start_o]});
		
				var _opacity2 = new Fx.Styles( as[next_index], {duration: duration,
					
					onStart: function(){
						this.element.style.display = 'block';
					},
					onComplete: function(){this.element.style.display = 'block';}		
					
					});
			
				_opacity2.set({'opacity': start_o});
				_opacity2.custom({'opacity': [start_o,end_o]});
		
				 curr_index = next_index;
			}
		}
		
		setTimeout('_switch(false)',duration + stop_duration);
		
	}
}

var boxes = [];
var triggers = [];
var ctriggers = [];
var active = false;

function initAccordion() {
		var _box = $('nav');
		if (_box)
		{
			if(_box.style.visibility == 'visible') return;
			var els = _box.getElements("li");
			els.each(function(el, i) {
				if (el.getElements("div").length)
				{
					boxes.push(el.getElements("div")[0]);
					triggers.push(el.getElements("a.link-2")[0]);
					if (el.className.indexOf("active") != -1)
					{
						active = boxes.length-1;
					}
				}
            else
            {
               _els = el.getElements("a.link-1");
               if(_els[0])
               {
                  ctriggers.push(_els[0]);
               }
            }
			}, this);
			
         ctriggers.each(function(el, i) {
            el.onclick = function()
            {
					Cookie.set("menu", -1, {duration: 1});
            }
         })
            
			var _cookie = false;
			if (Cookie.get("menu"))
			{
				_cookie = Cookie.get("menu");
			}

			var _tmp = $(_cookie);
			if (_tmp)
			{
				boxes.each(function(_box, i){
					if (_box.parentNode.id == _tmp.id && !active)
					{
						active = i;
					}
				});
			}
			
			if (boxes.length && triggers.length)
			{
				var accordion = new Accordion(triggers, boxes, {
					opacity: false,
					show: active,
					display: -1,
					alwaysHide: true,
					onActive: function(toggler, element){
						if (toggler.parentNode.className.indexOf("active") == -1)
						{
							toggler.parentNode.className += " active";
						}
						Cookie.remove(_cookie);
						Cookie.set("menu", toggler.parentNode.id, {duration: 1});
					},
					onBackground: function(toggler, element){
//						Cookie.remove(_cookie);
//						Cookie.set("menu", false, {duration: 1});
						toggler.parentNode.className = toggler.parentNode.className.replace("active", "");
					}
				}, _box);
			}			
			_box.style.visibility = 'visible';
		}	
}

function initPage()
{
	initMenu();
	initAccordion();
	initImgGall();
	setTimeout('showImgGall()',2000);
	setTimeout('initSlideShow()',1500);
	initTestimonials();
	initTeamEffect();
}


if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
