/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		carousel.options.auto = 0;
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		carousel.options.auto = 0;
        return false;
    });
};

// Ride the carousel...
jQuery(document).ready(function() {
    jQuery("#homeCarousel").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
		auto: 6,
		wrap: 'last',
		itemVisibleInCallback: updateControl // update control for next button
    });
    
    // news item next/previous
	$("a.previousBtn").live("click", function(){ previousPage(); });
	$("a.nextBtn").live("click", function(){ nextPage(); });
	// submit form
	$("a.btnSubmit").click(function() {
		  $('#sendContact').submit();
	});
	
	$("a#tab1").click(function(event) {
		  showTab(1);
		  event.preventDefault();		 
	});
	$("a#tab2").click(function(event) {
		  showTab(2);
		  event.preventDefault();		
	});
});

var activeTab = 1;
function showTab(number){	
	if(activeTab != number){
		$("div#tabContent"+activeTab).hide();
		$("div#tabContent"+number).show();
		activeTab = number;
		
		$("#iframeTabs a").removeClass('active');
		$("#iframeTabs #tab"+number).addClass('active');
		
		$("#iframeTabs").attr('class','activeTab'+number);

	}
}

//update control
function updateControl(carousel, li, currentItem){
	
	//set active button	
	activeNumber = currentItem;
	switchImage(activeNumber);	
}

function switchImage(activeNumber){
	$('.jcarousel-control a').removeClass('active');
	$('#control'+activeNumber).addClass('active');
}

var currentNewsPage = 1;
function previousPage(){
	// hide old item
	$('#newsGroupItems'+currentNewsPage).hide();
	// calculate new item
	currentNewsPage = currentNewsPage - 1;
	// show next item
	$('#newsGroupItems'+currentNewsPage).show();
	// calculate next / previous button
	calculateButtons();	
}

function nextPage(){
	// hide old item
	$('#newsGroupItems'+currentNewsPage).hide();
	// calculate new item
	currentNewsPage = currentNewsPage + 1;
	// show next item
	$('#newsGroupItems'+currentNewsPage).show();
	// calculate next / previous button
	calculateButtons();	
}

function calculateButtons(){
	var totalPages = $('#totalPages').val();
	
	// previous link
	if(currentNewsPage > 1){		
		$('span.previousBtn').hide();
		$('a.previousBtn').show();
	}else{
		$('span.previousBtn').show();
		$('a.previousBtn').hide();
	}
	
	// next link
	if(currentNewsPage <= (totalPages -1)){		
		$('span.nextBtn').hide();
		$('a.nextBtn').show();
	}else{
		$('span.nextBtn').show();
		$('a.nextBtn').hide();
	}
}
