window.addEvent('domready' , function() {
  
  new removeBlankClass('searchform', hInputValues);
  //alert(hInputValues.get('sword'));
  
  // Menu to show open by default
  var accordionIndex = -1;
  var subNavLists = $$('ul.subnavlist');
  subNavLists.each(function(element, index) {
		if (element.hasClass('active')) {
      accordionIndex = index;
    }
    
    element.setStyle('display', 'block');
	});

  //create our Accordion instance
	var menuAccordion = new Accordion($('navcontainer'), 'a.level1', 'ul.subnavlist', {
		opacity: false,
    alwaysHide: true,
    show: accordionIndex,
    //display: accordionIndex,
    //show: -1,
    duration: 700,
		onActive: function(toggler, element){
      toggler.addClass('active');
		},
		onBackground: function(toggler, element){
      toggler.removeClass('active');
		}
	});
	
	var accordion = new Accordion('h3.rgaccord1-toggle', 'div.rgaccord1-content', {
	  duration: 700,
  	alwaysHide:true,
  	opacity: false,
  	onActive: function(toggler, element){
  		toggler.addClass('act');
  	},

  	onBackground: function(toggler, element){
  		toggler.removeClass('act');		
  	}
  }/*, $$('div.rgaccord2-nest')*/);
  
  
  if ( $('right_gallery') || $('center_gallery') ) {
    frame_gallery = ($('right_gallery') ?  $('right_gallery') : $('center_gallery'));
    frame_gallery_id = frame_gallery.getProperty('id');
    //alert(frame_gallery_id);
    
    //frame_gallery.setStyle('display', 'none');
    
     if ($('myGallery')) {
       $('myGallery').setStyle('display', 'block');
     }
     
     if ($('right_gallery_content')) {
       $('right_gallery_content').setStyle('display', 'block');
     }
  
    if ($$('#' + frame_gallery_id + ' .company_gallery')[0] )
    {
      items = $$('#' + frame_gallery_id + ' .company_gallery');
      //alert();
      var V = new viewer(items, {
        onWalk: function(current_index){
          if (current_index == 0) {
            $('galleryLeftButton').setStyle('display', 'none');
          } else {
            $('galleryLeftButton').setStyle('display', 'block');
          } 
          
          if (current_index+1 == items.length) {
            $('galleryRightButton').setStyle('display', 'none');
          } else {
            $('galleryRightButton').setStyle('display', 'block');
          }
        },
        mode: 'rand',
        modes: ['bottom']
      });
      //V.play(true);
    }
    else if ($$('#' + frame_gallery_id + ' .csc-textpic-image')[0] ) {
      //$('galleryLeftButton').setStyle('display', 'block');
      items = $$('#' + frame_gallery_id + ' .csc-textpic-image');
      var V = new viewer(items, {
        onWalk: function(){ $('galleryLeftButton').setStyle('display', 'block'); },
        /*onWalk: function(current_index){
          if (current_index == 0) {
            $('galleryLeftButton').setStyle('display', 'none');
          } else {
            $('galleryLeftButton').setStyle('display', 'block');
          } 
          
          if (current_index+1 == items.length) {
            $('galleryRightButton').setStyle('display', 'none');
          } else {
            $('galleryRightButton').setStyle('display', 'block');
          }
        },*/
        mode: 'rand',
        modes: ['alpha'],
        //interval: 9000
        //interval: 5000
        //interval: 4000
        interval: 3000
      });
      V.play(true);

      // Per ogni Item creiamo un contenuto per il titolo dell'immagine
      /*items.each(function(item){
        img = $(item).getElement('img');
        if (img) {
          imgTitle = img.getProperty('title');
          if (imgTitle) {
            slideInfoZone = new Element( 'div', {
              'class': 'slideInfoZone',
              'html': '<h2>' + imgTitle + '</h2>'
            });
            slideInfoZone.inject(item);
          }
        }
      });*/
    }

    // Se c'è più di una immagine, mostriamo le frecce di scorrimento
    if (items[1]) {
      var leftArrow = new Element('a', {
        'class': 'left',
        'id': 'galleryLeftButton',
        'style': 'display:none;'
      });
      var rightArrow = new Element('a', {
        'class': 'right',
        'id': 'galleryRightButton'
      });
      leftArrow.inject(frame_gallery_id);
      rightArrow.inject(frame_gallery_id);

      // Aggiungiamo l'evento "click" alle frecce di scorrimento immagini
      leftArrow.addEvent('click',V.previous.bind(V));
      rightArrow.addEvent('click',V.next.bind(V));
      
      //Aggiungiamo gli eventi "mouseover" e "mouseout"
      // alle frecce di destra e sinistra
      // per modificare il Background
      // perchè tramite CSS non si riesce a farlo funzionare su IE 6/7
      leftArrow.addEvent('mouseover', function(){
          leftArrow.setStyle('background-position', '0 -100px');
      });
      leftArrow.addEvent('mouseout', function(){
          leftArrow.setStyle('background-position', '0 0');
      });
      rightArrow.addEvent('mouseover', function(){
          rightArrow.setStyle('background-position', '0 -100px');
      });
      rightArrow.addEvent('mouseout', function(){
          rightArrow.setStyle('background-position', '0 0');
      });
    }

  }

  /* Funzioni da caricare se ci troviamo nella pagina dei prodotti */
  if( $('menu_prodotti_container') ) {
    // Se una voce del menù è attiva andiamo li dove si trova all'apertura della pagina
    function goToActive() {
      arrMenuProdotti = $$('#menu_prodotti_container .menu_prodotti');
      activeElement = $$('#menu_prodotti_container .menu_prodotti a.active')[0];
      if (activeElement) {
        activeParent = activeElement.getParent('.menu_prodotti');
        //elementsBeforeActive = activeParent.getAllPrevious().length;
        //elementsBeforeActive = activeParent.getPrevious().length;
        arrMenuProdotti.each(function(item, index){
          if( item.hasChild(activeElement) ) {
            //alert(index + " = " + item);
            elementsBeforeActive = index;
          }
        });
        activePage = Math.floor(elementsBeforeActive/6);
        if (activePage > 0) {
          leftPosition = -activePage*750
          $('menu_prodotti_container').setStyle('left', leftPosition)
        }
      
      }
    }

    var menuProdottiScroll = new Fx.Morph('menu_prodotti_container', {
      duration: 1000,
      transition: Fx.Transitions.Sine.easeOut,
      onComplete: function(){
        showHideArrows();
      }
    });

    function showHideArrows() {
      //alert('complete');
      menu_prodotti_array = $('menu_prodotti_container').getElements('.menu_prodotti');
      num_menus = menu_prodotti_array.length;
      menu_prodotti_position = $('menu_prodotti_container').getStyle('left').toInt();
      if (isNaN(menu_prodotti_position)) { 
        menu_prodotti_position = 0;
      }
      menu_prodotti_length = 125 * num_menus;
      leftScroll = menu_prodotti_position - 750;
      rightScroll = menu_prodotti_position + 750;

      // Nella barra superiore possiamo visualizzare massimo 6 immagini per volta
      // Se gli elementi nell'array sono di più, visualizziamo la freccia di scorrimento
      // Freccia destra
      if ( num_menus > 6 && menu_prodotti_length > Math.abs(leftScroll) ) {
        $('menu_prodotti_next').setStyle('display', 'block');

        // Cliccando sulla freccia di destra il menu scorre di 6*125 pixels
        $('menu_prodotti_next').addEvent('click', function() {
          menuProdottiScroll.start({
            //'left': '-750px'
            'left': leftScroll + 'px'
          });
        });
      } else {
        $('menu_prodotti_next').setStyle('display', 'none');
      }

      // freccia sinistra
      if (menu_prodotti_position < 0) {
        $('menu_prodotti_prev').setStyle('display', 'block');

        // Cliccando sulla freccia di destra il menu scorre di 6*125 pixels
        $('menu_prodotti_prev').addEvent('click', function() {
          menuProdottiScroll.start({
            'left': rightScroll + 'px'
          });
        });
      } else {
        $('menu_prodotti_prev').setStyle('display', 'none');
      }
    }

    // Se è presente il menù dei prodotti
    // Andiamo al punto del prodotto attivo emostriamo eventualmente le frecce di scorrimento
  
    goToActive();
    showHideArrows();
  }

  $('sword').addEvent('blur', function(){
      blurTextboxes( this, hInputValues.get('sword') );
  });
  $('sword').addEvent('focus', function(){
      focusTextboxes( this, hInputValues.get('sword') );
  });
  
  $('littlesearch_submit').addEvent('click', function(){
      //alert($('sword').value);
  		//alert(hInputValues.get('sword'));
  		if($('sword').value == hInputValues.get('sword')) {
  			$('sword').value = '';
  		}
  		//alert($('sword').value);
  });

});

/*window.addEvent('load' , function() {
  //var size = $('menu_prodotti_container').getSize();
  //alert("The element is "+size.x+" pixels wide and "+size.y+"pixels high.");
  new removeBlankClass('searchform', hInputValues);
  alert(hInputValues.get('sword'));
});*/


var removeBlankClass = function(form, hashInputValues) {
	form = $(form);
	aFormInputsform = form.getElements('input');
	aFormInputsform.each(function(field) {
		var sFieldValue = field.value;
		var sFieldName = field.id;
		//alert(sFieldValue);
		//alert(sFieldName);
		if (sFieldValue != eval('hashInputValues.get(\'' + sFieldName + '\')') && field.type != 'hidden') {
			$(sFieldName).removeClass('blank');
		}
		
		if (sFieldValue == '' && field.type != 'hidden') {
			$(sFieldName).value = eval('hashInputValues.get(\'' + sFieldName + '\')');
			$(sFieldName).addClass('blank');
		}
	});
}

var blurTextboxes = function(element, defValue) {
	if (element.value.match(/^ *$/)) {
		element.value=defValue;
		$(element).addClass('blank');
	}
}

var focusTextboxes = function(element, defValue) {
	if (element.value == defValue) {
		element.value='';
		$(element).removeClass('blank');
	}
}

