/* Index page javascript */

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
 
function CustomInit() {
   CarouselInit();

setTimeout(function() { jQuery('#overlay').hide('slow'); }, 30000);
	  
}
var doautoscroll = true; //set autoscroll to true on load
 
function blogs_carousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function(e) {
    //    carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr("name")));
		doautoscroll = true; //turn off autoscroll if link is selected from menu
		
		return false;
    });
	
	 jQuery('#blogs-next').bind('click', function() {
        carousel.next();
		carousel.startAuto(0);
        return false;
    });

    jQuery('#blogs-prev').bind('click', function() {
        carousel.prev();
		carousel.startAuto(0);
        return false;
    });
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
	/*
	$(".jcarousel-control li").mouseout(function(){
     	carousel.startAuto();
		doautoscroll = true;
    }).mouseover(function(){
   		carousel.stopAuto();
		doautoscroll = false;
    });*/
	
	// Pause autoscrolling if the user moves cursor of menu
	jQuery('.jcarousel-control').hover(function() {
    carousel.stopAuto();
    }, function() {
    carousel.startAuto();
    });	
	
	// Select active link from menu
	jQuery(".jcarousel-control a").click(function() {	
	jQuery(".jcarousel-control a").removeClass("active");
	jQuery(this).addClass("active");
	});	
	
	
	/*
	 $().mousemove(function(e){
	 mouseX = e.pageX;
	 mouseY = e.pageY;
	  $("#position").text(e.pageX +', '+ e.pageY);
	  var eleOffset = $(".jcarousel-control").offset();	  
		var eleWidth = $(".jcarousel-control").width();		
		var eleHeight = $(".jcarousel-control").height();
	  var eleOffsetRight = eleOffset.left + eleWidth;
	  
	  if (mouseX > eleOffset.left) {
		  carousel.stopAuto();
	  } else {
		  carousel.startAuto();
	  };

   }); 
	 */
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered after animation.
 */
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    //display('Item #' + idx + ' is now visible'); //For debugging purposes

	
	if (doautoscroll == false){
	// if link from menu is selected stop autoscroll	
	carousel.stopAuto();
	}
	else if (doautoscroll == true) {
		
	var lastItemNum = $(".jcarousel-control li").length;
	var lastItem = parseInt(lastItemNum);
	var num = parseInt(idx);
	//$("#thisnum").text(num);
	//$('#thisnum').text(lastItemNum);
	
	if (idx == num){
	$(".jcarousel-control a").blur(); // remove focus from any menu items clicked
	$(".jcarousel-control a").eq(num - 2).removeClass("active");
	$(".jcarousel-control a").eq(num - 1).addClass("active");
	if (num == 1){$(".jcarousel-control a").eq(lastItem - 1).removeClass("active");}
	}
	}

	
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 * Triggered after animation.

 
//function mycarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
//    display('Item #' + idx + ' is no longer visible');
//};
 */


function mycarousel_itemVisibleInCallbackBeforeAnimation() {		

	
};

/**
 * Helper function for printing out debug messages.
 * Not needed for jCarousel.
 */
var row = 1;
function display(s) {
    // Log to Firebug (getfirebug.com) if available
    //if (window.console != undefined && typeof window.console.log == 'function')
      //  console.log(s);

    if (row >= 1000)
        var r = row;
    else if (row >= 100)
        var r = '&nbsp;' + row;
    else if (row >= 10)
        var r = '&nbsp;&nbsp;' + row;
    else
        var r = '&nbsp;&nbsp;&nbsp;' + row;

    jQuery('#display').html(jQuery('#display').html() + r + ': ' + s + '<br />').get(0).scrollTop += 10000;

    row++;
};




function CarouselInit() {
	
	jQuery('#blogs-list').jcarousel({
        scroll: 1,
        initCallback: blogs_carousel_initCallback,
		auto: 10,
		wrap: 'last',
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
		itemVisibleInCallback: {
			onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation,
			onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
			
        }


    });
	
}

