$(function() {

   /*$(window).resize(function() {
      updateScrollBar();
   });*/

   //helpMsg();

   // Remove focus from links
   $('a').click(function() {
      this.blur();
   });

});

function helpMsg() {
   // Show help messages from title attributes if helpmsg is defined
   if ($(".helpmsg").length) {
      $("[title]").bind("mouseenter mouseleave", function(e) {
         $(".helpmsg").stop(true, true);
         if (e.type == "mouseenter") {
            var title = $(this).attr("title");
            $(".helpmsg").text(title);
            $(".helpmsg").show();
         } else if(e.type == "mouseleave"){
            $(".helpmsg").hide();
         }
      });
   }
}


function updateScrollBar() {
   /* $(".scroll-pane").jScrollPane(
    {
        showArrows: true,
        maintainPosition: true,
        wheelSpeed: 80,
        animateTo: false
    }).scroll(function() { View.hasScrolled(); });*/
}


var loader = "<div class=\"loader\"><p><img src=\""+Resource.BASE_URL+"Views/Images/loader_white.gif\"></p></div>";

function popupAjaxSubmit(form, url) {
    $("#dialog").html(loader);
    var params = {};
    $(form).find("input, textarea, select option:selected")
	     .each(function() {
           var key = this.name || this.parentNode.name;
           if (this.type == 'checkbox') {
              this.value = this.checked;
           }
	        if (params[key] == undefined) {
	           params[key] = this.value;
	        } else {
	           params[key] = params[key] + ',' + this.value;
	        }
	     });
	var url = (url==undefined) ? (form).attr("action") : url;
	popupAjaxPost(url, params, "POST");
}

function popupAjaxPost(url, data, type) {
   $.ajax({
      url: url,
      dataType: "html",
      type: type,
      data: data,
      success: function(data) {
         $("#dialog").html(data);
         if ($(".ajaxSubmit").length > 0) {
            $("#dialog form").bind("submit", function() {
               popupAjaxSubmit($(this),undefined);
               return false;
            });
            $("#dialog form a.ajaxSubmit").bind("click", function() {
               var form = $("#dialog form");               
               popupAjaxSubmit(form, $(this).attr("href"));
               return false;
            });
         }
         $(".cancelBtn").click(function() {
            $("#dialog").dialog('close');
         });
      },
      error: function(data) {
         Debug.debug("error loading page..");
         $("#dialog").html("Error loading page..");
      }
   });
}

function showPopup(selector) {
    if (!$("#dialog").length || !$("#faded_bg").length) {
        $(selector).blur();
        var dialog = "<div id=\"dialog\" title=\"" + $(selector).attr("title") + "\">" + loader + "</div>";
        $(document.body).append("<div id=\"faded_bg\"></div>" + dialog);
        $("#faded_bg").css({ opacity: 0.8, display: "none" }).fadeIn(400);

        $("#dialog").dialog({
            autoOpen: true,
            bgiframe: false,
            resizable:false,
            //show: 'scale',
            //hide: 'scale',
            //minWidth: 252,
            //minHeight: 300,
            width: 650,
            height: 450,
            position: 'center',
            close: function() {
                hidePopup(selector);
            }
        });
        var url = $(selector).attr("href");

        popupAjaxPost(url, {}, "GET");

    }
    return false;
}



function hidePopup(selector) {
    $(selector).blur();
    $("#faded_bg").fadeOut(400, function() {
        $(this).remove();
    });
    $("#dialog").remove();

    return false;
}

function changeLang(lang) {
    jQuery.cookie('lang', lang);
    window.location.reload();
    return false;
}

function showConfirmation(link, selector) {
  if (!$("#dialog").length) {
     $(link).blur();
     var dialog = "<div id=\"dialog\" title=\"" + $(selector + " .title").text() + "\">" + $(selector + " .body").text() + "</div>";
     $(document.body).append(dialog);

     $("#dialog").dialog({
         autoOpen: true,
         bgiframe: false,
         minWidth: 252,
         //minHeight: 300,
         modal: true,
         buttons: {
		      "No": function() { 
			      $("#dialog").remove();
		      },
		      "Yes": function() {
		         window.location.href = $(link).attr("href");
		      }
	      },
         close: function() {
              $("#dialog").remove();
         }
     });
  }
  return false;
}

