/**
 * 
 * Enter description here ...
 * 
 * Last changed: $LastChangedDate: 2011-06-11 16:05:34 +0800 (Sat, 11 Jun 2011) $
 * @author $Author: charles $
 * @version $Revision: 77 $
 *
 */

;(function($) {
	
	$.AllClass = function() {
		this.construct();
	};
	
	$.extend($.AllClass.prototype, {
		construct: function() {
			this.fixscroll();
			this.addEvents();
			this.no_stupid_browser();
		},
		
		addEvents: function() {
			$('input[name="username"]').focus(function() {
				if (this.value == 'Email') {
					this.value = '';
				}
			});
			
			$('input[name="username"]').blur(function() {
				if (this.value == '') {
					this.value = 'Email';
				}
			});
			
			$('input[name="password"]').focus(function() {
				if (this.value == 'Password') {
					this.value = '';
				}
			});
			
			$('input[name="password"]').blur(function() {
				if (this.value == '') {
					this.value = 'Password';
				}
			});
			
			$('form[name="register-form"]').submit(function() {
				if ( $.trim($('form[name="register-form"] input[name="username"]').val()) == 'Email' ) {
					return false;
				}
				
				if ( $.trim($('form[name="register-form"] input[name="username"]').val()) == '' ) {
					return false;
				}
				
				var params = {
						username: encodeURIComponent( $.trim($('form[name="register-form"] input[name="username"]').val()))
				};
				
				$('#pbar-register').show();
				
				$.post('index.php?route=common/register', params, function(resp) {
					if (resp.err) {
						$('#dialog-header').html('');
						$('#dialog-detail').html('');
						
						$('#dialog-header').append(resp.msg);
						
						for (var i in resp.errmsg) {
							$('#dialog-detail').append('<li>'+ resp.errmsg[i] +'</li>');
						}
						
						$.All.mask();
					}
					else {
						$('#dialog-header').html('');
						$('#dialog-detail').html('');
						
						$('#dialog-header').append(resp.msg);
						
						for (var i in resp.errmsg) {
							$('#dialog-detail').append('<li>'+ resp.errmsg[i] +'</li>');
						}
						
						$.All.mask();
					}
					
					$('#pbar-register').hide();
					
				}, 'json' );
				
				return false;
			});
			
			$('form[name="login-form"]').submit(function() {
				if ( $.trim($('form[name="login-form"] input[name="username"]').val()) == 'Email' ) {
					return false;
				}
				
				if ( $.trim($('form[name="login-form"] input[name="username"]').val()) == '' ) {
					return false;
				}
				
				var params = {
						username: encodeURIComponent( $.trim($('form[name="login-form"] input[name="username"]').val())),
						password: encodeURIComponent( $.trim($('form[name="login-form"] input[name="password"]').val()))
				};
				
				$('#pbar-login').show();
				
				$.post('index.php?route=common/login', params, function(resp) {
					if (resp.err) {
						$('#dialog-header').html('');
						$('#dialog-detail').html('');
						
						$('#dialog-header').append(resp.msg);
						
						for (var i in resp.errmsg) {
							$('#dialog-detail').append('<li>'+ resp.errmsg[i] +'</li>');
						}
						
						$.All.mask();
					}
					else {
						if (resp.redirect) {
							location.href = resp.redirect;
						}
						else {
							location.href = location.href;
						}
					}
					
					$('#pbar-login').hide();
					
				}, 'json' );
				
				return false;
			});
			
			$('form[name="login-form"]').submit(function() {
				if ( $.trim($('form[name="login-form"] input[name="username"]').val()) == 'Email' ) {
					return false;
				}
				
				if ( $.trim($('form[name="login-form"] input[name="username"]').val()) == '' ) {
					return false;
				}
				
				if ( $.trim($('form[name="login-form"] input[name="password"]').val()) == 'Password' ) {
					return false;
				}
				
				if ( $.trim($('form[name="login-form"] input[name="password"]').val()) == '' ) {
					return false;
				}
				return true;
			});
			
			$('table tbody tr.alt').hover(
					function() { $(this).css({'background-color' : '#e1fbcd'}); },
					function() { $(this).css({'background-color' : '#ffffff'}); }
			);
			
			$('#nav ul:first li:last').css({'background':'transparent url(website/view/themes/default/images/new.gif) no-repeat scroll 0 0'});
		},
		
		mask: function() {
			//Get the screen height and width
	        var maskHeight = $(document).height();
	        var maskWidth = $(window).width();
	     
	        //Set height and width to mask to fill up the whole screen
	        $('#mask').css({'width':maskWidth,'height':maskHeight});
	        $('#mask').css('top',  0);
	        $('#mask').css('left', 0);
	        
	        //transition effect     
	        $('#mask').fadeTo(0, 0.8);
	     
	        //Get the window height and width
	        var winH = $(window).height();
	        var winW = $(window).width();
	               
	        //Set the popup window to center
	        $('#dialog').css('top',  winH/2-$('#dialog').height()/2);
	        $('#dialog').css('left', winW/2-$('#dialog').width()/2);
	     
	        //transition effect
	        $('#dialog').fadeIn(0);
	        
	        //if close button is clicked
	        $('.window .close').click(function (e) {
	            //Cancel the link behavior
	            e.preventDefault();
	            $('#mask, .window').hide();
	        });     
	         
	        //if mask is clicked
	        //$('#mask').click(function () {
	        //    $(this).hide();
	        //    $('.window').hide();
	        //});
		},
		
		fixscroll: function() {
            scrollElement = 'html, body';
            $('html, body').each(function () {
                var initScrollTop = $(this).attr('scrollTop');
                $(this).attr('scrollTop', initScrollTop + 1);
                if ($(this).attr('scrollTop') == initScrollTop + 1) {
                    scrollElement = this.nodeName.toLowerCase();
                    $(this).attr('scrollTop', initScrollTop);
                    return false;
                }
            });
        },
        
        scroll: function(el) {
		    var targetOffset = $(el).offset().top;
		    $(scrollElement).animate({scrollTop: targetOffset}, 1500, 'swing');
		},
		
		no_stupid_browser: function() {
			
			if ($.browser.msie && parseFloat($.browser.version) <= 7) {
				$(document.body).html( '<div style="width: 600px; margin: 0 auto;" align="center"><p>You are using a very old MS Internet Explorer. Either you upgrade to the latest version or use other reliable browser such as <a href="http://www.google.com/chrome">Google Chrome</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, <a href="http://www.opera.com/browser/download/">Opera</a> or <a href="http://www.mozilla.com">Firefox</a>. Internet Explorer is known to be unsafe browser.</p></div>' );
			}
		}
	});
	
	$(document).ready(function() {
		if (typeof $.All == 'undefined') {
			$.All = new $.AllClass();
		}
	});
	
})(jQuery);

