function getWindowHeight(){
  var viewportheight = window.getHeight();
  return viewportheight;
}

function align() {
  if(getWindowHeight() > 630){
    if($defined($('main_nav2'))){
      var theHeight = getWindowHeight() - 132;
      $('main_nav2').setStyle('height',theHeight);
    }
    $$('body').setStyle('overflow','hidden');
    var line;
    if($defined($('line-margin'))){
      line = $('line-margin').getStyle('height').toInt()
    } else {
      line = 3;
    }
    
    $('top_content').setStyle('height',(getWindowHeight() - (227+line)));
  } else {
    $$('body').setStyle('overflow','visible');
    $('top_content').setStyle('height','400px');
    if($defined($('main_nav2'))){
      $('main_nav2').setStyle('height','498px');
    }
  }
}
var img_id = 0;
var canPlay = false;
var total_img = 0;

function changePrev() {
var str = window.location.href;
var url = str.split("#");
  //if(canPlay) {
    if (img_id > 0) {
      img_id -= 1;
      SWFAddress.setValue(''+img_id);
       $('nav_right').erase('style');
//$('nav_right').removeAttribute('style');
      if (img_id == 0){
        $('nav_left').setStyle('backgroundPosition','center center');
      }
    }
  //}
}

function changeNext() {
var str = window.location.href;
var url = str.split("#");

  //if(canPlay) {
    if (img_id < (total_img-1)) {
      img_id += 1;
      SWFAddress.setValue(''+img_id);
       $('nav_left').erase('style');
//$('nav_right').removeAttribute('style');

      if (img_id == (total_img-1)){
        $('nav_right').setStyle('backgroundPosition','center center');
      }
    }
  //}
}

function setCurId(cur_id) {
img_id = cur_id;
  if($('nav_left')){
    if (img_id == (total_img-1)){
      $('nav_left').erase('style');
      $('nav_right').setStyle('backgroundPosition','center center');
    }else if (img_id == 0){
      $('nav_left').setStyle('backgroundPosition','center center');
      $('nav_right').erase('style');
    } else {
      $('nav_left').erase('style');
      $('nav_right').erase('style');
    }
  }
}

function setTotal(total){
  togglePlay();
  total_img = total;
}


function setTotalRed(total,filter,template){
  togglePlay();
  total_img = total;
  changeText(filter,0,template);
}

function togglePlay() {
    canPlay = true;
}

function changeText(filter, current_id,template_file){
  setCurId(current_id);
   //prevent the page from changing    
   //make the ajax call, replace text  
   var req = new Request.HTML({  
       method: 'get',  
       url: 'typo3conf/ext/tomaso_cms/php/services/getProductInfo.php?value='+filter+'&template='+template_file,  
       data: { 'do' : '1' },  
       onRequest: function() { },  
       update: $('product_info'),  
       onComplete: function(response) { }  
   }).send(); 
      
}


window.addEvent('resize', align);

var clicked_link;  

window.addEvent('domready', function() {
  setCurId(0);
  align();
  var status = {
		'true': 'open',
		'false': 'close'
	};
	
    if($('main_nav') != null){
		  var myVerticalSlide = new Fx.Tween('main_nav');//new Fx.Slide
      myVerticalSlide.addEvent('complete', function(){window.location.href = clicked_link;});
    }
    
    /*var myVerticalSlide2 = new Fx.Slide('top_content3');
    if(myVerticalSlide2 != null || myVerticalSlide2 != undefined){
      myVerticalSlide.addEvent('complete', function(){window.location.href = clicked_link;});
    }*/
  $('drop_down_menu').getElements('li.home').each( function( elem ){
               elem.addEvents({
          			'click' : function(e) {
          			
          			  var obj=document.getElementById('top_content');
          			  var line=document.getElementById('line-margin');
          			  if(line != null){
                    line.innerHTML = "";
          			  }
                  if ($('top_nav')) {
          			   var top=document.getElementById('top_nav');
          			  } else if ($('top_nav2')) {
          			   var top=document.getElementById('top_nav2');
                  }
                  if(top){
            			  top.innerHTML = "";
            			  top.style.width='1258px';
            			  top.style.border = "none"; 
                  }
                  e.stop();
                  if($('main_nav') != null){
                    var theHeight;
                    if(getWindowHeight() > 630){
            			     theHeight = getWindowHeight() - 132;
            			  } else {
                      theHeight = 498;
                    }
                   
                    $('main_nav').tween('height', theHeight);        			  
          			  }
                  obj.innerHTML = "";
          			  obj.style.width='1258px';
          			  var margin;
          			  if(getWindowHeight() > 630){
          			     margin = 307 - getWindowHeight();
          			  } else {
                    margin = -323;
                  }
                  
                  if(myVerticalSlide){
                    myVerticalSlide.start('margin-top', margin);
		              }
        			    clicked_link = e.target;
                  //$('top_content').tween('height', 200);350
                }
          		})
  });
 
  var contentTween = new Fx.Tween('top_content');
  contentTween.addEvent('complete', function(){window.location.href = clicked_link;});
  
  $('drop_down_menu').getElements('li.inactive').each( function( elem ){	
        var list = elem.getElement('ul.links');
    		var myFx = new Fx.Slide(list).hide();
         elem.addEvents({
    			'mouseenter' : function(){ 					
    				myFx.cancel();
    				myFx.slideIn();	
    			},
    			'mouseleave' : function(){ 
    				myFx.cancel();
    				myFx.slideOut();			
    			},
    			'click' : function(e) {
    			    //alert($('top_content').tween('height', 300));
      			  clicked_link = e.target;
              //myVerticalSlide.slideIn();354
              var newHeight;
              if(getWindowHeight() > 630){
          			    newHeight = getWindowHeight() - 304;
          		} else {
                    newHeight = 326;//getWindowHeight() - 254;
              }
              if($('main_nav2')){
      			      e.stop();
                  document.getElementById('content-left').innerHTML = "";
                  document.getElementById('content-right').innerHTML = "";
                  document.getElementById('footer-hold').innerHTML = "";
                  contentTween.start('margin-bottom', newHeight);
                  $('main_nav2').tween('height', 172);
              }
              //myVerticalSlide.tw
              //alert('when in home');
            }
    		})
  });
  
  $('drop_down_menu').getElements('li.active').each( function( elem ){
        var list = elem.getElement('ul.links');
        if(list != null){
      		var myFx2 = new Fx.Slide(list);
          $('drop_down_menu').getElements('li.inactive').each( function( elem ){
                 elem.addEvents({
            			'mouseenter' : function(){ 					
                    myFx2.cancel();
            				myFx2.slideOut();	
            			},
            			'mouseleave' : function(){ 
            				myFx2.cancel();
            				myFx2.slideIn();			
            			}
            		})
          })
      	}
  });
  


});