/*!
 * jQuery Slider Plugin
 * Originally based on the work of: http://css-tricks.com/moving-boxes/
 * Version: 1.1 (06-JUN-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.3.1 or later
 *
 */
(function($){ //jQuery.noConflict()compliant
   $.fn.slidewin = function(options) {

      // build main options before element iteration
      //----------------------------------------------------------------------
      var s = $.extend({}, $.fn.slidewin.defaults, options);

      // firebug console output
      //----------------------------------------------------------------------
      function debug(msg) {
         if (window.console && window.console.log && opts.debug) {
             window.console.log(msg);
         }
      }

      return this.each(function(){

         var obj = $(this);

         var blocked                = false;
         var slider                 = obj.find(".slider");
         var scroll                 = obj.find(".scroll");
         var totalPanels            = obj.find(".scrollContainer").children().size();
         var regWidth               = obj.find(".panel").css("width");
         var regHeight              = obj.find(".panel").css("height");
         var regImgWidth            = obj.find(".panel img").css("width");
         var regImgDivWidth         = obj.find(".panel .inside div").css("width");
         var regImgDivHeight        = obj.find(".panel .inside div").css("height");
         var regImgDivBorderImage   = obj.find(".panel .inside div").css("background-image");
         var regParSize             = obj.find(".panel p").css("font-size");
         var $panels                = obj.find('.scrollContainer  div.panel');
         var $container             = obj.find('.scrollContainer');

         //set callback to click
         if( typeof s.clickCallback == 'function')
         {
            $panels.each(function(){
               _o = $(this);
               _o.find('.inside').click(s.clickCallback, this);
            });
         }

         //set callback to checkboxes
         if( typeof s.checkboxCallback == 'function')
         {
            $panels.each(function(){
               _o = $(this);
               _o.find('input[type=checkbox]').click(s.checkboxCallback, this);
            });
         }


         $panels.css({'float' : 'left','position' : 'relative'});

         $container
            .css('width', ($panels[0].offsetWidth * $panels.length) + ($panels.length * s.scrollWidth) )
            .css('left', s.scrollLeft);

         scroll.css('overflow', 'hidden');


         function change(direction){

            //if not at the first or last panel
            if((direction && !(s.curPanel < totalPanels)) || (!direction && (s.curPanel <= 1))) { return false; }

            //if not currently moving
            if ((blocked == false)) {

               blocked = true;

               var next         = direction ? s.curPanel + 1 : s.curPanel - 1;
               var leftValue    = $container.css("left");
               var movement     = direction ? parseFloat(leftValue, 10) - s.movingDistance : parseFloat(leftValue, 10) + s.movingDistance;

               $container
                  .stop()
                  .animate({
                     "left": movement
                  }, function() {
                     blocked = false;
                  });

               returnToNormal(s.panelPrefix+s.curPanel);
               growBigger(s.panelPrefix+next);

               s.curPanel = next;

               //remove all previous bound functions
               nextPan = $(s.panelPrefix+(s.curPanel+1));
               nextPan.unbind();
               //go forward
               nextPan.click(function(){ change(true); });

               //remove all previous bound functions
               prevPan = s.panelPrefix+(s.curPanel-1);
               $(prevPan).unbind();
               //go back
               $(prevPan).click(function(){ change(false); });

               //remove all previous bound functions
               $(s.panelPrefix+s.curPanel).unbind();
            }
         }

         function growBigger(element) {
            $(element)
               .animate({
                  width: s.curWidth,
                  height: s.curHeight
               })
               .addClass('big')//.end()
               .find(".inside div")
               .animate({
                  width: s.curImgDivWidth,
                  height: s.curImgDivHeight
               }, 'slow' , "swing", function(){
                  $(this).css({'background-image':"url('"+s.curImgDivBorderImage+"')"})
               })
               .end()
               .find("img")
               .animate({ width: s.curImgWidth })
               .end()
               .find("p")
               .animate({ fontSize: s.curParSize })
               .css({'font-weight':s.curParWeight})
               .end();

            if( typeof s.afterGrow == 'function')
            {
               s.afterGrow(element);
            }
            //alert(s.curParSize);
         }

         function returnToNormal(element){
            $(element)
               .animate({
                  width: regWidth,
                  height: regHeight
               })
               .removeClass('big')//.end()
               .find(".inside div")
               .animate({
                  width: regImgDivWidth,
                  height: regImgDivHeight
               }, 'slow' , "swing", function(){
                  $(this).css({'background-image': regImgDivBorderImage})
               })
               .end()
               .find("img")
               .animate({ width: regImgWidth })
                .end()
               .find("p")
               .animate({ fontSize: regParSize })
               .css({'font-weight':"normal"})
               .end() ;

               //alert(regParSize);
         }


         // Set up "Current" panel and next and prev
         _panel = s.panelPrefix+s.curPanel;
         //alert(_panel);
         growBigger(_panel);

         //change(true, this);
         if(s.curPanel > 1)
         {
            _prevPanel = s.panelPrefix+(s.curPanel-1);
            $(_prevPanel).click(function(){ change(false); });
         }
         if(s.curPanel < totalPanels)
         {
            _nextPanel = s.panelPrefix+(s.curPanel+1);
            $(_nextPanel).click(function(){ change(true); });
         }

         //when the left/right arrows are clicked
         obj.find(".right").click(function(){ change(true); });
         obj.find(".left").click(function(){ change(false); });

      });
   };

   $.fn.slidewin.defaults = {
         movingDistance : 120,
         //ampiezza box  slide selezionata
         curWidth : 121,
         //altezza box slide selezionata
         curHeight : 121,
         //ampiezza div x bg immagine  slide selezionata
         curImgDivWidth : 106,
         //ampiezza div x bg immagine  slide selezionata
         curImgDivHeight : 106,
         //imnagine cornice box ingrandito
         curImgDivBorderImage : '../images/palette-border-big.png',
         //ampiezza immagine  slide selezionata
         curImgWidth : 91,
         //font-size p  slide selezionata
         curParSize : "0.9em",
         curParWeight : "bold",
         //per settare ampiezza scrollContainer
         // opts.$container.css('width', (opts.$panels[0].offsetWidth * opts.$panels.length) + (opts.$panels.length * opts.scrollWidth) )
         scrollWidth : 120,
         //spostamento laterale al click
         scrollLeft : '104px',
         //prefisso dei box (numerati progressivamente nell'html '#panel_1, #panel_2)
         panelPrefix : '#panel_',
         //box attivo di default
         curPanel : 2,
         //callback function for checkboxes
         checkboxCallback: null,
         clickCallback: null,
         //callback after grow big
         afterGrow: null
    };
})(jQuery);
