/* 
 * jrslider customized script
 * Developed by Roshan Bhattarai
 */
(function($){
 $.fn.jrslider = function(options) {

  var defaults = {
   speed: 5000,
   items: 4,
   activeClass:'selected'  
   };
  var options = $.extend(defaults, options);
  return this.each(function() { 
	  obj = $(this); 
          actClass = options.activeClass;
          //console.log($('.jrpanel',obj).length+' '+actClass);
          $('#tabnavs a:first',obj).addClass(actClass);
          
		  $('.jrpanel',obj).not(':first').css('display','none');
		  //for image
		  
		  
            
          var currIndex = 0;
          
          function rotateSlides(){
                
                var nextIndex = currIndex ==(options.items-1)? 0 : currIndex+1 ;
				//change the tabs background
                $('#tabnavs a:eq('+nextIndex+')',obj).addClass(actClass);
				$('#tabnavs a:eq('+currIndex+')',obj).removeClass(actClass);
                
				//now change the background image
				/*var $active = $('#background img.active');
				var $next =  $active.next().length ? $active.next()
        		: $('#background IMG:first'); */
				var $active = $('#background div:eq('+currIndex+')');
				var $next =   $('#background div:eq('+nextIndex+')');
				
				$active.addClass('last-active');
				//console.log(currIndex+' '+nextIndex);
				$next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 2.0}, 4000, function() {
						$active.removeClass('active last-active');
					});
				
				//change the slider
				$('.jrpanel:eq('+nextIndex+')',obj).fadeIn(4000);
				$('.jrpanel:eq('+currIndex+')',obj).fadeOut(4000);
				//console.log(currIndex+' '+nextIndex)
				currIndex++;

                if( currIndex >= options.items )
					currIndex = 0;
            }

          theInt = setInterval(rotateSlides, options.speed);
          //$('li h2',obj).unbind('mouseenter').unbind('mouseleave');
          $('#tabnavs a',obj).click(function(){
              	clearInterval(theInt);
              	var cuRdivIndex=$('#tabnavs a',obj).index(this);
              	currIndex = cuRdivIndex;
              	
				//change the tabs background
                $('#tabnavs a',obj).removeClass(actClass);
				$(this).addClass(actClass);
				
				//now change the background
				var $active = $('#background div.active');
				var $next =   $('#background div:eq('+currIndex+')');
				
				$active.addClass('last-active');
				//console.log(currIndex+' '+nextIndex);
				$next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 2.0}, 4000, function() {
						$active.removeClass('active last-active');
					}); 
				
				//now change the panel
				$('.jrpanel:visible',obj).fadeOut(4000);
				$('.jrpanel:eq('+currIndex+')',obj).fadeIn(4000);
				
          });	  
  });
 };
})(jQuery);


