// customer.js

// copyright  bpm consult ag, CH-Birsfelden
// contact    webdev@bpm.ch
// license    restricted

beeline.web.afterInit=function(El){

  if (!Ext.isObject(El)) return;
   // change select icon
  El.select('select').each(function(Select){
    var id = Ext.id(Select) + '-icon';
    var dh = Ext.DomHelper;
    var spec = { id: id, tag: 'span', cls: 'select' };
    dh.insertAfter(Select, spec);
    var Icon = Ext.get(id);
    Icon.on('mouseover', function(){
      Icon.setStyle('visibility', 'hidden');
    });
    Select.on('mouseover', function(){
      Icon.setStyle('visibility', 'hidden');
    });
    Select.on('mouseout', function(){
      Icon.setStyle('visibility', 'visible');
    });
  });

  // show advanced search after ajaxload
  var AdvancedSearch = El.select('.headerSearchAdvanced').item(0);
  if (Ext.isObject(AdvancedSearch)) {
    AdvancedSearch.fadeIn();
    AdvancedSearch.select('.closeSearch').each(function(CloseSearch){
      CloseSearch.on('click', function(){
        AdvancedSearch.hide();
      });
    });
  }

  // show advanced search if already exist
  var ExpandSearch = El.select('.headerSearchExpand').item(0);
  if (Ext.isObject(ExpandSearch)) {
    ExpandSearch.on('click', function(){
      var AdvancedSearch = El.select('.headerSearchAdvanced').item(0);
      if (Ext.isObject(AdvancedSearch)) {
        AdvancedSearch.fadeIn();
      }
    });
  }

  // detail: expand blurb
  var BlurbExpand = El.select('.detailRightBlurbExpand').item(0);
  if (Ext.isObject(BlurbExpand)) {
    BlurbExpand.on('click', function(){
      BlurbExpand.parent().next().show();
      BlurbExpand.parent().setStyle('display', 'none');
    });
  }


  // detail: show annot text in window
  function initShowAnnotText(El, AnnotText){
    function initShow(Element){
      Element.on('click', function(EventObj){
        EventObj.stopEvent();
        var text = El.select('.annotText').item(0).select('.'+AnnotText).item(0).dom.innerHTML;
        new beeline.web.Alert({
          title: El.select('.annotText').item(0).first().dom.innerHTML,
          content: text + '<p class="alertboxButton"><span class="button"><input class="alertboxClose" type="reset" name="close" value="OK"/></span></p>'
         });
        return false;
      });
    }
    El.select('.show'+AnnotText).each(function(Item){
      initShow(Item);
    });
  }
  initShowAnnotText(El, 'SAT');
  initShowAnnotText(El, 'SUM');
  initShowAnnotText(El, 'ATX');
  initShowAnnotText(El, 'PRE');
  initShowAnnotText(El, 'PRO');
  initShowAnnotText(El, 'CRI');



  // notepad send
  El.select('.notepadMail').each(function(El){
    El.select('.showSendForm').item(0).on('click', function(){
      El.select('.notepadMailLink').item(0).setStyle('display', 'none');
      El.select('.notepadMailForm').item(0).show();
    });
    El.select('.hideSendForm').item(0).on('click', function(){
      El.select('.notepadMailForm').item(0).setStyle('display', 'none');
      El.select('.notepadMailLink').item(0).show();
    });
    if(Ext.isObject(Ext.get('alertbox'))){
      El.select('.notepadMailLink').item(0).setStyle('display', 'none');
      El.select('.notepadMailForm').item(0).show();
    }
  });

  // voucher send
  El.select('.voucherMail').each(function(El){
    El.select('.showSendForm').item(0).on('click', function(){
      El.select('.voucherMailLink').item(0).setStyle('display', 'none');
      El.select('.voucherMailForm').item(0).show();
    });
    El.select('.hideSendForm').item(0).on('click', function(){
      El.select('.voucherMailForm').item(0).setStyle('display', 'none');
      El.select('.voucherMailLink').item(0).show();
    });
    if(Ext.isObject(Ext.get('alertbox'))){
      El.select('.voucherMailLink').item(0).setStyle('display', 'none');
      El.select('.voucherMailForm').item(0).show();
    }
  });




  // checkout2: invoice address private company switch
  var InvoicePrivate = El.select('.invoicePrivate').item(0);
  var InvoiceCompany = El.select('.invoiceCompany').item(0);
  if (Ext.isObject(InvoicePrivate)) {
    function setPrivate(){
      InvoicePrivate.addClass('active');
      InvoiceCompany.removeClass('active');
      Ext.get('Addition2').dom.value = '';
      Ext.select('.private').each(function(e){
        e.setStyle('display', 'inline-block');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '10px');
      });
      Ext.select('.company').each(function(e){
        e.setStyle('display', 'none');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '0');
      });
    }
    function setCompany(){
      InvoiceCompany.addClass('active');
      InvoicePrivate.removeClass('active');
      if (Ext.get('Addition2').getValue() == '') {
        Ext.get('Addition2').dom.value = ' ';
      }
      Ext.select('.company').each(function(e){
        e.setStyle('display', 'inline-block');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '10px');
      });
      Ext.select('.private').each(function(e){
        e.setStyle('display', 'none');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '0');
      });
    }
    if (Ext.get('Addition2').getValue() == '') {
      setPrivate();
    }
    else {
      setCompany();
      if (Ext.get('Addition2').getValue() == ' ') {
        Ext.get('Addition2').on('focus', function(){
          Ext.get('Addition2').dom.value = ''
        });
      }
    }
    InvoiceCompany.on('click', setCompany);
    InvoicePrivate.on('click', setPrivate);
  }

  // checkout2: delivery address private company switch
  var DeliveryPrivate = El.select('.deliveryPrivate').item(0);
  var DeliveryCompany = El.select('.deliveryCompany').item(0);
  if (Ext.isObject(DeliveryPrivate)) {
    function setPrivate2(){
      DeliveryPrivate.addClass('active');
      DeliveryCompany.removeClass('active');
      Ext.get('Addition2-2').dom.value = '';
      Ext.select('.private2').each(function(e){
        e.setStyle('display', 'inline-block');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '10px');
      });
      Ext.select('.company2').each(function(e){
        e.setStyle('display', 'none');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '0');
      });
    }
    function setCompany2(){
      DeliveryCompany.addClass('active');
      DeliveryPrivate.removeClass('active');
      if (Ext.get('Addition2-2').getValue() == '') {
        Ext.get('Addition2-2').dom.value = ' ';
      }
      Ext.select('.company2').each(function(e){
        e.setStyle('display', 'inline-block');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '10px');
      });
      Ext.select('.private2').each(function(e){
        e.setStyle('display', 'none');
        if (e.parent().is('td')) e.parent().setStyle('paddingTop', '0');
      });
    }
    if (Ext.get('Addition2-2').getValue() == '') {
      setPrivate2();
    }
    else {
      setCompany2();
      if (Ext.get('Addition2-2').getValue() == ' ') {
        Ext.get('Addition2-2').on('focus', function(){
          Ext.get('Addition2-2').dom.value = ''
        });
      }
    }
    DeliveryCompany.on('click', setCompany2);
    DeliveryPrivate.on('click', setPrivate2);
  }



  // info tool tips
  El.select('.infotip').each(function(e){
    beeline.web.createToolTip(e, 'infobox', e.getAttribute('alt'), e.getAttribute('title'), new Array(10, -20));
  });

  // set focus
  if (El.select('.focus').item(0)) {
    var lastFocusElement = Ext.select('.focus').getCount() - 1;
    Ext.select('.focus').item(lastFocusElement).focus();
  }

  // result: redesign search summary
  El.select('.resultDetailSummary').each(function(Summary){
    var str = Summary.dom.innerHTML;
    str = str.replace(/:/g, ':</span><span class="value">');
    str = str.replace(/,/g, '</span><br /><span class="key">');
    Summary.dom.innerHTML = str;
    Summary.parent().show();
  });

  // result: open or close search details
  El.select('.resultInfoHeading').each(function(Heading){
    Heading.on('click', function(){
      var Icon = El.select('.resultInfoHeadingIcon').item(0);
      var Detail = El.select('.resultDetail').item(0);
      if(Icon.hasClass('closed')) {
        Icon.removeClass('closed');
        Detail.show();
      } else {
        Icon.addClass('closed');
        Detail.slideOut('t', { useDisplay: true });
      }
    });
  });
  
  // result: add news alert
  El.select('.resultActionsNewsalertLinkAdd').each(function(Add){
    Add.on('click', function(event, target){
      Ext.get(target).parent('div').first('.resultActionsNewsalertForm').show();
    });
  });
 
  // show flash movie
  El.select('.flashMovie').each(function(e){
    var flashdir = '/csp/shop/bpmweb/css/default/flash/';
    var flashfile = e.first('.flashLink').dom.innerHTML;
    if (flashfile.search(/\//) == -1) flashfile = flashdir + flashfile;
    var playervars = {
      video: flashfile,
      preview: e.first('img').getAttribute('src'),
      autoscale: 'false',
      videowidth: 400,
      videoheight: 182
    };
    var params = {
      scale: 'noscale',
      allowfullscreen: 'true',
      salign: 'tl',
      base: '.'
    };
    var attributes = {
      align: 'left'
    };
    swfobject.embedSWF(flashdir + 'flvplayer.swf', 'flash', '400', '232', '9.0.28', flashdir + 'expressInstall.swf', playervars, params, attributes);
  });

  // facebook integration: like it
  El.select('.detailLike').each(function(Like){
    var child = '<iframe src="http://www.facebook.com/widgets/like.php?href=' + document.URL + '" scrolling="no" frameborder="0"></iframe>';
    Ext.DomHelper.append(Like, child);
  });
  
   El.select('.detailLikeMe').each(function(Like){
     var child = '<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=243853592305828&amp;xfbml=1"></script><fb:like href="" send="true" width="450" show_faces="false" action="like" font="verdana"></fb:like>';
   Ext.DomHelper.append(Like, child);
  });

  // facebook integration: activities
  var FacebookActivity = El.select('.facebookActivity').item(0);
  if (Ext.isObject(FacebookActivity)) {
    FacebookActivity.on('click', function(){
      var site = document.URL.split('/')[2];
      new beeline.web.Alert({
        title: 'Facebook',
        content: '<iframe class="facebookActivites" src="http://www.facebook.com/widgets/activity.php?site=' + site + '"></iframe><p class="alertboxButton"><span class="button black"><input class="alertboxClose" type="reset" name="close" value="OK"/></span></p>'
      });
    });
  }



}
// phononet integration: get tracks callback
function getTracklist(JSONData) {
  var content = JSONData['content'];
  var TracksInfo = Ext.select('.tracksContentData').item(0);
  TracksInfo.update(content);
  Ext.select('.tracksContentEmpty').item(0).setStyle('display', 'none');
}

// clear advanced search
function clearAdvancedSearch(){
  var Obj = document.forms[1];
  for (i = 0; i < Obj.elements.length; i++) {
    if (Obj.elements[i].type == 'text') {
      Obj.elements[i].value = '';
    }
    if (Obj.elements[i].type == 'select-one') {
      Obj.elements[i].selectedIndex = 0;
    }
  }
}

// check all articles
function checkall(val){
  beeline.web.checkAll(val);
}

function initIE6(El){
  if(!Ext.isIE6) return;
  El.select('.main.right').each(function(Div){
    Div.setStyle('width','760px');
    Div.setStyle('padding','10px 0 20px 20px');
  });
}


function refreshForm(Input){
  if (Ext.Ajax.isLoading()==false) { 
    var Form=Ext.get(Input).parent('form');
    var CSPCHD=document.getElementsByName('CSPCHD')[0].value;
    var divName=Form.parent('div').dom.className;
    if (divName.length>0){
      beeline.web.bpmupdate(divName,'?CSPCHD='+CSPCHD);
    }
  }
}

function openDlLibriDigital(url){
  var myleft=50;
  var mytop=50;
  var settings="width=640,height=480,top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no,dependent=no";
  var win=window.open(url,"downloads",settings);
  win.focus();
}
onerror = stopError;
function stopError(){
	return true;
}

