$(window).load(function () {
    hero.init();
});
var hero = {
    $images: $('#main div.section_images img'),
    $anchors: $('#main div.section_nav div.anchor'),
    autoCyclePeriodInSeconds: 5,
    autoCycleTimerName: 'HeroBoxAutoCycleTimer',
    init: function () {
        $('#main div.section_nav div a').mouseover(function (e) {
            $(document).stopTime(hero.autoCycleTimerName);
			var parentDiv = $(this).parent('div.anchor');			
            hero.selectItem(parentDiv);
        });
        $('#main').mouseout(function (e) {
            hero.startNextCycle();
        });
        hero.startNextCycle();
    },
    selectItem: function ($item) {
		var count = 0;
		hero.$anchors.each(function(){			
			if( $(this).attr('id') == $item.attr('id') ){
				linkOver = count;
			}
			count ++;				   
		});
        //var linkOver = $item.attr('id').split('-')[1] - 1;			
        hero.switchImage($item, linkOver);
    },
    switchImage: function ($t, index) {
        hero.$images.hide();
        $('#main div.section_nav div').removeClass('selected');
        $t.addClass('selected');
        hero.$images.eq(index).show();
    },
    startNextCycle: function () {
        $(document).stopTime(hero.autoCycleTimerName);
        $(document).oneTime(hero.autoCyclePeriodInSeconds * 1000, hero.autoCycleTimerName, function () {
            hero.onCycleComplete();
        });
    },
    onCycleComplete: function () {
        hero.autoCycleToNextItem();
        hero.startNextCycle();
    },
    autoCycleToNextItem: function () {
        var maxIndex = hero.$anchors.size() - 1;
        var currentItemIndex = hero.getCurrentItemIndex();
        var nextItemIndex = hero.circularIncrement(currentItemIndex, maxIndex, 0);
        $nextItem = hero.$anchors.eq(nextItemIndex);		
        hero.switchImage($nextItem, nextItemIndex);
    },
    getCurrentItemIndex: function () {
        return hero.$anchors.index(hero.$anchors.filter('.selected').eq(0));
    },
    circularIncrement: function (i, max, min) {
        var x = i + 1;
        return (x > max) ? min : x;
    }
};
