/**
 * shop.item.order.form.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/
 */
 
function content_frontend_plugin_shop_item_order_form_init() {}

if(typeof price == 'undefined') var price = new Object();
      
function content_frontend_plugin_shop_item_order_form_changePrice(value, boxID, index) {
  obj = getElementID('detailPrice'+boxID+index);
  if(!obj) return;  
  if(!price) return;
  if(!price[boxID]) return;
  if(!price[boxID][index]) return;

  if(!price[boxID][index][value]) return;
  var newPrice = price[boxID][index][value];
  writeHTML(obj, newPrice);
};
 
function content_frontend_plugin_shop_item_order_form_addPrice(boxID, index, priceName, value) {
  
  if(typeof price[boxID] == 'undefined') price[boxID] = new Object();
  if(typeof price[boxID][index] == 'undefined') price[boxID][index] = new Object();
  
  price[boxID][index][priceName] = value;
};

function content_frontend_plugin_shop_item_order_form_changeAdvised(value, boxID, index) {
  obj = getElementID('detailAdvised'+boxID+index);
  if(!obj) return;  

  if(!alvine['plugin'][boxID]['advised'][index]) return;
  var text = alvine['plugin'][boxID]['advised'][index][value];
  writeHTML(obj, text);
};


function content_frontend_plugin_shop_item_order_form_addAdvised(boxID, index, variant, label) {
  
  if(typeof alvine['plugin'][boxID] == 'undefined') alvine['plugin'][boxID] = new Object();
  if(typeof alvine['plugin'][boxID]['advised'] == 'undefined') alvine['plugin'][boxID]['advised'] = new Object();
  if(typeof alvine['plugin'][boxID]['advised'][index] == 'undefined') alvine['plugin'][boxID]['advised'][index] = new Object();
  
  alvine['plugin'][boxID]['advised'][index][variant] = label;
};

var alvineOrderFormCollectionSize = new Array();

function content_frontend_plugin_shop_item_order_form_initVariantSelection(id, changeCallback) {
  obj = element_isObject(id);
  if(!obj) return;
  if(!obj.getAttribute) return;

  var divList = obj.getElementsByTagName('div');
  var collectionSize = new Array();
  var collectionList = new Array();

  var refID = obj.getAttribute('alvine:ref');
  refObj = element_isObject(refID);
  if(!refObj) return;
  refObj.alvineValue = refObj.value;

  var buttonList = new Array();
  var buttonStruct = new Array();
  for(var i=0;i<divList.length;i++) {
    var o = element_isObject(divList[i]);
    if(!o) continue;
    if(!o.getAttribute) continue;
    var type = o.getAttribute('alvine:type');
    if(type!='button') continue;
    var l = o.getAttribute('alvine:level');
    if(!buttonStruct[l]) buttonStruct[l] = new Array();
    buttonList.push(o);
    buttonStruct[l].push(o);
  }
    
  var eObj;
  var optionList = refObj.getElementsByTagName('option');
  for(i=0;i<optionList.length;i++){
    eObj = element_isObject(optionList[i]);
    
    if(!eObj) continue;
    if(!eObj.value) continue;
    collectionList[eObj.value] = eObj.value;
  }

  var initFlag = false;
  var staticDoOnceFlag = false;


  function ctrlOnChance(ev) {
    if(!ev) ev = window.event;
    var selValue = this.value;
    var alvineValue = this.alvineValue;
    selValue = selValue.split('_');

    if(!strstr(this.value,'_')) {
      for(var i=0;i<buttonList.length;i++){
        eObj = buttonList[i];
        element_removeClassName(eObj, 'variantSelected');
        element_removeClassName(eObj, 'variantSelection');
        element_removeClassName(eObj, 'variantAvailable');
      }
      for(i=0;i<buttonList.length;i++){
        eObj = buttonList[i];
        element_addClassName(eObj, 'variantAvailable');
        var key = eObj.getAttribute('alvine:key');
        if(key==this.value) {
          element_removeClassName(eObj, 'variantSelected');
          element_addClassName(eObj, 'variantSelected');
          if(!initFlag) {
            element_addClassName(eObj, 'variantSelection');
            collectionSize.push(key);
            memObj = eObj;
          }
        } else {
          element_removeClassName(eObj, 'variantSelected');
        }
      }

      return;
    }

    var al = alvineValue.split('_');
    aLevel = al[0];
    aKey = al[1];

    memObj = NaN;

    for(var i=0;i<buttonList.length;i++){
      eObj = buttonList[i];
      var level = eObj.getAttribute('alvine:level');
      var key = eObj.getAttribute('alvine:key');

      if(key==selValue[level]) {
        element_removeClassName(eObj, 'variantSelected');
        element_addClassName(eObj, 'variantSelected');
        if(!initFlag) {
          element_addClassName(eObj, 'variantSelection');
          collectionSize.push(key);
          memObj = eObj;
        }
      } else {
        element_removeClassName(eObj, 'variantSelected');
      }

    }

    if(memObj && staticDoOnceFlag==false) {
      staticDoOnceFlag = true;
      event_dispatchEvent(memObj,'click');
      memObj = NaN;
    }

    initFlag = true;
    if(changeCallback) changeCallback(buttonList);
  }

  alvineOrderFormCollectionSize = collectionSize;

  refObj.alvineValue='';
  event_add(refObj, 'change', ctrlOnChance, false);

  initOnClickFlag = false;
  for(i=0;i<buttonList.length;i++) {
    eObj = buttonList[i];

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

      if(!this.getAttribute) return;
      var level = this.getAttribute('alvine:level');
      var key = this.getAttribute('alvine:key');
      refObj = element_isObject(refID);
      if(!refObj) return;

      for(var i=0;i<buttonList.length;i++) element_removeClassName(buttonList[i], 'variantClicked');
      element_addClassName(this, 'variantClicked');

      if(!strstr(refObj.value,'_')) {

        collectionSize = new Array;
        collectionSize[0] = key;
        alvineOrderFormCollectionSize = collectionSize;
        refObj.alvineValue = key;
        refObj.value = key;
        event_dispatchEvent(refObj,'change');
        return;
      }

      // Überprüfung: siehe CVS 29.09.2010
      if(collectionSize.length>2) {
        collectionSize = [collectionSize[0],collectionSize[1]];
      }



      collectionSize[level] = key;
      
      var v=collectionSize.join('_');
      var checkLevel = level==0?1:0;
      var checkAvailable = new Array;

      if(!collectionList[v]) {
        collectionSize = new Array();
        collectionSize[level] = key;
        v=collectionSize.join('_');
      } else {
        refObj.value = v;
      }

      for(s in collectionList) {
        s = s.split('_');
        if(s[level]!=key) continue;
        s = s[checkLevel];
        checkAvailable.push(s);
      }

      for(i=0;i<buttonList.length;i++) {
        o = buttonList[i];
        if(!o.getAttribute) continue;
        key = o.getAttribute('alvine:key');
        element_removeClassName(o, 'variantUnavailable');
        element_addClassName(o, 'variantUnavailable');
      }

      var o='';
      if(buttonStruct[checkLevel]) {
        for(i=0;i<buttonStruct[checkLevel].length;i++) {
          o = buttonStruct[checkLevel][i];
          element_removeClassName(o, 'variantAvailable');

          eObjKey =o.getAttribute('alvine:key');
          for(jj=0; jj<checkAvailable.length;jj++) {
            ca = checkAvailable[jj];
            if(eObjKey==ca) {
              element_removeClassName(o, 'variantAvailable');
              element_addClassName(o, 'variantAvailable');
              break;
            }
          }
        }
      }
      
      for(i=0;i<buttonList.length;i++) {
        o = buttonList[i];
        if(!o.getAttribute) continue;
        key = o.getAttribute('alvine:key');

        for(i2=0;i2<collectionSize.length;i2++) {
          element_removeClassName(o, 'variantSelection');

          v = collectionSize[i2];
          if(v==key) {
            element_removeClassName(o, 'variantSelection');
            element_addClassName(o, 'variantSelection');
            break;
          }
        }
      }

      alvineOrderFormCollectionSize = collectionSize;
      refObj.alvineValue = level+'_'+key;
      event_dispatchEvent(refObj,'change');
    }

  }

  $(document).ready(function() {
     event_dispatchEvent(refObj, 'change');
     obj = $('.variantSelected');
     if(obj.length>0) {
       event_dispatchEvent(obj[0], 'click');
     
     } else {
       event_dispatchEvent(buttonList[0], 'click');
     }

  })

}
