/**
 * Code to execute once the document is loaded
 */
function EkJs_onLoaded(func) {
  $(document).ready(func);
}

/**
 * String manipulation
 */

function EkJs_trim(str) {
  return jQuery.trim(str);
}

/**
 * Enable/disable the loading div
 */
function EkJs_doLoading() {
  var wpos = { left: 0, top: 0, width: $("body").width(), height: $("body").height() };
  var lpos = { left: 0, top: 0, width: $(window).width(), height: $(window).height() };

  // Add a div for the reloading
  if ($("#loading").length == 0)
    $("body").prepend('<div id="loading"><div class="wrapper"></div><div class="loader"></div></div>');
  $("#loading div.loader").css({ left: lpos.left, top: lpos.top, width: lpos.width, height: lpos.height }).show();
  $("#loading div.wrapper").css({ left: wpos.left, top: wpos.top, width: wpos.width, height: wpos.height }).show();

  return true;
}

/**
 * Popuping up an alert message
 *
 * @param string msg    message to display
 *
 * @return false
 */
function EkJs_alert(msg) {
  alert(msg);
  return false;
}

/**
 * Popuping up a confirm message and let the end-user accept or
 * reject the action
 *
 * @param string msg    message to display
 *
 * @return bool
 */
function EkJs_confirm(msg) {
  return confirm(msg);
}

/**
 * Over/out Event Management
 */
var Element_SwapClass = {

  classes: new Array(),

  set: function(object, classname) {
    if (!object)
      return;
    this.classes[object] = object.className;
    object.className = classname ? classname : 'over';
  },

  restore: function(object) {
    if (!object)
      return;
    object.className = this.classes[object];
  },

  override: function(object, name) {
    object.className = this.classes[object] = name;
  }
};

/**
 * Popup Window
 */

var _TOKEN_BLOCKED_WINDOW = "Your browser is blocking popup window. Disable it in order to proceed";

function EkJs_openWindow(url, params) {

    if (!params) params = { };
    if (!params['width'])  params['width']  = '640px';
    if (!params['height']) params['height'] = '480px';
    params['initialWidth']  = params['width'];
    params['initialHeight'] = params['height'];
    params['fixed']         = true;
    params['href']          = url;
    params['iframe']        = true;
    $.colorbox(params);
}

/**
 * Manage product attributes supported in the product home
 * page
 */
function EkJs_ProductAttributes(element_id, combinations) {
  this.element_id   = element_id;
  this.combinations =  [ ];
  for (var i = 0; i < combinations.length; i++)
      this.combinations[i] = { id: combinations[i].id, value: this.sort(combinations[i].value).join('*') };
}

EkJs_ProductAttributes.prototype = {

  sort: function(a) {
    return a.sort(function(a,b){ return a - b });
  },

  onChange: function(element) {
    // Get the combination value selected
    var v = new Array();
    for (var i = 0; i < element.form.length; i++) {
      var item = element.form.elements[i];
      if (item.name != element.name)
	continue;
      var v1 = typeof(item.options) == "undefined" ? item.value : item.options[item.selectedIndex].value;
      if (parseInt(v1) > 0)
	v[v.length] = parseInt(v1);
    }
    v = this.sort(v).join('*');

    // Try to find in the list of supported combinations
    var id = -1;
    for (var i = 0; i < this.combinations.length; i++)
	if (this.combinations[i].value == v) {
	    id = this.combinations[i].id;
	break;
      }

    // Get current selected id
    if (id == -1) {
      hideLayer(this.element_id + '_on');
      showLayer(this.element_id + '_off');
    } else if (id == element.form.elements['pvID'].value) {
	// This is the current product: no need to reload
      showLayer(this.element_id + '_on');
      hideLayer(this.element_id + '_off');
    } else {
      // Change the combination and reload it
      element.form.elements['pvID'].value = id;
      element.form.elements['action'].value = '';
      EkJs_doLoading();
      element.form.submit();
    }
  }
};

/**
 * Layer/Div Management
 *
 * This section defines functions to hide or show layers.
 * It also provide function to check whether a layer is visible
 * or not.
 */

function showLayer(id) { 
  $("#" + id).show();
} 

function hideLayer(id) {
  $("#" + id).hide();
} 

function EkJs_UseAddress(fid, aid, action) {
  // Find the form
  $('#' + fid + ' :input[name=new_id]').val(aid);
  $('#' + fid + ' :input[name=action]').val(action);
  $('#' + fid).submit();
}

function EkJs_OnCompanyChange(element, elt1, elt2) {
  // Find the id
  if ($(element).val().trim() == '') {
    $('#' + elt1).slideUp();
    $('#' + elt2).slideUp();
  } else {
    $('#' + elt1).slideDown();
    $('#' + elt2).slideDown();
  }
}

