// JavaScript Document
CORE = {
	multipleSelected : function(params) {    	
    	$.ajax({
            type: "GET",
            url: '?mdl='+params.mdl+'&action='+params.action+'&disable_layout=1',
            data: params.data,
            dataType : 'json',
            success: function(options){
            	$('#' + params.select).find('option').each(function(){
            		for(var i=0; i < options.length; i++) {
            			if($(this).val() == options[i]) {
            				$(this).attr('selected','selected');
            			}
            		}
            	});
            }
        });
	},
	edit : function(oParams) {
		var mdl = oParams.mdl;
		
		$.ajax({					 
			 url: '?mdl='+oParams.mdl+'&action=edit&'+oParams.pk+'=' + oParams.id + '&page=' + oParams.page + '&disable_layout=1',
			 dataType: 'json',
			 success: function(oData){				 
				  $('#' + mdl + 'IUForm').find('input').each(function(){
					  	
						if($(this).attr('name') != '' && $(this).attr('type') != 'password') {
							var name = $(this).attr('name');
							name = name.replace('[]','');
							var sData = eval('oData.' + name);
							
							var sType = $(this).attr('type');
							
							if(sType == 'text' || sType == 'hidden') {								
								if($(this).attr('class') == 'picture') {
									$('#' + $(this).attr('name') + 'info').find('div#__thumbnail__').html('<img src="../' + JSCONST.thumb_path + sData + '"/>');
								}								
								$(this).val( sData );								
							}
							if(sType == 'checkbox') {
								if(sData == 1)
									$(this).attr('checked','checked');
								else
									$(this).removeAttr('checked');
							}
							if(sType == 'radio') {
								if($(this).val() == sData)
									$(this).attr('checked','checked');
								else
									$(this).removeAttr('checked');
							}
						}
					});
					$('#' + mdl + 'IUForm').find('textarea').each(function(){
						$(this).val( eval('oData.' + $(this).attr('name') ) );
					});
					$('#' + mdl + 'IUForm').find('select').each(function(){
						var oSelect = $(this);
						var select_name = oSelect.attr('name');
						select_name = select_name.replace('\[\]', '');
						$(this).find('option').each(function(){
							if($(this).val() == eval('oData.' + select_name)) {
								$(this).attr('selected','selected');
							}
						});
					});
					
					$('#' + mdl + 'IUForm').attr('action','?mdl='+oParams.mdl+'&action=editsave&'+oParams.pk+'='+oParams.id+'&page='+oParams.page+'&disable_layout=1');
			 }
		 });
	},
	remove : function(oParams) {		
		if(oParams.confirmed == 1) {
			var aListParams = {mdl:oParams.mdl,page:oParams.page};
			$.ajax({
				 type: "POST",
				 url: '?mdl='+oParams.mdl+'&action=remove&'+oParams.pk+'='+oParams.id+'&page='+oParams.page+'&disable_layout=1',
				 data: {},
				 dataType : 'json',
				 success: function(oReturn){
					 CORE.showlist(aListParams);
					 $('#top-notification').html(oReturn.sz_Message);
				 }
			 });
		}
		else {
			var sParams = '{mdl:\''+oParams.mdl+'\',pk:\''+oParams.pk+'\',id:\''+oParams.id+'\',page:\''+oParams.page+'\',confirmed:\'1\'}';
			CORE_DIALOG.showYesNoButton({content:JSTRANS.are_you_sure, yesHandler:'CORE.remove('+sParams+')', noHandler:''});
		}		
	},
	removeselected : function(mdl, confirmed) {		
		if(confirmed == 1) {
			var aSelected = Array();
			$('#ListForm').find('input:checkbox').each(function(){
				if($(this).is(':checked')) {
					if($(this).val() != 'on')
						aSelected.push($(this).val());
				}
			});
			
			$.ajax({
				 type: "POST",
				 url: '?mdl='+mdl+'&action=removeselected&disable_layout=1',
				 data: {selected_id: aSelected},
				 dataType : 'json',
				 success: function(oReturn){
					 $('#top-notification').html(oReturn.sz_Message);				 
					 CORE.showlist({mdl:mdl,page:0});
				 }
			 });
			
		}
		else {
			var sParams = '\'' + mdl + '\', 1';
			CORE_DIALOG.showYesNoButton({content:JSTRANS.are_you_sure, yesHandler:'CORE.removeselected(' + sParams + ')', noHandler:''});
		}
	},
	filter : function(oParams) {
		var aListParams = {mdl:oParams.mdl,page:0};
		var oData = $('#FilterForm').serialize();
		if(oParams.filter == 1) {			
			$.ajax({
				 type: "POST",
				 url: '?mdl='+oParams.mdl+'&cmd=filter&disable_layout=1',
				 data: oData,
				 dataType : 'json',
				 success: function(oReturn){
					 CORE.showlist(aListParams);
				 }
			});
		}
		else {
			$.ajax({
				 type: "POST",
				 url: '?mdl='+oParams.mdl+'&cmd=removefilter&disable_layout=1',
				 data: {},
				 dataType : 'json',
				 success: function(oReturn){
					 CORE.showlist(aListParams);
				 }
			});
		}
	},
	togglefilter : function() {
		if($('#filter_form').attr('class') == 'is_on') {
			$('#filter_form').attr('class','is_off');
			$('#filter_form').hide('fast');
			$('#button_filter').val(JSTRANS.label_open);
		}
		else {
			$('#filter_form').attr('class','is_on');
			$('#filter_form').show('fast');
			$('#button_filter').val(JSTRANS.label_close);
		}
	},
	order : function(mdl, field) {
		var type = ($('#ordering').val() == 'ASC') ? 'DESC' : 'ASC';
		$('#ordering').val(type);		
		$.ajax({
			 type: "POST",
			 url: '?mdl='+mdl+'&action=order&field=' + field + '&type=' + type + '&disable_layout=1',
			 data: {},
			 dataType : 'json',
			 success: function(oReturn){
				 CORE.showlist({mdl:mdl,page:0});
			 }
		});
	},
	dosubmit : function(formName, beforeSubmit, mdl) {
		$('#top-notification').html('Working...');
        $(formName).ajaxSubmit({        	
        	dataType:  'json',
        	beforeSubmit : beforeSubmit,
        	success : function(msg){        		
        		//CORE_DIALOG.showOkButton(msg.sz_Message);
        		$('#top-notification').html(msg.sz_Message);
                $('#'+mdl+'IUForm').attr('action','?mdl='+mdl+'&action=save&disable_layout=1');
                CORE.clearform(mdl);
                var page = $('#current_page').val();
                CORE.showlist({page:page,mdl:mdl});
        	}
        });
        return false;
	},
	showlist : function(oParams) {
		var mdl = oParams.mdl.toLowerCase();
		if($('#' + mdl + '-list')) {
			
			$('#current_page').val(oParams.page);
			$.ajax({
				 type: "POST",
				 url: '?mdl='+oParams.mdl+'&action=showlist&page='+oParams.page+'&disable_layout=1',
				 data: {},
				 dataType : 'json',
				 success: function(oReturn){
					 $('#' + mdl + '-list').html(oReturn.sz_Message);
				 }
			 });
		}
		else {
			alert('div id=' + mdl + '-list is not existed!');
		}
	},
	checkall : function(checked) {
		$('#ListForm').find('input:checkbox').each(function(){
			if(checked)
				$(this).attr('checked','checked');
			else
				$(this).removeAttr('checked');
		});
	},
	openMedia : function(editor){
		$('body').append('<div id="media-dialog"></div>');
		$.ajax({
			dataType : 'json',
			type : 'post',
			data: {defined_function:"CORE.insertImage(this,'"+editor+"');"},
			url : '?mdl=media&action=popup&disable_layout=1',
			success : function(msg){
				$('#media-dialog').html(msg.sz_Message);
				$('#media-dialog').dialog(
	                {
	                    height : 450,
	                    width : 500,
	                    title : 'Media libraries',
	                    draggable : true,
	                    modal : false,
	                    resizable : false
	                }
				);
			}
		});
	},
	insertImage : function(li, editor) {
		var imgSrc = $(li).html();
		var currentData = $('#' + editor).val();
		$('#' + editor).val(imgSrc + currentData);
	},
	clearform : function(mdl) {
		CORE._clearForm('#' + mdl + 'IUForm');
	},
	_clearForm : function(formName){
		$(formName).find('input').each(function(){
			if($(this).attr('name') != '') {
				
				if($(this).attr('type') == 'text' || $(this).attr('type') == 'file' || $(this).attr('type') == 'hidden' || $(this).attr('type') == 'password') {
					$(this).val('');
					if($(this).attr('class') == 'picture') {
						$('#' + $(this).attr('name') + 'info').find('div#__thumbnail__').html('');
						$('#' + $(this).attr('name') + 'info').find('div#__selected_file__').html('');
						$('#' + $(this).attr('name') + 'info').find('div#__progess_bar__').html('');
					}
				}
				
				if($(this).attr('type') == 'checkbox')
					$(this).removeAttr('checked');
				
				if($(this).attr('type') == 'radio')
						$(this).removeAttr('checked');
				
			}
		});
		$(formName).find('textarea').each(function(){
			if($(this).attr('class') == 'editor') {
				eval (' CKEDITOR.instances.' + $(this).attr('name') + '.setData(\'\') ');
			}
			else {
				$(this).val('');
			}
		});
		/*
		$('#' + mdl + 'IUForm').find('select').each(function(){
			var oSelect = $(this);
			$(this).find('option').each(function(){					
					$(this).attr('selected','selected');
					return;
			});
		});
		*/
	}
};

$(document).ready(function(){
	$('input').bind('focus', function(){
		$(this).addClass('active_control').removeClass('inactive_control');
	});
	$('textarea').bind('focus', function(){
		$(this).addClass('active_control').removeClass('inactive_control');
	});
	$('select').bind('focus', function(){
		$(this).addClass('active_control').removeClass('inactive_control');
	});
	$('input').bind('blur', function(){
		$(this).addClass('inactive_control').removeClass('active_control');
	});
	$('textarea').bind('blur', function(){
		$(this).addClass('inactive_control').removeClass('active_control');
	});
	$('select').bind('blur', function(){
		$(this).addClass('inactive_control').removeClass('active_control');
	});
	CORE.togglefilter();
	$('.ui-datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
});
