
/****************************************************************************************************/
/* BEGIN INCLUDE: #1 - jquery.system.pack.js */
/****************************************************************************************************/
/*
 * @package     jSite!Framework - System 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($){
		$.extend({
				getScript: function(url, callback) {
						var head = document.getElementsByTagName("head")[0];
						var script = document.createElement("script");
						script.src = url;
						
						// Handle Script loading
						{
							var done = false;
							
							// Attach handlers for all browsers
							script.onload = script.onreadystatechange = function(){
								if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) {
									done = true;
									if (callback) callback();
								
									// Handle memory leak in IE
									script.onload = script.onreadystatechange = null;
								}
							}
						}
						
						head.appendChild( script );
						
						// We handle everything using the script element injection
						return undefined;
					}
			});
		
		/*
		Main Dialog Handler
		-----------------------------------------------------------------------*/		
		$.fn.extend({
				openDialogMsg: function( answers, callback ){
						if( this.size() === 0 ){return false}
						
						var container = this;
						
						if( !$.isFunction($.fn.boxy) ){
							$.getCss( '/javascripts/jquery/resources/css/jquery.boxy.css' );
							$.getScript( '/javascripts/jquery/plugins/jquery.boxy.pack.js', function(){
									container.openDialogMsg( answers, callback )
								});
							
							return container
						}
						
						var settings		= {modal: true, closeable: false, show: true, clickToFront: true,unloadOnHide: true},
							map				= {}, 
							answerStrings	= [];
						
						if( answers instanceof Array ){
							for( var i = 0; i < answers.length; i++ ){
								map[answers[i]] = answers[i];
								answerStrings.push( answers[i] )
							}
						} else {
							for( var k in answers ){
								map[answers[k]] = k;
								answerStrings.push( answers[k] )
							}
						}
						
						var buttons = $( '<form id="form-msg" name="form_msg" action="" method="post"></form>' );
						buttons.html(
								$.map(answerStrings, function(v){
										return '<button type="button" id="button-msg' + v + '" name="button_msg' + v + '" title="' + v + '" value="' + v + '">' + v + '</button>';
									}).join('&nbsp;&nbsp;&nbsp;')
							);
						
						var elButtons = $( 'button:button', buttons );
						elButtons.click(function(){
								var val = $( this ).val();
								
								Boxy.get( this ).hide(function(){
									if( typeof callback === 'function' ){callback.apply( $, [val] )}
								})
							});
						
						var autoClose = setTimeout(function(){
								elButtons.filter( ':last' ).click()
							}, Number(1000 * 10));
						
						$( document.body ).bind( 'keypress.jmsg', function(evt) {
								var key = evt.which || evt.keyCode;
								if( key == 13 ){
									$( document.body ).unbind( 'keypress.jmsg' );
									clearTimeout( autoClose );
									
									elButtons.filter( ':last' ).click()
								}
							});
						
						var question = container.children( '.jmsg-body:first' );
						
						container.children( '.jmsg-foot:first' ).append( buttons );
						container.width( Math.floor(Number($( 'html, body' ).innerWidth()*(40/100))) );
						
						new Boxy( container, settings );						
						return container
					}
			});
		
		/*
		Global Handler
		-----------------------------------------------------------------------*/
		$.extend( $, {
				urlEncode: function(o){
						if( !o ){return ""}
						
						var buf = [];
						
						for(var key in o){
							var ov = o[key], k = encodeURIComponent(key);
							var type = typeof ov;
							
							if(type == 'undefined'){
								buf.push(k, "=&")
							}else if(type != "function" && type != "object"){
								buf.push(k, "=", encodeURIComponent(ov), "&")
							}else if(ov instanceof Array){
								if (ov.length) {
									for(var i = 0, len = ov.length; i < len; i++) {
										buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&")
									}
								} else {
									buf.push(k, "=&")
								}
							}
						}
						
						buf.pop();
						return buf.join("")
					},
				urlDecode: function(string, overwrite){
						if( !string || !string.length ){return {}}
						
						var obj = {};
						var pairs = string.split('&');
						var pair, name, value;
						
						for(var i = 0, len = pairs.length; i < len; i++){
							pair = pairs[i].split('=');
							name = decodeURIComponent(pair[0]);
							value = decodeURIComponent(pair[1]);
							
							if(overwrite !== true){
								if(typeof obj[name] == "undefined"){
									obj[name] = value
								}else if(typeof obj[name] == "string"){
									obj[name] = [obj[name]];
									obj[name].push(value)
								}else{
									obj[name].push(value)
								}
							}else{
								obj[name] = value
							}
						}
						
						return obj
					},
				format: function(source, params) {
						if ( arguments.length == 1 ) 
							return function() {
								var args = $.makeArray(arguments);
								args.unshift(source);
								
								return $.format.apply( this, args )
							};
						
						if ( arguments.length > 2 && params.constructor != Array  ) {
							params = $.makeArray(arguments).slice(1)
						}
						
						if ( params.constructor != Array ) {
							params = [ params ]
						}
						
						$.each(params, function(i, n) {
								source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
							});
						
						return source
					},
				waitCursor: function(){
						document.body.style.cursor = 'wait'
					},
				hideSelectBoxs: function( scope ){
						$( 'select' ).hide()
					},			
				showSelectBoxs: function(){
						$( 'select' ).show()
					},
				defaultCursor: function(){
						document.body.style.cursor = 'default'
					},
				callback: function(fn, scope, params, delay){
						if( typeof fn !== 'function' ){return false}
						
						if((delay || null) !== null){fn.defer(delay, (scope || $), (params || []))}
						else{fn.apply((scope || $), (args || []))}
					},
				showMsg: function( message, callback ){
						Boxy.alert( message, callback );
						setTimeout(function(){$.jmsgHide()}, 50000)
					},
				showMessage: function(message, callback){$.showMsg(message, callback)},
				hideMsg: function(){Boxy.hide()},
				hideMessage: function(){$.hideMsg()},
				MSGBOX_TITLE: 'System Alert',
				setMsgTitle: function( text, defaultText ){
						return $.MSGBOX_TITLE = (text || (defaultText || $.MSGBOX_TITLE))
					},
				getMsgTitle: function( text ){
						return $.MSGBOX_TITLE
					},
				showInfo: function( msg, callback, scope, params, msgTTL, msgOK ){
						var oBox = $( '<div class="jmsg"></div>' ),
							oTtl = $( '<h4 class="jmsg-head info"></h4>' ),
							oMsg = $( '<blockquote class="jmsg-body info"></blockquote>' );
							oBtn = $( '<blockquote class="jmsg-foot"></blockquote>' );
							
						oBox.append( oTtl ).append( oMsg ).append( oBtn );
						oTtl.html($.setMsgTitle( msgTTL, 'System Information' ));
						oMsg.html( msg || 'Unknown information' );
						
						msgOK = msgOK || 'OK';
						
						oBox.openDialogMsg([msgOK], function( msgReturn ){
								if( msgOK !== msgReturn ){return false}
								if( typeof callback === 'function' ){callback.apply((scope || $), (params || []))}
							});
						
						return true
					},
				showWarning: function( msg, callback, scope, params, msgTTL, msgOK ){
						var oBox = $( '<div class="jmsg"></div>' ),
							oTtl = $( '<h4 class="jmsg-head warn"></h4>' ),
							oMsg = $( '<blockquote class="jmsg-body warn"></blockquote>' );
							oBtn = $( '<blockquote class="jmsg-foot"></blockquote>' );
							
						oBox.append( oTtl ).append( oMsg ).append( oBtn );
						oTtl.html($.setMsgTitle( msgTTL, 'System Warning' ));
						oMsg.html( msg || 'Unknown warning' );
						
						msgOK = msgOK || 'OK';
						
						oBox.openDialogMsg([msgOK], function( msgReturn ){
								if( msgOK !== msgReturn ){return false}
								if( typeof callback === 'function' ){callback.apply((scope || $), (params || []))}
							});
						
						return false
					},
				showWarn: function( msg, callback, scope, params, msgTTL, msgOK ){return $.showWarning( msg, callback, scope, params, msgTTL, msgOK )},
				showError: function( msg, callback, scope, params, msgTTL, msgOK ){
						var oBox = $( '<div class="jmsg"></div>' ),
							oTtl = $( '<h4 class="jmsg-head error"></h4>' ),
							oMsg = $( '<blockquote class="jmsg-body error"></blockquote>' );
							oBtn = $( '<blockquote class="jmsg-foot"></blockquote>' );
							
						oBox.append( oTtl ).append( oMsg ).append( oBtn );
						oTtl.html($.setMsgTitle( msgTTL, 'System Error' ));
						oMsg.html( msg || 'Unknown error' );
						
						msgOK = msgOK || 'OK';
						
						oBox.openDialogMsg([msgOK], function( msgReturn ){
								if( msgOK !== msgReturn ){return false}
								if( typeof callback === 'function' ){callback.apply((scope || $), (params || []))}
							});
						
						return false
					},
				showErr: function( msg, callback, scope, params, msgTTL, msgOK ){$.showError( msg, callback, scope, params, msgTTL, msgOK )},
				showConfirmation: function( msg, callback, scope, params, msgTTL, msgYES, msgNO ){
						var oBox = $( '<div class="jmsg"></div>' ),
							oTtl = $( '<h4 class="jmsg-head confirm"></h4>' ),
							oMsg = $( '<blockquote class="jmsg-body confirm"></blockquote>' );
							oBtn = $( '<blockquote class="jmsg-foot"></blockquote>' );
							
						oBox.append( oTtl ).append( oMsg ).append( oBtn );
						oTtl.html($.setMsgTitle( msgTTL, 'System Confirmation' ));
						oMsg.html( msg || 'Unknown confirmation' );
						
						msgYES = msgYES || 'YES';
						msgNO = msgNO || 'NO';
						
						oBox.openDialogMsg([msgYES, msgNO], function( msgReturn ){
								if( msgYES !== msgReturn ){return false}
								if( typeof callback === 'function' ){callback.apply((scope || $), (params || []))}
							});
						
						return false
					},
				showConfirm: function( msg, callback, scope, params, msgTTL, msgYES, msgNO ){return $.showConfirmation( msg, callback, scope, params, msgTTL, msgYES, msgNO )},
				addStyleSheet: function( url, media ){
						var $url = url || '',
							$media = media || 'all';
						
						if( $url=='' ) return false;
						
						var $link = '<link rel="stylesheet" href="' + $url + '" type="text/css" media="'+ $media +'" />';
						$( 'head' ).append( $link )
					},
				getCss: function( href, media ){$.addStyleSheet( href, media );$.preloadCssImages()},
				addIcon: function( url ){
						var $url = url || '';
						
						if( $url=='' ) return false;
						
						var $link = '<link rel="shortcut icon" href="' + $url + '" />';
						if( !$.browser.msie ){
							$( 'head' ).append( $link );
						}
					},
				addScript: function( url, type, callback ){
						var $url = url || '';
						var $type = type || 'text/javascript';
						
						if( $url=='' ) return false;
						
						var $script = '<script type="' + $type + '" src="' + $url + '" onerror="alert(\'Error loading \' + this.src)"></script>';
						$( 'head' ).append( $script )
					},
				preloadCssImages: function(){
						/*
						var allImgs = [];//new array for all the image urls  
						var k = 0; //iterator for adding images
						var sheets = document.styleSheets;//array of stylesheets
						
						for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
								var cssPile = '';//create large string of all css rules in sheet
								var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
								var baseURLarr = csshref.split('/');//split href at / to make array
								baseURLarr.pop();//remove file path from baseURL array
								var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
								if(baseURL!="") baseURL+='/'; //tack on a / if needed
								if(document.styleSheets[i].cssRules){//w3
										var thisSheetRules = document.styleSheets[i].cssRules; //w3
										for(var j = 0; j<thisSheetRules.length; j++){
												cssPile+= thisSheetRules[j].cssText;
										}
								}
								else {
										cssPile+= document.styleSheets[i].cssText;
								}
								
								//parse cssPile for image urls and load them into the DOM
								var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
								if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
										var arr = jQuery.makeArray(imgUrls);//create array from regex obj        
										jQuery(arr).each(function(){
												allImgs[k] = new Image(); //new img obj
												allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this;     //set src either absolute or rel to css dir
												k++;
										});
								}
						}//loop
						
						return allImgs;
						*/
					},
				resizeFrame: function( o ){
						var doc = (o.contentDocument) ? o.contentDocument : o.contentWindow.document;
						
						if( $.browser.msie ){
							if( doc.all ){
								var h = doc.body.scrollHeight;
								if( h ){
									$( o )
										.removeClass( 'ajax-progressbar' )
										.css({height: ((h || 400) + 20) + 'px'})
								}
							}
						} else {
							var h = doc.body.scrollHeight;
							if( h ){
								$( o )
									.removeClass( 'ajax-progressbar' )
									.css({height: ((h || 400) + 20) + 'px'})
							}
						}
					},
				popupWindow: function( win_url, win_name, win_w, win_h, win_scroll ) {
						if( (document[win_name] || null) !== null ){
							document[win_name].close();
							delete document[win_name]
						}
						
						var win_w_full = Math.floor( screen.width ),
							win_h_full = Math.floor( screen.height )
							win_w = (win_w || win_w_full), 
							win_h = (win_h || win_h_full),
							win_t = Math.floor( (win_h_full - win_h)/2 ),
							win_l = Math.floor( (win_w_full - win_w)/2 ),
							win_cfg = 'toolbar=no,'
										+ 'location=no,'
										+ 'directories=no,'
										+ 'status=no,'
										+ 'menubar=yes,'
										+ 'scrollbars=yes,'
										+ 'resizable=yes,'
										+ 'copyhistory=no,'
										+ 'width=' + win_w + ','
										+ 'height=' + win_h + ','
										+ 'left=' + win_l + ','
										+ 'top=' + win_t + ','
										+ 'screenX=' + win_l + ','
										+ 'screenY=' + win_t + '';
						
						document[win_name] = popupOpen( win_url, win_name, win_cfg );
						if( !document[win_name] ){
							// alert( "Dialog popup tidak dapat ditampilkan.\nKemungkinan seting popup pada browser Anda sedang diblok!" );
							alert( 'Unable to open popup dialog.  Perhaps you have a popup blocker installed?' );
							return false
						}
						
						if ( parseInt(navigator.appVersion) >= 4 ){ document[win_name].window.focus()}
					},
				popupFullWindow: function( win_url, win_name ) {
						if( (document[win_name] || null) !== null ){
							document[win_name].close();
							delete document[win_name]
						}
						
						var win_w = Math.floor( screen.width ), 
							win_h = Math.floor( screen.height ), 
							win_cfg = 'toolbar=no,'
										+ 'location=no,'
										+ 'directories=no,'
										+ 'status=no,'
										+ 'menubar=yes,'
										+ 'scrollbars=yes,'
										+ 'resizable=yes,'
										+ 'copyhistory=no,'
										+ 'width=' + win_w + ','
										+ 'height=' + win_h + ','
										+ 'left=0,'
										+ 'top=0,'
										+ 'screenX=0,'
										+ 'screenY=0';
						
						document[win_name] = popupOpen( win_url, win_name, win_cfg );
						if( !document[win_name] ){
							// alert( "Dialog popup tidak dapat ditampilkan.\nKemungkinan seting popup pada browser Anda sedang diblok!" );
							alert( 'Unable to open popup dialog.  Perhaps you have a popup blocker installed?' );
							return false
						}
						
						if ( parseInt(navigator.appVersion) >= 4 ){ 
							document[win_name].window.focus()
						}
					}
			});
		
		
		
		$.fn.extend({
				setCenter: function( c ){
						if( this.size() == 0 ){return false}
						
						var o = this,
							p = o.parent();
							
						var oTop = Math.floor(Number(p.innerHeight() - o.innerHeight()) / 2),
							oLeft = Math.floor(Number(p.innerWidth() - o.outerWidth()) / 2);
						
						var c = c || '';
						
						if( c === 'x' ){o.css({left: oLeft})}
						else if( c === 'y' ){o.css({top: oTop})}
						else {o.css({top: oTop, left: oLeft})}
						
						return this
					},
				addRelative: function(){
						if( this.size() === 0 ){return false}
						
						if( !this.hasClass( 'relative' ) ){this.addClass( 'relative' )}
						return this
					},
				removeRelative: function(){
						if( this.size() === 0 ){return false}
						
						if( this.hasClass( 'relative' ) ){this.removeClass( 'relative' )}
						return this
					},
				topPage: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						var o = this;
						
						var animate = false; 
						o.animate({scrollTop: 0}, 500, function(){
								if( animate == true ){return false}
								animate = true;
								
								setTimeout(function(){
										if( typeof callback === 'function' ){callback.apply((scope || o), (params || []))}
									}, 100)
							})
						
						return this
					},
				maskForm: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						var mask = $( '<span class="jmask">&nbsp;</span>' ),
							box = this;
							
						var w = Number(this.innerWidth()),
							h = Number(this.innerHeight());
						
						box.addRelative();
						mask.appendTo( box ).css({
								width: w + 'px', 
								height: h + 'px', 
								opacity: .5, 
								backgroundColor: '#333'
							});
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				unmaskForm: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						var box = this,
							mask = $( 'span.jmask:first', box );
							
						mask.remove();
						box.removeRelative();
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				maskFormLoading: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						var loader = $( '<span class="jmask-loading">&nbsp;</span>' ),
							box = this;
						
						box.addRelative();
						loader.appendTo( box ).setCenter();
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				maskFormUnloading: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						var box = this,
							loader = $( 'span.jmask-loading:first', box );
							
						loader.remove();
						box.removeRelative();
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formSubmit: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						this.submit(function(){
								if( typeof callback === 'function' ){return callback.apply((scope || o), (params || []))}
								else{return true}
							}).submit();
						
						return this
					},
				formAjaxSubmit: function( beforeFn, afterFn ){
						if( this.size() === 0 ){return false}
						var me = this;
						
						if( !$.isFunction($.fn.ajaxSubmit) ){
							$.getScript('/javascripts/jquery/plugins/jquery.form.pack.js', function(){me.jformAjaxSubmit( beforeFn, afterFn )});
							return false
						}
						
						me.ajaxSubmit({
							error: function( http, describe, options ){
								if( typeof afterFn === 'function' ){afterFn.apply(me, [{success: false, message: describe}])} 
								else{$.showError( describe || 'Undefined error.' )}
							}, beforeSubmit: function( data, form, options ){
								if( typeof beforeFn === 'function' ){beforeFn.apply( form, [options] )}
							}, success: function( response, status ){
								var result = ( eval( "(" + response + ")" ) || null );
								if( result === null ){return $.showError( 'There is problem submiting ajax form.' )}
								
								if( typeof afterFn === 'function' ){afterFn.apply(me, [result])}
							}});
						
						return me
					},
				formReset: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						if((typeof this.get(0).reset == 'function') || ((typeof this.get(0).reset == 'object') && ((this.get(0).reset.nodeType || null) == null))){
							this.get(0).reset()
						}
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formClear: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						$( 'input:text, input:password, input:file, input:hidden, textarea', this ).val( '' );
						$( 'input:radio, input:checkbox', this ).attr( 'checked', false );
						$( $( 'select option', this ).get(0) ).attr( 'selected', true );
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formEnable: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						$( 'input:text, input:password, input:file, textarea', this ).attr( 'disabled', false ).css( 'opacity', 1 );
						$( 'input:radio, input:checkbox', this ).attr( 'disabled', false ).css( 'opacity', 1 );
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formDisable: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						$( 'input:text, input:password, input:file, textarea', this ).attr( 'disabled', true ).css( 'opacity', .5 );
						$( 'input:radio, input:checkbox', this ).attr( 'disabled', true ).css( 'opacity', .5 );
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formIsReadOnly: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						$( 'input:text, input:password, input:file, textarea', this ).attr( 'readonly', true );
						$( 'input:radio, input:checkbox', this ).attr( 'readonly', true );
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					},
				formNotReadOnly: function( callback, scope, params ){
						if( this.size() === 0 ){return false}
						
						$( 'input:text, input:password, input:file, textarea', this ).attr( 'readonly', false );
						$( 'input:radio, input:checkbox', this ).attr( 'readonly', false );
						
						if( typeof callback === 'function' ){callback.apply((scope || this), (params || []))}
						return this
					}
			});
		
		
		
		/*
		Old System Include Handler
		-----------------------------------------------------------------------*/
		$.extend({
				jserror: function( text, close_fn ) {
						$.showErr( text, close_fn )
					},
				jswarn: function( text, close_fn ) {
						$.showWar( text, close_fn )
					},
				jsinfo: function( text, close_fn ) {
						$.showInfo( text, close_fn )
					}
			});
		
	})( jQuery );
/****************************************************************************************************/
/* END INCLUDE: #1 - jquery.system.pack.js */
/****************************************************************************************************/
