/*--- top gallery ---*/
function initGall1(){
	var wait_time = 3000; // in ms
	var change_speed = 700; // in ms
	var _hold = $('#info-gallery');
	if(_hold.length){
		var _t;
		var _f = true;
		var _f1 = false;
		var box_hold = _hold.find('div.gallery-content');
		var _btn = _hold.find('ul.gallery-tools a');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		$.ajax({
			url: _btn.eq(_a).attr('href'),
			dataType: 'html',
			success: function(_html){
				_btn.get(_a)._box = $(_html);
				_btn.get(_a)._box.addClass('active');
				box_hold.append(_btn.get(_a)._box);
				_f1 = true;
			}
		});
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _btn.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		});
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		if(_f && wait_time){
			_t = setTimeout(function(){
				if(_a < _btn.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, wait_time);
		}
		
		function changeEl(_ind){
			if(_ind != _a && _f1){
				_f1 = false;
				if(_t) clearTimeout(_t);
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				if(_btn.get(_ind)._box){
					if($.browser.msie){
						_btn.get(_a)._box.removeClass('active').hide();
						_btn.get(_ind)._box.addClass('active').show();
					}
					else{
						_btn.get(_a)._box.removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
						_btn.get(_ind)._box.addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
					}
					_f1 = true;
					_a = _ind;
					if(_f && wait_time){
						_t = setTimeout(function(){
							if(_a < _btn.length - 1) changeEl(_a + 1);
							else changeEl(0);
						}, wait_time+change_speed);
					}
				}
				else{
					$.ajax({
						url: _btn.eq(_ind).attr('href'),
						dataType: 'html',
						success: function(_html){
							_btn.get(_ind)._box = $(_html);
							if($.browser.msie) _btn.get(_ind)._box.hide();
							else _btn.get(_ind)._box.css('opacity', 0);
							box_hold.append(_btn.get(_ind)._box);
							if($.browser.msie){
								_btn.get(_a)._box.removeClass('active').hide();
								_btn.get(_ind)._box.addClass('active').show();
							}
							else{
								_btn.get(_a)._box.removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
								_btn.get(_ind)._box.addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
							}
							_f1 = true;
							_a = _ind;
							if(_f && wait_time){
								_t = setTimeout(function(){
									if(_a < _btn.length - 1) changeEl(_a + 1);
									else changeEl(0);
								}, wait_time+change_speed);
							}
						}
					});
				}
			}
		}
		
	}
}
/*--- content gallery ---*/
function initGall2(){
	var wait_time = 3000; // in ms
	var change_speed = 700; // in ms
	var _hold = $('#fade-gallery');
	if(_hold.length){
		var _t;
		var _f = true;
		var _btn = _hold.find('ul.gal-sl-tools a');
		var box_hold = _hold.find('div.gal-sl-content');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.get(_a)._box = $('<img src="'+_btn.eq(_a).attr('href')+'">');
		_btn.get(_a)._box.addClass('active');
		box_hold.append(_btn.get(_a)._box);
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _btn.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		});
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		if(_f && wait_time){
			_t = setTimeout(function(){
				if(_a < _btn.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, wait_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				if(!_btn.get(_ind)._box){
					_btn.get(_ind)._box = $('<img src="'+_btn.eq(_ind).attr('href')+'">');
					_btn.get(_ind)._box.css('opacity', 0);
					box_hold.append(_btn.get(_ind)._box);
				}
				_btn.get(_a)._box.removeClass('active').animate({opacity: 0},{queue:false, duration:change_speed});
				_btn.get(_ind)._box.addClass('active').animate({opacity: 1},{queue:false, duration:change_speed});
				_a = _ind;
			}
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _btn.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time + change_speed);
			}
		}
	}
}
/*--- ajax tabs ---*/
function ajaxTabs(){
	var _speed = 600; //in ms
	$('div.tabs-box').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('ul.tabset a');
		var box_hold = _hold.find('div.tabs-hold');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		var _f = false;
		_btn.each(function(){
			this._url = this.href;
			this._box = false;
			this.onclick = function(){
				changeEl(_btn.index(this));
				return false;
			}
		});
		$.ajax({
			url: _btn.get(_a)._url,
			dataType: 'html',
			success: function(_html){
				_btn.get(_a)._box = $(_html);
				_btn.get(_a)._box.addClass('active');
				box_hold.append(_btn.get(_a)._box);
				_f = true;
			}
		});
		function changeEl(_ind){
			if(_ind != _a && _f){
				_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				if(_btn.get(_ind)._box){
					box_hold.stop().height(box_hold.height());
					if($.browser.msie){
						_btn.get(_a)._box.removeClass('active').hide();
						_btn.get(_ind)._box.addClass('active').show();
					}
					else{
						_btn.get(_a)._box.stop().removeClass('active').animate({opacity: 0}, _speed, function(){$(this).hide();});
						_btn.get(_ind)._box.stop().addClass('active').show().animate({opacity: 1}, _speed);
					}
					box_hold.animate({height: _btn.get(_ind)._box.outerHeight()}, _speed/2, function(){ $(this).height('auto');});
					_a = _ind;
					_f = true;
				}
				else{
					$.ajax({
						url: _btn.get(_ind)._url,
						dataType: 'html',
						success: function(_html){
							_btn.get(_ind)._box = $(_html);
							if($.browser.msie) _btn.get(_ind)._box.hide();
							else _btn.get(_ind)._box.css({display:'none', opacity: 0});
							box_hold.append(_btn.get(_ind)._box);
							box_hold.stop().height(box_hold.height());
							if($.browser.msie){
								_btn.get(_a)._box.removeClass('active').hide();
								_btn.get(_ind)._box.addClass('active').show();
							}
							else{
								_btn.get(_a)._box.stop().removeClass('active').animate({opacity: 0}, _speed, function(){$(this).hide();});
								_btn.get(_ind)._box.stop().addClass('active').show().animate({opacity: 1}, _speed);
							}
							box_hold.animate({height: _btn.get(_ind)._box.outerHeight()}, _speed/2, function(){ $(this).height('auto');});
							_a = _ind;
							_f = true;
						}
					});
				}
			}
		}
	});
}
/*--- form validation ---*/
function formValidation(){
	var _hold = $('#contact-form');
	if(_hold.length){
		var _inputs = _hold.find('div.holder input:text, div.holder textarea');
		_inputs.each(function(){
			var _el = $(this);
			this._hold = _el.parents('div.holder:eq(0)');
			this._val = _el.val();
			this._f = (this._hold.hasClass('no-validation'))?(true):(false);
			_el.focus(function(){
				if(this.value == this._val) _el.stop().animate({opacity: 0.5}, 200);
			}).blur(function(){
				if(this.value == '' || this.value == this._val){
					if(this._f) this._hold.addClass('no-validation');
					_el.stop().css('opacity', '0.5').val(this._val).animate({opacity: 1}, 200);
				}
			}).keydown(function(){
				_el.stop().css('opacity', 1);
				if(this.value == this._val){
					this.value = '';
				}
				if(this._f) this._hold.removeClass('no-validation');
			});
		});
		_hold.find('input.btn-send-message').click(function(){
			if(checkFields()){
				return false;
			}
			else{
				var _ajaxURL = $(this).parents('form:eq(0)').attr('action');
				var _formData = $(this).parents('form:eq(0)').serialize();
				jQuery.ajax({
					type: "POST",
					url: _ajaxURL,
					data: _formData,
					error: function(){
						alert('Error');
					},
					success: function(msg){
						_hold.find('form').hide();
						_hold.find('div.confirm-box').show();
					}
				});
				return false;
			}
		});
		var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
		function checkFields(){
			var _f = false;
			_inputs.filter('.required').each(function(){
				if((this.value == '' || this.value == this._val)|| ($(this).attr('name') == 'email' && !_regEmail.test($(this).val()))){
					this._hold.addClass('no-validation');
					this._f = true;
					_f = true;
				}
			});
			return _f;
		}
	}
}
$(document).ready(function(){
	initGall1();
	initGall2();
	formValidation();
	ajaxTabs();
});
