/**
 *
 * Chromatik Media Inc. - jSlider
 *
 * @author		Marc Giroux
 * @version		1.0
 * @Copyright	Chromatik Media Inc.
 * @License		GPL
 * 
 * This library requires jQuery 1.4+
 *
 */

//(function($){$.fn.jslide=function(options){var defaults={orientation:'horizontal',preselected:1,autoResize:false,autoReset:false,autoSlide:false,autoSlideTime:2000,speed:800,fade:true,className:'slide',containerClass:'slidecontainer',showArrows:true,arrowSize:16,arrowLeft:null,arrowRight:null,useHash:false,hashClass:'navig'};var options=$.extend(defaults,options);var num=0;var container_width;var container_height;var master=$(this);setupContainer($('.'+options.containerClass+' :first-child',this),$(this));if(options.orientation=='horizontal'){setupHorizontal($('.'+options.className,this));}else{setupHorizontal($('.'+options.className,this));}var totalwidth=num*container_width;$('.'+options.containerClass,this).css('position','relative');$('.'+options.containerClass,this).css('width',totalwidth);var current_slide=1;var total_slides=num;var current_instance=$('.'+options.containerClass,this);if(options.showArrows===true){$(this).append('<div id="'+options.containerClass+'_arrow_left"><img src="'+optionsh)-container_width);moveSlide(current_instance,move,current_slide-1);}}});}if(options.useHash===true){$('.'+options.hashClass).click(function(){var slide=$(this).attr('href');var slide_id=slide.substr(6);if(slide_id>current_slide){current_slide=slide_id-1;var move=-(current_slide*container_width);}else{current_slide=slide_id;var move=-((current_slide*container_width)-container_width);}moveSlide(current_instance,move,slide_id-1);return false;});}if(options.autoSlide===true){window.setInterval(function(){$('#'+options.containerClass+'_arrow_right',master).click();},options.autoSlideTime);}if(options.preselected>1){window.setTimeout(function(){var move=-((options.preselected*container_width)-container_width);moveSlide(current_instance,move,options.preselected-1);},10);}function setupHorizontal(obj){obj.each(function(){$(this).css('float','left').css('position','relative');if(options.autoResize===true){if(num==0){$(this).height(container_height);}}else{$(this).height(container_height);}num++;});}function setupContainer(obj,thisObj){container_width=obj.width();container_height=obj.height();thisObj.css('overflow','hidden').css('position','relative');thisObj.css('clear','both').width(container_width).height(container_height);}function refreshControls(active_slide){var cycle=1;$('.'+options.className,master).each(function(){if(cycle==active_slide+1){master.stop().animate({height:$(this).height()},'medium');container_height=$(this).height();if(options.showArrows===true){$('#'+options.containerClass+'_arrow_left',master).stop().animate({top:(container_height/2)-(options.arrowSize/2)},options.speed);$('#'+options.containerClass+'_arrow_right',master).stop().animate({top:(container_height/2)-(options.arrowSize/2)},options.speed);}}cycle++;});}function moveSlide(instance,move,slide){if(options.fade===true){instance.fadeTo('fast',0.5).animate({left:move},options.speed,function(){$(this).fadeTo('fast',1.0);if(options.autoResize===true){refreshControls(slide);}});}else{instance.animate({left:move},options.speed);}}};})(jQuery);

/**
*
* jSlider - jQuery plugin
*
* @version        1.0
* @author        Marc Giroux
* @copyright    2010 Chromatik Media Inc.
*
*/

/*
TODO
   MAKE VERTICAL MODE (with arrow support)
*/
var interval;
(function($)
{
   $.fn.jslide = function(options) 
   {
       /* Default options */
       var defaults = 
       {
           /* Orientation */
           orientation: 'horizontal',
           
           /* Preselected slide */
           preselected: 1,
           
           /* Resizing option */
           autoResize: false,
           
           /* Autoreset option */
           autoReset: false,
       
           /* Auto slide config */
           autoSlide: true, autoSlideTime: 10000,
           
           /* Animation related */
           speed: 800, fade: false,
           
           /* Master dom elements */
           className: 'slide', containerClass: 'slidecontainer',
           
           /* Arrow support */
           showArrows: true, arrowSize: 16, arrowLeft: null, arrowRight: null,
           
           /* Hash support */
           useHash: false, hashClass: 'navig'
       };
       
       /* Merge defaults with passed options */
       var options = $.extend(defaults, options);
        
       /* System variables */
       var num = 0;
       var container_width;
       var container_height;
       var master = $(this);
       
       /* Setup the container size */
       setupContainer($('.' + options.containerClass + ' :first-child', this), $(this));
       
       /* Orientation setup */
       if (options.orientation == 'horizontal') {
           setupHorizontal($('.' + options.className, this));
       } else {
           setupHorizontal($('.' + options.className, this));
           
           // TODO
           //setupVertical($('.' + options.className, this));
       }
       
       /* Make the container the right size */
       var totalwidth = num * container_width;
       /* Container width */
       $('.' + options.containerClass, this).css('position', 'relative');
       $('.' + options.containerClass, this).css('width', totalwidth);
       
       /* Slide maintenance */
       var current_slide = 1;
       var total_slides = num;
       var current_instance = $('.' + options.containerClass, this);
       
        /* Show arrows and enable the clicks */
       
           /* Left Arrow */
           $(this).append('<div id="' + options.containerClass + '_arrow_left"><img src="' + options.arrowLeft + '" /></div>');
           $('#' + options.containerClass + '_arrow_left', this).css('cursor', 'pointer').css('width', options.arrowSize).css('position', 'absolute').css('left', 0).css('top', (container_height / 2) - (options.arrowSize / 2));
           
           /* Right Arrow */
           $(this).append('<div id="' + options.containerClass + '_arrow_right"><img src="' + options.arrowRight + '" /></div>');
           $('#' + options.containerClass + '_arrow_right', this).css('cursor', 'pointer').css('width', options.arrowSize).css('position', 'absolute').css('left', container_width - options.arrowSize).css('top', (container_height / 2) - (options.arrowSize / 2));
           
           /* Slide to the right */
           $('#' + options.containerClass + '_arrow_right', this).click(function()
           {
               if (current_slide < total_slides) {
                   var move = -(current_slide * container_width);
                   moveSlide(current_instance, move, current_slide);
                   slider_linker(current_slide,false);
                   current_slide++;
               } else if (current_slide == total_slides) {
                   if (options.autoReset === true) {
                       moveSlide(current_instance, 0, 0);
                       slider_linker(current_slide,true);
                       current_slide = 1;                       
                   }
               }
           });
            
           /* Slide to the left */
           $('#' + options.containerClass + '_arrow_left', this).click(function()
           {
               if (current_slide > 1) {
                   current_slide--;

                   var move = -((current_slide * container_width) - container_width);
                   moveSlide(current_instance, move, current_slide-1);
               } else {
                   if (options.autoReset === true) {
                       current_slide = total_slides;
                       var move = -((current_slide * container_width) - container_width);
                       moveSlide(current_instance, move, current_slide-1);
                   }
               }
           });
       if (options.showArrows === false) {
       		$('#slidecontainer_arrow_left').css('display','none');
       		$('#slidecontainer_arrow_right').css('display','none');
       }
        /* Enable hash support */
       if (options.useHash === true) {
           $('.' + options.hashClass).click(function()
           {
               /* Get the slide id (href should be a slideX, X being the slide number) */
               var slide = $(this).attr('href');
               var slide_id = slide.substr(6);
               
               if (slide_id > current_slide) {
                   /* Set new slide has current slide */
                   current_slide = slide_id - 1;
                   var move = -(current_slide * container_width);
               } else {
                   /* Set new slide has current slide */
                   current_slide = slide_id;
                   var move = -((current_slide * container_width) - container_width);
               }
               
               moveSlide(current_instance, move, slide_id - 1);
               
               
               /* Cancel anchor effect */
               return false;    
           });
       }
        /* Activate timers for autoslide (if requested) */
       
       if (options.autoSlide == true) {
           interval = setInterval(function()
           {
               $('#' + options.containerClass + '_arrow_right', master).click();
           }, options.autoSlideTime);
       }
       
       /* Set a timer to slide to the preselected slide */
       if (options.preselected > 1) {
           window.setTimeout(function()
           {
               var move = -((options.preselected * container_width) - container_width);
               moveSlide(current_instance, move, options.preselected - 1);
           }, 10);
       }
        
       /*
        ------------------------------------
        -            Plugin Methods             -
        ------------------------------------
       */
       
       /**
        *  
        * setupHorizontal
        *
        */
       function setupHorizontal(obj)
       {
           /* Make all .classname elements float side by side */
           obj.each(function()
           {
               $(this).css('float', 'left').css('position', 'relative');
               
               if (options.autoResize === true) {
                   if (num == 0) {
                       $(this).height(container_height);
                   }
               } else {
                   $(this).height(container_height);
               }
               
               num++;
           });
       }
       
       /**
        *
        * setupContainer
        *
        */
       function setupContainer(obj, thisObj)
       {
           /* Get the size of the slides in the container */
           container_width = obj.width();
           container_height = obj.height();
           
           /* Setup the container box */
           thisObj.css('overflow', 'hidden').css('position', 'relative');
           thisObj.css('clear', 'both').width(container_width).height(container_height);
       }
       
       /**
        *
        * refreshControls
        *
        */
       function refreshControls(active_slide)
       {
           var cycle = 1;

           $('.' + options.className, master).each(function()
           {
               if (cycle == active_slide + 1) {
                   master.stop().animate({height: $(this).height()}, 'medium');        
                   container_height = $(this).height();
                   
                   /* Move arrows if they are used */
                   if (options.showArrows === true) {
                       $('#' + options.containerClass + '_arrow_left', master).stop().animate({top: (container_height / 2) - (options.arrowSize / 2)}, options.speed);
                       $('#' + options.containerClass + '_arrow_right', master).stop().animate({top: (container_height / 2) - (options.arrowSize / 2)}, options.speed);
                   }
               }
               cycle++;
           });
       }
        /**
        *
        * moveSlide
        *
        */
       function moveSlide(instance, move, slide)
       {
           if (options.fade === true) {
               instance.fadeTo('fast', 0.5).animate({left: move}, options.speed, function()
               {
                   $(this).fadeTo('fast', 1.0);
                   
                   /* Resize the containing div because we have the diff to stretch to the height of the next one */
                   if (options.autoResize === true) {
                       refreshControls(slide);                    
                   }
               });
           } else {
               instance.animate({left: move}, options.speed);
           }
       }
   };
})(jQuery);
