var curFirstIndexIn = 1,  curLastIndexIn = 0, curSelectedIndex = 1, prevSelectedIndex = 0;
var updatedIndex;
var countNextClick = 0;



var $preIdex;

$preIndex = document.getElementById("itemsNews-0");

function mycarousel_initCallback(carousel){  

jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
		countNextClick = countNextClick + 1;
		
		if((countNextClick * 4) < $totalItems ) {
		    curFirstIndexIn = curFirstIndexIn + 4;
		} else {	
		    curFirstIndexIn = 1;
			countNextClick = 0;
		}
		curLastIndexIn = curFirstIndexIn + 3;
		if (curLastIndexIn > $totalItems) {
		    // readjust FirstIndexIn
			var oddIdexIn = curLastIndexIn - $totalItems;
			curFirstIndexIn = curFirstIndexIn - oddIdexIn;
		    curLastIndexIn = $totalItems;
		}				
		var tempStr = curFirstIndexIn + ' - ' + curLastIndexIn + ' of ' + $totalItems;
	    document.getElementById('items-visible-count').innerHTML= tempStr;		
		
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
		
		if($totalItems > curFirstIndexIn) {
		    curFirstIndexIn = curFirstIndexIn - 4;
		} 
		if (curFirstIndexIn <= 0){ 
		    curFirstIndexIn = 1; 
			countNextClick = 0;
		}		
		curLastIndexIn = curFirstIndexIn + 3;		
		var tempStr = curFirstIndexIn + ' - ' + curLastIndexIn + ' of ' + $totalItems;
	    document.getElementById('items-visible-count').innerHTML= tempStr;
        return false;
    });
	
	jQuery('#mycarousel > li').bind('click', function() {	
	 //set mouseclick on small image
	 
		    curSelectedIndex = $("li.jcarousel-list").index(this);
			
			//take care for double click on the last item of the strip
			if (curSelectedIndex == prevSelectedIndex) { 
			   return false;
			}else {	 
				change_newsLargeItem(this);				
			}	
					
	 });
 
 // Disable autoscrolling if the user clicks the prev or next button. 
  // carousel.buttonNext.bind('click', function() {carousel.startAuto(0);});       
   //carousel.buttonPrev.bind('click', function() {carousel.startAuto(0);});
 
 // Pause autoscrolling if the user moves with the cursor over the clip. 
    //carousel.clip.hover(function() {carousel.stopAuto();}, function() {
        //carousel.startAuto();});
    
  };

jQuery(document).ready(function() {
       
        $totalItems = $("#mycarousel li").length; 	
        curLastIndexIn = curFirstIndexIn + 3;
	    updatedIndex = jQuery('<div>' + curFirstIndexIn + ' - ' + curLastIndexIn + ' of ' + $totalItems + '</div>');
	    updatedIndex.prependTo('#items-visible-count');
		
		//for the top banner
		setInterval( "slideSwitch()", 5000 );
				
     jQuery('#mycarousel').jcarousel({
        vertical: true, 
		scroll: 4, 
		//auto scroll strip after certain time in second
		//auto: 5, 
		wrap: 'last', 
		itemFirstInCallback:  mycarousel_itemFirstInCallback,
		initCallback:mycarousel_initCallback});


	 // rotate news items every 10 seconds
	 $("#mycarousel").everyTime(10000,function(i) {
			rotate_items();
		});	
	 //install a calendar
	 $("#calendar").datepick({});
	 
	 
	 
	 //accordion
	 $(".accordion2 h3").eq(2).addClass("active");
	$(".accordion2 p").eq(2).show();

	 
$(".accordion2 h3").click(function(){
		$(this).next("p").slideToggle("fast")
		 
.siblings("p:visible").slideUp("fast");
		$(this).toggleClass("active");
		 
$(this).siblings("h3").removeClass("active");
	});
	 
	
});



/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {      
    curFirstIndexIn = idx;
	//alert('Item #' + idx + ' is now the last item');
	//if (idx == 5) {
	  //carousel.startAuto(1);
	//}else {
	  //carousel.startAuto(0); 
	//}  		
	$($preIndex).removeClass("ui-tabs-selected");
	$(item).addClass("ui-tabs-selected");
	$preIndex = $(item);			
		
	$(item).trigger('click');
			
	return false;
};


function rotate_items(){
if (curSelectedIndex < $totalItems){
				curSelectedIndex = curSelectedIndex + 1;
				
				$curScrollItem =  $("#mycarousel li").eq(curSelectedIndex);	
				change_newsLargeItem($curScrollItem);	
					if (curSelectedIndex > (curLastIndexIn -1)){
			    		$("#mycarousel-next").trigger('click');
					}					
			}else { curSelectedIndex = 0;}
		
			//return FALSE causing the function run once only
			//return false;
}

function change_newsLargeItem(curObjecSelected) {
    $($preIndex).removeClass("ui-tabs-selected");
	$(curObjecSelected).addClass("ui-tabs-selected");
	$preIndex = curObjecSelected;
					
	$frame = "#frame-" + curSelectedIndex;
	$($frame).removeClass("hide");
	$frame = "#frame-" + prevSelectedIndex;
	$($frame).addClass("hide");
		
	prevSelectedIndex = curSelectedIndex;	
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}