
var loading_message = '<img src="'+ base_folder + 'themes/' + theme + '/assets/loading.gif"> Loading...';
var process_message = '<img src="'+ base_folder + 'themes/' + theme + '/assets/loading.gif"> Processing...';
var saving_message = '<img src="'+ base_folder + 'themes/' + theme + '/assets/loading.gif"> Saving...';



/*
	Shows content to page. remove existing content.
	
	Basic Usage:
		showContent('#firstHandler', {title:'First Handler', url:'generic_admin/show_page_one'});
	
	Advanced Usage:
		showContent('#firstHandler', {title:'First Handler', url:'generic_admin/show_page_one', cache:false, param: {name:'test'}},
			function(o){
				alert("test");
			});
*/
function showContent(el, config, returnFunc) {
	var handler = el;
	var param = (config.param) ? config.param : {} ;
	var title = config.title;
	var url = config.url;
	var cache = (config.cache == undefined) ? false : config.cache;
		
	setTitle(title);
	if (!cache) {
		$(handler).html('');	
	}	
	$(handler).show();	
	if ($(handler).html() == '') {
		$(handler).html(loading_message);
		$.post(base_url + url, param,
			function(o) {
				$(handler).html(o);
				if (returnFunc && typeof returnFunc == "function") {
					returnFunc(o);
				}
			}
		);
	}
}

function setTitle(text) {
	$('.pageTitle').html(text);
}

/*
* Usage:
	var config = {
		timeout: 2000,
		onTimeout: function() {
			alert('ase');	
		}
	};
	setMessage('Search box is required', config);
*/
function setMessage(text, config) {

	if (config != undefined) {
		var timeout = (config.timeout) ? config.timeout : 6000 ;
		var on_timeout = (config.onTimeout) ? config.onTimeout : '' ;
		
		if (config.showBefore) {
			message_id = 'temp_message';
			sharp_message_id = '#temp_message';
			$("<div class='message' id='"+ message_id +"' style='display:none'></div>").insertBefore(config.showBefore);			
		} else {
			sharp_message_id = '.message';	
		}
		
		if (config.showAfter) {
			message_id = 'temp_message';
			sharp_message_id = '#temp_message';
			$("<div class='message' id='"+ message_id +"' style='display:none'></div>").insertBefore(config.showAfter);			
		} else {
			sharp_message_id = '.message';	
		}
		
		if (config.showIn) {
			message_id = 'temp_message';
			sharp_message_id = '#temp_message';
			$(config.showIn).append("<div class='message' id='"+ message_id +"' style='display:none'></div>");			
		} else {
			sharp_message_id = '.message';	
		}
		
		$(sharp_message_id).fadeIn(500);
		$(sharp_message_id).html(text);
		
		if (timeout != '') {
			if (on_timeout != '') {
				//setTimeout(function(){
				//	$(sharp_message_id).hide().html('');
				//	config.onTimeout();
				//}, timeout);				
				$(sharp_message_id).fadeOut(timeout, function(){
					$(sharp_message_id).html('');
					config.onTimeout();
				});				
			} else {
/*				setTimeout(function(){
					$(sharp_message_id).hide().html('');	
				}, timeout)*/
				$(sharp_message_id).fadeOut(timeout, function(){
					$(sharp_message_id).html('');
				});	
			}
		}
	} else {
		$('.message').fadeIn(500);
		$('.message').html(text);
/*		setTimeout(function(){
			$('.message').hide().html('');	
		}, 5000);*/
		
		$('.message').fadeOut(6000, function(){
			$('.message').html(''); 
		});
		//$("a").bind('click', function(){ $('.message').hide().html('') });
		//$(window).bind("ajaxSend", function() {
			//$('.message').hide().html('');
		//});		
	}
}

/*
* Usage:
	var config = {
		link_1: {
			label: 'Yes',
			onClick: function() {
				alert('Yes');	
			}
		},
		link_2: {
			label: 'No',
			onClick: function() {
				alert('No');	
			}
		}
	};
	setMessageLink('Search box is required', config)
*/
function setMessageLink(text, config) {
	if (config != undefined) {
		//var total_link = (config.total_link) ? config.total_link : 1 ;
		if (config.link_1 && !config.link_2) {
			total_link = 1;	
		} else if (config.link_1 && config.link_2 && !config.link_3) {
			total_link = 2;	
		} else if (config.link_1 && config.link_2 && config.link_3) {
			total_link = 3;
		}

		if (config.showAfter) {
			message_id = 'temp_message';
			sharp_message_id = '#temp_message';
			$("<div class='message' id='"+ message_id +"' style='display:none'></div>").insertAfter(config.showAfter);			
		} else {
			sharp_message_id = '.message';	
		}
		//$('#addQuizForm').block({ message: null, css:{backgroundColor: '#FFF', cursor:'auto'}, fadeIn: 3000, overlayCSS: {backgroundColor:'#FFF',opacity:'0.6'} });
		//$("<div id='tanga' style='display:none'>Tanga K arin</div>").insertAfter("#addQuizForm");		

		//$('.message').show();
		switch (total_link) {
			case 1:
				var link_1_text = (config.link_1.label) ? config.link_1.label : '' ;
				var button = '<span style="cursor:pointer" class="link_1">'+ link_1_text +'</span>';
				var text = text + '<div class="messageLinks">'+ button +'</div>';
				$(sharp_message_id).html(text);
				$('.link_1').bind('click', function(){
					$(sharp_message_id).hide().html('');			
					config.link_1.onClick();
				});
			break;
			case 2:
				var link_1_text = (config.link_1.label) ? config.link_1.label : '' ;
				var link_2_text = (config.link_2.label) ? config.link_2.label : '' ;
				var button = '<span style="cursor:pointer" class="link_1">'+ link_1_text +'</span> | ';
				button += '<span style="cursor:pointer" class="link_2">'+ link_2_text +'</span>';
				var text = text + '<div class="messageLinks">'+ button +'</div>';
				$(sharp_message_id).html(text);
				$('.link_1').bind('click', function(){
					$(sharp_message_id).hide().html('');
					config.link_1.onClick();
				});
				$('.link_2').bind('click', function(){
					$(sharp_message_id).hide().html('');
					config.link_2.onClick();
				});	
			break;
			case 3:
				var link_1_text = (config.link_1.label) ? config.link_1.label : '' ;
				var link_2_text = (config.link_2.label) ? config.link_2.label : '' ;
				var link_3_text = (config.link_3.label) ? config.link_3.label : '' ;
				var button = '<span style="cursor:pointer" class="link_1">'+ link_1_text +'</span> | ';
				button += '<span style="cursor:pointer" class="link_2">'+ link_2_text +'</span> | ';
				button += '<span style="cursor:pointer" class="link_3">'+ link_3_text +'</span>';
				var text = text + '<div class="messageLinks">'+ button +'</div>';
				$(sharp_message_id).html(text);
				$('.link_1').bind('click', function(){
					$(sharp_message_id).hide().html('');
					config.link_1.onClick();
				});
				$('.link_2').bind('click', function(){
					$(sharp_message_id).hide().html('');
					config.link_2.onClick();
				});	
				$('.link_3').bind('click', function(){
					$(sharp_message_id).hide().html('');
					config.link_3.onClick();
				});				
			break;			
		}
		$(sharp_message_id).fadeIn(500);
	}
}

/* scroll page. delay is optional. scrollPage('#footer', 8000) */
function scrollPage(el, delay) {
	var delay = (delay) ? delay : 800;
	$.scrollTo(el, delay);
}

/* blocks specific element. blockElement('#myelement') */
function blockElement(el) {
	$(el).block({ message: null, css:{backgroundColor: '#FFF', cursor:'auto'}, fadeIn: 3000, overlayCSS: {backgroundColor:'#EFEFEF',opacity:'0.6'} });
}

/* blocks the full screen */
function blockFullScreen() {
	var loader_image = base_folder + 'themes/' + theme + "/assets/loader.gif";
	$.blockUI({ message: '<div style="background-color:#FFFFFF; border:1px solid #CCCCCC;"><div style="font:verdana; font-weight:bold; font-size:12px ">Loading...</div><img src="'+ loader_image +'"></div>', overlayCSS: { backgroundColor: '#CCCCCC' }, css: { border: '0px' } });	
}

/* Locks screen then if the document is already loaded unlock it. this is put inside template.php */
function lockScreen() {
	blockFullScreen();
	$(document).ready(function() {
		$.unblockUI();
	});
}

function navigate(url) {
	blockFullScreen();
	document.location = base_url + url; 
}

/*
* @desc this will convert date in which month is in abbreviated format
* Ex: 2009-03-11 to 2009-Mar-11
* usage convertDateToString(2009-03-11)
*/
function convertDateToString(date) {
	//var date_explode = date.split('-', date); 
	var date_explode = date.split('-');
	var year = date_explode[0];
	var month = date_explode[1];
	var day = date_explode[2];
	var monthname = new Array("Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	return year + '-' + monthname[month-1] + '-' + day;
}

// Search Box
/* Usage:
	<input type="text" name="title" id="title" class="searchBox" onfocus="_searchFieldFocus(this)" onblur="_searchFieldBlur(this)" value="Test" />
*/
var temp_search_value = '';
var temp_search_default_value;
function _searchFieldFocus(control) {
	if (!temp_search_default_value) {
		temp_search_default_value = $(control).val();
	}
	temp_search_value = $(control).val();
	if (temp_search_value == temp_search_default_value) {
		$(control).val('');
		$(control).css({color:'black', border:'1px solid #6694E3'});
	}
}
function _searchFieldBlur(control) {	
	if ($(control).val() == '') {
		$(control).css({color:'#CCCCCC', border:'1px solid #CCCCCC'});
		$(control).val(temp_search_default_value);
	}
}

// for simple modal
$.extend($.modal.defaults, {
	closeClass: "modalClose",
	closeHTML: "<div id='dialogHeader'><a href='#'>Close</a></div>",
	overlay:80
});
function closeDialog() {
	$.modal.close();	
}

// openDialog("#newCategoryDialog", {width:300, height:170});
// openDialog("Are you sure?", {width:300, height:170});
function openDialog(el, config) {
	var height = 80;
	var width = 200;
	if (config) {
		height = (config.height) ? config.height : height ;
		width = (config.width) ? config.width : width ;
	}
	var conf = {
		minHeight:height,
		minWidth:width		
/*		containerCss:{
			height:height,
			width:width
		}*/
	}
	var total = $(el).length;
	if (total > 0) {
		//$('#dialogFooter').remove();
		//$(el).append('<div id="dialogFooter"><input type="button" value="Hello" /></div>');
		$(el).modal(conf);
	} else {
		$.modal(el, conf);	
	}
}

function openOkDialog(message, config) {
	var height = 90;
	var width = 300;
	if (config) {
		height = (config.height) ? config.height : height ;
		width = (config.width) ? config.width : width ;
	}
	var conf = {
		minHeight:height,
		minWidth:width,
		containerCss:{
			height:height,
			width:width
		}		
	}
	//var total = $(el).length;
	var button = '<br /><br /><div id="dialogFooter"><input id="_dialog_ok_" class="dialog_ok" type="button" value="Ok" /></div>';
	$.modal('<center>' + message + button + '</center>', conf);	
	
	if (config) {
		if (config.onClick) {
			$('#_dialog_ok_').bind('mouseup', function(){
				closeDialog();
				config.onClick();
			});
		}
	} else {
		$('#_dialog_ok_').bind('mouseup', function(){closeDialog();});		
	}	
}

function blockPopUp() {
		var windowHeight = document.documentElement.clientHeight;
		
		
		$("#backgroundPopup").css({
		"height": windowHeight
		});
		$("#backgroundPopup").css({
			"opacity": "0.2"
		});
		
		$("#backgroundPopup").fadeIn("slow");	
}


function disablePopUp() {
	$("#backgroundPopup").fadeOut("slow");
}

//THIS IS FOR FORM SUBMISSION
//sample dialogYesNoForm('#test','#form_id','hr/appliccant',title,height,width,modal)
function dialogYesNoForm(dialog_id,form_id,yes_url, title,height,width,modal) {
	
	var dialog_id = (dialog_id == undefined) ? 'content' :  dialog_id;
	var title	  = (title == undefined) ? 'Message' :  title;
	var height	  = (height == undefined) ? 170:  height;
	var width	  = (width == undefined) ? 400:  width;
	var modal	  = (modal == undefined) ? true:  false ;
	var yes_url	  = (yes_url == undefined) ? '':  yes_url ;
	
	var form_id	  = (form_id == undefined) ? '':  form_id ;

	blockPopUp();
	var $dialog = $(dialog_id);
	$dialog.dialog({
		title: title,
		resizable: false,
		width: width,
		height: height,
		modal: modal,
		close: function() {
				   $dialog.dialog("destroy");
				   $dialog.hide();
					disablePopUp();
				},
		buttons: {
					'No' : function(){
						$dialog.dialog("close");
						disablePopUp();	
							
					},
					'Yes' : function(){
						$dialog.dialog("close");
						disablePopUp();
						$(form_id).ajaxSubmit({
							success:function(o) {
								if(yes_url!='') {
									location.href = base_url + yes_url;
								}
							}
						});
						
						
						   
						
					}
				}
			}).show();	

}

// this is NOT FOR FORM DIALOG BOX
// this is for data table
function dialogYesNoBox(dialog_id,yes_url,go_url,title,height,width,modal) {
	
	var dialog_id = (dialog_id == undefined) ? 'content' :  dialog_id;
	var title	  = (title == undefined) ? 'Message' :  title;
	var height	  = (height == undefined) ? 170:  height;
	var width	  = (width == undefined) ? 400:  width;
	var modal	  = (modal == undefined) ? true:  false ;
	var yes_url	  = (yes_url == undefined) ? '':  yes_url ;
	var go_url	  = (go_url == undefined) ? '':  go_url ;

	
	
	blockPopUp();
	var $dialog = $(dialog_id);
	$dialog.dialog({
		title: title,
		resizable: false,
		width: width,
		height: height,
		modal: modal,
		close: function() {
				   $dialog.dialog("destroy");
				   $dialog.hide();
					disablePopUp();
				},
		buttons: {
							'No' : function(){
								$dialog.dialog("close");
								disablePopUp();
								
							},
							'Yes' : function(){
								$dialog.dialog("close");
								disablePopUp();
								
								$.post(base_url + yes_url,{},function(){});
								location.href = base_url + go_url;
								
							}
						}
					}).show();	

}
//add <div id='msg_wrapper'></div>
function simpleDialogOkBox(msg,redirect,title,width,height)
{
	var title = (title == undefined) ? 'Message' :  title;
	var height	  = (height == undefined) ? 170:  height;
	var width	  = (width == undefined) ? 400:  width;
	var modal	  = (modal == undefined) ? true:  false ;
	var redirect   = (redirect == undefined) ? '':  redirect ;
		$("#msg_wrapper").html(msg);
			var $dialog = $("#msg_wrapper");
			$dialog.dialog({
			title: title,
			height: 170,
			width: 280,
			model:modal,
			buttons: {
					'Ok' : function(){
						$dialog.dialog("close");
						disablePopUp();
						if(redirect!='') {
							location.href = base_url + redirect;	
						}		
					}
				}
			});
}
function dialogOkBox(dialog_id,title,height,width,modal,ok_url) {
	
	var dialog_id = (dialog_id == undefined) ? 'content' :  dialog_id;
	var title	  = (title == undefined) ? 'Message' :  title;
	var height	  = (height == undefined) ? 170:  height;
	var width	  = (width == undefined) ? 400:  width;
	var modal	  = (modal == undefined) ? true:  false ;
	var ok_url 	  = (ok_url == undefined) ? '':  ok_url ;

	
	blockPopUp();
	var $dialog = $(dialog_id);
	$dialog.dialog({
		title: title,
		resizable: false,
		width: width,
		height: height,
		modal: modal,
		close: function() {
				   $dialog.dialog("destroy");
				   $dialog.hide();
					disablePopUp();
				},
		buttons: {
							'Ok' : function(){
								$dialog.dialog("close");
								disablePopUp();
								if(ok_url!='') {
									location.href = base_url + ok_url;	
								}
							}
						}
					}).show();	
}

//=============================================
// Message
//=============================================
jQuery.fn.showMsg = function(msg)
{
	$(this).html(msg);
	$(this).show();
}

jQuery.fn.hideMsg = function()
{
	this.hide();
}

Msg = {
	element: '#message',
	show: function(msg)
	{
		$(this.element).css(msg_getStyle());
		$(this.element).html(msg);
		$(this.element).show();
	},
	hide: function()
	{
		$('#message').show();	
	}
}
//=============================================
// Checkbox
//=============================================
var checkbox = {
	check: function(element) {
		return $("input[@type='checkbox']", element).each(function() {
			  this.checked = true;
		});
	},
	uncheck: function(element){
		return $("input[@type='checkbox']", element).each(function() {
			  this.checked = false;
		});	
	},
	checkGroup: function(name){
		$("input[@name='" + name + "']").each(function(){
			this.checked = true;								  
     	});
	},
	uncheckGroup: function(name){
		$("input[@name='" + name + "']").each(function(){
			this.checked = false;								  
     	});
	}	
}


