(function ($) {

/**
 * UserRegister
 * @constructor jQuery
 */
$.widget('ui.UserRegister', $.extend({}, $.ui.Component.prototype, {
    type: 'UserRegister',
    init: function () {
        var self = this;
        
        var regular = /approve=([0-9abcdef]{32})/i;
		var userCode = regular.exec(location.search);
		try{
			if(userCode[1].length == 32){
				self.reload({
					userCode: userCode[1],
					approve: '1'
				});
			}
		}catch(e){}

        $('#changePassword').live('click', function () {
        	self.reload({
        		isChangePassword: 1
        	});
        });
        
        $('#registerButton').live('click', function () {
        	self.reload({
        		isRegister: 1
        	});
        });
        
        $('#loginButton').live('click', function () {
        	self.reload();
        });
        
        $('#logoutButton').live('click', function () {
        	self.reload({
        		isLogOut: 1
        	});
        });
        
        $('#forgetButton').live('click', function () {
        	self.reload({
        		isForget: 1
        	});
        });

		$('a.blockTiltle').live('click', function () {
			$('a.blockTiltle').removeClass('plus');
			$('a.blockTiltle').next().hide('fast');
			$('a.blockTiltle').addClass('plus');
			if ($(this).hasClass('plus') == true) {
				$(this).next().show('fast');
				$(this).removeClass('plus');
			} else {
				$(this).next().hide('fast');
				$(this).addClass('plus');
			}
			return false;
		});
        
        $('#captchaRefresh').live('click', function (e) {
            self.reload({
        		isRegister: 1
        	});
            return false;
        });
        
        $('#registerForm .submitBtn').live('click', function (e) {
            self.reload({
            	email: $('#registerForm input.textInput[name="email"]').val(),
            	captcha: $('#registerForm input.textInput[name="captcha"]').val(),
            	currentPage: location.href,
            	submit: 'submit'
            });
            return false;
        });
        
        $('#loginForm .submitBtn').live('click', function (e) {
            self.reload({
            	email: $('#loginForm input.textInput[name="email"]').val(),
            	password: $('#loginForm input.textInput[name="password"]').val(),
            	submitLogin: 'submitLogin'
            });
            return false;
        });
        
        $('#changePasswordForm .submitBtn').live('click', function (e) {
            self.reload({
            	password: $('#changePasswordForm input.textInput[name="password"]').val(),
		        password2: $('#changePasswordForm input.textInput[name="password2"]').val(),
		        oldpassword: $('#changePasswordForm input.textInput[name="oldpassword"]').val(),
            	submitChangePassword: 'submitChangePassword'
            });
            return false;
        });
        
        $('#approveForm .submitBtn').live('click', function (e) {
            try{
				if(userCode[1] != null){
					self.reload({
		            	password: $('#approveForm input.textInput[name="password"]').val(),
		            	password2: $('#approveForm input.textInput[name="password2"]').val(),
		            	submit: 'submit',
		            	approve: '1',
		            	nickname: $('#approveForm input.textInput[name="nickname"]').val(),
		            	userCode: userCode[1],
		            	rules: $('#approveForm input.checkbox[name="rules"]').attr('checked') ? 1 : 0
	            	});
				}
			}catch(e){}
            return false;
        });
        
        $('#forgetForm .submitBtn').live('click', function (e) {
            self.reload({
            	email: $('#forgetForm input.textInput[name="email"]').val(),
            	submitForget: 'submitForget'
            });
            return false;
        });
		
		$('div.popup_container div.title a.close.user').live('click', function (e) {
			self.reload({
				isCloseWindow: 1
			});
            return false;
		});
		
		$('#loginButtonFromRegister').live('click', function (e) {
            $('a.blockTiltle.plus').trigger("click");
        });

        if (location.host != 'tutatama.com') {
        	//for changing sitelanguage
	        $('#triggerLangua').live('click', function (e) {
	            $.cookie("lang", "ua", { expires: null, path: '/', domain: location.cookiedomain});
	        });
	        $('#triggerLangru').live('click', function (e) {
	            $.cookie("lang", "ru", { expires: null, path: '/', domain: location.cookiedomain});
	        });
        } else {
        	//for changing sitelanguage
	        $('#triggerLangua').live('click', function (e) {
	            $.cookie("LANG", "ua", { expires: null, path: '/', domain: '.' + location.host});
	        });
	        $('#triggerLangru').live('click', function (e) {
	            $.cookie("LANG", "ru", { expires: null, path: '/', domain: '.' + location.host});
	        });
        }

/* from common.js BEGIN */

	/* MOUSE OVER AND PRESSDOWN*/
	
		var ActivePopUp = false;
		$('div.dropdown').live('mousemove',function(){
			ActivePopUp = true;
		});
		$(document).click(function(){
			if (ActivePopUp == true) {
				$('div.dropdown').filter('.activePopUp').hide();
				ActivePopUp = false;
			}
		});

/* from common.js END */

    }
}));

})(jQuery);