$(document).ready(function() {

	$(".ajax").colorbox();
	$(".truncate").ellipsis();
	$(".tip[title]").tooltip({offset: [-5,0]});

   jQuery("#nivoslider-125").nivoSlider({
			borderRadius: 6,
        effect:"fade",
        slices:15,
        boxCols:8,
        boxRows:4,
        animSpeed:500,
        pauseTime:3000,
        startSlide:0,
        directionNav:true,
        directionNavHide:true,
        controlNav:true,
        controlNavThumbs:false,
        controlNavThumbsFromRel:true,
        keyboardNav:true,
        pauseOnHover:true,
        manualAdvance:false
    });



   $('.accordionButton').click(function() {

      $('.accordionButton').removeClass('on');

      $('.accordionButton').removeClass('six-round-top');
      $('.accordionButton').removeClass('six-round');
      $('.accordionButton').addClass('six-round');

      $('.accordionContent').slideUp('normal');

      if($(this).next().is(':hidden') == true) {
			$(this).addClass('on');
			$(this).removeClass('six-round');
			$(this).removeClass('six-round-top');
			$(this).addClass('six-round-top');
			$.jStorage.set('sidebar-selected', $(this).attr('id'));
         $(this).next().slideDown();
       }
    });


   //ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER 
   $('.accordionButton').mouseover(function() {
      $(this).addClass('over');

   //ON MOUSEOUT REMOVE THE OVER CLASS
   }).mouseout(function() {
      $(this).removeClass('over');
   });

   /*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/

   /********************************************************************************************************************
   CLOSES ALL S ON PAGE LOAD
   ********************************************************************************************************************/
   $('.accordionContent').hide();

	var selected_sidebar = $.jStorage.get('sidebar-selected');
	if ( !selected_sidebar || !$('#' + selected_sidebar) ) {
		$('.defaultaccordion').click();
	} else {
		$('#' + selected_sidebar).click();
	}

	$('.justify').characterJustify();

	if ( $('.featured-next').length > 0 ) {

		var current_featured=0;
		var featured_data = {};

		// get featured content from server
		$.getJSON('/json/featured', function(data) {
			featured_data=data;
			updateFeatured(0);	
		});

		$('.featured-next').click(function() {
			updateFeatured('next');
		});

		$('.featured-previous').click(function() {
			updateFeatured('previous');
		});

	}


	function updateFeatured ( id ) {
	
		if ( id == 'next' ) id = parseInt(current_featured)+1;
		if ( id == 'previous' ) id = parseInt(current_featured)-1;

		if ( id >= 0 && id < featured_data.length ) {

			$('#featured-photo').attr('src',featured_data[id].photo);
			$('#featured_item').html(featured_data[id].company_item_id);
			$('#featured_description').html(featured_data[id].name);
			current_featured=id;

			if ( current_featured == 0 ) {
				$('.featured-previous').hide();
			} else { 
				$('.featured-previous').show();
			}

			if ( current_featured == (featured_data.length-1) ) {
				$('.featured-next').hide();
			} else { 
				$('.featured-next').show();
			}
		}
		
	}

});


