$(document).ready(function(){
	
	// main form validation ****************************************************
	var _userID;
	var _mainForm = $('#form');
	if (_mainForm.length) {
		var _name = $('#name',_mainForm),
			_teamLead = $('#team-lead',_mainForm),
			_email = $('#email',_mainForm),
			_confirmEmail = $('#confirm-email',_mainForm),
			_phoneNumber = $('#phone-number',_mainForm),
			_proposalDocument = $('#proposal-document', _mainForm),
			_checkboxs = $('#checkbox1, #checkbox5, #checkbox6', _mainForm),
			_labels = $('label', _mainForm),
			_error = $('p.error'),
			_submitBt = $('a.btn-submit', _mainForm);
			
		var _submitForm = true,
			_emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			
		_error.hide();
			
		_submitBt.click(function(){
			_mainForm.submit();
			return false;
		})
	
		_mainForm.submit(function(){
			_labels.removeClass('error');
			_submitForm = true;
			
			$([_name, _teamLead, _phoneNumber, _proposalDocument]).each(function(i,obj){
				if ($(this).val() == '') _labels.filter('[for="'+$(obj).attr('id')+'"]').addClass('error');
			});
			
			if (!_emailReg.test(_email.val())) {
				_labels.filter('[for="email"]').addClass('error');
				_labels.filter('[for="confirm-email"]').addClass('error');
			}
			if (_email.val() != _confirmEmail.val()) _labels.filter('[for="confirm-email"]').addClass('error');
			
			_checkboxs.each(function(){
				if (!this.checked) _labels.filter('[for="'+$(this).attr('id')+'"]').addClass('error');
			});
			if (_mainForm.find('label.error').length) {
				_submitForm = false;
				_error.show();
				window.location = window.location.toString() + _submitBt.attr('href');
			}
			return _submitForm;
		});
	}
	
	// jQueryBox ***************************************************************
	$('a.ad, a.btn-more-info, a.btn-done').jQueryBox({
		boxDesignPadding:'0',
		closeBoxByFader:false,
		boxMarkup:'<div class="jqb-lightbox"><div id="jqb-insert"></div></div>',
		boxBtnClose:'a.btn-close, a.btn-exit-voting, a.btn-go-back',
		overlayColor:'#000',
		overlayOpacity:0.65,
		boxAnimate:0,
		boxScrollDuration:0,
		overlayFadeDuration:200,
		boxContentFadeDuration: 300,
		callbackShow: function(_lb){
		var _btnLockInMyVotes = $('a.btn-lock-in-my-votes'),
			_btnReturn2 = $('a.btn-return2, a.btn-return'),
			_btnSubmit = $('a.btn-submit'),
			_btnGetEmailUpdates = $('a.btn-get-email-updates'),
			_btnClose = $('a.btn-close');
			
		if (_lb.find('form.register-form')) {
			validation();
		}
		if (_btnClose.length && _btnClose.parents('#keep_me_update').length) {
			_btnClose.click(function(){
				$.jQueryBox.close();
			});
		}
		if (_btnSubmit.length) {
			$('label[for*=email]').removeAttr('style');
			_btnSubmit.unbind('click').bind('click',function(){
				var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				var _this = this;
				if (!_emailReg.test($('#jqb-insert #email2').val())) {
					$('label[for*=email]').css('color','#ff0000');
				} else {
					$.ajax({
						url:$(_this).attr('rel'),
						data:'email='+$('#jqb-insert #email2').val(),
						success:function(){
							$.jQueryBox.boxShow(_this.href);
						}
					});
				}
				return false;
			});
		}
		if (_btnGetEmailUpdates.length) {
			_btnGetEmailUpdates.click(function(){
				$.jQueryBox.boxShow(this.href);
				return false;
			});
		}
		if (_btnLockInMyVotes.length) {
			_btnLockInMyVotes.click(function(){
				var _this = this;
				$.jQueryBox.boxShow(this.href);
				setTimeout(function(){
					_btnStartOver.hide();
					_btnDone.hide();
					_textHolder.hide();
					_textLine.hide();
					_voteCount = 0;
				},$.jQueryBox.options.duration);
				$('span.place1').each(function(){
					_idEl += $(this).prev('a').attr('rel')+', ';
					$(this).remove();
				});
				$('span.place2').each(function(){
					_idEl += $(this).prev('a').attr('rel')+', ';
					$(this).remove();
				});
				$('span.place3').each(function(){
					_idEl += $(this).prev('a').attr('rel')+', ';
					$(this).remove();
				});
				_idEl = _idEl.substr(0,_idEl.length-2);
				$.ajax({
					url:$(_this).attr('rel'),
					data:'projects='+_idEl+',userID='+_userID
				});
				return false;
			});
		}
		/*if (_btnReturn2.length) {
			_btnReturn2.click(function(){
				firstLightbox();
				return false;
			});
		}*/
		initCustomForms(_lb);
		return true;
	}
	});
	
	if ($('a.on-start').length > 0) $.jQueryBox.boxShow($('a.on-start').attr('href'));
	var _registerBtn = $('a.register'),
		_btnVoteNow = $('a.btn-vote-now');
		
	var _idEl = '';
	$('#second-popup').hide();
		

	$.jQueryBox.options.callbackBeforeClose = function(_lb){
		return true;
	};
		
	_btnVoteNow.click(function(){
		if (_registerBtn.length) {
			$.jQueryBox.boxShow(_registerBtn.attr('href'));
			return false;
		}
	});
	
	var _voteItem = $('ul.gallery div.img-holder'),
		_btnStartOver = $('a.btn-start-over'),
		_btnDone = $('a.btn-done'),
		_textHolder = $('div.heading-alt div.txt'),
		_textLine = $('li', _textHolder);
		
	
	
	var _voteCount = 0;	
	function voteClick(){
		
		var _voteNum = ['<span class="place1">1</span>', '<span class="place2">2</span>', '<span class="place3">3</span>'];
			
		_btnStartOver.hide();
		_btnDone.hide();
		_textHolder.hide();
		_textLine.hide();
			
		_btnStartOver.click(function(){
			$('span.place1,span.place2,span.place3').remove();
			_voteCount = 0;
			_btnStartOver.hide();
			_textLine.hide();
			_textLine.eq(0).show();
			_textHolder.hide().fadeIn(250);
			_btnDone.hide();
			return false;
		});
			
		_voteItem.each(function(i,_vI){
			var _link = $('a',_vI);
			_link.click(function(){
				if (_voteCount > 2) return false;
				$(_vI).append(_voteNum[_voteCount]);
				if (_voteCount == 2) _btnDone.show();
				_voteCount++;
				_textHolder.hide();
				_textLine.hide();
				_textLine.eq(_voteCount).show();
				_textHolder.fadeIn(250);
				if (_voteCount == 1) _btnStartOver.fadeIn(250);
				return false;
			});
		});
		
	}
	
	voteClick();
	
	// form validation *********************************************************
	function validation() {
		var _mainForm = $('form.register-form');
		if (_mainForm.length) {
			var _name = $('#first-name2',_mainForm),
				_lastName = $('#last-name',_mainForm),
				_email = $('#email2',_mainForm),
				_message1 = $('#message1',_mainForm),
				_checkboxs = $('#checkbox2', _mainForm),
				_labels = $('label', _mainForm),
				_submitBt = $('a.btn-submit2');
				
			var _submitForm = true,
				_emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				
			_submitBt.click(function(){
				_mainForm.submit();
				return false;
			})
		
			_mainForm.submit(function(){
				_labels.css('color','#ffffff').removeClass('errors');
				_submitForm = true;

				$([_name, _lastName,_message1]).each(function(i,obj){
					if ($(this).val() == '') _labels.filter('[for="'+$(obj).attr('id')+'"]').css('color','red').addClass('errors');
				});
				
				if (!_emailReg.test(_email.val())) {
					_labels.filter('[for="email2"]').css('color','red');
				}
				_checkboxs.each(function(){
					if (!this.checked) _labels.filter('[for="'+$(this).attr('id')+'"]').css('color','red').addClass('errors');
				});
				if (_mainForm.find('label.errors').length) {
					_submitForm = false;
				} else
				if ($('a.on-start').length > 0) {
					$.ajax({
						type:'POST',
						url:_mainForm.attr('action'),
						data:_mainForm.serialize(),
						success:function(msg){
							_userID = msg;
							var _firstLightbox = $('#first');
							firstLightbox();
							function firstLightbox(){
								if (_firstLightbox.length) {
									$.jQueryBox.boxShow('#first');
									$('a.btn-close, a.btn-start-voiting-now', '#jqb-insert').bind('click',function(){
										$.jQueryBox.close();
										$(this).unbind('click');
										$.jQueryBox.options.callbackClose = function(){
											_textLine.eq(0).show();
											_textHolder.fadeIn(250);
											$.jQueryBox.options.callbackClose = function(){return false};
										}
										return false;
									});
								}
							}
						},
						error:function(){
							alert('ajax error');
						}
					});
					_submitForm = false;
				}
				return _submitForm;
			});
		}
	}
});

