/*--- fade gallery ---*/
function fadeGallery(){
	var stay_time = 4000; //in ms
	var change_speed = 600; //in ms
	$('#visual').each(function(){
		var _f = true;
		var _t;
		var _hold = $(this);
		var _list = _hold.find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.show();
		if($.browser.msie) _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else _list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		if(stay_time && _f){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time+change_speed);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				if($.browser.msie){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_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(stay_time && _f){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- tooltip's ---*/
function initTooltip(){
	var _speed = 200; //in ms or '0' if not needed
	var _tooltip = $('<div class="tooltip"><div class="t"><a class="close" href="#">close</a></div><div class="c"><p></p></div><div class="b"></div></div>');
	var tooltip_text = _tooltip.find('div.c p');
	if($.browser.msie) _tooltip.hide();
	else _tooltip.css({display:'none', opacity: 0});
	$('body').append(_tooltip);
	var _t;
	var _a = false;
	$('div.tooltip-hold a.open').each(function(){
		var _text = $(this).attr('rel');
		$(this).removeAttr('rel');
		if(_text.length){
			$(this).mouseenter(function(){
				if(_t) clearTimeout(_t);
				tooltip_text.text(_text);
				_tooltip.stop().css({top:-9999, left:0,  display:'block'});
				_tooltip.css({
					top: $(this).offset().top -  _tooltip.outerHeight(),
					left: $(this).offset().left
				});
				if($.browser.msie) _tooltip.show();
				else _tooltip.animate({opacity: 1}, _speed);
				if(_a) $(_a).removeClass('active');
				_a = this;
				$(_a).addClass('active');
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					if($.browser.msie) _tooltip.hide();
					else _tooltip.stop().animate({opacity: 0}, _speed, function(){ $(this).hide();});
					if(_a) $(_a).removeClass('active');
				}, 200);
			});
		}
	});
	_tooltip.mouseenter(function(){
		if(_a) $(_a).addClass('active');
		if(_t) clearTimeout(_t);
		if($.browser.msie) _tooltip.show();
		else _tooltip.css('opacity',1);
	}).mouseleave(function(){
		if(_t) clearTimeout(_t);
		_t = setTimeout(function(){
			if($.browser.msie) _tooltip.hide();
			else _tooltip.stop().animate({opacity: 0}, _speed, function(){ $(this).hide();});
			if(_a) $(_a).removeClass('active');
		}, 200);
	}).find('a.close').click(function(){
		if($.browser.msie) _tooltip.hide();
		else _tooltip.stop().animate({opacity: 0}, _speed, function(){ $(this).hide();});
		if(_a) $(_a).removeClass('active');
	});
}


/*--- gallery 2 --- */
function initGall(){
	var _speed = 500; //in ms
	$('div.fade-gallery').each(function(){
		var _hold = $(this);
		var img_hold = _hold.find('div.main-image');
		var _btn = _hold.find('ul.thumbnails a');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		var _f = true;
		_btn.get(_a)._img = $('<img src="'+_btn.eq(_a).attr('href')+'" onmousemove="originalSizePhoto()" onload="imgOnloadFnc()"/>');
		_btn.get(_a)._img.addClass('active');
		img_hold.append(_btn.get(_a)._img);
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
				
		function changeEl(_ind){
			if(_ind != _a && _f){
				_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_btn.get(_a)._img.removeClass('active').animate({opacity: 0},{queue:false, duration: _speed});
				if(_btn.get(_ind)._img){
					_btn.get(_ind)._img.addClass('active').animate({opacity: 1},{queue:false, duration: _speed});
					_a = _ind;
					_f = true;
				}
				else{
					imgAcildi('1');
					_btn.get(_ind)._img = $('<img onmousemove="originalSizePhoto()"/>');
					_btn.get(_ind)._img.load(function(){
						_btn.get(_ind)._img.addClass('active').animate({opacity: 1},{queue:false, duration: _speed});
						_a = _ind;
						_f = true;
					});
					_btn.get(_ind)._img.css('opacity', 0);
					img_hold.append(_btn.get(_ind)._img);
					_btn.get(_ind)._img.attr('src', _btn.eq(_ind).attr('href'));
					//setTimeout('imgKpat()',1000);
				}
				
			}
		}
	});
}
/*---- clear inputs ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
}
$(document).ready(function(){
	fadeGallery();
	initTooltip();
	initGall();
	clearInputs();
});
