// #############
// # CONSTANTS #
// #############

$.fn.cycle.defaults = {
    fx:           'fade',                   // One of: fade, shuffle, zoom, slideX, slideY, scrollUp/Down/Left/Right
    timeout:       3500,                    // Milliseconds between slide transitions (0 to disable auto advance)
    speed:         1500,                    // Speed of the transition (any valid fx speed value)
    speedIn:       null,                    // Speed of the 'in' transition
    speedOut:      null,                    // Speed of the 'out' transition
    before:        function() { },          // Transition callback (scope set to element to be shown)
    after:         function() { },          // Transition callback (scope set to element that was shown)
    easing:        null,                    // Easing method for both in and out transitions
    easeIn:        null,                    // Easing for 'in' transition
    easeOut:       null,                    // Easing for 'out' transition
    shuffle:       { top: 15, left: -100 }, // Coords for shuffle animation, ex: { top:15, left: 200 }
    animIn:        null,                    // Properties that define how the slide animates in
    animOut:       null,                    // Properties that define how the slide animates out
    startingSlide: 0,                       // Zero-based index of the first slide to be displayed
    sync:          1,                       // True if in/out transitions should occur simultaneously
    random:        1,                       // True for random, false for sequence (not applicable to shuffle fx)
    fit:           1,                       // Force slides to fit container
    pause:         1,                       // True to enable 'pause on hover'
    autostop:      0,                       // True to end slideshow after X transitions (where X == slide count)
    delay:         0,                       // Additional delay (in ms) for first transition (hint: can be negative)
    slideExpr:     null,                    // Expression for selecting slides (if something other than all children is required)
    cleartype:     1                        // True if clearType corrections should be applied (for IE)
};

// ####################
// # HELPER FUNCTIONS #
// ####################

function teaserRotor(target, elements) {
  var cycle = $(target).cycle($.fn.cycle.defaults);
  
  var slides = new Array();
  
  var restart = function() {
    cycle.stop();
    
    cycle = $(target).cycle($.fn.cycle.defaults);
  };
  
  $(elements).each(function(i) {
    slides.push($(this).attr('href'));
  });
  
  var image = document.createElement('img');
  
  $(image).bind('load', function() {
    if (slides[0]) {
      this.src = slides.shift();
      
      var markupSlide = [
        '<div class="slide">',
          '<img src="' + this.src + '" height="198" width="450" title="metafinanz - living dynamic X&sup2;cellence" alt="metafinanz - living dynamic X&sup2;cellence" />',
        '</div>'
      ].join('');
      
      $(target).append(markupSlide);
    } else { restart(); }
  }).trigger('load');
};

// #############
// # BUGFIXING #
// #############

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

// ########
// # SIFR #
// ########

if (typeof sIFR != 'undefined') {
  var frutigerLight = { src: '/fileadmin/lib/flash/mfWebsite/sifr_frutiger_light.swf' };
  var frutigerBoldCondensed = { src: '/fileadmin/lib/flash/mfWebsite/sifr_frutiger_bold_condensed.swf' };
  
  sIFR.fitExactly = true;
  // sIFR.forceWidth = true;
  sIFR.useStyleCheck = true;
  
  sIFR.activate(frutigerLight, frutigerBoldCondensed);
}

$(document).ready(function() {
  $('body').addClass(browserDetect.OS);
  
  $('div#header div#menu_bar form#search input#words').focus(function() {
    if ($(this).val() == 'Ihr Suchbegriff') {
      $(this).val('');
    }
  });
  
  $('div#header div#menu_bar form#search input#words').blur(function() {
    if ($(this).val() == '') {
      $(this).val('Ihr Suchbegriff');
    }
  });
  
  var teaser = $('div#teaser');
  
  if (teaser) {
    var backgroundColor = teaser.css('background-color');
    var color = new RGBColor(backgroundColor);
    
    var teaserH1 = teaser.find('div.text h1');
    
    if (teaserH1) {
      var teaserH1Height = teaserH1.height();
      
      sIFR.replace(frutigerLight, {
        selector: 'div#teaser div.text h1',
        css: {
          '.sIFR-root': {
            'background-color': color.toHex(),
            'font-size': '26px',
            'line-height': '32px',
            'color': '#FFFFFF'
          }
        },
        tuneHeight: teaserH1Height - 12, /* line-height - 20px */
        wmode: 'transparent'
      });
      
      teaserH1.find('object.sIFR-flash').height(teaserH1Height);
    }
    
    var teaserP = teaser.find('div.text p');
    
    if (teaserP) {
      var teaserPHeight = teaserP.height();
      
      sIFR.replace(frutigerBoldCondensed, {
        selector: 'div#teaser div.text p',
        css: {
          '.sIFR-root': {
            'background-color': color.toHex(),
            'font-size': '18px',
            'line-height': '23px',
            'color': '#FFFFFF'
          }
        },
        tuneHeight: teaserPHeight - 23 /* line-height */,
        wmode: 'transparent'
      });
      
      teaserP.find('object.sIFR-flash').height(teaserPHeight);
    }
  }
  
  // ###################
  // # TEASER ROTATION #
  // ###################
  
  teaserRotor('div#header div#teaser div.image', 'div#header div#teaser div.slides a.slide');
  
  // #############
  // # SLIDESHOW #
  // #############
  
  $('div#content div.slideshow').cycle($.fn.cycle.defaults);
  
  // ###########
  // # ADDTHIS #
  // ###########
  
  if (typeof $.addthis != 'undefined') {
    var addthis_config = { 'data_track_clickback': true };
    
    $.addthis({
      username: 'metafinanz'
    });
  }
});
