/****** preloaders *******/

function showLoading(id) {
	$('#'+id).html('<div style="width:100%; text-align:center;"><img src="/images/loader.gif" border="0" />');
}

function hideLoading(id) {
	$('#'+id).html('');
}

function loading(message) {

	_message = '<div style="width:100%; text-align:center; position: relative; z-index: 100;"><br /><br /><br /><img src="/images/loader2.gif" border="0" />';

	if( message != '' && message )
		_message = _message+'&nbsp;'+message;
	
	_message = _message + "</div>";
	
	//alert($('.ui-dialog-titlebar-close'));

		$('#loading').html(_message);
		$('#loading').dialog('destroy');
		$('#loading').dialog({ 
			bgiframe: true,
	    modal: true,
	    resizable: false,
	    draggable: false,
	    width: 300,
		  overlay: { 
		   	opacity: 0.5, 
				background: 'black' 
		  },
		  closeOnEscape: false,
   		open: function(event, ui) {
	   		$(".ui-dialog:has(div[id='loading'])").addClass('ui-widget-content-transparent');
	   		$(".ui-widget-content-transparent .ui-dialog-titlebar").hide();
   		},
		  resizable: false
		});
		//$('.ui-dialog-titlebar-close').hide();

/*
	$('<div id="loadingLayer"><img src="/images/loader.gif" border="0" style="z-index:9999" /></div>').appendTo(document.body)
			.addClass('ui-widget-overlay').css({
				width: $(this).width(),
				height: $(this).height()
			}).css('z-index',9998);
	*/		
}

function closeLoading(_parent) {
	//$('#loadingLayer').remove();
	if(_parent) {
		window.parent.$('#loading').dialog('close');
	}
	else
		$('#loading').dialog('close');
}

/****** messages ******/
function messageSuccess(message, reloadTop) {

	closeLoading(); 

	_message = '<div style="width:100%; text-align:center; padding-top:15px; padding-bottom:15px;">';

	if( message != '' )
		_message = _message+'&nbsp;'+message;
	
	_message = _message + "</div>";

	$('#dialog').html(_message);
	$('#dialog').dialog('destroy');
		$('#dialog').dialog({
			bgiframe: true,
		  modal: true,
		  resizable: false,
		  draggable: false,
		  width: 300,
		  overlay: { 
		  	opacity: 0.5, 
		  	background: 'black' 
		  }
		  , close: function(event, ui) {
		  	if( reloadTop ) {
		  		loading();
		  		top.location.reload();
		  	}		  		
		  }
		});
}

function messageSuccessReloadByFunc(message, func, _loading) {

	closeLoading(); 

	_message = '<div style="width:100%; text-align:center; padding-top:15px; padding-bottom:15px;">';

	if( message != '' )
		_message = _message+'&nbsp;'+message;
	
	_message = _message + "</div>";

	$('#dialog').html(_message);
	$('#dialog').dialog('destroy');
		$('#dialog').dialog({
			bgiframe: true,
		  modal: true,
		  resizable: false,
		  draggable: false,
		  width: 300,
		  overlay: { 
		  	opacity: 0.5, 
		  	background: 'black' 
		  }
		  , close: function(event, ui) {
		  	if( func ) {
		  		if(_loading) loading();
		  		eval(func);
		  	}
		  }
		});
}

function messageError(message, width, height, funcOnClose) {
	closeLoading(); 
	$('#dialog').html('<div style="text-align:center; padding-top:15px; padding-bottom:15px;">'+message+'</div>');
	$('#dialog').dialog('destroy');
		$('#dialog').dialog({ 
				bgiframe: true,
		    modal: true,
		    resizable: false,
		    draggable: false,
		    width: width?width:500,
		    overlay: { 
		        opacity: 0.5, 
		        background: 'black' 
		    }
		    , close: function(event, ui) {
			  	if( funcOnClose ) {
			  		eval(funcOnClose);
			  	}
		  	}
		});	
}

function messageErrorParent(message) {
	parent.$('#dialog').html(message);
	//parent.$('#dialog').dialog('open');
	
	parent.$('#dialog').dialog('destroy');
		parent.$('#dialog').dialog({ 
		    modal: true,
		    bgiframe: true,
		    overlay: { 
		        opacity: 0.5, 
		        background: 'black' 
		    } 
		});
	
}


/*******************************/

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

function reloadCaptcha(id,url) {
	$('#'+id).attr('src',url+'?id='+Math.round(Math.random(0)*1000)+1);
}

function ajaxAction(_url,_data, respId, noLoading) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() { if(!noLoading)loading(); },
   complete: function () { if(!noLoading)closeLoading(); },
   success: function(resp){
   	$('#'+respId).html(resp.data);
   	$('#'+respId).show();
   }
	});
}


function ajaxActionMessage(_url,_data, reload) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() {loading()},
   complete: function () { closeLoading(); },
   success: function(resp){
   	if( resp.re == 1 ) {
   		if(reload == 'top')
				messageSuccess(resp.message,1);
			else
				messageSuccess(resp.message); 
		}
		else {
			messageError(resp.message); 
	  }
   }
	});
}

function ajaxActionMessageReloadByFunc(_url,_data, func) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() {loading()},
   complete: function () { closeLoading(); },
   success: function(resp){
   	if( resp.re == 1 ) {
			messageSuccessReloadByFunc(resp.message, func);
		}
		else {
			messageError(resp.message); 
	  }
   }
	});
}


function layerBoxAction(_url,_data, width, height) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() {loading()},
   complete: function () { closeLoading(); },
   success: function(resp){
     layerBox(resp.data, width, height);
   }
	});
}

function layerBox(content, width, height) {
	width = parseInt(width);
	//height = parseInt(height);
	$('#layerBox').html(content);
		$('#layerBox').dialog('destroy');
		$('#layerBox').dialog({
				bgiframe: true,
		    modal: true,
		    resizable: false,
		    draggable: false,
		    width: width?width:500,
		    overlay: {
		        opacity: 0.5, 
		        background: 'black' 
		    }		    
		});

}


function messageErrorTimeout(message, redirectUrl) {

	if( !message )
		message = "Tylko dla zalogowanych.";

	if( !redirectUrl )
		redirectUrl = "/logowanie";
	
	alert(message);
	//$('#dialog').html('<div style="text-align:center;">'+message+'</div><br /><div style="text-align:center;"><a href="'+redirectUrl+'">Zamknij</a></div><br />');
}


function setHomepageTab(_url, _data, respId, _index) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() {loading()},
   complete: function () { closeLoading(); },
   success: function(resp){
   	$('#'+respId+'Result').html(resp.data).show();
   	$('#'+respId+' > li').each( function(i) {
   		$(this).removeClass('active');
   	});
   	
   	$('#'+respId+' > li:eq('+_index+')').addClass('active');
   }
	});
}

function setMainMenuTab(mainMenuId, _index, categoryType) {

	$('.menu_top_dol').each( function(i) {
  	$(this).hide();
  });
  
  $('#menu_top_gora > li').each( function(i) {
  	$(this).removeClass('active');
  });
  
  $('#subMenu'+mainMenuId).show();
  
  if(_index)
  	$('#menu_top_gora > li:eq('+_index+')').addClass('active');
  else
  	$('#mainMenuTab'+mainMenuId).addClass('active');
  	
  if( categoryType == 'recipes' ) {
  	$('#alfabet').show();
  }
  else
  	$('#alfabet').hide();
}

function setSubMenuTab(obj) {
	$('.menu_top_dol > li').each( function(i) {
  	$(this).removeClass('active');
  });
  
  $(obj).addClass('active');
}

function unsetSubMenuTab(obj) {
  $(obj).removeClass('active');
}


/*
function setMainMenuTab(_url, _data, respId, _index) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401)
    	messageErrorTimeout();
   },
   beforeSend: function() {},
   complete: function () {},
   success: function(resp){
   	$('#'+respId+'_dol').html(resp.data).show();
   	$('#'+respId+'_gora > li').each( function(i) {
   		$(this).removeClass('active');
   	});
   	
   	$('#'+respId+'_gora > li:eq('+_index+')').addClass('active');
   }
	});
}*/

function ajaxActionMessageInline(_url,_data, respId, onSuccess, onError) {
	$.ajax({
   type: "POST",
   dataType: "json",
   url: _url,
   data: _data,
   error: function (XMLHttpRequest, textStatus, errorThrown) {
		if (XMLHttpRequest.status == 401 || XMLHttpRequest.status == 403)
    	messageErrorTimeout();
   },
   beforeSend: function() {loading()},
   complete: function () { closeLoading(); },
   success: function(resp){
   	if( onSuccess && resp.re == 1 ) {
   		eval(onSuccess);
   	}
   	if( onError && resp.re == 0 )
   		eval(onError);
   	$('#'+respId).html(resp.message);
   }
	});
}
