page_load=false;
var Async=true;
var modalClass="";
function pageload(hash) {
	if(hash) {
		/*if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}*/
		ajax_operation(hash);
	} else {
		if (page_load){
			/*$('#method').val('load');
			$('#Module').val('');
			document.f1.method="GET";
			document.f1.action=location.href;
			document.f1.su
			$('#f1').submit();*/
			if ($('#AMBIENTE').val()!='FRONT'){
				
				location.href = location.href.replace('?fa=new','')+"?fa=new";//location.reload(); //ajax_default(hash);
			}
		}
	}
}

$(document).ready(function() {
	//load_datepicker();
	$.history.init(pageload);
	page_load=true;
	//$.historyInit(pageload, "jquery_history.html");
	$("#contenedor").show();
});




function call_func_confirm(accion,id){
	if(confirm('Estas Seguro?')){
		call_func(accion,id);
	}
}

function call_func(accion,id){
	$("#ProductId").val(id);
	call_bak(accion);
}

function call_func_module(obj,accion,id){
	module=$(obj).closest("div.module-contents").attr("id");
	$("#ProductIdSel").val(id);
	call_bak_module(module,accion);
}

function call_bak_module(module,method){
	if (module){
		$('#Module').val(module);
	}
	else{
		$('#Module').val('');
	}
	call_bak_operation(module,method);
}

function call_bak_method(obj,method){
	if ($(obj).closest(".row").attr("id")){
		$("#row_id").val($(obj).closest(".row").attr("id"));
	}
	
	module=$(obj).closest("div.module-contents").attr("id");
	call_bak_module(module,method);
}


function call_bak(method){
	$('#Module').val('');
	call_bak_operation(null,method);
}

function call_bak_send(){
	method=$('#method').val();
	module=$('#Module').val();
	call_bak_module(module,method);
}


var centerPopup = function(hash){
        windowHeight   = jQuery(window).height();
        linkOffset     = jQuery("html").scrollTop();
        hash.w.css('top', linkOffset + (windowHeight * .1)).show();
}

var data_return;
function call_bak_operation(module,method){

	/*if ($('#PATH_URL_MRW').val()){
		url_call_bak=$('#PATH_URL_MRW').val()+"ajax_operation.php";
	}
	else{
		url_call_bak="ajax_operation.php";
	}*/
	url_call_bak=$('#Page').val();
	//url_call_bak=$('#Page').val()+"/"+method;
	var valid=true;
	$('#method').val(method);
	$.ajax({
		type: 'POST',
		url: url_call_bak,
		cache: false,
		async:Async,
		dataType: "json",
		data: $('#f1').serialize()+"&post_ajax=1",
		error: function (xhr, ajaxOptions, thrownError) {
			console.log(xhr.status);
			console.log(thrownError);
	       /* alert(xhr.status);
	        alert(thrownError);
	        alert(url_call_bak);
	        alert ($('#f1').serialize()+"&post_ajax=1");
	        */
	      },
		success: function(data){
			if (data.controls){
			$.each(data.controls,function( intIndex, objValue ){
				
					if (objValue.type=="contenedor"){
				 	 	$('#'+objValue.contenedor).writeCapture().html(objValue.html);
				 	 	$('#'+objValue.contenedor).show();
				 	 	$(".button").removeClass('in_process');
				 	 	$("button").removeClass('in_process');
				 	}else if (objValue.type=="append"){
				 	 	$('#'+objValue.contenedor).append(objValue.html);
				 	 	$('#'+objValue.contenedor).show();
				 	 	$(".button").removeClass('in_process');
				 	 	$("button").removeClass('in_process');
				 	}else if (objValue.type=="contenedor_show"){
				 	 	$('#'+objValue.contenedor).show();
				 	}else if (objValue.type=="contenedor_hide"){
				 	 	$('#'+objValue.contenedor).hide();
				 	}else if (objValue.type=="message"){
				 		alert(objValue.message);
				 	}else if (objValue.type=="placeholder"){
				 		$('.'+objValue.contenedor).html(objValue.html);
				 	}else if (objValue.type=="placeholder_show"){
				 		$('.'+objValue.contenedor).show();
				 	}else if (objValue.type=="formulario"){
				 		$('#'+objValue.variable).val(objValue.valor);
				 	}else if (objValue.type=="call_bak"){
				 		call_bak(objValue.method);
				 	}else if (objValue.type=="function"){
				 		data_return=objValue.data;
				 		eval(objValue.funccion+"()");
				 	}else if (objValue.type=="historyLoad"){
				 		data=objValue.history;
						$.history.load(data);
				 	}else if (objValue.type=="url_refresh"){
				 		window.history.pushState("object or string", "Title", objValue.url);
				 	}else if (objValue.type=="location"){
				 		if(objValue.parent=="parent"){
				 			parent.location.href=objValue.page;
				 		}
				 		else{
				 			location.href=objValue.page;
				 		}
				 	}else if (objValue.type=="back"){
				 		history.go(-1);

				 	}else if (objValue.type=="return"){
				 		valid= objValue.valor;
				 	}else if (objValue.type=="error"){
				 	 	$('#'+objValue.control).html(objValue.message);
				 	 	valid=false;
				 	}else if (objValue.type=="anchor"){
				 		//alert(objValue.target);
				 	 	window.scrollTo(0, $('#'+objValue.target).position().top);
				 	}else if (objValue.type=="modal"){
				 		$(".button").removeClass('in_process');
				 		$("button").removeClass('in_process');
				 		if (/*$('#AMBIENTE').val()=='backend' */$('#myModal').html() ){
				 			if ($('#myModal').hasClass('in') && objValue.width!="refresh"){
				 				if (objValue.width=='full'){
					 				$('#myModal2 .modal-dialog').addClass('full-screen');
					 			}
					 			else{
					 				$('#myModal2 .modal-dialog').removeClass('full-screen');
					 			}
				 				if (objValue.class){
					 				$('#myModal .modal-dialog').addClass(objValue.class);
					 			}
				 				$("#content_modal2").html(objValue.html);
				 				$('#myModal2').modal('show');
				 			}
				 			else{
				 				
				 				if (objValue.width=='full'){
					 				$('#myModal .modal-dialog').addClass('full-screen');
					 			}
					 			else{
					 				$('#myModal .modal-dialog').removeClass('full-screen');
					 			}
				 				if (objValue.class){
				 					modalClass=objValue.class;
					 				$('#myModal .modal-dialog').addClass(modalClass);
					 			}
				 				else{
				 					$('#myModal .modal-dialog').removeClass(modalClass);
							 		
				 				}
				 				
				 				$("#content_modal").html(objValue.html);
						 		
				 				if (objValue.class=='static'){
				 					console.log('static');
				 					$('#myModal').modal({backdrop: 'static', keyboard: false})  
				 				}
				 				else{
				 					console.log('normal')
				 					$('#myModal').modal('show');
				 				}
				 				
				 				//$('#myModal').modal('show');
				 			}
				 			
				 		}
				 		else{
				 			$("#content_modal").html(objValue.html);
					 		
				 			$("#jqEdit").jqm().jqmShow();
					 		if (IsNumeric(objValue.width)){
						 		width=objValue.width;
					 		}
					 		else{
					 			width=337;
					 		}
					 		margin_left=width/ 2;
					 		$("#jqEdit").css('margin-left', '-'+margin_left+'px');
					 		$("#jqEdit").css('width', width+'px');
				 		}
				 		load_editor();

				 	 	//$("#jqEdit").jqm({onShow:centerPopup}).jqmShow();
						//$("#jqEdit").jqm().css('top',800).jqmShow();
				 	}else if (objValue.type=="modal_hide"){
				 		if (/*$('#AMBIENTE').val()=='backend' */$('#myModal').html() ){
					 		
				 			if ($('#myModal2').hasClass('in')){
				 				$('#myModal2').modal('hide');
				 			}
				 			else{
				 				$('#myModal').modal('hide');
				 			}
				 			
				 			
				 		}
				 		else{
				 			$("#jqEdit").jqm().jqmHide();
				 		}
				 	}else if (objValue.type=="remove_process"){
				 	 	$(".button").removeClass('in_process');
				 	 	$("button").removeClass('in_process');
				 	}

			});
			
			load_data_modules();
			}
		}
		
	});
	

	return valid;
}





function ajax_default(){
	$("#url_params").val('');
	call_bak('method_default');
}

function ajax_operation(param) {
	if (param=='comentarios' || param=='ancla_clasificados') return;
	
	$('#method').val('');
	v_param=param.split("/");
	if(v_param[0]!="0"){
		$("#url_params").val(param);
		//CFF Change
		//call_bak(v_param[0]);
		call_bak("call_bak");
	}
	//eval(v_param[0]+"_bak(v_param);");
}


function CatalogListView_bak(v_param) {
	$("#class").val("ModCatalogs");
	$("#method").val(v_param[0]);
	$("#id").val(v_param[1]);
	$("#Catalog_CatalogId").html(v_param[1]);
	$("#ProductId").val("");
	$("#p").val(v_param[2]);
	$("#filters").val(v_param[3]);
	$("#property_orden").val(v_param[4]);
	$('#sub_menu_'+$("#id").val()).addClass("active");

	call_bak($("#method").val());
}

function ProductEditView_bak(v_param) {
	$("#class").val("ModCatalogs");
	$("#method").val(v_param[0]);
	$("#id").val(v_param[1]);
	$("#ProductId").val(v_param[2]);
	call_bak($("#method").val());
}

function ProductGoView_bak(v_param) {
	$("#class").val("ModCatalogs");
	$("#method").val(v_param[0]);
	$("#id").val(v_param[1]);
	$("#ProductId").val(v_param[2]);
	call_bak($("#method").val());
}


function scollToId(id){
   window.scrollTo(0,$("#"+id).offset().top);
}

function scollTo(id){
	   window.scrollTo(0,$(id).offset().top);
}

function local_reload(){
	location.reload(); 

}