

/* for adding a function to the list of functions to be executed at load */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function insertModal()
{
    var modal = document.createElement('div');
    modal.id="modalDialogBox"; 
    document.getElementsByTagName('body')[0].appendChild(modal)
}

function setMCTab(event)
{
      if(location.pathname != '/')
        return false;

      try 
      {
            evt = event ? event: window.event; 
            target = evt.target ? evt.target : evt.srcElement;
            target.parentNode.className='mcActiveTab';
         
            var tab1 = document.getElementById('mctab1');
            var con1 = document.getElementById('content1');
            var tab2 = document.getElementById('mctab2');
            var con2 = document.getElementById('content2');
         
            if(target.parentNode.id=='mctab1')
            { // TAB 2 OFF 
                tab1.style.borderLeft='solid 1px #cccccc'; 
                tab2.className='mcInactiveTab';
                tab2.style.borderRight='none'; // #dddddd'
                Effect.Appear(con1, {queue:'end'})
                Effect.Fade(con2, {queue:'front'})
            }
            else
            {   // TAB 1 OFF 
                tab2.style.borderLeft='solid 1px #cccccc'; // #dddddd'
                tab2.style.borderRight='solid 1px #cccccc'; // #dddddd'
                tab1.className='mcInactiveTab';
                tab1.style.borderLeft='none'; // #dddddd'
                tab1.style.borderRight='none'; // #dddddd'
                Effect.Appear(con2,{queue:'end'})
                Effect.Fade(con1,{queue:'front'})
            }  


      }
      catch(e)
      {
        return false;

      }

      return false;
}


function setSVCTab(event)
{

    if(location.pathname != '/services/')
        return false;


    evt = event ? event: window.event; 
    target = evt.target ? evt.target : evt.srcElement;
    target.parentNode.className='svcActiveTab';
   
    var tab1 = document.getElementById('svctab1');
    var con1 = document.getElementById('svccontent1');
    var tab2 = document.getElementById('svctab2');
    var con2 = document.getElementById('svccontent2');
    var tab3 = document.getElementById('svctab3');
    var con3 = document.getElementById('svccontent3');
   
    if(target.parentNode.id=='svctab1')
    { // TAB 2 and 3 OFF 
      tab1.style.borderLeft='solid 1px #cccccc'; 
      tab1.style.borderRight='solid 1px #cccccc'; 
      tab2.className='svcInactiveTab';
      tab3.className='svcInactiveTab';
      tab2.style.borderRight='none'; 
      tab2.style.borderLeft='none'; 
      tab3.style.borderLeft='solid 1px #cccccc'; 
/*
      Effect.Fade(con2, {queue:'front'})
      Effect.Fade(con3, {queue:'front'})
      Effect.Appear(con1, {queue:'end',duration:0.2})
*/
      new Effect.Parallel(
        [  Effect.Fade(con2 )
          ,Effect.Fade(con3)
          ,Effect.Appear(con1)
        ]
      )
    }
    else if(target.parentNode.id=='svctab2') 
    { // TAB 1 and 3 OFF 
   
      tab1.className='svcInactiveTab';
      tab3.className='svcInactiveTab';
      tab1.style.borderLeft='none'; 
      tab1.style.borderRight='none'; 
      tab3.style.borderLeft='none'; 
      tab2.style.borderLeft='solid 1px #cccccc'; 
      tab2.style.borderRight='solid 1px #cccccc'; 
/*
      Effect.Fade(con1, {queue:'front'} );
      Effect.Fade(con3, {queue:'front'} );
      Effect.Appear(con2, {queue:'end',duration:0.2} );
*/
      new Effect.Parallel(
        [  Effect.Fade(con1 )
          ,Effect.Fade(con3)
          ,Effect.Appear(con2)
        ]
      )


    }
    else
    {   // TAB 1 and 2 OFF 
      tab1.className='svcInactiveTab';
      tab2.className='svcInactiveTab';
      tab2.style.borderLeft='solid 1px #cccccc'; 
      tab3.style.borderLeft='solid 1px #cccccc'; 
      tab2.style.borderRight='none'; 
      tab1.className='svcInactiveTab';
      tab1.style.borderLeft='none'; 
      tab1.style.borderRight='none'; 
      new Effect.Parallel(
        [  Effect.Fade(con1 )
          ,Effect.Fade(con2)
          ,Effect.Appear(con3)
        ]
      )
    }

}


// This might be evil but I gotta have sane heights
function fixHeight()
{
    var divs = new Array(document.getElementById('sideA'),
                         document.getElementById('sideB'),
                         document.getElementById('mainColumn')
                        );

    var tallest = document.getElementById('sideA').offsetHeight; 
    if( document.getElementById('sideB').offsetHeight > tallest)
        var tallest = document.getElementById('sideB').offsetHeight; 

    var pad = 0;
    while( tallest > document.getElementById('mainColumn').offsetHeight  )
    {
        pad += 20;
        document.getElementById('mainColumn').style.paddingBottom = pad + 'px';
    }

}


function setEventHandlers()
{
    if( location.pathname == '/')
    {
        document.getElementById('mctab1').childNodes[0].onclick=setMCTab;
        document.getElementById('mctab2').childNodes[0].onclick=setMCTab;
    }
    else if (location.pathname == '/services/') 
    {
        document.getElementById('svctab1').childNodes[0].onclick=setSVCTab;
        document.getElementById('svctab2').childNodes[0].onclick=setSVCTab;
        document.getElementById('svctab3').childNodes[0].onclick=setSVCTab;

    }

}


var QS_INDEX = -1;
var QUOTES = new Array();


function initQuote()
{
    if(location.pathname != '/')
        return false;
    var sigPrefix = '<span style="display:block;margin-top: 4px;" >';
    var sigSuffix = '</span>';
    
    QUOTES[0] = new Array();
    QUOTES[0].push('&quot;CSM Worldwide turned our four-month business planning process');
    QUOTES[0].push('into a two-week process with more accurate results.  We now make');
    QUOTES[0].push('decisions in real time.&quot;');
    QUOTES[0].push(sigPrefix+'&mdash; Lydall, Inc.'+sigSuffix);
    
    QUOTES[1] = new Array();
    QUOTES[1].push('&quot;Magna Electronics is forever changing to meet the challenges'); 
    QUOTES[1].push('of our industry and we do this effectively by relying on CSM\'s accurate'); 
    QUOTES[1].push('market data and analysis.&quot;');
    QUOTES[1].push(sigPrefix+'&mdash; Magna Electronics'+sigSuffix); 
    
    QUOTES[2] = new Array();
    QUOTES[2].push('&quot;We truly value the working relationship with CSM, which goes');
    QUOTES[2].push('well beyond just receiving a sales or production forecast.&quot;');
    QUOTES[2].push(sigPrefix+'&mdash; Toyota'+sigSuffix); 
    
    QUOTES[3] = new Array();
    QUOTES[3].push('&quot;I find that of all the other services, CSM\'s');
    QUOTES[3].push('information is the most current and accurate.&quot;');
    QUOTES[3].push(sigPrefix+'&mdash; Johnson Controls'+sigSuffix); 
    
    QUOTES[4] = new Array();
    QUOTES[4].push('&quot;CSM has provided us with so much more than a vehicle forecast. Its'); 
    QUOTES[4].push('product line forecasts are the most thorough on the market and always tie'); 
    QUOTES[4].push('right into the vehicle forecast so all of our reports are consistent.&quot;');   
    QUOTES[4].push(sigPrefix+'&mdash; Takata'+sigSuffix);  
    
    QUOTES[5] = new Array();
    QUOTES[5].push('&quot;Teleflex Global Marketing relies on CSM Worldwide\'s vehicle platform and');
    QUOTES[5].push('powertrain data as a \'secondary reality check\' as we build our sales forecasts.&quot;');
    QUOTES[5].push(sigPrefix+'&mdash; Teleflex'+sigSuffix); 
    
    QUOTES[6] = new Array();
    QUOTES[6].push('&quot;CSM\'s broad range of experienced professionals has been a significant');
    QUOTES[6].push('benefit to our organization from fundamental forecasting to complex');
    QUOTES[6].push('strategic product issues.  Their knowledge of OEM\'s has also provided');
    QUOTES[6].push('additional insight for our new product development process.&quot;');
    QUOTES[6].push(sigPrefix+'&mdash; IEE Sensing, Inc.'+sigSuffix); 
    
    QUOTES[7] = new Array();
    QUOTES[7].push('&quot;The economic indicators given during the CSM soirees help form');
    QUOTES[7].push('a frame of reference for our \'living\' strategic plan.&quot;');
    QUOTES[7].push(sigPrefix+'&mdash; Teleflex'+sigSuffix); 
    
    QUOTES[8] = new Array();
    QUOTES[8].push('&quot;We find CSM\'s in-depth knowledge and long-term forecast of Global'); 
    QUOTES[8].push('Light Vehicle markets and OEMs most helpful in our efforts to identify');
    QUOTES[8].push('opportunities for future growth and diversification.&quot;');                                                                                                         
    QUOTES[8].push(sigPrefix+'&mdash; BorgWarner'+sigSuffix); 
           
    
   var q = document.createElement('div');
   q.id='quote';
   q.style.display = 'none';
   q.style.position = 'absolute';
   q.style.top = '40px';
   q.style.left = '40px';
   q.style.font = 'bold 11px verdana, sans-serif';
   q.style.color = '#cccccc';

   var s = document.createElement('div');
   s.id='shadow';
   s.style.display = 'none';
   s.style.position = 'absolute';
   s.style.top = '41px';
   s.style.left = '41px';
   s.style.font = 'bold 11px verdana, sans-serif';
   s.style.color = '#5a270a';
   s.style.color = '#000000';
   var hd = document.getElementById('header');
   hd.appendChild(s);
   hd.appendChild(q);

   if (QS_INDEX >= QUOTES.length-1)
       QS_INDEX=0;
   else
       QS_INDEX += 1;

   $('quote').innerHTML = QUOTES[QS_INDEX].join('<br />');
   $('shadow').innerHTML = QUOTES[QS_INDEX].join('<br />')

   new Effect.Parallel (
     [ new Effect.Appear('shadow'),new Effect.Appear('quote') ],
        { queue:'end' } 

   )

    new PeriodicalExecuter(quoteSpinner,10)
}


function quoteSpinner()
{

    if(location.pathname != '/')
        return false 

    if (QS_INDEX >= QUOTES.length-1)
        QS_INDEX=0;
    else
        QS_INDEX += 1;

    new Effect.Parallel(
        [ new Effect.Fade('shadow'),new Effect.Fade('quote') ],
        { 
          afterFinish: function(effect) 
          { 
            $('quote').innerHTML = QUOTES[QS_INDEX].join('<br />');
            $('shadow').innerHTML = QUOTES[QS_INDEX].join('<br />');
            new Effect.Parallel(
                [ new Effect.Appear('shadow'),new Effect.Appear('quote') ],
                { queue:'end' } 
            )
   

          } 
        }
    )


}



// The languages drop down menu
//Effect.DropDown = Class.create();
//
//Object.extend(Object.extend(Effect.DropDown.prototype, Effect.Base.prototype), {
//  initialize: function(element) {
//    this.element = $(element);
//    var options = arguments[1] || {};
//    this.start(options);
//  },
//
//  setup: function(element){
//    this.beginH = this.element.offsetHeight;
//    this.endH;
//    if(this.options.endSize) this.endH = this.options.endSize;
//    else {
//            this.element.style.visibility = 'hidden';
//            this.element.style.height = '';
//            this.endH = this.element.offsetHeight;
//            this.element.style.visibility = 'visible';
//            this.element.style.height = this.beginH+'px';
//        if(this.endH < this.beginH && !this.options.endSize){ this.cancel(); }
//    }
//  },
//  update: function(position){
//        this.element.style.height = Math.round(this.beginH+position*(this.endH-this.beginH))+'px';
//  },
//  finish: function(position) {
//        this.element.style.height = this.endH+'px';
//  }
//});
//
//

function languageClick(e)
{

    if( Element.hasClassName('langs','up') )
    {
        new Effect.BlindDown($('langs'), {queue:'end', duration: 0.3} );
        Element.removeClassName('langs','up');
    }
    else
    {
        new Effect.BlindUp( 'langs',{queue:'end',duration:0.3} );
        Element.addClassName('langs','up');

    }

    return false;
}






function setTab()
{
    var path = location.pathname;


    if(path.match('/clients_only/') )
        $('mnclients-only').addClassName('currentTab');
    else if(path.match('/about/') )
        $('mnabout').addClassName('currentTab');
    else if(path.match('/contact/') )
        $('mncontact').addClassName('currentTab');
    else if(path.match('/speakers/') )
        $('mnspeakers').addClassName('currentTab');
    else if(path.match('/careers/') )
        $('mncareers').addClassName('currentTab');
    else if(path.match('/press/') )
        $('mnpress').addClassName('currentTab');
    else if(path.match('/services/') )
        $('mnservices').addClassName('currentTab');
    else if(path.match('/news/') )
        $('mnnews').addClassName('currentTab');
    else if(path.match('/clients-only/') )
        $('mnclients-only').addClassName('currentTab');
    else if(path.match('/') )
        $('mnhome').addClassName('currentTab');
    else
        alert('unmatched path: '+path);


}


function hlCurrent()
{
    var sn = $('subnav');
    if(sn)
        var subnav = sn.getElementsByTagName('a');
    else
        return false;

    subnavLinks = new Array();
    if(!subnav)
        return;


    for(i=0;i<subnav.length;i++)
    {
        if(subnav[i].hostname == location.hostname)
        {

            if (subnav[i].pathname == document.location.pathname )
            {
                subnav[i].style.color='#666666';
                Event.observe(subnav[i],'click',function() { return false } );
            }

            if ( location.pathname.search( subnav[i].pathname ) >= 0 )
            {
                subnav[i].style.color='#666666';
                Event.observe(subnav[i],'click',function() { return false } );
            }

        }

    }

}


function languageUrls()
{
    var path = document.location.pathname;
    var langs = ['us','cn','jp','pt','fr','de'];
    for(i=0;i<langs.length;i++) {
        if( $(langs[i] ))
           $(langs[i]).href += path.substr(1); 
    }
}

function set_flags()
{
    var flags = new Array();
    flags['www.csmauto.net'] = 'us';
    flags['csmauto.net']     = 'us';
    flags['www.csmauto.com'] = 'us';
    flags['csmauto.com']     = 'us';
    flags['pt.csmauto.com']     = 'pt';
    flags['www.csmworldwide.net.cn'] = 'cn';
    flags['www.csmworldwide.com.cn'] = 'cn';
    flags['www.csmworldwide.cn'] = 'cn';
    flags['csmworldwide.cn'] = 'cn';
    flags['csmworldwide.com.cn'] = 'cn';
    flags['cn.csmauto.net'] = 'cn';
    flags['jp.csmauto.net'] = 'jp';
    flags['csmauto.jp'] = 'jp';
    flags['csmauto.fr'] = 'fr';
    flags['fr.csmauto.net'] = 'fr';
    flags['pt.csmauto.net'] = 'pt';
    flags['de.csmauto.net'] = 'de';
    flags['csmauto.de'] = 'de';
    flags['www.csmauto.de'] = 'de';


    if($(flags[document.domain]))
        $(flags[document.domain]).style.display='none';

    new Effect.Appear('pagecontainer',{queue:'end'})

}


Event.observe(window,'load',function() {
    set_flags();
    hlCurrent();
    //initQuote();
    setTab();
    insertModal()
    setEventHandlers();
    languageUrls();




});

//new PeriodicalExecuter(quoteSpinner,5)

