window.addEvent( 'domready', function() {
	
	var bSaveNavigationOrder = false;
	
	// Stop the link redirection if CTRL is down
	var oNavigationLinks = $$( '#navigation_secondary li a' );
	oNavigationLinks.each( function( oItem, i ) {
		
		oItem.addEvent( 'click', function( oEvent ) {
			if( !oEvent.control ) {
				return true;
			}
			
			oEvent.stop();
		} );
		
	} );
	
	// Make the navigation sortable
	var oSortables = new Sortables( "navigation_secondary", {
		constrain: true,
		clone: false,
		revert: true,
		onComplete: function() {
			bSaveNavigationOrder = true;
		}
	});
	
	window.addEvent( 'unload', function() {
		if( bSaveNavigationOrder ) {
			var aNavItems = $$( '#navigation_secondary li' );
			var sNavIds = "";
			aNavItems.each( function( oItem, i ) {
				sNavIds += ( oItem.getProperty( "id" ).substring( 4 ) ) + ",";
			} );
			
			var oRequest = new Request({
				method: 'post',
				url: '/ajax/updateSecondaryNavOrder',
				isSuccess: function() {
					//alert( 'Saving Secondary Navigation' );
				}
			}).send( "ids=" + sNavIds );
			
			alert( 'Saving Secondary Navigation' );
			
			bSaveNavigationOrder = false;
		}
	});
	
});