/**
 * Fill in the maintain ports with given engine ports
 */
Drupal.behaviors.veFillMaintainPorts = function (context){
  $('#engine_ports input:not(.fill-maintain-ports)', context)
  .blur(function (){
      var engine_ports = $('#engine_ports input').val();
      $('#maintain_ports input').val(engine_ports);
  })
  .addClass('fill-maintain-ports');
}

/**
 * Update the maintenance ports when selection for the licenses change
 */
Drupal.behaviors.veUpdateMaintainReferenceNo = function (context){
  $('#available_lic select:not(.veAvailLicSelect-processed)', context)
    .change(function() {
        ve_license_select(this.id);
    })
    .addClass('veAvailLicSelect-processed');
}

/**
 * Fill in the maintain ports and number of ports with given 5 pork pack
 */
Drupal.behaviors.veFillWith5PortPack = function (context){
  $('#five_port_pack input:not(.fill-with-five-port-pack)', context)
  .blur(function (){
      var num_five_port_pack = $('#five_port_pack input').val();
      $('#engine_ports input').val(num_five_port_pack*5);
      $('#maintain_ports input').val(num_five_port_pack*5);
  })
  .addClass('fill-with-five-port-pack');
}

/**
 * Handle the license selection event
 */
function ve_license_select(license_select){
    var options = { 'license_id' : $('#' + license_select).val() };
    $.post(Drupal.settings.basePath + '?q=ve_js_util/license_select', options,
         function (contents) {
             //-Parsing the return string
             var splStr = contents.split('&');
             var num_ports = splStr[0];
             var os = splStr[1];
             var arch = splStr[2];
             if(num_ports == '0'){
                 $('#maintain_block_display').empty();
                 $('#maintain_ports input').val('0');
                 $('#reference_no input').val('0');
             }else{
                 var lic_info = '<br />Engine ports: ' + num_ports + '<br />' +
                    'Operating system: ' + os + '<br />' +
                    'Architecture: ' + arch + '<br />';
                 $('#maintain_block_display').empty().append(lic_info);
                 $('#maintain_ports input').val(num_ports);
                 $('#reference_no input').val($('#' + license_select).val());
             }
    });
}

/**
 * Update the host list with given user
 */
Drupal.behaviors.veUpdateUser = function (context){
  $('#user select:not(.veUserSelect-processed)', context)
    .change(function() {
        ve_user_order_select(this.id);
        ve_user_host_select(this.id);
    })
    .addClass('veUserSelect-processed');
}

/**
 * Handle the user selection event
 */
function ve_user_order_select(user_select){
    var options = { 'uid' : $('#' + user_select).val() };
    $.post(Drupal.settings.basePath + '?q=ve_js_util/user_order_select', options,
         function (contents) {
             $('#order select').empty().append(contents).change();
             //alert($('#host select').parent().html());
    });
}

/**
 * Handle the user selection event
 */
function ve_user_host_select(user_select){
    var options = { 'uid' : $('#' + user_select).val() };
    $.post(Drupal.settings.basePath + '?q=ve_js_util/user_host_select', options,
         function (contents) {
             $('#host select').empty().append(contents).change();
             //alert($('#host select').parent().html());
    });
}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popupHost").fadeIn("fast");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("fast");
        $("#popupHost").fadeOut("fast");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupHost").height();
    var popupWidth = $("#popupHost").width();
    //centering
    $("#popupHost").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
        });
        //only need force for IE6
        $("#backgroundPopup").css({
        "height": windowHeight
    });
}

//CLOSING POPUP
//Click the CLOSE event!
$("#popupHostClose").click(function(){
    disablePopup();
});
/*Click out event!
$("#backgroundPopup").click(function(){
    disablePopup();
});
*/
