/*--- IE6 hover ---*/
function ieHover(h_list){
	if($.browser.msie && $.browser.version < 7){
		$(h_list).hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- tabs ---*/
function initTabs(){
	var nav_hold = $('#tabs-nav');
	var tabs_hold = $('#tabs-container');
	if(nav_hold.length && tabs_hold.length){
		var _loader = $('<div class="loader">Loading...</div>');
		_loader.css({display: 'none', opacity: 0});
		tabs_hold.append(_loader);
		var _btn = nav_hold.find('a');
		var _a = _btn.parent().index(_btn.parent().filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.parent().removeClass('active').eq(_a).addClass('active');
		$.ajax({
			url: _btn.eq(_a).attr('href'),
			dataType: 'html',
			success: function(_html){
				_btn.get(_a)._box = $(_html);
				tabs_hold.append(_btn.get(_a)._box);
				initGall();
			}
		});
		var _f = true;
		_btn.click(function(){
			var t_btn = this;
			if(_a != _btn.index(this) && _f){
				_f = false;
				_btn.eq(_a).parent().removeClass('active');
				$(t_btn).parent().addClass('active');
				tabs_hold.height(tabs_hold.height());
				if(this._box){
					_btn.get(_a)._box.animate({opacity: 0}, 300, function(){
						$(this).hide();
						t_btn._box.show().animate({opacity: 1}, 300);
						tabs_hold.animate({height: t_btn._box.height()}, 200, function(){
							$(this).height('auto');
							_f = true;
							_a = _btn.index(t_btn);
						});
					});
				}
				else{
					_loader.show().animate({opacity: 1}, 200);
					_btn.get(_a)._box.animate({opacity: 0}, 300, function(){
						$(this).hide();
						$.ajax({
							url: t_btn.href,
							dataType: 'html',
							success: function(_html){
								t_btn._box = $(_html);
								t_btn._box.css('opacity', 0);
								tabs_hold.append(t_btn._box);
								t_btn._box.show().animate({opacity: 1}, 300);
								_loader.animate({opacity: 0}, 200, function(){$(this).hide();});
								tabs_hold.animate({height: t_btn._box.height()}, 200, function(){
									initGall();
									$(this).height('auto');
									_f = true;
									_a = _btn.index(t_btn);
								});
							}
						});
					});
				}
			}
			return false;
		});
		
	}
}
/*--- gallery function ---*/
function initGall() {
	var _hold = $('#slide-gallery');
	if(_hold.length && _hold.get(0)._f == undefined){
		_hold.get(0)._f = true;
		var btn_prev = _hold.find('a.btn-prev');
		var btn_next = _hold.find('a.btn-next');
		var list_h = _hold.find('div.holder > ul');
		var _step = list_h.parent().width();
		var _a = 0;
		var _w = list_h.children().length * list_h.children().width();
		if(_step < _w){
			var _max = Math.ceil(_w/_step) - 1;
			btn_prev.click(function(){
				if(_a > 0) moveList(_a - 1);
				else moveList(_max);
				return false;
			});
			btn_next.click(function(){
				if(_a < _max) moveList(_a + 1);
				else moveList(0);
				return false;
			});
		}
		else{
			btn_prev.hide();
			btn_next.hide();
		}
		function moveList(_ind){
			if(_ind != _a){
				list_h.animate({left:-_ind*_step}, {queue:false, duration:600});
				_a = _ind
			}
		}
	}
}
/*--- slider for bg ---*/
function initSlider(){
	
}
$(document).ready(function () {
	clearInputs();
	initTabs();
	ieHover('.nav li, .top-nav li');
	initGall();
	initSlider();

	initGallL3($('body'));
});

function GoHealthwise(myButton) {
	var ValueFromTextbox = "search=" + myButton.value;
	__doPostBack('__Page', ValueFromTextbox);
}

function dropDownGoTo(myDDLControl) {

	var _ParamValue = myDDLControl.value;

	if (_ParamValue.substring(0, 1) == 'p') {
		_ParamValue = _ParamValue.replace('p=', '');
		var PostBackParams = "page=" + _ParamValue;
		__doPostBack('__Page', PostBackParams);
	}
	else {
		_ParamValue = _ParamValue.replace('u=', '');
		location.href = _ParamValue;
	}
}

/*--- gallery like3 function ---*/
function initGallL3(_cont) {
	var saty_time = 8000; //in ms or 'false' if not needed
	var change_speed = 600; //in ms

	if (!_cont) _cont = $('body');
	_cont.find('div.gallery-l3').each(function () {
		if (!this._f) {
			this._f = true;
			var _f = true;
			var _t;
			var _hold = $(this);
			var _list = _hold.find('div.image-gallery li');
			var _a = _list.index(_list.filter('.active:eq(0)'));
			if (_a == -1) _a = 0;
			_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
			if (_list.length > 1) {
				var _btn = $('<ul class="controls"></ul>');
				for (var i = 0; i < _list.length; i++) {
					_btn.append('<li><a href="#">' + (i + 1) + '</a></li>');
				}
				_hold.append(_btn);
				_btn = _btn.find('a');
				_btn.eq(_a).addClass('active');
				_btn.click(function () {
					_f = false;
					changeEl(_btn.index(this));
					return false;
				});
				if (_f && saty_time) {
					_t = setTimeout(function () {
						if (_a < _list.length - 1) changeEl(_a + 1);
						else changeEl(0);
					}, saty_time);
				}
			}
			function changeEl(_ind) {
				if (_t) clearTimeout(_t);
				if (_ind != _a) {
					_btn.eq(_a).removeClass('active');
					_btn.eq(_ind).addClass('active');
					_list.eq(_a).removeClass('active').animate({ opacity: 0 }, { queue: false, duration: change_speed });
					_list.eq(_ind).addClass('active').animate({ opacity: 1 }, { queue: false, duration: change_speed });
					_a = _ind;
				}
				if (_f && saty_time) {
					_t = setTimeout(function () {
						if (_a < _list.length - 1) changeEl(_a + 1);
						else changeEl(0);
					}, saty_time + change_speed);
				}
			}
		}
	});
}
