/*
* Author : Omer Abbas
* Contact: omer.abbas01@gmail.com
* CopyRights: Copy Rigths All rights Reserved by HafeezCentre.pk
* Description: This plugins are developed into jquery for Hafeezcentre.pk, You can't use these plugIns without the permession of 	owner. 
*/

/* Error Msgs */
var err_no_connection = 'Seems like your Internet Connection, Isn\'t working. Please try again.'; // Ajax error on return null

(function($) {
	
	$.centerbox = {
		verticalOffset: 0,                // vertical offset of the dialog from center screen, in pixels
		horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
		repositionOnResize: true,           // re-centers the dialog on window resize
		overlayOpacity: .01,                // transparency level of overlay
		overlayColor: '#FFF',               // base color of overlay
		okButton: '&nbsp;OK&nbsp;',         // text for the OK button
		cancelButton: '&nbsp;Cancel&nbsp;', // text for the Cancel button
		closeButton: '&nbsp;Close&nbsp;',   // text for the Close button
		
		alert: function(message, title, callback) {
			if( title == null ) title = 'Alert';
			$.centerbox._show(title, message, null, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},
		confirm: function(message, title, callback) {
			if( title == null ) title = 'Confirm';
			$.centerbox._show(title, message, null, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},
		popup: function(href, spContentId, spContent) {
			$.centerbox._show('', '', null, 'popup', function(result) {
				if( callback ) callback(result);
			});
			$.centerbox._getContent(href, spContentId, spContent);
		},
		
		_show: function(title, msg, value, type, callback) {
			$.centerbox._hide();
			$.centerbox._overlay('show');
			
			$('body').append(
			  '<div id="hcf_alert">'+
			    '<div id="hcfa_title"></div>'+
				'<div id="hcfa_content"></div>'+
				'<div id="hcfa_nav"></div>'+
			  '</div>');
			
			if( $.centerbox.dialogClass ) $("#hcf_alert").addClass($.centerbox.dialogClass);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#hcf_alert").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			
			if(title != null || title != ''){
				$("#hcfa_title").text(title);
			} else {
				$("#hcfa_title").hide();
			}
			
			$("#hcf_alert").addClass(type);
			$("#hcfa_content").html(msg);
			
			$.centerbox._reposition();
			$.centerbox._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$("#hcfa_nav").html('<div class="hcfa_btn_wr"><input type="button" value="' + $.centerbox.okButton + '" id="hcfab_ok" /></div><div class="clear"></div>');
					$("#hcfab_ok").click( function() {
						$.centerbox._hide();
						callback(true);
					});
					$("#hcfab_ok").focus().keypress( function(e) {
						if( e.keyCode == 13 || e.keyCode == 27 ) $("#hcfab_ok").trigger('click');
					});
				break;
				
				case 'confirm':
					$("#hcfa_nav").html('<div class="hcfa_btn_wr"><input type="button" value="' + $.centerbox.cancelButton + '" id="hcfab_cancel" /></div><div class="hcfa_btn_wr"><input type="button" value="' + $.centerbox.okButton + '" id="hcfab_ok" /></div><div class="clear"></div>');
					$("#hcfab_ok").click( function() {
						$.centerbox._hide();
						if( callback ) callback(true);
					});
					$("#hcfab_cancel").click( function() {
						$.centerbox._hide();
						if( callback ) callback(false);
					});
					$("#hcfab_ok").focus();
					$("#hcfab_ok, #hcfab_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#hcfab_ok").trigger('click');
						if( e.keyCode == 27 ) $("#hcfab_cancel").trigger('click');
					});
				break;
				
				case 'popup':
					$("#hcf_alert").prepend('<div id="hfca_close"></div>');
					$("#hcfa_content").addClass('loader');
					$("#hcfa_nav").html('<div class="hcfa_btn_wr"><input type="button" value="' + $.centerbox.closeButton + '" id="hcfab_close" /></div><div class="clear"></div>');
					$("#hfca_close, #hcfab_close, #body_overlay").click( function() {
						$.centerbox._hide();
					});
				break;
			}
		},
		
		_hide: function() {
			$("#hcf_alert").remove();
			$.centerbox._overlay('hide');
			$.centerbox._maintainPosition(false);
		},
		
		_overlay: function(s) {
			switch(s) {
				case 'show':
					$.centerbox._overlay('hide');
					$("#body_overlay").css({
						background: $.centerbox.overlayColor,
						opacity: $.centerbox.overlayOpacity,
						display: 'block'
					});
				break;
				case 'hide':
					$("#body_overlay").hide();
				break;
			}
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#hcf_alert").height() / 2)) + $.centerbox.verticalOffset;
			var left = (($(window).width() / 2) - ($("#hcf_alert").width() / 2)) + $.centerbox.horizontalOffset;

			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#hcf_alert").css({
				top: top + 'px',
				left: left + 'px'
			});
		},
		
		_maintainPosition: function(status) {
			if( $.centerbox.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', $.centerbox._reposition);
					break;
					case false:
						$(window).unbind('resize', $.centerbox._reposition);
					break;
				}
			}
		},
		
		_getContent: function (href, spContentId, spContent){
			if(href != '' && href != null){
				$.ajax({
					url: href,
					dataType: "html",
					type: 'post',
					success: function(data) {
						if(data == null){
							$.centerbox._changeContent(err_no_connection);
						} else {
							$.centerbox._changeContent(data);
						}
					},
					error: function(jqXHR, textStatus, errorThrown){
						$.centerbox._changeContent('ERROR: ' + textStatus + ' ' + errorThrown);
					}
				});
			} else if(spContentId != '' && spContentId != null){
				$.centerbox._changeContent($(spContentId).html());
			} else if(spContent != '' && spContent != null){
				$.centerbox._changeContent(spContent);
			} else {
				$.centerbox._changeContent('HafeezCentre.pk Simple Popup content will be here...');
			}
		},
		
		_changeContent: function (c){
			$('#hcfa_content').removeClass('loader').html(c);
			$.centerbox._reposition();
			$.centerbox._maintainPosition(true);
		}
	}
	

	
	/*
	 * Tool Tip Functionality
	 */
	$.framework = {
		tooltip : function(s, e){
			if($('.tooltip_holder').length == ''){
				$('body').append('<div class="tooltip_holder"><div class="tpw_con">Tool Tip gose here</div><div class="tpw_arrow"></div></div>');
			}
			if(e.type == 'mouseover'){
				var pos = $(s).position();
				var tooltip = $(s).attr('tooltip');
				$('.tooltip_holder .tpw_con').html(tooltip);
				var w = pos.top - parseInt($('.tooltip_holder').height());
				$('.tooltip_holder').css({'left': pos.left - 6, 'top': w, 'display':'block'});
			} else {
				$('.tooltip_holder').hide();
			}
		},
		
		emptySelected : function (s){
			var val = $(s).val();
			var def = $(s).attr('removeDefault');
			
			if(val == def){
				$(s).val('');
			} else if(val == '') {
				$(s).val(def);
			}
		},
		
		sortValue : function (s){
			var val = $(s).val();
			var def = $(s).attr('sortVal');
			
			switch(def) {
				case 'alpha':
					var pattern = new RegExp('[^a-zA-Z ]+', 'g');
					val = val.replace(pattern, '');
					break;
					
				case 'numeric':
					var pattern = new RegExp('[^0-9]+', 'g');
					val = val.replace(pattern, '');
					break;

				case 'alphanumeric':
					var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');
					val = val.replace(pattern, '');
					break;

				default:
					var pattern = new RegExp('[^'+def+']+', 'g');
					val = val.replace(pattern, '');
					break;
			}
			
			$(s).val(val);
		}
	};

	// Shortuct functions
	hcAlert = function(message, title, callback) {
		$.centerbox.alert(message, title, callback);
	};
	
	hcConfirm = function(message, title, callback) {
		$.centerbox.confirm(message, title, callback);
	};
	
	hcPopup = function(href, spContentId, spContent) {
		$.centerbox.popup(href, spContentId, spContent);
	};
	
})(jQuery);

$(document).ready(function(){
	/* Check Over Layer */
	if($('#body_overlay').length == ''){
		var sPopupHtml = '<div id="body_overlay"></div>';
		$('body').prepend(sPopupHtml);
	}
	
	$('[confirm]').live('click', function(){
		var msg = $(this).attr('confirm');
		var url = $(this).attr('href');
		
		hcConfirm(msg, 'Confirm', function(r){
			if(r === true){
				window.location = url;
			}
		});
		return false;
	});
	
	$('.hcSPopup').live('click', function(){
		var href = null;
		var spContentId = null;
		var spContent = null;
		
		/* GET values */
		if($(this).attr('href') != '') href = $(this).attr('href');
		if($(this).attr('spContentId') != '') spContentId = $(this).attr('spContentId');
		if($(this).attr('spContent') != '') spContent = $(this).attr('spContent');
		
		hcPopup(href, spContentId, spContent);
		return false;
	});
	
	$('[tooltip]').live('mouseover mouseout', function(e){
		$.framework.tooltip(this, e);
	});
	
	$('[removeDefault]').live('blur focus', function(){
		$.framework.emptySelected(this);
	});
	
	$('[sortVal]').live('keydown keyup', function(){
		$.framework.sortValue(this);
	});
})
