jQuery.noConflict();
var $j = jQuery;

$j(document).ready(function () {
  $j('.fields_login').focus(function() {$j(this).val("");});
  $j("#notlogged #password").keypress(function(e){
      if(e.which == 13){
    	  loginUser();
          }
         });
  $j("#searchfield").keypress(function(e){
      if(e.which == 13){
    	  SendSearch();
          }
         });

});


$j(function() {

	$j("#sortable1, #sortable2").sortable({
		connectWith: '.connectedSortable',items: 'li:not(.movedisabled)',update: savelayout
	}).disableSelection();

	$j(".portlet_header").click(function() {
		$j(this).toggleClass("minusthick").toggleClass("plusthick");
		$j(this).parents(".portlet:first").find(".portlet_content").toggle();
		savelayout();
	});
	
	$j(".portlet_header").hover(function() {
		 $j(this).css('cursor','move');
		 }, function() {
		 $j(this).css('cursor','auto');
		});


	
});

function collapse(portlet)
{
		$j("#"+portlet+"portlet .portlet_header").toggleClass("minusthick").toggleClass("plusthick");
		$j("#"+portlet+"portlet .portlet_header").parents(".portlet:first").find(".portlet_content").toggle();		
	
}

function Arrange(left,right,lefthidden,righthidden)
{
var result;
var handle = {async: false,callback: function(reply) {result = reply;}}
PortletsDao.Arrange(left,right,lefthidden,righthidden,handle);
return result;
}

function savelayout()
{
	var leftpositions = "";		
	$j("#sortable1 .portlet").each(function(){leftpositions+=(this.id + "-");});		
	var rightpositions="";	
	$j("#sortable2 .portlet").each(function(){rightpositions+=(this.id + "-");});	
	var lefthidden="";
	$j("#sortable1 .portlet .portlet_content").filter(":hidden").each(function(){lefthidden+=(this.id + "|");});
	var righthidden="";
	$j("#sortable2 .portlet .portlet_content").filter(":hidden").each(function(){righthidden+=(this.id + "|");});
	Arrange(leftpositions,rightpositions,lefthidden,righthidden);
}

// customize validate jquery plugin
(function($) {
    $.extend($.fn, {
        // http://docs.jquery.com/Plugins/Validation/validate
        validate_ew: function( options ) {
            options = options || {};
            options.highlight = function( element, errorClass, validClass ) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element).parent('span.ckwrap').addClass(errorClass).removeClass(validClass);
            };
            options.unhighlight = function( element, errorClass, validClass ) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element).parent('span.ckwrap').removeClass(errorClass).addClass(validClass);
            };

            var validateObj = $(this).validate(options);
            if (!validateObj)
                return validateObj;

            var oldCheckFn = validateObj.checkForm;
            validateObj.checkForm = function() {
                var ret = oldCheckFn.call(this);
                if (this.errorList.length>0) {
                    setTimeout(function() {
                        alert("Παρακαλώ συμπληρώστε όλα τα απαιτούμενα πεδία");
                    }, 20);
                }
                return ret;
            };
            return validateObj;
        }
    });
})(jQuery);

// customize tooltips
(function($) {
    $.extend($.fn, {
        bt_kots: function() {
            $(this).bt({ fill: 'rgba(25, 50, 100, .9)',
                strokeWidth: 0, /*no stroke*/
                cssStyles: {color: 'white', fontWeight: 'normal'},
                positions: ['right', 'bottom']});
        }
    });
})(jQuery);

// extend validator for afm
(function($) {
if ($.validator) $.validator.addMethod("afm", function(val, node, params) {
    if (params) {
        // if precondition not met, mark all as ok
        if ($(params).size()==0)
            return true;
    }
    return checkAFM(val);
}, "Λανθασμένο ΑΦΜ");
})(jQuery);




