/*
 * @package     Kepingin!Framework - Site Scripts
 * @author      Tu Masdan E.P. <utu.eka@gmail.com>
 * @copyright   2008 Babon Multimedia
 * @website		http://www.babonmultimedia.com
 * @version     1.0.1
 * @changed		10/30/2008 1:02 AM
 * @license     -
 * @link        -
*/


/*------------------------------------------------------------------------------
Site Scripts
------------------------------------------------------------------------------*/
(function($){
	$.popupAction = null;
	$.popupBeforeAction = null;
	$.popupAfterAction = null;
	
	$.openPopup = function( id, ttl, url, onshow, onhide, onprocess, xlength, ylength){
		if( !url ){
			alert( 'Not found URL' );
			return false;
		}
		
		var fw = (xlength || Number(Math.floor( (70/100)*screen.width ))),
			fh = (ylength || Number(Math.floor( (60/100)*screen.height )-100)),
			html = '<table class="popup">'
							+'<thead><tr><th><h3><img src="/resources/images/icon-form.png" align="absmiddle"/> ' + ttl + '</h3></th></tr></thead>'
							+'<tbody><tr><td>'
								+'<iframe id="popup-' + id + '" name="popup_' + id + '" src="about:blank" width="'+fw+'" height="'+fh+'" hspace="0" vspace="0" scrolling="auto" frameborder="0" allowtransparency="true" class="transparent loader">'
								+'</iframe>'
							+'</td></tr></tbody>'
							+'<tfoot><tr><td><ul class="h-menu-list">'
								+ $.popupButton( 'Submit', 'tick.png', 'submit' )
								+ $.popupButton( 'Cancel', 'icon-cancel.png', 'cancel' )
							+'</ul></td></tr></tfoot>'
						+'</table>',
			oid = 'popup-' + id,
			o = null,
			action = null;	
		
		$.facebox(html, null, {
				onshow: function(){
						$('#facebox .footer').hide();
						if( o == null ){
							o = $( 'iframe#' + oid );
							action = $( 'a.popup-action' );
							
							o.load(function(){
									action.unbind().bind('click', function(){
											switch( $( this ).attr( 'rel' ) ){
												case 'submit':
													if( typeof onprocess == 'function' ) onprocess.apply( this, [$.facebox, o] );
												break;
												
												case 'cancel':
													if( confirm('Are you sure?') ){
														$( this ).unbind();
														$.facebox.close();
													}
												break;
											}
										});
									
									if( typeof onshow == 'function' ) onshow.call();
								}).attr( 'src', url || 'about:blank' );
						}
					}, 
				onhide: function(){
						if( typeof onhide == 'function' ) onhide.call()
					}
			})
	};
	
	$.popupButton = function( caption, icon, rel ){
		var o = String('<li><a href="javascript:void(0)" rel="' + rel + '" class="popup-action" title="' + caption + '"><img src="/resources/images/' + icon + '" align="absmiddle"/> ' + caption + '</a></li>')
		return o;
	};
})(jQuery);


(function($){
	$.fn.pageError = function(){
			var msg = $( this ).html();
			
			if( msg ){
				var redirect = $.pageRedirect();
				if( redirect ) $.jserror( msg, function(){ self.location.href = redirect } );
				else $.jserror( msg );
				
				$( this ).remove();
			}
		};
	
	$.fn.pageWarning = function(){
			var msg = $( this ).html();
			
			if( msg ){
				var redirect = $.pageRedirect();
				if( redirect ) $.jswarn( msg, function(){ self.location.href = redirect } );
				else $.jswarn( msg );
				
				$( this ).remove();
			}
		};
	
	$.fn.pageInfo = function(){
			var msg = $( this ).html();
			
			if( msg ){
				var redirect = $.pageRedirect();
				if( redirect ) $.jsinfo( msg, function(){ self.location.href = redirect } );
				else $.jsinfo( msg );
				
				$( this ).remove();
			}
		};
	
	$.pageRedirect = function(){
			var o = $( 'a#jredirect' );
			if( o[0] ){
				return o.attr( 'href' );
				
				o.remove();
			}
		};
	
	$.fn.clearErrorMsg = function( fid, clear ){
			$( this ).unbind().bind( 'click', function(){
					$( 'em.error' ).remove();
							
					var err_inputs = $( ':text.error, textarea.error, select.error', $( 'form#' + fid ) );
					if( err_inputs.length ){
						for( var x=0; x<err_inputs.length; x++ ){
							var input = err_inputs[x];					
							$(input).removeClass( 'error' );
						}
					}
					
					if( clear == true ) $( 'form#' + fid )[0].reset()
				})
		};
	
	$.loadCaptcha = function( img, text, callback ){
			img.addClass( 'hide' );
			
			var loader = new Image();
			loader.onload = function(){
					this.onload = null;
					
					img.attr( 'src', this.src );
					img.removeClass( 'hide' );
					
					text.val( '' );
					text.focus();
					
					if( typeof callback == 'function' ) callback.call()
				};
			
			var rand = new Date();
			loader.src = '/plugins/captcha/img.php?' + rand.getTime()
		};
	
	$.fn.refreshCaptcha = function( img, text ){
			$( this ).unbind().bind( 'click', function(){
					var me = this;
					
					$.loadCaptcha(img, text, function(){
							$( me ).refreshCaptcha( img, text )
						});
					
					return false
				})
		};
	
	$.fn.JSiteForm = function(){
			var o = this[0];
			
			$( 'button:button#action-submit', o ).click(function(){
					if( confirm( 'Are you sure to send this form?' ) ){
						$( this ).attr( 'disabled', true );
						
						$('html, body')
							.animate({scrollTop:0}, 'slow', function(){
									$( o.task ).val( 'login' );
									$( o )
										.attr( 'action', self.location.href )
										.submit()
								}) 
					}
				});
			
			$( 'button:button#action-reset', o ).click(function(){
					var t = $( this );
					
					t.attr( 'disabled', true );
					
					$('html, body')
						.animate({scrollTop:0}, 'slow', function(){
								$( 'input:text, textarea', $(o) ).val( '' );
								$( 'input:radio, input:checkbox', $(o) ).attr( 'checked', false );
								$( $( 'select option', $(o) )[0] ).attr( 'selected', true );
								
								t.attr( 'disabled', false )
							})
				});
		};
	
	$.fn.ajaxThumb = function(){
			var img = this;
			document.ajax_thumbs = new Array(this.size());
			img.each(function(i){
					var src = img[i].src;
					img[i].src = '/resources/images/s.gif';
					
					document.ajax_thumbs[i] = new Image;
					document.ajax_thumbs[i].onload = function(){
							this.onload = null;
							
							$( img[i] )
								.width( this.width )
								.height( this.height )
								.attr( 'src', this.src )
								.fadeIn( 'slow' )							
						};
					
					var cache = document.ajax_thumbs[i];
					cache.src = src
				})
		},
		
	$.fn.jenlargeImage = function(){
			if( this.size() === 0 ){return false}
			
			var images = this;
			images.each(function( i ){
					var image = $( images[i] );
					
					var wrapper = $( '<a href="javascript:void(0)" title="Enlarge Photo"></a>' );
					wrapper
						.css({
								display: 'block',
								float: (image.attr( 'align' ) || 'none'),
								textDecoration: 'none',
								width: (image.attr( 'width' ) || 'auto'),
								margin: 10
							})
						.attr('rel', image.attr('src'));
					
					image.wrap( wrapper )
						.css('display', 'block');
						
					image.parent().click(function(){
							var src = $( this ).attr( 'rel' );
							$.facebox.open( src )
						})
				})
		}
})(jQuery);
	
$(function(){
	if( ($.browser.msie == true) && ( ($.browser.version <= 7.0) && ($.browser.version  >= 5.5) ) ){
		$.getScript('/resources/javascripts/fixed.js', function(){
				var win = window;
				
				fixed_scan();
				fixed_scanner = win.setInterval( fixed_scan, fixed_SCANDELAY );
				
				$( win )
					.unbind()
					.bind( 'load', fixed_stop )
					.bind( 'resize', fixed_delayout )
					.bind( 'scroll', fixed_scroll );
			});
		
		$('img[src$=.png]').each(function(){ // get all pngs on page
				if (!this.complete) {
					this.onload = function(){ $.fixPng(this) };
				} else {
					$.fixPng(this);
				}
			});
	}
	
	$.preloadCssImages();
	
	$( 'p#jerror' ).pageError();
	$( 'p#jwarn' ).pageWarning();
	$( 'p#jinfo' ).pageInfo();
	
	$( 'input:checkbox, input:radio' ).css({margin: 0, border: 'none'});
	$( 'input:text' ).addClass( 'text' );
	$( 'textarea, input:text' ).focus(function(){
			if(this.value) this.select()
		});
	
	$( 'label.required, label.mandatory' ).append( '<span class="asterisk">*</span>' );
	
	$( '.content-msg' ).fadeIn('slow', function(){
			var o = $( this );
			
			o.addClass( 'block' );
			
			setTimeout(function(){
					o.fadeOut('slow', function(){
							$( this ).remove()
						})
				}, 1000*15)
		});
	
	$( 'a.jpopup-img' ).click(function(){
				var src = $( this ).attr( 'rel' );
				if( src ) $.facebox.open( src )
			});
	
	$( 'a.jpopup-tips' ).click(function(){
				var tips = $( this ).attr( 'title' );
				if( tips ) $.facebox( tips )
			});
	
	$( 'form#site-form' ).JSiteForm();	
	$( 'img.ajax-thumb' ).ajaxThumb();
	
	$.getScript('/resources/javascripts/jquery/plugins/jquery.rightclick.pack.js', function(){
			$( 'body' ).rightClick(function(){
					$( 'html, body' )
						.animate({scrollTop:0}, 'slow', function(){void(0)})
				});
		});
	
	$( 'img.jenlarge-image' ).jenlargeImage()
});

/*------------------------------------------------------------------------------
Package Scripts
------------------------------------------------------------------------------*/
(function($){
		$.fn.jinputDate = function(){
				var o = this;
				
				if( typeof $.fn.datepicker != 'function' ){
					$.getCss( '/resources/javascripts/jquery/resources/css/jquery.datepicker.css' );
					$.getScript('/resources/javascripts/jquery/plugins/jquery.datepicker.pack.js', function(){
							o.jinputDate()
						});
				} else {				
					var now = new Date();
					
					o.datepicker({
							dateFormat: 'yy-mm-dd',
							minDate: new Date(now.getFullYear(), now.getMonth(), now.getDate()+1),  
							maxDate: new Date(2010, 12 - 1, 31),  
							beforeShow: function(){void(0)},     
							onSelect: function(){void(0)},
							closeAtTop: false,  
							mandatory: true, 
							showOtherMonths: true,  
							showStatus: true,
							statusForDate: function(date, inst){
									var today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
									return $.datepicker.dateStatus(date, inst) + (today.getTime() == date.getTime() ? ' (today)' : '')
								}
						});
				}
			};
			
		$.fn.jinputTime = function(){
				var o = this;
				
				if( typeof $.fn.offset != 'function' ){
					$.getScript('/resources/javascripts/jquery/plugins/jquery.dimensions.pack.js', function(){
							o.jinputTime()
						});
				} else if ( typeof $.fn.ptTimeSelect != 'function' ){
					$.getCss( '/resources/javascripts/jquery/resources/css/jquery.timepicker.css' );
					$.getScript('/resources/javascripts/jquery/plugins/jquery.timepicker.pack.js', function(){
							o.jinputTime()
						});
				} else {				
					var now = new Date();
					
					o.ptTimeSelect({
							labels: {
									x: '<img src="/resources/images/cancel.png" align="absmiddle"> <u>C</u>lose', 
									popup_link: '<img src="/resources/images/clock.png" align="absmiddle">', 
									set_time: '<img src="/resources/images/tick.png" align="absmiddle"> <u>A</u>pply'
								}, 
							cntr_class: 'time-picker' 
						}); 
				}
			}
	})(jQuery);

$(function(){
		$( 'input:text.jinput-date' ).jinputDate();
		$( 'input:text.jinput-time' ).jinputTime();
	});
