/*

	XMX-TeaserGallery - jQuery Content Gallery Plugin
	Author: 		Hamidreza Sharifi
	Version:		1.0 (July 23, 2011)
	Web: 	http://www.xpect-media.de
	
	Please use this development script if you intend to make changes to the
	plugin code.  For production sites, please use jquery.xpectteasergallery.
	
*/

(function($){
	$.fn.xmcteasergallery = function(options) {
       var opts = $.extend($.fn.xmcteasergallery.defaults,options);
       var gallery;  
       var filmstripp;     
       var frames;
       var panels;      
       var stripwrapper;
       var itemscount = 0;
       var filmstripp_wrapp_width = 0;
       var currItem = -1;
       var prev_inactive = 0;
       var next_inactive = 0;
             
       
       function showItem(i){  
            if(currItem == i)
            return;              
            frames.removeClass("clsframe_act").eq(i%itemscount).addClass("clsframe_act");             
            panels.fadeOut(opts.transition_interval).eq(i%itemscount).fadeIn(opts.transition_speed);
            currItem = i;
            
            indexer = i+1 + ' von ' + itemscount;
				    $('#imageviewer #imageviewer-indexer').html('Bild ' + indexer);
       };
       
       function nextPrevImage() {
              
    		      //prev
    	        if(getPos(filmstripp).left > -3 ) {
                  $('#photos .nav-prev').attr('src', opts.img_path + '/prev_des.gif')  ; 
                  $('#photos .nav-prev').css({'cursor':'default'});
                  prev_inactive = 1;                            
              }
              else {
                  $('#photos .nav-prev').attr('src', opts.img_path + '/prev.gif')  ;
                  $('#photos .nav-prev').css({'cursor':'pointer'});
                  prev_inactive = 0;              
              }
              
              //next
              if(stripwrapper.width() - getPos(filmstripp).left >= filmstripp.width() + getPos(filmstripp).left) {
                  $('#photos .nav-next').attr('src', opts.img_path + '/next_des.gif')  ; 
                  $('#photos .nav-next').css({'cursor':'default'});                  
                  next_inactive = 1;           
              }
              else {
                  $('#photos .nav-next').attr('src', opts.img_path + '/next.gif')  ; 
                  $('#photos .nav-next').css({'cursor':'pointer'});
                  next_inactive = 0;             
              }            
        
        } ;
       
       
       function showPrevItem(){
          movePx = 0;
          //ul -> filmstripp
          // div -> stripwrapper
          
          if(getPos(filmstripp).left>= 0 ) {
              nextPrevImage(); 
              return ;
          }        
         
                   
          if(getPos(filmstripp).left * -1 >= stripwrapper.width())  {               
                movePx = stripwrapper.width();                
          }          
          else {
                movePx = -1 * getPos(filmstripp).left;
                
          }
          
          movePx = Math.ceil(movePx);
        
          
          $('.nav-prev').unbind();
          $(filmstripp).animate({
            left: '+=' + movePx},
            opts.transition_speed,opts.easing,function() {
                $('.nav-prev').click(showPrevItem).hover(
                function(){
                  if(prev_inactive == 0)   {
                      $(this).attr('src', opts.img_path+'/prev_hover.gif');
                  }
                },
                function() {
                    nextPrevImage(); 
                });                  
                nextPrevImage();
          });
               
          
       } ;
       
       function showNextItem(){
           movePx = 0;
          //ul -> filmstripp
          // div -> stripwrapper
          
          if(filmstripp.width() <=   stripwrapper.width() ) {
              nextPrevImage(); 
              return ;
          }        
          
          /*
          if(filmstripp.width() - getPos(filmstripp).left < stripwrapper.width() - getPos(filmstripp).left)    {
              nextPrevImage();
              return; 
          } */
             
          
          var k = filmstripp.width()- stripwrapper.width() +  getPos(filmstripp).left - opts.strip_wrapper_marginleft;
          
          
          if( k <= 0 ) {
              nextPrevImage();
              return; 
          }
          //if(stripwrapper.width()- getPos(filmstripp).left >= filmstripp.width() - getPos(filmstripp).left)  {
          if(stripwrapper.width() <= k )  {
               movePx = stripwrapper.width();
               //alert(1);              
          }          
          else {
                //movePx = filmstripp.width() - (stripwrapper.width() - getPos(filmstripp).left) - 2;                
                movePx = k + opts.strip_wrapper_marginleft;                 
          }
          
          movePx = Math.ceil(movePx);
          
          //alert(movePx);
        
          $('.nav-next').unbind();
          $(filmstripp).animate({
              left: '-=' + movePx},
              opts.transition_speed,opts.easing, function() {                   
                   $('.nav-next').click(showNextItem).hover(
                    function(){
                        if(next_inactive == 0)   {
                              $(this).attr('src', opts.img_path+'/next_hover.gif');
                        }
                    },
                    function() {
                        nextPrevImage();
                    });   
                    nextPrevImage();                
              });
              
          
          
       } ;
       
       function getPos(item){
          var objLeft;
          var objTop;
          objLeft = $(item).position().left;
          objTop = $(item).position().top;
          return {'left':objLeft,'top':objTop};
          
       }
       
       function buildPannel() {
          frames.each(function(i){
              
              $(this).addClass("clsframe");
              $(this).removeClass("clsframe_act");
              $(this).unbind();
              $(this).bind("click", function() {
              
                showItem(i);
              
              });
              
              // marginleft and right
              $(this).css({                                         
                 marginRight : opts.overlay_margin_right,
                 marginLeft : opts.overlay_margin_left, 
                 width: opts.overlay_width + 'px',
              });
              filmstripp_wrapp_width = filmstripp_wrapp_width + (opts.overlay_margin_right)  + (opts.overlay_margin_left)  + $(this).width();
              filmstripp_wrapp_width = filmstripp_wrapp_width + opts.overlay_border + opts.overlay_border;
              filmstripp_wrapp_width = Math.ceil(filmstripp_wrapp_width)  ;
          });
          $(filmstripp).css(
              {
      				'position':'relative',        				
              }
          );
          
          $(filmstripp).css({
             'width' :  filmstripp_wrapp_width + 'px',
             'margin' : '0px auto',
          });
          
          $(filmstripp).wrap('<div class="strip_wrapper"></div>');
          stripwrapper  = $('.strip_wrapper');
          stripwrapper.css({
      				'position':'relative',
      				'zIndex':'998',
      				'width' : opts.panel_width - 38 + 'px',
      				'height': opts.overlay_height + 'px',
      				'overflow' : 'hidden',
      				'left' : opts.strip_wrapper_marginleft + 'px',
      				//'paddingLeft' : '2px',
      				'top' : opts.panel_height + opts.overlay_topmargin  + 6 +'px',
      			});
          // ADD NEXT-PREV BUTTON to StripWrapper
          if(opts.show_moving) {
             $('<img />').addClass('nav-next').attr('src',opts.img_path+'/next.gif').appendTo(gallery).css({
      				'position':'absolute',
      				'cursor':'pointer' ,
              'left' : opts.panel_width - 14 +'px',
              'top' : opts.panel_height + (opts.overlay_height/2)  + 5 + 'px', 
             }).click(showNextItem).hover(
                function(){                       
                      if(next_inactive == 0) {               
                           $(this).attr('src', opts.img_path+'/next_hover.gif');
                      }
                } ,
                function() {               
                  nextPrevImage();                     
                }               
            );
      			
      			$('<img />').addClass('nav-prev').attr('src',opts.img_path+'/prev.gif').appendTo(gallery).css({
      				'position':'absolute',
      				'cursor':'pointer',
      				//'right' : '-' + opts.panel_height - (opts.overlay_height) +'px',
              'left' : '5px',              
      				'top' : opts.panel_height + (opts.overlay_height/2) + 5 + 'px',      			
      			}).click(showPrevItem).hover(
                function(){                      
                  if(prev_inactive == 0)   {
                        $(this).attr('src', opts.img_path+'/prev_hover.gif');
                  }
                  
                } ,
                function() {
                  nextPrevImage();                                     
                }               
            );  
          }
       } ;
       
       // MAIN FUNCTION
       return this.each(function() {
           gallery = $(this);
           wrapper = $('.strip_wrapper', this);
           filmstripp = $(".filmstrip", this);
           frames = $(".filmstrip > li", this);
           panels = $(".panel", this); 
           gallery.css({'position':'relative'});
           panels.css({'position':'absolute'});      
           itemscount = panels.length;     
           buildPannel() ; 
             
           if(itemscount > 0) {
              showItem(0);
              nextPrevImage();
           }   
               
       });
            
      /* $('#imageviewer-close').hover(
          function(){
                $(this).attr('src', '/fileadmin/Domain/Files/IMAGES/images/close.gif');
          },
          function(){
                $(this).attr('src', '/fileadmin/Domain/Files/IMAGES/images/close_de.gif');
          }
       ); */
       
  }
  
  
  $.fn.xmcteasergallery.defaults = {    
		panel_width: 666,
		panel_height: 444,
		overlay_width:110,
		overlay_height:88,
    overlay_border:2,				
		overlay_margin_right:6,
		overlay_margin_left:6,
		overlay_topmargin : 12,
		transition_speed: 100,
		transition_interval: 400,
		show_moving:true,
		img_path:'/cms/fileadmin/Domain/Files/IMAGES/images',	
		strip_wrapper_marginleft : 18,
	};
})(jQuery);
