$(function() {

	ePad = {};
	tpl = {};
	G_mode3 = item_no = 0;
	result = {};
	formOn = 0;
	section = 0;
	thumb_s = typeof(thumb_s) == 'undefined' ? 0 : thumb_s;
	thumb_src = thumb_s.length > 3 ? thumb_s : 'images/avatar_34x35.png';

/*	On Before Unload Warnings
---------------------------------------------------------- */
ePad.beforeUnload = function(){

	if(formOn == 1){
		var msg = 'Are You Sure You Want to Leave This Page?\n'+
		'You are in the middle of entering data into a form.\n'+
		'If you leave now, all the data you entered will be lost.\n'+
		'Click OK to confirm leaving or Cancel to stay.';
		var response = confirm(msg);
		
		if(response){
			formOn = 0;
			return true;
		}else{
			formOn == 1
			return false;
		}
	}

}
window.onbeforeunload = ePad.beforeUnload;


// Delete Empty elements in an Array
Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}



//############  NOTIFICATION TOP MESSAGE  ############	
ePad.showTopMessage = function(message, type){
	
	var tg_out = '#notification-outter';
	var tg_in = '#notification-inner';

	if(type == "success"){
		$(tg_in).css("color", "#8BCADC");
	}
	if(type == "error"){
		$(tg_in).css("color", "#D60000");
		var message = 'ERROR! '+message;
	}
	if(type == "warning"){
		$(tg_in).css("color", "#F5B800");
		var message = 'WARNING! '+message;
	}
	
	if($(tg_out).css("display") == "none"){
		$(tg_out).toggle();
		
	}	
		if(typeof(tMsgTimer)!=undefined) window.clearTimeout(tMsgTimer);
		$(tg_in).html(message);	
		var tMsgTimer = window.setTimeout(function(){$(tg_out).fadeOut("slow");},6000);

}



// Generic Input Default message reset
ePad.inputMsgAutoReset = function(target,msg){	
	$('#'+target+'').die('click').live('click',function(){if($('#'+target+'').val() == msg)$('#'+target+'').val('');}).blur(function(){if($('#'+target+'').val() == msg || $('#'+target+'').val().length == 0)$('#'+target+'').val(msg);}).keydown(function(){if($('#'+target+'').val() == msg){$('#'+target+'').val('');}})
}


//$('.signup').colorbox({iframe:true, width:650, height:440,scrolling:false,opacity:0.4});	

$('.signup').live('click', function(){
ePad.signUpPopout();
})
$('.login').live('click', function(){
ePad.loginPopout();
})


ePad.loadModalInputForm  = function(formTitle,inputsHtml,iWidth,iHeight,w,h,onComplete,onClosed,submitBtn){
var submitBtnTxt = typeof(submitBtn)=='undefined' ? 'Create' : submitBtn;
var formHtml = ePad.populateModalBox(formTitle,inputsHtml,submitBtn);
$('#player').hide();
$.fn.colorbox({html:formHtml, initialWidth:iWidth, width:w, initialHeight: iHeight,  height:h,speed:50,opacity:0.4,scrolling:false,overlayClose:false,open:true,onComplete:onComplete,onCleanup:onClosed});
}

ePad.populateModalBox = function(formTitle,inputsHtml,submitBtn){
var formHtml = '<form id="modalForm" onsubmit="return false;">\
<div class="form-wrap" id="form-wrap">\
<div class="form-title" id="form-title">\
'+formTitle+'\
</div>\
<div id="form-content">\
'+inputsHtml+'\
</div>\
<div class="form-actions">\
	<div class="form-inputs" id="form-inputs">\
		<input type="button" value="Cancel" class="form-btn-sm" id="formCancel">\
		<input type="submit" value="'+submitBtn+'" class="form-btn-lg" id="formSubmit">\
	</div>\
</div>\
<div id="form-errors" class="form-errors">\
</div>\
</div>\
</form>';
return formHtml;
}


ePad.populateModalMsg = function(formTitle,msgHtml){
var formHtml = '<form id="modalForm" onsubmit="return false;">\
<div class="form-wrap" id="form-wrap">\
<div class="form-title" id="form-title">\
'+formTitle+'\
</div>\
<div id="form-content">\
'+msgHtml+'\
</div>\
</div>\
</form>';
$('#cboxLoadedContent').html(formHtml);
}

ePad.loadModalInfoForm  = function(formTitle,inputsHtml,iWidth,iHeight,w,h,onComplete,onClosed){
var formHtml = '<form id="modalForm" onsubmit="return false;">\
<div class="form-wrap">\
<div class="form-title">\
'+formTitle+'\
</div>\
'+inputsHtml+'\
<div id="form-errors" class="form-errors">\
</div>\
</div>\
</form>';$('#player').hide();
$.fn.colorbox({html:formHtml, initialWidth:iWidth, width:w, initialHeight: iHeight,  height:h,speed:50,opacity:0.4,scrolling:false,overlayClose:false,open:true,onComplete:onComplete,onClosed:onClosed});
}


ePad.popoutFormMessage = function(title, content){
	$('#form-title').html(title);
	$('#form-content').html(content);
	$('#formSubmit').hide();
	$('#formCancel').removeClass('form-btn-sm').addClass("form-btn-lg").val('Understood');
}

ePad.popoutFormErrors = function(errors){
	$('#form-errors').css('display','inline').html(errors);
}



/*	Shared Common Actions
---------------------------------------------------------- */

// --------  Remove Item DOM Item After Succesfull Deletion ------------	

ePad.removeDomItem = function(item_arr){
	$.each(item_arr, function(i){
		$("#"+item_arr[i]+"").remove();
	})
}

// Simple Email validation
ePad.validateEmail = function(elId) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = $('#'+elId+'').val();
   if(reg.test(address) == false) {
      return false;
   }else{
		return true;
	}
}


// --------  Update Item after any input  ------------	

ePad.updateItem = function(section,md){

	$.getJSON(""+section+"_details_get.php",{id: item_no}, function(d){
		if(d){
			if(d.ok.success == 1){
				ePad.updateItemFromObj(d.itemObj);
				ePad.animateBgColor('#'+item_no,'orange','#222222');
			}
			if(d.ok.success == 0){
				ePad.showTopMessage(d.ok.msgDisplay,'error');
			}
		}
	})			
}

// --------  Update Item From Object ------------	

ePad.updateItemFromObj = function(itemObj,md){
	var item_html = tpl[section](itemObj);
	if(md=='insert'){
		$("#input_wrap").after(item_html);
	}else{
		$("#"+itemObj.id+"").replaceWith(item_html);	
	}
	ePad.animateBgColor('#'+itemObj.id,'orange','#FFF');
	//ePad.clearHiddenItems();		
}


ePad.insertNewItem = function(d){
	var newItemHtml = tpl[section](d);
	$('#items').prepend(newItemHtml);
	ePad.animateBgColor('#'+d.id,'#FFFFB3','#E5F7FF');
}

ePad.animateBgColor = function(target,from,to) {
	$(target).css('background-color',from);
	$(target).animate({backgroundColor:to},1250);
}


});
