var home_rotator_index = 1, home_rotator_panes = 2, home_rotator_active = true;
var news_rotator_index = 1, news_rotator_panes = 3, news_rotator_active = true;

var i = 0;

function home_rotator_rotate(specified_tab) {
    var tab_obj = $('#ht_tab_' + specified_tab);
    if (!$(tab_obj).hasClass('active')) {
        $('#homepage_rotator .rotator_panes_large li').css('display', 'none');
        $($('#homepage_rotator .rotator_panes_large li')[specified_tab - 1]).fadeIn(500);
        $('#homepage_rotator_control li').attr('class', '');
        $(tab_obj).addClass('active');
        return true;
    }
    return false;
}
function news_rotator_rotate(news_index) {
    var news_obj = $($('#control_news li')[news_index - 1]);

    if (!$(news_obj).hasClass('active')) {
        $('#news_rotator div.li').css('display', 'none'); //(600);
        $($('#news_rotator div.li')[news_index - 1]).fadeIn(450);
        $('#control_news li').attr('class', '');
        $(news_obj).addClass('active');
        return true;
    }
    return false;
}
function news_rotator_auto() {
    if (news_rotator_active) {
        news_rotator_index += 1;
        if (news_rotator_index > news_rotator_panes) {
            news_rotator_index = 1;
        }
        news_rotator_rotate(news_rotator_index);
    }
}
function home_rotator_auto() {
    if (home_rotator_active) {
        home_rotator_index += 1;
        if (home_rotator_index > home_rotator_panes) {
            home_rotator_index = 1;
        }
        home_rotator_rotate(home_rotator_index);
    }
    news_rotator_auto();
}
function news_rotator_init() {
    $('#control_news a').click(function (e) {
        e.preventDefault();
        var news_index = parseInt($(this).html());
        news_rotator_rotate(news_index);
        return false;
    });
    $('#news_rotator_module').mouseover(function () {
        news_rotator_active = false;
    });
    $('#news_rotator_module').mouseout(function () {
        news_rotator_active = true;
    });
    $('#control_news li').mouseover(function (e) {
        e.preventDefault();
        news_rotator_active = false;
        if (!$(this).hasClass('activehover')) {
            $(this).addClass('activehover');
        }
    });
    $('#control_news li').mouseout(function (e) {
        e.preventDefault();
        if ($(this).hasClass('activehover')) {
            $(this).removeClass('activehover');
        }
        news_rotator_active = true;
    });

}

function home_rotator_init() {

    var num = $('#homepage_rotator .rotator_panes_large li').length;
    home_rotator_panes = num;
    if (num > 0) {
        for (i = 1; i < num + 1; i+=1) {
            // Create a tab for the slide
            $('#homepage_rotator_control ul').append('<li id="ht_tab_' + i + '"><a href="#">' + i + '</a></li>');
            // Set the position of the overlay from the class
            var div_object = $($('#homepage_rotator .rotator_panes_large li')[i-1]).find('div'), div_class = $(div_object).attr('class');
            div_class = (div_class !== undefined) ? div_class : '';
            if (div_class.substr(0, 2) === 'px') {
                $(div_object).attr('style', 'top: ' + div_class.substr(2) + 'px');
            }
        }
        $('#homepage_rotator_control').css('display', 'block');
        $('#homepage_rotator_control li:first').attr('class', 'active');
    }

    $('#homepage_rotator').mouseover(function () {
        home_rotator_active = false;
    });
    $('#homepage_rotator').mouseout(function () {
        home_rotator_active = true;
    });
    $('#homepage_rotator_control li').mouseover(function (e) {
        e.preventDefault();
        home_rotator_active = false;
        if (!$(this).hasClass('activehover')) {
            $(this).addClass('activehover');
        }
    });
    $('#homepage_rotator_control li').mouseout(function (e) {
        e.preventDefault();
        if ($(this).hasClass('activehover')) {
            $(this).removeClass('activehover');
        }
        home_rotator_active = true;
    });

    $('#homepage_rotator_control li').click(function (e) {
        e.preventDefault();
        var specified_tab = parseInt($(this).attr('id').substring(7));
        home_rotator_rotate(specified_tab);
        return false;
    });

    news_rotator_init();
}

function scrollToActivePage() {
	var this_page = window.location.pathname;
	var menu_inner = $('.module_section_menu .menu_inner');
	if (menu_inner.length && this_page !== '') {
		var menu_offset = menu_inner.offset().top;
		$('.module_section_menu .menu_inner ul a').each(function (i, item) {
			if ($(item).attr('href') === this_page) {
				menu_inner.attr('id', 'activemenu');
				document.getElementById('activemenu').scrollTop = ($(item).offset().top - menu_offset - 30);
			}
		});
	}
}

$(document).ready(function () {

    home_rotator_init();
    setInterval('home_rotator_auto()', 7000);

    $('.print_moi_link').css('display', 'block');
    $('.print_moi_link').click(function (e) {
        e.preventDefault();
        window.print();
        return false;
    });

    scrollToActivePage();
	
	$('.module_section_menu > div > ul > li.open > a').click(function() {
		$a = $(this);
		$a.siblings().slideToggle('slow').parent().toggleClass('open').toggleClass('formerlyopen');
		return false;
	});
	$('.module_section_menu > div > ul > li.open > ul > li.open > a').click(function() {
		$b = $(this);
		$b.siblings().slideToggle('slow').queue(function(next){$b.parent().toggleClass('open').toggleClass('formerlyopen'); next();});
		return false;
	});

});
