/**
 * jQuery Flip Dipendenti
 * Copyright (c) 2010 Webmaori
 * Date: 05/08/2010
 * @author Webmaori
 * @version 1.0  
*/
  
// PARAMETRI   
var num_elements = 172; 
var num_slots = 15;
var path_images = '/k-content/riservatabsstefano/themes/banstefano/html/img/dipendenti/';
var switch_enabled = 1;
var doneArray = new Array();
// *********
 
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery) 

function setActiveTab(tab){
  if(tab == 'button_mutuo'){
    $('#button_mutuo').addClass('button_active');
    $('#button_mutuo').removeClass('button_deactive');
    
    $('#button_chisiamo').addClass('button_deactive');
    $('#button_chisiamo').removeClass('button_active');
  } else if(tab == 'button_chisiamo'){
    $('#button_chisiamo').addClass('button_active');
    $('#button_chisiamo').removeClass('button_deactive');
    
    $('#button_mutuo').addClass('button_deactive');
    $('#button_mutuo').removeClass('button_active');
  } 
}
 
$(document).ready(function(){
  startFlip();
  
  $('#button_mutuo').click(function(e){
    e.preventDefault();
    
    if(switch_enabled){
      switch_enabled = 0;
      $('#flip_chisiamo').fadeOut('normal', function() {
        $('#flip_mutuo').fadeIn('normal', function(){
          stopFlip();
          startFlash('flash_mutuo');
          
          setActiveTab('button_mutuo');          
        });
        switch_enabled = 1;
      });
    } else return false;   
  });
  
  $('#button_chisiamo').click(function(e){
    e.preventDefault();
    
    if(switch_enabled){
      switch_enabled = 0;
      $('#flip_mutuo').fadeOut('normal', function() {
        $('#flip_chisiamo').fadeIn('normal', function(){
          setActiveTab('button_chisiamo');
          startFlip();
        });
        switch_enabled = 1;  
      });
    } else return false;
  });
 
});

// Cambia l'src con una foto random prendendo dalla cartella size
function changeImg(id){
  if($('#'+id) != undefined) {
    var sizeChoice = $('#'+id).attr('class').replace('size_','');
    var theChanger = id.replace('element_flip_','');
    var theChoice = theChanger;
    
    // Fino a che non trova un elemento diverso da sè stesso
    while (theChoice == theChanger){
      var theChoice = giveMeRandom();
    }    
    
    // Preload...
    jQuery.preLoadImages(path_images + sizeChoice + "/" + theChoice + ".jpg", path_images + sizeChoice + "/" + theChoice + ".jpg");
        
    // Animation FadeIn/FadeOut  
    /*$('#'+id).fadeOut(100, function() {
      $(this).attr({ src: path_images + sizeChoice + "/" + theChoice +".jpg" });
      if (this.complete) $(this).fadeIn(100);
    });*/
    
    $('#'+id).animate({ opacity: 0.0 }, 1100, function() {
      $(this).attr({ src: path_images + sizeChoice + "/" + theChoice +".jpg" });
      if (this.complete) {
        $('#'+id).animate({opacity: 1.0}, 1100, function(){});
      } 
    });
  }
  else return false;
}

// Parte il flip
function startFlip(){
  SlideAuto = setInterval(function(){
    changeImg('element_flip_'+giveMeRandomSlot(1));    
  }, 2900);
  
  SlideAutoSecond = setInterval(function(first){
    changeImg('element_flip_'+giveMeRandomSlot(2));
  }, 3000);  
  
  SlideAutoThird = setInterval(function(){
    changeImg('element_flip_'+giveMeRandomSlot(3));
  }, 3200);
  
  SlideAutoFor = setInterval(function(){
    changeImg('element_flip_'+giveMeRandomSlot(1));    
  }, 3400);
  
  SlideAutoFive = setInterval(function(first){
    changeImg('element_flip_'+giveMeRandomSlot(2));
  }, 3600);  
  
  SlideAutoSix = setInterval(function(){
    changeImg('element_flip_'+giveMeRandomSlot(3));
  }, 3800);
  
  SlideAutoSeven = setInterval(function(){
    changeImg('element_flip_'+giveMeRandomSlot(3));
  }, 4000);
}

// Ferma il flip
function stopFlip(){
  if(SlideAuto) clearInterval(SlideAuto);
  if(SlideAutoSecond) clearInterval(SlideAutoSecond);
  if(SlideAutoThird) clearInterval(SlideAutoThird);
  if(SlideAutoFor) clearInterval(SlideAutoFor);
  if(SlideAutoFive) clearInterval(SlideAutoFive);
  if(SlideAutoSix) clearInterval(SlideAutoSix);
  if(SlideAutoSeven) clearInterval(SlideAutoSeven);
}

// Restituisce un numero random compatibile con il numero di foto
function giveMeRandom(){
  var theChoiceChange = Math.random();
  theChoiceChange = theChoiceChange * num_elements;
  theChoiceChange = Math.ceil(theChoiceChange);
  return parseInt(theChoiceChange);
}

// Restituisce un numero random compatibile con il numero di slot
function giveMeRandomSlot(step){
  var theChoiceChange = Math.random();
  theChoiceChange = theChoiceChange * num_slots;
  theChoiceChange = parseInt(Math.ceil(theChoiceChange));
  
  if(step == 2 || step == 3){
    //alert('step 2 o 3')
    doneArray[step] = theChoiceChange;
    
    if(step == 2) {
      var check_1 = 1;
      var check_2 = 3;
    } else {
      var check_1 = 2;
      var check_2 = 1;
    }
    
    // Fino a che non trova un elemento diverso da sè stesso
    while (theChoiceChange == doneArray[check_1] || theChoiceChange == doneArray[check_2]){
      var theChoiceChange = Math.random();
      theChoiceChange = theChoiceChange * num_slots;
      theChoiceChange = parseInt(Math.ceil(theChoiceChange));
    }    
  }
  
  return theChoiceChange;
}

// Fa partire il flash flash_mutuo
function startFlash(div_id){
  var flashvars = {};
  var params = {wmode:"transparent"};
  var attributes = { id: div_id + '_object'};
  swfobject.embedSWF("/k-content/riservatabsstefano/themes/banstefano/html/flash/flash_mutuo.swf", div_id, "900px", "300px", "9.0.0","expressInstall.swf",flashvars, params, attributes);
}
