$(document).ready(function() {
	slideShow();
	newsLine();
	toolTip();
	initTab();
	clientLogin();
	initaccordion();
	initTestimonial();
	$('.btn-send').hover(function(){$(this).css({'background':'#a83539'});},function(){$(this).css({'background':'#343232'});});
});
/*--- slideShow function ---*/
function slideShow() {
	var _duration = 10000;
	var change_speed = 600;
	var _active = 0;
	var _timer;
	var _list = $('#visual .box-list li');
	if(_list.length > 0) {
		_list.each(function(_ind, _el) {
			if($(_el).hasClass('active')){
				_active = _ind;
				$(_el).removeClass('active');
			}
		});
		_list.css({zIndex: 1, opacity:0}).eq(_active).css({zIndex: 2, opacity:1});
		_timer = setTimeout(function(){ nextEl();}, _duration);
		function changeEl(_ind) {
			if(_ind != _active) {
				if (_timer) { clearTimeout(_timer);}
				_active = _ind;
				_list.css('z-index', 1).animate({opacity: 0}, { queue:false, duration: change_speed }).eq(_active).css('z-index', 2).animate({opacity: 1}, { queue:false, duration: change_speed });
				_timer = setTimeout(function(){ nextEl();}, _duration + change_speed);
			}
		}
		function nextEl() {
			if(_active == _list.length - 1) { changeEl(0);}
			else { changeEl(_active + 1);}
		}
	}
}
/*--- newsLine function ---*/
function newsLine() {
	var _duration = 5000;
	var change_speed = 500;
	var _active = 0;
	var _timer;
	var _holder = $('#news-line ul');
	if(_holder.length > 0) {
		var _list = _holder.find('li');
		_list.each(function(_ind, _el) {
			if($(_el).hasClass('active')){
				_active = _ind;
				$(_el).removeClass('active');
			}
		});
		_list.css({opacity:0, display:'none'}).eq(_active).css({opacity:1, display:'block'});
		_timer = setTimeout(function(){ nextEl();}, _duration);

		function changeEl(_ind) {
			if (_timer) {clearTimeout(_timer);}
			_list.eq(_active).stop().animate({opacity: 0}, change_speed, function() {
				_holder.width(_list.eq(_active).width());
				_list.eq(_active).hide();
				_holder.animate({width: _list.eq(_ind).width()}, 300);
				_list.eq(_ind).show().animate({opacity: 1}, change_speed, function() {
					_active = _ind;
					_holder.stop().css('width','auto');
					_timer = setTimeout(function(){ nextEl();}, _duration);
				});
			});
		}
		function nextEl() {
			if(_active == _list.length - 1) { changeEl(0);}
			else { changeEl(_active + 1);}
		}
	}
}
/*--- toolTip function ---*/
function toolTip() {
	var _duration = 300;
	var _list = $('.with-tooltip');
	if(_list) {
		_list.children('span').css({opacity: 0, display: 'none'});
		_list.each( function(_ind, _el) {
			var _box = $(this).children('span');
			_el.onmouseover = function() {
				_box.stop().show().animate({opacity: 1}, _duration);
			}
			_el.onmouseout = function() {
				_box.stop().animate({opacity: 0}, _duration, function() { $(this).hide();});
			}
		});
	}
}
/*--- initTab function ---*/
function initTab() {
	var wait_change = 400;
	var _timer;
	var _hold = $('.tabs-holder');
	if(_hold.length > 0) {
		_hold.each(function(_ind, _el) {
			var btn_hold = $(_el).children('.nav');
			var box_hold = $(_el).children('.text-h');
			var _active = 0;
			var _btn = btn_hold.find('a').each(function(_ind, _el) {
				if($(_el).hasClass('active')) {
					_active = _ind;
				}
			});
			var _box = box_hold.children('.box').hide();
			var a_box = document.getElementById(_btn.get(_active).href.substr(_btn.get(_active).href.indexOf("#") + 1));
			if(a_box) { a_box.style.display = 'block';}
			else{ _box.eq(_active).show();}
			
			_btn.hover(function() {
				if (_timer) clearTimeout(_timer);
				changeTab(this);
			}, function() {
				if (_timer) clearTimeout(_timer);
			});
			
			function changeTab(a_btn) {
				var a_box;
				a_box = document.getElementById(a_btn.href.substr(a_btn.href.indexOf("#") + 1));
				if(a_box) { a_box = $(a_box);}
				else { a_box = _box.eq(_btn.index(a_btn));}
				
				_timer = setTimeout(function() {
					_btn.removeClass('active');
					_box.hide();
					$(a_btn).addClass('active');
					a_box.show();
				}, wait_change);
			}
		});
	}
}
/*--- clientLogin function ---*/
function clientLogin() {
	var _duration = 500;
	var _hold = $('#client-login');
	if(_hold.length > 0) {
		var _box = _hold.find('.container');
		_hold.find('.btn-login').click(function() {
			if(_hold.hasClass('opened')){
				_box.stop().animate({width: 0}, _duration, function(){
					_hold.removeClass('opened');
				});
			}
			else {
				_box.stop().animate({width: _box.children().width()}, _duration, function(){
					_hold.addClass('opened');
				});
			}
			return false;
		});
	}
}
/*--- initaccordion function ---*/
function initaccordion() {
	var _duration = 300;
	var _list = $('.accordion-block ul li');
	if(_list.length > 0) {
		_list.each(function(_ind, _el){
			var _btn = $(_el).children('.title').find('a,span.btn');
			var _box = $(_el).children('.text-h');
			if($(_el).hasClass('opened')) { _box.height('auto');}
			else { _box.height(0);}
			
			_btn.click(function() {
				if($(_el).hasClass('opened')) {
					$(_el).removeClass('opened');
					_box.stop().stop().animate({height: 0}, _duration);
				}
				else {
					$(_el).addClass('opened');
					_box.stop().stop().animate({height: _box.children().outerHeight()}, _duration, function(){
						_box.height('auto');
					});
				}
				return false;
			});
		});
	}
}
/*--- initTestimonial function ---*/
/* random quote */
function initTestimonial() {
	var _hold = $('.customers-say-block ul');
	if(_hold.length > 0) {
		var _list = _hold.find('li');
		_list.hide();
		var _random = Math.round(Math.random()*(_list.length-1));
		_list.eq(_random).show();
	}
}

