function initHeight(){
	var _hardcode = 189;
	var _hold = $('div.page-wrap');
	var _holdH = _hold.height(); 
	if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
	else {
		_height = document.documentElement.clientHeight;
		_width = document.documentElement.clientWidth;
	}
	if(_holdH + _hardcode < _height + 5){
		_hold.css('height', (_height - _hardcode));
	}
	
	$(window).resize(function(){
		if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if(_holdH + _hardcode < _height + 5){
			_hold.css('height', (_height - _hardcode));
		}
	});
};

function drop(){
	$('#nav li').each(function(){
		var _d = 400;
		var _drop = $(this).find('div.drop')
		$(this).mouseenter(function(){
			var _this = this;
			if(_drop.is(':animated')) _drop.stop(true, true);
			if(_drop.length){
				$(_this).addClass('hover');
				_drop.css({opacity: 0,display: 'block'}).animate({opacity: 1},_d);
			}else $(this).addClass('hover');
		}).mouseleave(function(){
			var _this = this;
			if(_drop.length){
				_drop.fadeOut(_d,function(){
					$(_this).removeClass('hover');
				});
			}else $(this).removeClass('hover');
		})
	})
}

/* Gallery */
jQuery.fn.gallery = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: false,
		slideElement: 1,
		effect: false,
		fadeEl: 'ul',
		switcher: 'ul > li',
		disableBtn: false,
		next: 'a.link-next, a.btn-next, a.next',
		prev: 'a.link-prev, a.btn-prev, a.prev',
		circle: true
	},_options);

	return this.each(function(){
		var _hold = $(this);
		if (!_options.effect) var _speed = _options.duration;
		else var _speed = $.browser.msie ? 0 : _options.duration;
		var _timer = _options.autoSlide;
		var _sliderEl = _options.slideElement;
		var _wrap = _hold.find(_options.fadeEl);
		var _el = _hold.find(_options.switcher);
		var _next = _hold.find(_options.next);
		var _prev = _hold.find(_options.prev);
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		if (((_wrapHolderW-1)*_w + _w/2) > _wrap.parent().width()) _wrapHolderW--;
		if (_timer) var _t;
		var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		if (!_options.effect) var rew = _count - _wrapHolderW + 1;
		else var rew = _count;
		
		if (!_options.effect) _wrap.css({marginLeft: -(_w * _active)});
		else {
			_wrap.css({opacity: 0}).removeClass('active').eq(_active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
			_el.removeClass('active').eq(_active).addClass('active');
		}
		if (_options.disableBtn) {
			if (_count < _wrapHolderW) _next.addClass(_options.disableBtn);
			_prev.addClass(_options.disableBtn);
		}
		
		function fadeElement(){
			_wrap.eq(_last).animate({opacity:0}, {queue:false, duration: _speed});
			_wrap.removeClass('active').eq(_active).addClass('active').animate({
				opacity:1
			}, {queue:false, duration: _speed, complete: function(){
				$(this).css('opacity','auto');
			}});
			_el.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		function scrollEl(){
			_wrap.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed});
		}
		function toPrepare(){
			if ((_active == rew) && _options.circle) _active = -_sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active++;
				if (_active > rew) {
					_active--;
					if (_options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
				}
			};
			if (_active == rew) if (_options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
		}
		function runTimer(){
			_t = setInterval(function(){
				toPrepare();
			}, _timer);
		}
		_next.click(function(){
			if(_t) clearTimeout(_t);
			if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_options.disableBtn);
			toPrepare();
			if (_timer) runTimer();
			return false;
		});
		_prev.click(function(){
			if(_t) clearTimeout(_t);
			if (_options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_options.disableBtn);
			if ((_active == 0) && _options.circle) _active = rew + _sliderEl;
			for (var i = 0; i < _sliderEl; i++){
				_active--;
				if (_active < 0) {
					_active++;
					if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
				}
			};
			if (_active == 0) if (_options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_options.effect) _el.click(function(){
			_active = _el.index($(this));
			if(_t) clearTimeout(_t);
			fadeElement();
			if (_timer) runTimer();
			return false;
		});
		if (_timer) runTimer();
	});
}

function initValidate(){
	var _form = $('.validate');
	_form.each(function(){
		var _this = this;
		_this.reset();
		var _thankyoubox = $('.thankyoubox', this);
		var _errorbox = $('.error-box',this); //.hide();
		var _submit = $('.submit-form', this);
		var _reset = $('.btn-clear', this);
		_reset.click(function(){
			_this.reset();
			return false;
		});

		var _required = $('.required', this);
		// Use jquery watermark (i.e. $.Watermark)
		/*
		_required.each(function(){
			$(this).attr('def', $(this).attr('value'));
			$(this).focus(function(){
				if($(this).attr('value')==$(this).attr('def')) $(this).attr('value','');
			}).blur(function(){
				if($(this).attr('value')=='') $(this).attr('value',$(this).attr('def'));
			})
		});*/
		var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		_submit.click(function(){
			var eror = false;
			// Hide all the watermarks before form submission so the watermark is not treated like an actual value.
			$.Watermark.HideAll(); 
			// Hide if form is resubmitted, refresh on resubmit or display error messages.
			_thankyoubox.hide(); 
			_errorbox.find('span').remove();
			_errorbox.empty().append('<ul></ul>');
			var errorlist = _errorbox.find('ul');
			_required.each(function(){
				if ($(this).val()==0 || $(this).val()==$(this).attr('def')) {
					eror = true;
					errorlist.show().append('<li><span class="errorMessage">'+$(this).attr('title')+' is required</span></li>');
					$(this).parent().addClass('error');
				} else {
					$(this).parent().removeClass('error');
					if ($(this).hasClass('email')){
						if (!_emailReg.test($(this).val())){
							eror = true;
							errorlist.show().append('<span>'+$(this).attr('title')+' is not correct'+'</span>');
							$(this).parent().addClass('error');
						}else {
							if(_required.filter('.email').length > 1 && (this!= _required.filter('.email').get(0))){
								if(_required.filter('.email').eq(0).val() != _required.filter('.email').eq(1).val()){
									eror = true;
									errorlist.show().append('<span>'+$(this).attr('title')+' does not match Email'+'</span>');
									$(this).parent().addClass('error');
								}else{
									$(this).parent().removeClass('error');
								}
							}
						}
					};
					if ($(this).hasClass('number')){
						if (parseInt($(this).val(),10)!=$(this).val()){
							eror = true;
							errorlist.show().append('<span>'+$(this).attr('title')+' is not correct'+'</span>');
							$(this).parent().addClass('error');
						}else $(this).parent().removeClass('error');
					};
				}
			}
			);
			if (eror) {
				// Show all watermarks for missing fields so the required fields can be identified.
				$.Watermark.ShowAll(); 
				// Don't submit the form
				return false;
			}
			// Using traditional form submit to avoid integrating jquery with
			// struts2.  Architecture will need to be updated to handle JSON 
			// responses; mostly for handling action errors and field errors.
			/*else {
				$.ajax({
					url: $(_this).attr('action'),
					type: "POST",
					data: $(_this).serialize(),
					success: function(answer){
						$('#submit-form-results').html(answer);
						$('#submit-form-results').show();
					},
					error: function(){
						alert('Sorry, unexpected error. Please try again later.');
					}
				});
			}
			return false;*/
			return true;
		});
	})
}

$(document).ready(function(){
	initHeight();
	initValidate();
	drop();
	$('div.project-video').gallery({
		duration: 1500,
		effect: 'fade',
		fadeEl: 'ul.top-video-wrapper > li',
		switcher: '.video-thumb a'
	});
	$('.product-gallery').each(function(){
		$(this).mousemove(function(e){
			$('.product-gallery li').removeClass('hover').removeAttr('style');
			$(this).find('img').each(function(){
				if(e.pageX > $(this).offset().left && e.pageX < ($(this).offset().left+$(this).width())){
					if(!$(this).hasClass('hover')) $(this).parents('li:eq(0)').addClass('hover').css({
						position: 'relative',
						zIndex: 100
					});
				}
			});
		});
	});
	$('.product-gallery').mouseleave(function(){
		$('.product-gallery li').removeClass('hover').removeAttr('style');
	});
});
