var navSlideShow;
var headlineName = new Array();

colors = {
  ".phaseOne"  : "#000",
  ".phaseTwo"  : "#000",
  ".phaseThree": "#fff",
  ".phaseFour" : "#fff"
}

window.addEvent('domready', function() {
  // get current active accordion element
  var accordionElements = $$('#accordion .element').get('class');
  var activeElement = accordionElements.indexOf('element active');

  // create our Accordion instance
  var myAccordion = new Accordion($('accordion'), '.toggler>li', 'ul.element', {
    opacity: false,
    display: activeElement,
    alwaysHide: false,
    initialDisplayFx: false
  });

  if ($$('#slideshow').length != 0) {

    (function() {
      setupSlideShow();
    }).delay(500)
  }
  drawHeadline();

});



window.addEvent('keyup', function(event) {

  if (event.key == "w") {
    morphTo('.phaseOne', 0);
  }
  if (event.key == "e") {
    morphTo('.phaseTwo', 0);
  }
  if (event.key == "v") {
    morphTo('.phaseThree', 0);
  }
  if (event.key == "b") {
    morphTo('.phaseFour', 0);
  }
});


function setupSlideShow() {


  numberOfElements = $$('#slideshow>li');

  numberOfElements.each(function(item, index) {

    trigger = new Element('a', {
      'href': '#',
      'html': '&bull;'
    });

    if (index == 0) {
      trigger.addClass('current')
    }

    trigger.inject($('navigation-slideshow'))
  })

  // cache the navigation elements
  var navs = $('navigation-slideshow').getElements('a');

  // create a basic slideshow
  navSlideShow = new SlideShow('slideshow', {
    delay: 5000,
    duration: 400,
    autoplay: true,
    onShow: function(data) {
      navs[data.previous.index].removeClass('current');
      navs[data.next.index].addClass('current');
    }
  });

  navs.each(function(item, index) {
    // click a nav item ...
    item.addEvent('click', function(event) {
      event.stop();
      // pushLeft or pushRight, depending upon where
      // the slideshow already is, and where it's going
      // var transition = (navSlideShow.index < index) ? 'pushLeft' : 'pushRight';
      // call show method, index of the navigation element matches the slide index
      // on-the-fly transition option
      // pausing slideshow because of manual selection
      // delaying playback by 1min and resuming playback afterwards
      navSlideShow.pause();
      (function() {
        navSlideShow.play()
      }).delay(60000);
      navSlideShow.show(index);
    });
  });

  drawArrows();

  $('slideshow-prev').addEvent('click', function(event) {
    event.stop();
    var index = navSlideShow.index - 1

    if (navSlideShow.index == 0) {
      index = navs.length - 1;
    }
    navSlideShow.show(index);
  })

  $('slideshow-next').addEvent('click', function(event) {
    event.stop();
    var index = navSlideShow.index + 1

    if (navs.length - 1 == navSlideShow.index) {
      index = 0;
    }
    navSlideShow.show(index);
  })
}

function drawArrows() {
  var leftArrowPaper = Raphael($('slideshow-prev'));
  var rightArrowPaper = Raphael($('slideshow-next'));
  var scaleFactor = 0.3;
  var leftArrow = "M10.537 42.492L10.632 42.402 10.814 42.582 32.231 63.999 53.306 63.999 29.002 39.692 21.353 32.042 21.171 31.862 28.726 24.303 53.031 0 31.955 0 10.537 21.417 0 31.956z";
  var rightArrow = "M42.77 21.506L42.674 21.598 42.492 21.418 21.074 0 0 0 24.303 24.306 31.953 31.957 32.135 32.137 24.58 39.696 0.275 64 21.352 64 42.77 42.583 53.307 32.044z";

  leftArrowPaper.path(leftArrow).attr({
    stroke: 0,
    fill: "#777"
  }).scale(scaleFactor);
  rightArrowPaper.path(rightArrow).attr({
    stroke: 0,
    fill: "#777"
  }).scale(scaleFactor);
}

function drawHeadline() {
  var r = Raphael($('headline'));
  var scaleFactor = 0.9;

  var name = ["M5.813,17.474h6.901v12.443h-2.04l-0.781-1.836c-1.258,1.496-2.278,2.176-4.25,2.176C2.313,30.257,0,27.877,0,23.968V11.321c0-3.468,2.04-6.29,6.188-6.29c4.216,0,6.323,3.06,6.323,6.731v1.564H8.499c0-3.671-0.747-4.624-2.413-4.624c-1.021,0-1.87,0.612-1.87,2.686v12.613c0,1.564,0.51,2.584,1.972,2.584c1.7,0,2.312-0.952,2.312-3.4v-2.38H5.813V17.474z", "M17.001,5.372h4.216v18.528c0,1.802,0.714,2.686,2.243,2.686c1.53,0,2.244-0.884,2.244-2.686V5.372h4.216v18.358c0,4.216-2.448,6.527-6.46,6.527s-6.459-2.312-6.459-6.527V5.372z M22.61,0v3.672h-3.672V0H22.61z M24.31,3.672V0h3.672v3.672H24.31z", "M34.001,29.917V5.372h3.909l5.134,14.041h0.067V5.372h3.809v24.545h-3.672l-5.372-15.333h-0.067v15.333 H34.001z", "M53.448,9.043h-4.079V5.372h12.375v3.671h-4.08v20.874h-4.216V9.043z", "M64.227,29.917V5.372h4.215v9.995h4.488V5.372h4.215v24.545H72.93V19.038h-4.488v10.879H64.227z", "M81.227,29.917V5.372h11.287v3.671h-7.072v6.323h5.439v3.672h-5.439v7.207h7.548v3.672H81.227z", "M100.537,29.917h-4.216V5.372h5.915c4.726,0,7.14,2.006,7.14,7.241c0,3.943-1.53,5.542-2.958,6.188 l3.569,11.117h-4.283l-2.991-10.165c-0.545,0.068-1.429,0.102-2.176,0.102V29.917z M100.537,16.386h1.495 c2.278,0,3.128-0.85,3.128-3.773s-0.85-3.774-3.128-3.774h-1.495V16.386z", "M137.766,5.372v24.545h-3.672V10.743h-0.068l-3.808,19.174h-2.04l-3.808-19.174h-0.067v19.174h-3.672V5.372 h5.541l2.992,14.346h0.068l2.991-14.346H137.766z", "M144.09,29.917h-4.079l4.521-24.545h5.235l4.521,24.545h-4.08l-0.747-5.439h-4.624L144.09,29.917z M145.45,20.806h3.399l-1.666-10.539h-0.067L145.45,20.806z", "M160.953,29.917h-4.215V5.372h5.915c4.726,0,7.14,2.006,7.14,7.241c0,3.943-1.53,5.542-2.958,6.188 l3.569,11.117h-4.283l-2.992-10.165c-0.543,0.068-1.428,0.102-2.176,0.102V29.917z M160.953,16.386h1.496 c2.278,0,3.128-0.85,3.128-3.773s-0.85-3.774-3.128-3.774h-1.496V16.386z", "M181.421,11.423c-0.17-2.108-1.292-2.72-2.243-2.72c-1.36,0-2.108,0.884-2.108,2.414 c0,4.182,8.567,6.188,8.567,12.851c0,4.045-2.72,6.289-6.663,6.289c-3.91,0-6.153-3.127-6.323-6.799l4.012-0.578 c0.17,2.481,1.155,3.706,2.516,3.706c1.462,0,2.447-0.782,2.447-2.244c0-4.862-8.566-6.154-8.566-13.191c0-3.91,2.379-6.12,6.527-6.12c3.434,0,5.473,2.482,5.847,5.95L181.421,11.423z", "M197.333,13.564v-1.904c0-2.074-0.952-2.958-2.04-2.958c-1.699,0-2.244,0.884-2.244,2.72v12.443 c0,1.835,0.545,2.72,2.244,2.72c1.53,0,2.04-0.884,2.04-2.482v-2.958h4.216v2.822c0,3.467-2.04,6.289-6.256,6.289c-4.42,0-6.459-2.822-6.459-6.289V11.321c0-3.468,2.039-6.29,6.459-6.29c4.216,0,6.256,3.06,6.256,6.731v1.802H197.333z", "M205.834,29.917V5.372h4.215v9.995h4.488V5.372h4.215v24.545h-4.215V19.038h-4.488v10.879H205.834z", "M225.281,29.917h-4.079l4.521-24.545h5.235l4.521,24.545h-4.08l-0.747-5.439h-4.624L225.281,29.917z M226.642,20.806h3.399l-1.666-10.539h-0.067L226.642,20.806z", "M237.726,29.917V5.372h4.216v20.874h6.629v3.672H237.726z", "M250.951,29.917V5.372h4.216v20.874h6.629v3.672H250.951z"];

  var position = ["M282.195,12.783c-0.34-3.875-1.563-5.507-3.638-5.507c-3.094,0-4.453,2.72-4.453,10.369 c0,7.649,1.359,10.369,4.453,10.369c2.481,0,3.91-1.7,3.978-6.255h2.652c-0.068,5.439-2.448,8.499-6.63,8.499 c-4.386,0-7.105-3.332-7.105-12.613c0-9.281,2.72-12.613,7.105-12.613c3.638,0,5.882,2.516,6.29,7.751H282.195z", "M295.388,30.257c-4.147,0-6.936-3.468-6.936-12.613s2.788-12.613,6.936-12.613s6.936,3.468,6.936,12.613 S299.535,30.257,295.388,30.257z M295.388,7.275c-2.38,0-4.283,2.04-4.283,10.369c0,8.33,1.903,10.369,4.283,10.369 s4.284-2.04,4.284-10.369C299.672,9.315,297.768,7.275,295.388,7.275z", "M305.793,5.372h5.779c3.943,0,6.527,2.652,6.527,6.731c0,4.08-2.584,6.731-6.527,6.731h-3.128v11.083h-2.651 V5.372z M308.444,16.59h2.788c2.651,0,4.215-1.428,4.215-4.488c0-3.06-1.563-4.487-4.215-4.487h-2.788V16.59z", "M322.383,5.372l4.216,10.743l4.079-10.743h2.856l-5.644,14.143v10.403h-2.652V19.514l-5.711-14.143H322.383z", "M336.73,5.372l2.924,19.174h0.068l3.672-19.174h2.176l3.808,19.174h0.067l2.72-19.174h2.448l-3.943,24.545 h-2.244l-3.876-19.276h-0.067l-3.876,19.276h-2.244l-4.079-24.545H336.73z", "M357.098,5.372h5.915c3.943,0,6.731,2.312,6.731,6.357c0,2.753-1.224,5.065-3.943,6.017l4.147,12.171h-2.788 l-3.943-11.627l-3.468,0.136v11.491h-2.651V5.372z M359.749,16.114h2.312c3.808,0,5.031-1.496,5.031-4.25 c0-2.753-1.224-4.25-5.031-4.25h-2.312V16.114z", "M376.953,5.372v24.545h-2.652V5.372H376.953z", "M392.354,5.372v2.244h-5.032v22.302h-2.651V7.615h-5.031V5.372H392.354z", "M406.089,5.372v2.244h-8.567v8.363h6.323v2.244h-6.323v9.451h8.975v2.244H394.87V5.372H406.089z", "M409.966,5.372h5.915c3.943,0,6.731,2.312,6.731,6.357c0,2.753-1.224,5.065-3.943,6.017l4.147,12.171h-2.788 l-3.943-11.627l-3.468,0.136v11.491h-2.651V5.372z M412.617,16.114h2.312c3.808,0,5.032-1.496,5.032-4.25 c0-2.753-1.225-4.25-5.032-4.25h-2.312V16.114z", "M431.93,30.257l7.887-25.226h2.244l-7.887,25.226H431.93z", "M461.575,12.783c-0.34-3.875-1.563-5.507-3.638-5.507c-3.094,0-4.453,2.72-4.453,10.369 c0,7.649,1.359,10.369,4.453,10.369c2.482,0,3.91-1.7,3.978-6.255h2.652c-0.068,5.439-2.448,8.499-6.63,8.499 c-4.386,0-7.105-3.332-7.105-12.613c0-9.281,2.72-12.613,7.105-12.613c3.638,0,5.882,2.516,6.289,7.751H461.575z", "M468.513,5.372h5.915c3.943,0,6.731,2.312,6.731,6.357c0,2.753-1.224,5.065-3.943,6.017l4.147,12.171h-2.788 l-3.943-11.627l-3.468,0.136v11.491h-2.651V5.372z M471.164,16.114h2.312c3.808,0,5.032-1.496,5.032-4.25 c0-2.753-1.225-4.25-5.032-4.25h-2.312V16.114z", "M496.731,5.372v2.244h-8.567v8.363h6.323v2.244h-6.323v9.451h8.975v2.244h-11.626V5.372H496.731z", "M507.747,5.372l5.405,24.545h-2.651l-1.462-7.275h-6.255l-1.462,7.275h-2.652l5.406-24.545H507.747z M503.158,20.398h5.507l-2.72-13.191h-0.067L503.158,20.398z", "M525.155,5.372v2.244h-5.032v22.302h-2.651V7.615h-5.031V5.372H525.155z", "M530.527,5.372v24.545h-2.652V5.372H530.527z", "M536.171,5.372l4.318,19.446h0.067l4.317-19.446h2.652l-5.779,24.545h-2.448L533.52,5.372H536.171z", "M561.533,5.372v2.244h-8.567v8.363h6.323v2.244h-6.323v9.451h8.976v2.244h-11.627V5.372H561.533z", "M572.958,5.372h4.556c5.167,0,8.295,2.855,8.295,12.272c0,9.417-3.128,12.273-8.295,12.273h-4.556V5.372z M575.609,27.673h1.428c3.876,0,6.119-1.632,6.119-10.029c0-8.397-2.243-10.029-6.119-10.029h-1.428V27.673z", "M592.813,5.372v24.545h-2.651V5.372H592.813z", "M597.506,5.372h5.915c3.943,0,6.731,2.312,6.731,6.357c0,2.753-1.224,5.065-3.943,6.017l4.147,12.171h-2.788 l-3.943-11.627l-3.468,0.136v11.491h-2.651V5.372z M600.157,16.114h2.312c3.808,0,5.031-1.496,5.031-4.25 c0-2.753-1.224-4.25-5.031-4.25h-2.312V16.114z", "M625.725,5.372v2.244h-8.567v8.363h6.323v2.244h-6.323v9.451h8.976v2.244h-11.627V5.372H625.725z", "M639.663,12.783c-0.34-3.875-1.563-5.507-3.638-5.507c-3.094,0-4.453,2.72-4.453,10.369 c0,7.649,1.359,10.369,4.453,10.369c2.482,0,3.91-1.7,3.978-6.255h2.652c-0.068,5.439-2.448,8.499-6.63,8.499 c-4.386,0-7.105-3.332-7.105-12.613c0-9.281,2.72-12.613,7.105-12.613c3.638,0,5.882,2.516,6.289,7.751H639.663z", "M657.31,5.372v2.244h-5.032v22.302h-2.651V7.615h-5.031V5.372H657.31z", "M666.081,30.257c-4.147,0-6.936-3.468-6.936-12.613s2.788-12.613,6.936-12.613s6.936,3.468,6.936,12.613 S670.229,30.257,666.081,30.257z M666.081,7.275c-2.38,0-4.283,2.04-4.283,10.369c0,8.33,1.903,10.369,4.283,10.369 s4.284-2.04,4.284-10.369C670.365,9.315,668.461,7.275,666.081,7.275z", "M676.826,5.372h5.915c3.943,0,6.731,2.312,6.731,6.357c0,2.753-1.224,5.065-3.943,6.017l4.147,12.171h-2.788 l-3.943-11.627l-3.468,0.136v11.491h-2.651V5.372z M679.478,16.114h2.312c3.808,0,5.031-1.496,5.031-4.25 c0-2.753-1.224-4.25-5.031-4.25h-2.312V16.114z"];

  var group = r.group();

  name.each(function(item, index) {
    var shape = r.path(item).attr({
      stroke: 0,
      fill: "#fff"
    });
    group.push(shape);
    headlineName.push(shape);
  })
  position.each(function(item, index) {
    var shape = r.path(item).attr({
      stroke: 0,
      fill: "#777"
    });
    group.push(shape)
  })

  group.scale(scaleFactor);
  
  animateToColor(headlineName, colors['.'+$$('body').get('class')]);
}
function animateToColor(element, color) {
  if (element.length == 1) {
    element.animate({
      fill: color
    },
    2000)
  } else {
    element.each(function(element, index) {
      element.animate({
        fill: color
      },
      2000)
    })
  }
}
function morphTo(cssClass, delay) {
  (
  function() {
    
    $$('body').set('morph', {
      duration: 'long',
      transition: Fx.Transitions.Sine.easeOut
    })
    $$('a').set('morph', {
      duration: 'long',
      transition: Fx.Transitions.Sine.easeOut
    })
    $$('body').morph(cssClass);
    $$('#header ul a, #imprint a, #accordion a').morph(cssClass);

    animateToColor(headlineName, colors[cssClass])

  }).delay(delay);

  (function() {
    $$('body').set('class', cssClass.substr(1));
  }).delay(delay + 1000);
}

var req = new Request.HTML({
  onRequest: function() {},
  onSuccess: function(text, xml) {

    sunrise               = Date.parse(xml.getElements('sunrise')[0].get('text'));
    sunset                = Date.parse(xml.getElements('sunset')[0].get('text'));
    localtime             = Date.parse(xml.getElements('localtime')[0].get('text'));

    var timeOffsetSunrise = sunrise - localtime - 900000;
    var timeOffsetSunset  = sunset - localtime - 900000;
    if (timeOffsetSunrise > 0) {
      if (timeOffsetSunset > 0) {
        morphTo('.phaseThree', timeOffsetSunrise);
        morphTo('.phaseTwo', timeOffsetSunrise + 900000);
        morphTo('.phaseOne', timeOffsetSunrise + 1800000);
      };

      if (timeOffsetSunset > 900000) {
        morphTo('.phaseTwo', timeOffsetSunrise + 900000);
        morphTo('.phaseOne', timeOffsetSunrise + 1800000);
      };

      if (timeOffsetSunset > 1800000) {
        morphTo('.phaseOne', timeOffsetSunrise + 1800000);
      };
    }

    if (timeOffsetSunset > 0) {
      // console.log("there is still some daylight");
      if (timeOffsetSunset > 1800000) {
        morphTo('.phaseTwo', timeOffsetSunset);
        morphTo('.phaseThree', timeOffsetSunset + 900000);
        morphTo('.phaseFour', timeOffsetSunset + 1800000);
      };

      if (timeOffsetSunset < 900000) {
        morphTo('.phaseThree', timeOffsetSunset + 900000);
        morphTo('.phaseFour', timeOffsetSunset + 1800000);
      };
      // 		
      // if (timeOffsetSunset < 0) {
      // 	morphTo('.phaseFour', 	timeOffsetSunset+1800000);
      // };
    }
    // else{	
    // 			morphTo('.phaseFour', 	timeOffsetSunset);
    // 		}
  },
  onComplete: function() {},
  onFailure: function() {}
});
req.send({
  url: 'sunrise.php'
});
