// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**********
 * add code
 **********/

function addCodeToFunction(func, code) {
  if(func == undefined) {
    return code;
  } else {
    return function(){
      func();
      code();
    }
  }
}

function addCodeToOnload(code) {
  window.onload = addCodeToFunction(window.onload, code);
}

/**********
 * helping text field
 **********/

function initializeHelpingTextField(field, help) {
  field = $(field);
  form = field.up('form');
  field.onfocus = addCodeToFunction(field.onfocus, function() {
    if (field.value == help)
      field.value = '';
  });
  field.onblur = addCodeToFunction(field.onblur, function() {
    if (field.value == '')
      field.value = help;
  });
  form.onsubmit = addCodeToFunction(form.onsubmit, function() {
    field.onfocus();
  });
  field.onblur();
}

/**********
 * multiselect
 **********/

function showHideMultiselect(id) {
  var root = $(id);
  var trigger = root.down('div.multiselect_trigger');
  var list = root.down('div.multiselect_list');
  trigger.removeClassName(list.visible() ? 'opened' : 'closed');
  trigger.addClassName(list.visible() ? 'closed' : 'opened');
  Effect.toggle(list, 'blind');
}

function updateMultiselectSelected(id) {
  var root = $(id);
  var checkboxes = root.down('div.multiselect_list').getElementsBySelector('input');
  var selected = 0;
  for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].checked)
      selected++;
  }
  root.down('span.selected').update(selected == 0 ? '' : '(' + selected + ' ausgewählt)');
}

function initializeMultiselect(id) {
  addCodeToOnload(function() {
    updateMultiselectSelected(id)
  });
}

/**********
 * color picker
 **********/

/* some color picker functions by Horde <http://www.horde.org/> */
function getColor(e, img){
  if (typeof e == 'undefined') {
    var e = window.event;
  }
  var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
  var xTemp = safari ? 0 : getCurrentPosition()[0]
  var yTemp = safari ? 0 : getCurrentPosition()[1];
  var x = e.clientX - (findPos(img)[0] - yTemp)
  var y = e.clientY - (findPos(img)[1] - xTemp);

  // adjust for gray border
  x = Math.max(0, Math.min(100, x - 3));
  y = Math.max(0, Math.min(192, y - 3));

  var rmax = 0, gmax = 0, bmax = 0;
  if (y <= 32) {
    rmax = 255;
    gmax = y / 32 * 255;
    bmax = 0;
  }
  else
    if (y <= 64) {
      y = y - 32;
      rmax = 255 - y / 32 * 255;
      gmax = 255;
      bmax = 0;
    }
    else
      if (y <= 96) {
        y = y - 64;
        rmax = 0;
        gmax = 255;
        bmax = y / 32 * 255;
      }
      else
        if (y <= 128) {
          y = y - 96;
          rmax = 0;
          gmax = 255 - y / 32 * 255;
          bmax = 255;
        }
        else
          if (y <= 160) {
            y = y - 128;
            rmax = y / 32 * 255;
            gmax = 0;
            bmax = 255;
          }
          else {
            y = y - 160;
            rmax = 255;
            gmax = 0;
            bmax = 255 - y / 32 * 255;
          }
  var r, g, b;
  if (x <= 50) {
    r = Math.abs(Math.floor(rmax * x / 50));
    g = Math.abs(Math.floor(gmax * x / 50));
    b = Math.abs(Math.floor(bmax * x / 50));
  }
  else {
    x -= 50;
    r = Math.abs(Math.floor(rmax + x / 50 * (255 - rmax)));
    g = Math.abs(Math.floor(gmax + x / 50 * (255 - gmax)));
    b = Math.abs(Math.floor(bmax + x / 50 * (255 - bmax)));
  }
  return rgb2hex(r, g, b);
}

function rgb2hex(r, g, b) {
  color = '#';
  color += Math.floor(r / 16).toString(16);
  color += (r % 16).toString(16);
  color += Math.floor(g / 16).toString(16);
  color += (g % 16).toString(16);
  color += Math.floor(b / 16).toString(16);
  color += (b % 16).toString(16);
  return color.toUpperCase();
}

/* find position function by PPK <http://www.quirksmode.org/> */
function findPos(obj) {
  var x = 0, y = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      x += obj.offsetLeft;
      y += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    x += obj.x;
    y += obj.y;
  }
  return new Array(x,y);
}

function getCurrentPosition() {
  var x = 0, y = 0;
  if (document.body && document.body.scrollTop) {
    x = document.body.scrollTop;
    y = document.body.scrollLeft;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    x = document.documentElement.scrollTop;
    y = document.documentElement.scrollLeft;
  } else if (window.pageYOffset) {
    x = window.pageYOffset;
    y = window.pageXOffset;
  }
  return new Array(x, y);
}

/*********
 * CMS
 *********/

function updateCmsSortLevel1(container){
  updateCms(container, '#' + container.id + ' li.cms_level_1');
}

function updateCmsSortLevel2(container) {
  updateCms(container, '#' + container.id + ' li.cms_level_2');
}

function updateCms(container, selector) {
  var child_ids = $$(selector).collect(function(a) {
    return a.id.substring(8);
  }).join(',');
  new Ajax.Request('/pages/' + container.id.substring(16) + '/order?child_ids=' + child_ids, {method:'post',evalScripts:true});
}

/* new school roole */

var sorterImagesSwitcher = function(dropdown, element) {
  if ($(dropdown)) {
    $(dropdown).observe('change', function(e) {
      var image = $(dropdown).value
      if (image == 'off') {
        $(element).hide();
      } else {
        $(element).show();
        if (element.match(/logo/)) {
          $(element).src = '/images/advertisement-order-logo-' + image + '.gif';
        }
      }
      return false;
    });
  }
};

Event.observe(window, 'load', function(){
  sorterImagesSwitcher('advertisement_logo_position', 'order_logo');
  sorterImagesSwitcher('advertisement_image_position', 'order_image');
});

