/* JS DOCUMENT colorSwitcher.js */

/* BEGIN colorSwitcher */

window.addEvent('domready', function(){
    setColorSwitcher();
});

mosaico_colors = new Array('orange','blue','green','yellow','purple');

function setColorSwitcher () {
  $('orange_switcher').addEvent('click', function(){
    setColor('orange');
  });
  $('dark_orange_switcher').addEvent('click', function(){
    setColor('orange');
  });
  $('blue_switcher').addEvent('click', function(){
    setColor('blue');
  });
  $('dark_blue_switcher').addEvent('click', function(){
    setColor('blue');
  });
  $('green_switcher').addEvent('click', function(){
    setColor('green');
  });
  $('dark_green_switcher').addEvent('click', function(){
    setColor('green');
  });
  $('yellow_switcher').addEvent('click', function(){
    setColor('yellow');
  });
  $('dark_yellow_switcher').addEvent('click', function(){
    setColor('yellow');
  });
  $('purple_switcher').addEvent('click', function(){
    setColor('purple');
  });
  $('dark_purple_switcher').addEvent('click', function(){
    setColor('purple');
  });


  $('orange_switcher').addEvent('mouseenter', function(){
    sizeReduct('orange');
  });
  $('orange_switcher').addEvent('mouseleave', function(){
    sizeRestore('orange');
  });
  $('blue_switcher').addEvent('mouseenter', function(){
    sizeReduct('blue');
  });
  $('blue_switcher').addEvent('mouseleave', function(){
    sizeRestore('blue');
  });
  $('green_switcher').addEvent('mouseenter', function(){
    sizeReduct('green');
  });
  $('green_switcher').addEvent('mouseleave', function(){
    sizeRestore('green');
  });
  $('yellow_switcher').addEvent('mouseenter', function(){
    sizeReduct('yellow');
  });
  $('yellow_switcher').addEvent('mouseleave', function(){
    sizeRestore('yellow');
  });
  $('purple_switcher').addEvent('mouseenter', function(){
    sizeReduct('purple');
  });
  $('purple_switcher').addEvent('mouseleave', function(){
    sizeRestore('purple');
  });

  setTimeout("$('color_switcher_helper').set('tween', {duration: 'long'});$('color_switcher_helper').tween('opacity', [0, 1]);",1000);
  setTimeout("sizeReduct('purple')",500);
  setTimeout("sizeRestore('purple')",600);
  setTimeout("sizeReduct('yellow')",700);
  setTimeout("sizeRestore('yellow')",800);
  setTimeout("sizeReduct('orange')",900);
  setTimeout("sizeRestore('orange')",1000);
  setTimeout("sizeReduct('green')",1100);
  setTimeout("sizeRestore('green')",1200);
  setTimeout("sizeReduct('blue')",1300);
  setTimeout("sizeRestore('blue')",1400);
}



function sizeReduct(color) {
  var myEffect = new Fx.Morph(color+'_switcher', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
  myEffect.start({
    'height': ['50%', '70%'], //Morphs the 'height' style from 10px to 100px.
  });
}



function sizeRestore(color) {
  var myEffect = new Fx.Morph(color+'_switcher', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
  myEffect.start({
    'height': ['70%', '50%'], //Morphs the 'height' style from 10px to 100px.
  });
}



function setColor (color) {
  $$('.color_switcher_waiting').set('styles', {'visibility': 'visible'});

  $$('.title')[0].id = 'title_'+color;
  
  bg_path = $('scaled_bg_img').src;
  title_path = $('title_'+color).getStyle('backgroundImage');
  title_path = title_path.replace('url(','');
  title_path = title_path.replace(')','');
//  news_title = $$('.news_title')[0].getStyle('backgroundImage');
//  news_title = news_title.replace('url(','');
//  news_title = news_title.replace(')','');
//  comunicato_title = $$('.comunicato_title')[0].getStyle('backgroundImage');
//  comunicato_title = comunicato_title.replace('url(','');
//  comunicato_title = comunicato_title.replace(')','');

  i=0;
  while ( i!=5 ) {
    $$('#box_logo').removeClass('box_logo_'+mosaico_colors[i]);
    $$('#text_color').removeClass('text_'+mosaico_colors[i]);
    $$('.news_title').removeClass('news_title_'+mosaico_colors[i]);
    $$('.news_body').removeClass('news_body_'+mosaico_colors[i]);
    $$('.comunicato_title').removeClass('comunicato_title_'+mosaico_colors[i]);
    $$('.comunicato_body').removeClass('comunicato_body_'+mosaico_colors[i]);
    $$('.invito_button').removeClass('invito_button_'+mosaico_colors[i]);
    bg_path = bg_path.replace(mosaico_colors[i]+'.jpg','___color___.jpg');
//    news_title = news_title.replace(mosaico_colors[i]+'.jpg','___color___.jpg');
//    comunicato_title = comunicato_title.replace(mosaico_colors[i]+'.jpg','___color___.jpg');
    i++;
  }

  bg_path = bg_path.replace('___color___.jpg',color+'.jpg');
//  news_title = news_title.replace('___color___.jpg',color+'.jpg');
//  comunicato_title = comunicato_title.replace('___color___.jpg',color+'.jpg');

  if (navigator.appName!="Microsoft Internet Explorer")
  {
    var progressBar = new dwProgressBar({
      container: $('color_switcher_progress'),
      startPercentage: 10,
      speed:750,
      boxID: 'color_switcher_progress_box',
      percentageID: 'color_switcher_progress_perc',
      displayID: 'color_switcher_progress_text',
      displayText: true
    });

    var images = [bg_path,title_path/*,news_title,comunicato_title*/];
    var loader = new Asset.images(images, {
      onProgress: function(counter,index) {
        progressBar.set((counter + 1) * (100 / images.length));
      },
      onComplete: function() {
        images.each(function(im) {
          $$('.color_switcher_waiting').set('styles', {'visibility': 'hidden'});
          new Request.HTML({url:'main/setcolorcookie'}).get({'color': color});
        });
      }
    });
  }

  $$('#box_logo').addClass('box_logo_'+color);
  $$('#text_color').addClass('text_'+color);
  $$('.news_title').addClass('news_title_'+color);
  $$('.news_body').addClass('news_body_'+color);
  $$('.comunicato_title').addClass('comunicato_title_'+color);
  $$('.comunicato_body').addClass('comunicato_body_'+color);
  $$('.invito_button').addClass('invito_button_'+color);
  $('scaled_bg_img').set('src',bg_path);

}

/* END colorSwitcher */