/*--- gallery ---*/
function initGall(){
	var stay_time = 3000; //in ms
	var change_speed = 1000; //in ms
	var _hold = $('#gallery');
	if(_hold.length){
		var list_hold = _hold.find('div.hold > ul');
		var btn_prev = _hold.find('a.link-prev');
		var btn_next = _hold.find('a.link-next');
		var hold_w = list_hold.parent().width();
		var _list = list_hold.children();
		var list_w = _list.length * _list.outerWidth();
		var _f1 = false;
		var _t;
		if(list_w > hold_w){
			list_hold.append(_list.clone()).append(_list.clone());
			var _flag = true;
			_f1 = true;
			var _m = list_w;
			list_hold.css({left: -_m});
			
			btn_prev.click(function(){
				moveList(false);
				return false;
			});
			btn_next.click(function(){
				moveList(true);
				return false;
			});
			if(_f1){
				_t = setTimeout(function(){
					moveList(true);
				}, stay_time);
			}
			_hold.mouseenter(function(){
				_f1 = false;
				if(_t) clearTimeout(_t);
			}).mouseleave(function(){
				_f1 = true;
				_t = setTimeout(function(){
					moveList(true);
				}, stay_time);
			});
		}
		else{
			btn_prev.parent().css('visibility','hidden');
			btn_next.parent().css('visibility','hidden');
		}
		function moveList(_f){
			if(_t) clearTimeout(_t);
			if(_flag){
				_flag = false;
				if(_f){
					if(_m + hold_w > list_w*3 - hold_w){
						_m -= list_w;
						list_hold.css('left', -_m);
					}
					_m += hold_w;
				}
				else{
					if(_m - hold_w < 0){
						_m += list_w;
						list_hold.css('left', -_m);
					}
					_m -= hold_w;
				}
				list_hold.animate({left: -_m}, change_speed, function(){
					_flag = true;
				});
			}
			if(_f1){
				_t = setTimeout(function(){
					moveList(true);
				}, stay_time+change_speed);
			}
		}
	}
}
/*--- profile gallery ---*/
function profileGall(){
	var _speed = 600; //in ms
	var _hold = $('#profile-hold');
	var _btn = $('#profile-list li');
	if(_hold.length && _btn.length){
		var btn_prev = _hold.find('a.link-prev');
		var btn_next = _hold.find('a.link-next');
		var box_hold = _hold.find('div.hold');
		var _loader = $('<div class="loader">Loading...</div>').css({display:'none', opacity: 0});
		box_hold.append(_loader);
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		var _f = false;
		var PersonId = _btn.eq(_a).find('a').attr('rel');
		var $Original = $("#" + PersonId);
		$('img', $Original).each(function() {
            		$(this).attr("src", $(this).attr("rel"));
	        });
		_btn.get(_a)._box = $Original
		_btn.get(_a)._box.addClass('active');
		box_hold.append(_btn.get(_a)._box);
		_f = true;
		_btn.click(function(){
			changeEl(_btn.index(this));
			return true;
		});
		btn_prev.click(function(){
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_btn.length - 1);
			return false;
		});
		btn_next.click(function(){
			if(_a < _btn.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		function changeEl(_ind){
			if(_f && _a != _ind){
				_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				box_hold.stop().height(box_hold.height());
				if($.browser.msie && $.browser.version < 7) _btn.get(_a)._box.stop().removeClass('active').hide();
				else _btn.get(_a)._box.stop().removeClass('active').animate({opacity: 0}, _speed, function(){ $(this).hide();});
				if(_btn.get(_ind)._box){
					if($.browser.msie && $.browser.version < 7) _btn.get(_ind)._box.addClass('active').show();
					else _btn.get(_ind)._box.stop().show().addClass('active').animate({opacity: 1}, _speed);
					box_hold.animate({height: _btn.get(_ind)._box.outerHeight()}, _speed, function(){ $(this).height('auto');});
					_a = _ind;
					_f = true;
				}
				else{
					box_hold.stop().height(box_hold.height());
					_loader.show().animate({opacity: 1}, 150);
					
					//Loads new block here
					var PersonId = _btn.eq(_ind).find('a').attr('rel');
					var $Original = $("#" + PersonId);
					$('img', $Original).each(function() {
            					$(this).attr("src", $(this).attr("rel"));
				        });
					_btn.get(_ind)._box = $Original;
					if($.browser.msie && $.browser.version < 7) _btn.get(_ind)._box.addClass('active').hide();
					else _btn.get(_ind)._box.addClass('active').css('opacity', 0);
					box_hold.append(_btn.get(_ind)._box);
					_loader.show().animate({opacity: 0}, 150, function(){ $(this).hide();});
					if($.browser.msie && $.browser.version < 7) _btn.get(_ind)._box.show();
					else _btn.get(_ind)._box.animate({opacity: 1}, _speed);
					box_hold.animate({height: _btn.get(_ind)._box.outerHeight()}, _speed, function(){
						$(this).height('auto');
						_a = _ind;
						_f = true;
					});
				}
			}
		}
	}
}

/*--- IE6 hover ---*/
function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).live('mouseover', function(){
			$(this).addClass(h_class);
		}).live('mouseout', function(){
			$(this).removeClass(h_class);
		});
	}
}
/*--- input hover ---*/
function btnHover(){
	$('input.image').mouseenter(function(){
		this.src = this.src.replace(".gif", "-hover.gif");
	}).mouseleave(function(){
		this.src = this.src.replace("-hover.gif", ".gif");
	});
}
$(document).ready(function(){
	initGall();
	profileGall();
	btnHover();
	ieHover('.innovative ul li, .lawyers ul li, .innovative-top ul li, .preference ul li');
	$('.watermark-name input').watermark({watermarkText: 'Name', watermarkCssClass: 'contact-watermark'});
	$('.watermark-email input').watermark({watermarkText: 'Email Address', watermarkCssClass: 'contact-watermark'});
	$('.watermark-message textarea').watermark({watermarkText: 'Message', watermarkCssClass: 'contact-watermark'});
});