/*------------------------------------------------------------------
	jquery.zoomSlider.js 2010/11/24 Daniel Schneider, info@hd7b.de
	this file is part of the youexpress-cms
	copyright (c) 2010 HD7B-MEDIEN www.hd7b.de
  ----------------------------------------------------------------*/
  
/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}  

jQuery.fn.zoomSlider = function(options) {
  var settings = jQuery.extend(
    {
      slideDuration: 700,
      fadeDuration: 700, 
      autoPlay: true, 
      autoSpeed: 4500, 
      maxHeight: 750, 
      heightMulti: 0.6,
      activeOpacity: 1.0,
      inactiveOpacity: 0.2
    }, options
  );
  
  var currentIndex=0;
  var slider=$(this);
  var slideArea=$("#slideArea");  
  var images=slider.find('img'); 
  var numImages=images.length;
  var zoomSliderAuto;  
  var play=true;
  var wait=false; 
  var maxLineWidth = 2000*numImages;
  slideArea.css("width", parseInt(maxLineWidth)+'px');
  resizeImages();
  createControls(); 
  showImage(currentIndex, 0);   
  if(settings.autoPlay) setAutoPlay(true);
  
  slider.hover(
    function () {
      $("#zoomSlider_btn_next, #zoomSlider_btn_prev").css("display", "block");
    },
    function () {
      $("#zoomSlider_btn_next, #zoomSlider_btn_prev").css("display", "none");
    }
  );
  
  $(window).resize(function() {     
    resizeImages();
    if(!play) showImage(currentIndex, settings.slideDuration);
  });
  
  function resizeImages(){
    var newHeight=parseInt(parseInt($(window).height())*settings.heightMulti); 
    var maHeigth=parseInt(settings.maxHeight);
    if(newHeight > maHeigth) newHeight=maHeigth; 
    
    images.each(function(index){
      var oldWidth=parseInt($(this).css("width"));
      var oldHeight=parseInt($(this).css("height"));
      var newWidth= parseInt((oldWidth * 100) / oldHeight * newHeight);     
      $(this)      
      .css("height",  parseInt(newHeight)+'px')
      .css("width",  parseInt(newWidth/100)+'px');
    }); 
    
  }
  
  function showImage(newIndex, slideDuration){
  if(!wait){
  	wait=true;
    	currentIndex=newIndex;
    	trimmCurrentIndex();
    	var delta_x=0; 
    	images.each(function(index) {
    	  if(index < currentIndex){
    	    delta_x-=parseInt($(this).css("margin-right")); 
    	    delta_x-=parseInt($(this).css("margin-left"));
    	    delta_x-=parseInt($(this).css("width"));
    	  }
    	});               
    	var cImageWidth=parseInt(slider.find('img').eq(currentIndex).css("width"));
    	delta_x=parseInt(delta_x)*10;   
    	delta_x-=parseInt(cImageWidth)*5;   
    	delta_x+=parseInt(slider.css("width"))*5;    
    	delta_x=parseInt(delta_x/10); 
    	delta_x=delta_x-(20*currentIndex);  //wegen 10px margin links und rechts.  	
    	slideArea.css({"marginLeft": slideArea.css("marginLeft")});
    	slideArea.stop().animate(
    	  {marginLeft: delta_x+'px'},
    	  slideDuration,
    	  'jswing',
    	  function(){
    	    hightLight(currentIndex);
    	  }
    	);
    	wait=false;
    }
  }
  
  function hightLight(cindex){       
    images.css('opacity', settings.inactiveOpacity);
    images.eq(cindex).stop().animate({'opacity': settings.activeOpacity}, settings.fadeDuration);
  }
  
  function trimmCurrentIndex(){
      if(currentIndex <= -1) currentIndex=numImages-1;
      if(currentIndex >= numImages) currentIndex=0;
  }
  
  function createControls(){
    slider.append('<div id="zoomSlider_btn_prev"></div>');
    slider.append('<div id="zoomSlider_btn_next"></div>');
    $("#zoomSlider_btn_prev").click(function(){
      if(play) setAutoPlay(false);
      currentIndex--;
      trimmCurrentIndex();
      showImage(currentIndex, settings.slideDuration);
    });
    $("#zoomSlider_btn_next").click(function(){
      if(play) setAutoPlay(false);
      currentIndex++;
      trimmCurrentIndex();
      showImage(currentIndex, settings.slideDuration);
    });
  }
  
  function setAutoPlay(status){ 
    clearInterval(zoomSliderAuto);
    if(status){ 
      play=true;
      zoomSliderAuto=setInterval(function() {
        currentIndex++;
        trimmCurrentIndex();
        showImage(currentIndex, settings.slideDuration);
      }, settings.autoSpeed);    
    }
    else{
      play=false;
    }
  }
  
};

