/**
 * object.lib.js - insert headline here
 *
 * COPYRIGHT: All  title   and  proprietary  rights,  including  trade
 * secrets,   in   the   Software   and   any   copies thereof and the
 * accompanying  written   materials,   are  owned  by   schukai  GmbH
 * and  are  protected  by  German  copyright  laws,  other applicable
 * copyright   laws  and  international  treaty  provisions.
 *
 * @package    alvine
 * @author     schukai GmbH <info@schukai.de>
 * @copyright  Copyright (C) 2002, 2003, 2004, 2005, 2006 schukai GmbH
 * @license    http://www.alvine.de/license/
 * @version    20061114
 * @link       http://www.alvine.de/
 */

if(typeof alvine == 'undefined') alvine = new Object();
alvine_object_createObjectPath(alvine, 'data.move');

function object_initLibs(ev){
  if(!ev) ev = window.event;

  //init moving  
  if(typeof alvine.data.move.object == 'undefined') alvine.data.move.object = null;

  event_add(document, 'mousemove', object_moveObject);
  event_add(document, 'mouseup',   object_toggleMoving);

  //..
  
  return true;
}
 
function object_count(obj){
  
  if(typeof obj != 'object') return 0;
  
  var cnt = 0;
  for(var i in obj){
    cnt++;
  }
  
  return cnt;
}


function alvine_object_addObserver(srcID, targetID, callBackFunction){
  
  var sourceObj = element_isObject(srcID);
  var thisObj = element_isObject(targetID);
    
  if(!sourceObj) return null;
  if(!thisObj) return null;
  
  callBackFunction = (typeof callBackFunction == 'undefined')?'alvine_handleEvent':callBackFunction;
  
  if(typeof thisObj['alvine_observers'] == 'undefined') thisObj['alvine_observers'] = new Object();
  if(typeof thisObj['alvine_observers'][callBackFunction] == 'undefined') thisObj['alvine_observers'][callBackFunction] = new Array();
  
  //Observer Eintrag bauen / hinzufügen
  var newObserver = new Object();
  newObserver.id = srcID;
  
  thisObj['alvine_observers'][callBackFunction].push(newObserver);
  
  
  //Notify Observer Funktion hinzufügen
  if(typeof thisObj.alvine_notifyObserver == 'undefined'){
    thisObj['alvine_notifyObserver'] = function(observerKey, data){
      
      observerKey = (typeof observerKey == 'undefined')?'alvine_handleEvent':observerKey;
      
      if(typeof this.alvine_observers[observerKey] == 'undefined') return false;
      
      var myObservers = this.alvine_observers[observerKey];
      
      if(myObservers.length==0) return false;
      
      if(typeof data == 'undefined') data = new Object();
      
      for(var i in myObservers){
        var entry = myObservers[i];
        var oObj = element_isObject(entry.id);

        oObj[observerKey](data, this);
      }
    
      return true;
    };
  }
  
  
  //wenn die CallBackFunktion nicht vorhanden ist >> anlegen
  if(typeof sourceObj[callBackFunction] == 'undefined'){
    sourceObj[callBackFunction] = function(data, srcObj){
      
      
    
      return true;
    };
  }
  
  return true;

}


function alvine_object_hasObservers(obj){
  obj = element_isObject(obj);
  
  if(typeof obj.alvine_notifyObserver == 'undefined') return false;
  if(typeof obj.alvine_observers == 'undefined') return false;
  
  return true;
}

function alvine_object_createObjectPath(initObject, pathString){
  
  if(!element_isObject(initObject)) initObject = document;

  var pathParts = pathString.split('.');
  var newObjName = pathParts.shift();
  
  if(!element_isObject(initObject[newObjName])) initObject[newObjName] = new Object();
  
  if(pathParts.length > 0) alvine_object_createObjectPath(initObject[newObjName], pathParts.join('.'));

  return true;
}

function element_buildUniqueObjectID(object){
  var obj = element_isObject(object);
  
  var id = null;
  if(!obj){
    id = (!isNull(object))?object:'generic';
  } else {
    if(isDefined(obj.id) && obj.id!='') {
      id = obj.id;
    } else if(isDefined(obj.tagName)){
      id = obj.tagName.toLowerCase();
    } else if(isDefined(obj.nodeName)){
      id = obj.nodeName.toLowerCase();
    } else if(isDefined(obj.localName)){
      id = obj.localName.toLowerCase();
    }
  }

  var now = new Date();
  var newID = id + '_' + now.getTime() + now.getMilliseconds() + js_rand(0000, 9999);
  
  alvine_object_createObjectPath(alvine, 'session.generatedObjectIDs');

  if(!isDefined(alvine.session.generatedObjectIDs[newID])){
    if(obj && !obj.id) {
      obj.id = newID;
    }
    alvine.session.generatedObjectIDs[newID]  = newID;
  } else {
    newID = element_buildUniqueObjectID(obj);
  }
  return newID;
}

function alvine_getViewport(){
  
  var w = null;
  var h = null;
    
  if(typeof window.innerWidth != 'undefined'){
    w = window.innerWidth;
    h = window.innerHeight;

    return {width:w, height:h};
  }

  if(typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth != 'undefined' 
     && document.documentElement.clientWidth != 0){
    w = document.documentElement.clientWidth;
    h = document.documentElement.clientHeight;

    return {width:w, height:h};
  }

  if(typeof document.getElementsByTagName('body')[0].clientWidth != 'undefined'){
    w = document.getElementsByTagName('body')[0].clientWidth;
    h = document.getElementsByTagName('body')[0].clientHeight;

    return {width:w, height:h};
  }

  return false;
}

function alvine_object_selectAddOption(selectObj, newString, newValue, selected){
  selectObj = element_isObject(selectObj);
  
  var newOption = document.createElement("OPTION");
  newOption.text     = newString;  
  newOption.value    = newValue;
  newOption.selected = (!isDefined(selected) || selected == null)?'':true;
  
  try {
    selectObj.add(newOption, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    selectObj.add(newOption); // IE only
  }
  
  return true;
}

function alvine_object_selectRemoveOption(selectObj, delValue){
  selectObj = element_isObject(selectObj);
  
  for(var i in selectObj.options){
    var tmpOption = selectObj.options[i];    
    if(tmpOption == null) continue;
    if(!isDefined(tmpOption.value)) continue;
    if(tmpOption.value != delValue) continue;
    
    selectObj.remove(i);
    return true;
  }
  
  return false;
}

function alvine_array_keys(mixed){
  var ret = new Array();
    
  for(var key in mixed) {
    ret.push(key);
  }
  return ret;
}

function alvine_bubble_kSort(mixed, direction){
  
  if(typeof mixed != 'object') return mixed;       
  direction = !direction?'regular':direction;
    
  var x, y, holder;
  var mixedKeys   = alvine_array_keys(mixed);  
  var cKey, nKey;
  for(x = 0; x < mixedKeys.length; x++) {    
    for(y = 0; y < (mixedKeys.length-1); y++) {
      cKey = mixedKeys[y];
      nKey = mixedKeys[y+1];
      var proceed = false;
      
      if(direction=='regular'){
        if(parseInt(cKey) > parseInt(nKey)) proceed = true;
      } else if(direction=='reverse'){
        if(parseInt(cKey) < parseInt(nKey)) proceed = true;
      }
      
      if(!proceed) continue;
      
      holder         = mixedKeys[y+1];
      mixedKeys[y+1] = mixedKeys[y];
      mixedKeys[y]   = holder;
  
    }
  }  
  var ret, mode;
  if(typeof mixed.length == 'undefined'){ 
    //object
    ret = new Object();
    mode = 'object';
  } else {
    //array
    ret = new Array();
    mode = 'array';
  }
  
  for(var i in mixedKeys){
    var key = mixedKeys[i];
    if(mode == 'object'){      
      ret[key] = mixed[key];
    } else if(mode == 'array'){
      ret.push(mixed[key]);
    }      
  }
  
  return ret;
}


/****************moving*/


function object_makeMovable(srcID, popupID){
  
  popupID = (typeof popupID == 'undefined')?srcID:popupID;
  
  var srcObj = element_isObject(srcID);
  if(!srcObj) return false;
  
  if(typeof srcObj.alvineMovingEnabled != 'undefined') return true;
  
  var popupObj = element_isObject(popupID);
  if(!popupObj) return false;

  srcObj.alvine_popupObj = popupObj;
  
  srcObj.alvine_popupObj['alvine_initPosition']     = element_getPosition(popupObj);
  
  event_add(srcObj, 'mousedown', object_toggleMoving);
  event_add(srcObj, 'mouseover', 'element_addClassName("'+srcObj.id+'", "moving_enabled")');
  event_add(srcObj, 'mouseout',  'element_removeClassName("'+srcObj.id+'", "moving_enabled")');
  
  srcObj.alvineMovingEnabled = true;
  
  return true;

}

function object_getMouseOffset(target, ev){
  if(!ev) ev = window.event;

  var docPos    = element_getPosition(target);
  var mousePos  = document.alvine_mousePos;
  if(typeof mousePos == 'undefined') mousePos = {x:0, y:0};

  return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}




function object_toggleMoving(ev){
  if(!ev) ev = window.event;

  switch(ev.type){
    case 'mousedown':


      this.alvine_popupObj.alvine_mouseOffset = object_getMouseOffset(this.alvine_popupObj, ev);

      element_addClassName(this.alvine_popupObj, 'moving_enabled');

      alvine.data.move.object = this.alvine_popupObj;
      break;
    case 'mouseup':

      if(!alvine.data.move.object) break;
      
      var obj = alvine.data.move.object;
      
      obj.alvine_mouseOffset = null;
      obj.alvine_moveStatus  = null;

      element_removeClassName(obj, ' moving_enabled');

      alvine.data.move.object = null;
      break;
  }

  return true;
}

function object_moveObject(ev){
  if(!ev) ev = window.event;

  if(!alvine.data.move.object) return true;

  var obj = alvine.data.move.object;
  
  //Just once when start moving
  if(!obj.alvine_moveStatus){
    //code here
    
    
    obj.alvine_moveStatus = 'moving';
  }

  var mousePos = document.alvine_mousePos;
  var mouseOffset = obj.alvine_mouseOffset;

  var left = mousePos.x - mouseOffset.x;
  var top  = mousePos.y - mouseOffset.y;

  element_move(obj, left, top);

  return true;
}

page_onLoad(object_initLibs);
