fortess = {}

fortess.rotateReferences = function() {
        var d = $('#ref-rotation-window > .rotation').first().fadeOut('fast', function (){
            $('#ref-rotation-window > .rotation').first().fadeIn('fast');
        }).detach().appendTo('#ref-rotation-window');
}

// set event handlers for main menu
fortess.set_menu_handlers = function() {
        // main buttons' handlers
	$('#header2 .page_item').mouseover(function() {
                $(this).addClass('active');
                $('div', this).stop(true).fadeTo('fast', 1).width($(this).width());
        });
	$('#header2 .page_item').mouseout(function() {
                $(this).removeClass('active');
                $('div', this).fadeOut('fast');
        });
	$('#header2 .page_item div').mouseover(function() {
                $(this).parent().addClass('active');
                $(this).stop(true).fadeTo('fast', 1);
        });
};

fortess.set_menu_handlers_horizontal  = function() {
        // main buttons' handlers
	$('#header2 .page_item').mouseover(function() {
                // hide all submenus, show active submenu
                // this way we get '15' from className 'page_item page-item-15 current_page_ancestor'
                var id = this.className.split(' page-item-').pop().split(' ').shift();
                $('div.submenu-wrap > :not(div.parent-id-'+id+')').stop(true).hide();
                $('a', this).addClass('active');
                $('.submenu-wrap div.parent-id-'+id).stop(true).fadeTo('fast', 1).show();
        });
	$('#header2 .page_item').mouseout(function() {
                $('.submenu-wrap div').fadeOut('fast');
                $('a',this).removeClass('active');
        });

        // submenu handlers
	$('.submenu-wrap div').mouseover(function() {
                // show active menu
                var id = this.className.split('-').pop();
                $('#header2 .page-item-'+id+' a').addClass('active');
                $(this).stop(true).fadeTo('fast', 1);
        });
	$('.submenu-wrap div').mouseout(function() {
                // hide active menu
                var id = this.className.split('-').pop();
                $('#header2 .page-item-'+id+' a').removeClass('active');
                $('.submenu-wrap div').delay(200).stop(true).fadeOut('fast');
        });
};

// set up toggle behavior on references' page
fortess.ajax_post_content_toggle = function() {
        // set onclick handling for all
	$('ol.archive .excerpt .entry-toggle').unbind().click(function() {
                
                jQuery('.archive > li').removeClass('entry-active');
                jQuery(this.parentNode.parentNode).addClass('entry-active');
                jQuery(':not(.entry-active) .content-toggle', '.archive').hide();
		jQuery(this).parent().next().slideToggle();
                return false;
        });

        // show the selected post, if any
        if(null != location.hash) {
                jQuery('#post-content-' + location.hash.substr(2) + '-target').show().parent().addClass('entry-active');
        }
}

$(document).ready(function () {
        // main menu
        fortess.set_menu_handlers();  
        fortess.ajax_post_content_toggle();

        // customer references on main page
        $('#ref-rotation-window').children(':not(:first-child)').hide();
        window.setInterval(fortess.rotateReferences, 6000 );
        $('#ref-rotation-window').click(function () {
                // 2do open reference page
                var to = $('#ref-rotation-window > .rotation').first().attr('id');
                window.location = 'customer/references/#'+to;
        });
 });
