/*
 * Rauno Väli
 * Front page accordion menu
 */
jQuery(document).ready(function(){
	toggleSubmenu();
	toggleLogin();
});

function delayAction(el) {	
	delay = window.setTimeout(
		function(){
			jQuery('#accordion').accordion("activate",el);
		},200);	
}
function cancelAction() {
	window.clearTimeout(delay);
}
/*
 * Rotator script for images on frontpage
 */
function theRotator() {
	//Set the opacity of all images to 0
	jQuery('#rotator  li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	jQuery('#rotator  li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',5000);
	
}
function rotate() {	
	//Get the first image
	var current = (jQuery('#rotator  li.show') ?  jQuery('#rotator  li.show') : jQuery('#rotator  li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jQuery('#rotator  li:first') :current.next()) : jQuery('#rotator  li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
}
/*
 * Toggle submenu to show or not 
 */

function toggleSubmenu() {
	//By default we are not showing any submenus	
	jQuery('#sub_menu ul').hide();
	var opened = "";
	//If cursor leaves from header, we will close menu or open focused menu
	
	jQuery('#header').bind('mouseleave',function(){
		setTimeout(function(){
			jQuery('#sub_menu ul').hide();
			if(opened != "") {
				jQuery('#'+opened+'_submenu').show();
			}
		},0);
	});
	//Loop all main menu items. Adding mouseover and mouseout functions for links that have id=submenu.
	//If we have opened menu(active) then we show it by default
	jQuery('#main_menu ul li').each(function(){	
		if(jQuery(this).children('a').hasClass('active')) {
			jQuery('#'+jQuery(this).attr('id')+'_submenu').show();
			opened = jQuery(this).attr('id');
		}
		//If we have id attribute, then we have a submenu and we add mouseover effect
		if(jQuery(this).attr('id') != "") {
			jQuery(this).bind('mouseover', function(){
				jQuery('#sub_menu ul').hide();
				jQuery('#'+jQuery(this).attr('id')+'_submenu').show();
			});
		//If no id then we do not have submenu and mouseover will close submenu or will show opened(active) submenu.
		}else if(jQuery(this).attr('id') == "") {			
			jQuery(this).bind('mouseover', function(){
				jQuery('#sub_menu ul').hide();
				if(opened != "") {
					jQuery('#'+opened+'_submenu').show();
				}
			});
		}		
	});	
}

keepLogInPopup = false;//when focus in on a login element, do not hide login
mouseOverLogin = false;//when mouse is over login, do not hide it
tabbingLogin = false;  //allow tabbing even if mouse is not over login

/*
 * Toggle login to show or not
 */
function toggleLogin() {
	//By default we are not showing the login
	jQuery('#login_box').hide();


	//If cursor leaves from login, we will close login popup
	jQuery('#all_login').bind('mouseleave',function(){
		if(!keepLogInPopup) {
			jQuery('#login_box').hide();
		}
                mouseOverLogin = false;
	});

	jQuery('#all_login').bind('mouseover', function() {
            if(jQuery('#login_box').is(':visible') == false) {
		jQuery('#login_box').show();
                jQuery('#login_field_textbox_id').focus();
            }
            mouseOverLogin = true;
	});

	jQuery('.login_input')
            .bind('focus', function() {
		keepLogInPopup = true;
            })

            .bind('keydown', function(e) {
                tabbingLogin = false;
                if(e.keyCode == 9) {
                    tabbingLogin = true;
                }
            })

            .bind('blur', function() {
		keepLogInPopup = false;
                if(mouseOverLogin == false && tabbingLogin == false) {
                    jQuery('#login_box').hide();
                }
                tabbingLogin = false;
        });

	//If mouseover login, popup the login box
	jQuery('#login_title').bind('mouseover', function(){
		jQuery('#login_box').show();
	});

	jQuery('.submit_form').bind('click', function() {
		jQuery(this).parents('form:first').submit();
	});
}
