/**
 * Used to log to the console.
 */
jQuery.fn.log = function (msg)
{
	try
	{
		if (console)
			console.log("%s: %o", msg, this);
	}
	catch(e) {}

	return this;
}

jQuery(document).ready(function() {
    featureSlide();
    newsPagination();
    printLinks();
    menuActiveState();
});

var ITEMS_PER_PAGE = 10;
var ITEM_COUNT = 0;

function newsPagination() {
    if (jQuery("#news-listing").length > 0) {
        ITEM_COUNT = jQuery("#news-listing .news-item").length;
        var params = {
            callback: pageSelectCallback,
            items_per_page: ITEMS_PER_PAGE,
		    prev_text: "&#171; previous",
            next_text: "next &#187;"
        };
            
        jQuery("#news-listing .news-item").hide();
        jQuery("#news-pagination").pagination(ITEM_COUNT, params);
    }
}

/**
 * Callback function that displays the content.
 *
 * Gets called every time the user clicks on a pagination link.
 *
 * @param {int}page_index New Page index
 * @param {jQuery} jq the container with the pagination links as a jQuery object
 */
function pageSelectCallback(pageIndex, jq){
    // Change which items are currently being displayed.
    var start = pageIndex * ITEMS_PER_PAGE;
    var end = pageIndex * ITEMS_PER_PAGE + ITEMS_PER_PAGE;

    if (end > ITEM_COUNT) { end = ITEM_COUNT; }

    var $hiddenItems = $newsItems = jQuery("#news-listing .news-item");
    var $visibleItems = $newsItems.slice(start, end);

    $hiddenItems.hide();
    $visibleItems.show();

    return false;
}


var $slides;
var $currentSlide = null;
var slideCount = 0;
var timeoutId = -1;

function featureSlide() {
    var $firstSlide = jQuery('#feature-slide-player .feature-slide:first');
    $currentSlide = $firstSlide;
    $slides = jQuery('#feature-slide-player .feature-slide');
    slideCount = $slides.length;

    $slides.not($firstSlide).hide();
    jQuery("div.pagination a:eq(1)").addClass("active");

    var $previous = jQuery('#feature-slide-player .pagination a.previous');
    var $next = jQuery('#feature-slide-player .pagination a.next');
    var $links = jQuery('#feature-slide-player .pagination a').not($previous).not($next);

    $previous.click(function() {
        var index = $slides.index($currentSlide);
        index--;
        if (index < 0) {
            index = slideCount-1;
        }
        $previousSlide = $slides.eq(index);
        showSlide($previousSlide);
    });

    $next.click(function() {
        var index = $slides.index($currentSlide);
        index++;
        if (index >= slideCount) {
            index = 0;
        }
        $nextSlide = $slides.eq(index);
        showSlide($nextSlide);
    });

    $links.click(function() {
        var index = $links.index($(this));
        $slide = $slides.eq(index);
        showSlide($slide);
    });
    
    // Start automated animation.
    timeoutId = setTimeout("rotateFeatureSlide()", 5000);
}

function rotateFeatureSlide() {
    // We're running the automated rotation, so clear out the timeoutId to -1.
    timeoutId = -1;
    
    // This code is currently duplicate of the next click function.
    // @todo: refactor it a little...
    var index = $slides.index($currentSlide);
    index++;
    if (index >= slideCount) {
        index = 0;
    }
    $nextSlide = $slides.eq(index);
    showSlide($nextSlide);
}

function showSlide($newSlide) {
    $currentSlide.fadeOut('normal', function() {
        $newSlide.fadeIn();
    });

    $currentSlide = $newSlide;
    
    // Set the corresponding navigation link to active (removing other active states).
    // +1 to offset the << previous link.
    var i = $slides.index($newSlide) + 1;
    jQuery("div.pagination a").removeClass("active");
    jQuery("div.pagination a:eq(" + i + ")").addClass("active");
    
    // We've just shown a new slide so avoid animating too quickly,
    // but set the automated rotation to run x seconds from now.
    if (timeoutId != -1) {
        clearTimeout(timeoutId);
        timeoutId = -1;
    }        
    timeoutId = setTimeout("rotateFeatureSlide()", 5000);
}

function printLinks() {
    jQuery('a.print-link').click(function() {
        window.print();
        return false;
    });
}

function menuActiveState() {

    // This client-side function is used to select and highlight the active tab
    // in the fixed top-level menu set in MainMenu.ascx [Adrian Viti 09-07-2010]

    // List of page names from MainMenu.ascx [Adrian Viti 09-07-2010]
    var u = ["home", "sell", "bill", "procure", "customers", "partners", "company"];

    // List of menu item text entries (i.e. the "Text" attribute)
    // These will be used to search for the correct menu item to set active
    var m = ["Home", "Sell", "Bill", "Procure", "Customers", "Partners", "Company"];
    
    var url = window.location.pathname;
    for (var i = 0; i < u.length; i++) {
        if (url.indexOf(u[i]) != -1) {
            var $anchor = jQuery("#main-menu li a:contains(" + m[i] + ")");            
            var $li = $anchor.parent();
            $li.addClass("active");            
            
            return;
        }
    }
}
