$(document).ready(function(){
$('#right_content img').fadeTo(1,0.3);
$('#right_content img').mouseover(function(){
 index = $('#right_content img').index($(this))+1;  
 src = $("#last_div div:nth-child("+index+") img").attr("src");    
    $('#header_wrapper').BgImageTransition(src,{duration:150 });
      $(this).fadeTo(100,1);
    
});



$('#right_content img').mouseout(function(){
   // $('#header_container').stop(true,true);
    index = $('#right_content img').index($(this))+1;  
    src = $("#last_div div:nth-child("+index+") img").attr("src");    
   $('#header_wrapper').css('background-image','url('+src+')')
    //$('#header_container').BgImageTransition(src,{duration:100});
    $('#header_wrapper2').hide();
    $(this).fadeTo(100,0.3);
});


//main menu hacks for red ball
$('#menu_container>div>div a').mouseover(function() { 

if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {
  $('#red_ball_helper').css('left',($(this).position()["left"]+$(this).width()/2-10)+"px");
  $('#red_ball_helper').css('top',$(this).position()["top"]+"px");
  $('#red_ball_helper').css('zIndex',10000);
  $('#red_ball_helper').show();
}  
  //modified file see file typo3/sysext/cms/tslib/media/scripts/jsfunc.layermenu.js around line 137 to make the hide - search in that file for $('#red_ball_helper').hide();

});

$('#arrows_container').hide();
setTimeout("init_more_header_images()",500);

});

// golbal variables for header and thumb images
var current_img_offset = 8;
var last_move_direction = 'next';
var header_thumbs;
var header_large;


function init_more_header_images(){

  // finding ajax request url
  if (window.location.href.indexOf('?')>0){
    requesturl = window.location+"&"
  }
  else{
    requesturl = window.location+"?"
  }
  //request for the rest of the small images
  $.ajax({
    url: requesturl+'type=95',
    success: function(data) {    

	//cleaning data
	data = data.replace(/<img src="/g,"");      
	data = data.replace(/" width="86" height="86" border="0" alt="" title="" \/>/g,",");
	
	allthumbs=data.split(",");
	allthumbs.pop(); // get rid of the last one since its "" from the spit
	realthumbs = new Array();      

      for (x in allthumbs)
	{      	
	  if (!(allthumbs[allthumbs.length-1] === allthumbs[x])){ // if they are the same , they are empty
	    realthumbs.push(allthumbs[x]);	
	  }

	}
	
	header_thumbs = realthumbs;      

	display_and_activate_navigation_buttons();
    }
  });

  //request for the rest of the big images
  $.ajax({
    url: requesturl+'type=96',
    success: function(data) {    

	//cleaning data
	data = data.replace(/<img src="/g,"");   
	data = data.replace(/" width="937" height="398" border="0" alt="" title="" \/>/g,",");      
	
	alllarge=data.split(",");
	alllarge.pop(); // get rid of the last one since its "" from the spit
	reallarge = new Array();      
	
      for (x in alllarge)
	{      	
	  if (!(alllarge[alllarge.length-1] === alllarge[x])){ // if they are the same , they are empty
	    reallarge.push(alllarge[x]);	
	  }

	}      
	header_large = reallarge;      		
    }
  });
  
}

function display_and_activate_navigation_buttons(){
  if(header_thumbs.length > 9){
      $('#right_arrow').click(function(){
	next_images();
      });
      $('#left_arrow').click(function(){
	previous_images();	
      });
      $('#arrows_container').show();
  }
}

function next_images(){  

  if (last_move_direction == 'prev'){current_img_offset+=9;}
  
  $('#right_content img').each(function(index){        
    current_img_offset++;
    current_img_offset = current_img_offset%(header_large.length);
    $(this).attr("src",header_thumbs[current_img_offset]);          
    $("#last_div div:nth-child("+(index+1)+") img").attr("src",header_large[current_img_offset]);      
  });
  
  last_move_direction = 'next';
  
}	

function previous_images(){  

  if (last_move_direction == 'next'){current_img_offset-=9;}
  
  $($('#right_content img').get().reverse()).each(function(index){    
    current_img_offset = current_img_offset%header_large.length;
    if (current_img_offset<0){
      current_img_offset += header_large.length;	
    }
    $(this).attr("src",header_thumbs[current_img_offset]);  
    $("#last_div div:nth-child("+(index+1)+") img").attr("src",header_large[current_img_offset]);      
    current_img_offset--;
  });
  
  last_move_direction = 'prev';
}

