
/****************************************************************************************************/
/* BEGIN INCLUDE: #1 - common-site.js */
/****************************************************************************************************/
/*
 * @package     jSite!Framework - Common Site Scripts
 * @author      Tu Masdan E.P. <utu.eka@gmail.com>
 * @copyright   2008 - 2009 © Babon Multimedia
 * @website		http://www.babonmultimedia.com
 * @version     1.0.0
 * @changed		08/23/2009 03:47am
 * @license     -
 * @link        -
*/


;(function(){
		$.fn.extend({
				formRegister: function( actionParams ){
						if( this.size() === 0 ){return false}
						
						var form = this;
						var buttons = $( 'button:button.button', form );
						var formBox = $( '.form-box-min:first', form );
						
						buttons.filter( '.submit:first' ).click(function(){
								form.formAjaxSubmit(function( options ){
										options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
										options.type		= 'post';
										options.timeout		= Number(1000*30);
										options.clearForm	= false;
										options.resetForm	= false;
										
										formBox.maskForm().maskFormLoading();
										buttons.attr( 'disabled', true )
									}, function( result ){
										formBox.maskFormUnloading().unmaskForm();
										buttons.attr( 'disabled', false );
										
										if( (result.success || false) == false ){
											$.showWarn(result.message, function(){
													$( 'input:first', this ).focus()
												}, form)
										} else {
											$.showInfo(result.message, function(){
													this.formReset(function(){
															var redirect = $( this.get(0).redirect ).val() || '/index.php?action=member&task=account';
															window.location.href = redirect
														}, this)
												}, form)
										}
									})
							});
						
						buttons.filter( '.reset:first' ).click(function(){
								form.formReset(function(){
										$( 'input:first', this ).focus()
									}, form )
							});
						
						buttons.filter( '.back:first' ).click(function(){
								form.formReset(function(){
										var redirect = $( this.get(0).referer ).val() || '/index.php';
										window.location.href = redirect
									}, form )
							})
					},
				formLogin: function( actionParams ){
						if( this.size() === 0 ){return false}
						
						var form = this;
						var buttons = $( 'button:button.button', form );
						var formBox = $( '.form-box-min:first', form );
						
						var urlHash = document.location.hash || '',
							onIframe = Boolean(('' != urlHash) && ('iframe_on' == String(urlHash).substr(1)));
												
						buttons.filter( '.submit:first' ).click(function(){
								form.formAjaxSubmit(function( options ){
										options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
										options.type		= 'post';
										options.timeout		= Number(1000*30);
										options.clearForm	= false;
										options.resetForm	= false;
										
										formBox.maskForm().maskFormLoading();
										buttons.attr( 'disabled', true )
									}, function( result ){
										formBox.maskFormUnloading().unmaskForm();
										buttons.attr( 'disabled', false );
										
										if( (result.success || false) == false ){
											if( true == onIframe ){
												alert( "WARNING:\n\t" + result.message );
												$( 'input:first', form ).focus();
											} else {
												$.showWarn(result.message, function(){
														$( 'input:first', this ).focus()
													}, form)
											}
										} else {
											if( true == onIframe ){
												$( 'form#form-login-redirect' ).submit()
											} else {
												window.location.reload()
											}
										}
									})
							});
						
						buttons.filter( '.reset:first' ).click(function(){
								form.formReset(function(){
										$( 'input:first', this ).focus()
									}, form )
							})
					},
				formGetPassword: function( actionParams ){
						if( this.size() === 0 ){return false}
						
						var form = this;
						var buttons = $( 'button:button.button', form );
						var mask = $( '.form-box-min:first', form );
						
						buttons.filter( '.submit:first' ).click(function(){
								form.formAjaxSubmit(function( options ){
										options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
										options.type		= 'post';
										options.timeout		= Number(1000*30);
										options.clearForm	= false;
										options.resetForm	= false;
										
										mask.maskForm().maskFormLoading();
										buttons.attr( 'disabled', true )
									}, function( result ){
										mask.maskFormUnloading().unmaskForm();
										buttons.attr( 'disabled', false );
										
										if( (result.success || false) == false ){
											$.showWarn(result.message, function(){
													$( 'input:first', this ).focus()
												}, form)
										} else {
											$.showInfo(result.message, function(){
													this.formClear(function(){
															$( 'input:first', this ).focus()
														}, this)
												}, form)
										}
									})
							});
						
						buttons.filter( '.reset:first' ).click(function(){
								form.formReset(function(){
										$( 'input:first', this ).focus()
									}, form )
							})
					},
				formAccount: function( actionParams ){
						if( this.size() === 0 ){return false}
						
						var form = this;
						var buttons = $( 'button:button.button', form );
						var formBox = $( '.form-box-min:first', form );
						
						buttons.filter( '.submit:first' ).click(function(){
								form.formAjaxSubmit(function( options ){
										options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
										options.type		= 'post';
										options.timeout		= Number(1000*30);
										options.clearForm	= false;
										options.resetForm	= false;
										
										formBox.maskForm().maskFormLoading();
										buttons.attr( 'disabled', true )
									}, function( result ){
										formBox.maskFormUnloading().unmaskForm();
										buttons.attr( 'disabled', false );
										
										if( (result.success || false) == false ){
											$.showWarn(result.message, function(){
													$( 'input:first', this ).focus()
												}, form)
										} else {
											$.showInfo(result.message, function(){
													$( 'input:first', this ).focus()
												}, form)
										}
									})
							});
						
						buttons.filter( '.reset:first' ).click(function(){
								form.formReset(function(){
										$( 'input:first', this ).focus()
									}, form )
							})
					},
				memberLogout: function( beforeFn, afterFn ){
						if( this.size() === 0 ){return false}
						
						$.showConfirm('Are you sure you want to logout now?', function( beforeFn, afterFn ){
								var self = this, 
									ajax = $.post;
								
								var action = '/index.php',
									params = {action: 'member', doctype: 'ajax', output: 'json', task: 'logout'};
								
								if( $.isFunction(beforeFn) ){
									if((beforeFn.apply(this, [action, params, ajax]) || false) == false ) return false
								}
								
								ajax.apply( $, [
										'/index.php',
										params,
										function( response ){
											var result = ( eval( "(" + response + ")" ) || {success: false, msg: {error: 'There is problem getting result.'}});
											if( $.isFunction(afterFn) ){afterFn.apply(self, [result, action, params, ajax])}
										}
									])
							}, this, [beforeFn, afterFn])
						
						return this
					},
				memberMenu: function(){
						if( this.size() === 0 ){return false}
						
						this.click(function(){
								var o = $( this ),
									rel = o.attr( 'rel' );
								
								switch(String( rel ).toLowerCase()){
									case 'logout':
										var loader = $( '<strong>Logging out...</strong>' );
										
										o.memberLogout(function(action, params, ajax){
												this.hide();
												this.parent().append( loader );
												
												return true
											}, function(result, action, params, ajax){
												loader.remove();
												this.show();
												
												if( (result.success || false) == false ){
													$.showWarn( result.message )
												} else {
													window.location.href = '/index.php'
												}
											});
									break;
									
									default:
										o.hide();
										o.parent().append( 'Loading...' );
										
										window.location.href = rel;
									break;
								}
							});
						
						return false
					},
				formPersonal: function(){
						if( this.size() === 0 ){return false}
							
						var rowCheckedAll = function(){
									$( 'td input:checkbox.chk-personal-id' ).attr( 'checked', true ) 
								},
							rowUncheckedAll = function(){
									$( 'td input:checkbox.chk-personal-id' ).attr( 'checked', false )
								},
							rowChecked = function( n ){
									rowUncheckedAll();
									$( 'td input:checkbox.chk-personal-id.r' + n + ':first' ).attr( 'checked', true )
								},
							rowGetChecked = function(){
									var o = $( 'td input:checkbox.chk-personal-id' ).filter(function(){
													return (true === $( this ).attr( 'checked' ))
												});
									
									return Number( o.size() )
								};
								
						var postRowDelete = function(){
								$.showConfirm('Are you sure you want to delete the selected personal?', function( actionParams ){
										var formBox = $( '.form-box:first', this );
										
										this.formAjaxSubmit(function( options ){
												options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
												options.type		= 'post';
												options.timeout		= Number(1000*30);
												options.clearForm	= false;
												options.resetForm	= false;
												
												formBox.maskForm().maskFormLoading();
											}, function( result ){
												formBox.maskFormUnloading().unmaskForm();
												
												if( (result.success || false) == false ){
													$.showWarn(result.message, function(){
															$( 'input:first', this ).focus()
														}, this)
												} else {
													$.showInfo(result.message, function(){
															window.location.href = '/index.php?action=member&task=personal'
														}, this)
												}
											})
									}, form, ['action=member&task=drop.personal&output=json&doctype=ajax']);
							};
						
						var form = this,
							barButtons = $( '#personal-toolbar .buttons a' );
							tableButtons = $( '.personal-listview-bar.buttons a' );
						
						barButtons.click(function(){
								var rel = $( this ).attr( 'rel' );
								
								switch( String( rel ).toLowerCase() ){
									case 'add':
										form.submit(function(){
												$( this.option ).val( 'add' );
												return true
											}).submit();
									break;
									
									case 'edit':
										form.submit(function(){
												if( 0 === rowGetChecked() ){
													$.showWarn( 'Please make a selection from the list!' )
													return false
												}
												
												$( this.option ).val( 'edit' );
												return true
											}).submit();
									break;
									
									case 'drop':
										if( 0 === rowGetChecked() ){
											$.showWarn( 'Please make a selection from the list!' )
										} else {
											postRowDelete()
										}
									break;
									
									case 'store':
										$.showConfirm('Are you sure you want to save form data now?', function( actionParams ){
												var formBox = $( '.form-box:first', this );
												
												this.formAjaxSubmit(function( options ){
														options.url			= '/index.php' + String(((actionParams || null) == null) ? '' : String('?' + actionParams));
														options.type		= 'post';
														options.timeout		= Number(1000*30);
														options.clearForm	= false;
														options.resetForm	= false;
														
														formBox.maskForm().maskFormLoading();
													}, function( result ){
														formBox.maskFormUnloading().unmaskForm();
														
														if( (result.success || false) == false ){
															$.showWarn(result.message, function(){
																	$( 'input:first', this ).focus()
																}, this)
														} else {
															$.showInfo(result.message, function(){
																	window.location.href = '/index.php?action=member&task=personal'
																}, this)
														}
													})
											}, form, ['action=member&task=store.personal&output=json&doctype=ajax']);
									break;
									
									case 'reset':
										$.showConfirm('Are you sure you want to reset this form?', function(){
												this.formReset(function(){
														$( 'input:first', this ).focus()
													}, this )
											}, form );
									break;
									
									case 'cancel':
										$.showConfirm('Are you sure you want to close this form?', function(){
												this.submit(function(){
														$( this.option ).val( '' );
														return true
													}).submit()
											}, form );
									break;
								}
								
								return false
							});
						
						tableButtons.click(function(){
								var rel = $( this ).attr( 'rel' );
								
								switch( String( rel ).toLowerCase() ){
									case 'checkall':
										rowCheckedAll();
									break;
									
									case 'uncheckall':
										rowUncheckedAll();
									break;
								}
							});
						
						$( 'td a.min-button-drop' ).click(function(){
								var rel = $( this ).attr( 'rel' );
								rowChecked( rel );
								postRowDelete()
							});
							
						return this
					},
				setFormOrderPersonal: function( key ){
					},
				getFormOrderPersonal: function( key ){
					}
			});
	})( jQuery );

$(function(){
		$( '.content-msg:first' ).fadeIn( 'slow', function(){
				var o = $( this );
				o.addClass( 'block' );
				
				setTimeout(function(){
						o.fadeOut( 'slow', function(){
								o.remove()
							})
					}, (1000 * 15))
			});
		
		$( 'input:checkbox, input:radio' ).css({margin: 0, border: 'none'});
		$( 'input:text, input:password, textarea, select' ).addClass( 'text' );
		$( 'input:text, input:password, textarea' ).focus(function(){
				if( $.trim($(this).val()) !== '' ) this.select()
			});
		
		$( 'label.required, label.mandatory' ).append( '<span class="asterisk">*</span>' );
		
		$( 'form#form-register:first' ).formRegister( 'action=member&task=register&output=json&doctype=ajax' );
		$( 'form#form-login:first' ).formLogin( 'action=member&task=login&output=json&doctype=ajax' );
		$( 'form#form-getpassword:first' ).formGetPassword( 'action=member&task=getpassword&output=json&doctype=ajax' );
		$( 'form#form-account:first' ).formAccount( 'action=member&task=updateaccount&output=json&doctype=ajax' );
		
		$( 'form#form-personal:first' ).formPersonal();
		
		$( 'a.member-menu' ).memberMenu();
	});

$(function(){
		var urlHash = document.location.hash || '',
			onIframe = Boolean(('' != urlHash) && ('iframe_on' == String(urlHash).substr(1)));
			
		if( true == onIframe ){
			var isIE = Boolean('Microsoft Internet Explorer' == String(navigator.appName));
			if( true != isIE ){
				setInterval(function(){
						var h = Number( $( 'html, body' ).height() );
						parent.window.location.hash = h;
					}, 500)
			}
		}
	});
/****************************************************************************************************/
/* END INCLUDE: #1 - common-site.js */
/****************************************************************************************************/
