function togglerClicked (obj, ev) {
    if($(obj).is('.opened')) {
        $(obj).removeClass('opened').next('.toggle-panel').slideUp('fast' , hideFloatingParsys);
    } else {
        $(obj).addClass('opened').next('.toggle-panel').slideDown('fast' , showHiddenParsys);
    }
    ev.preventDefault();
}

function hideFloatingParsys() {
    if ( isAuthor() ) {
        setTimeout(
        function() 
        {
            $('#CQ').children().each(function() {
                if ($(this).css('width') == '0px') {
                    $(this).css('display', 'none');
                    $(this).addClass('hiddenFloatParsys');
                }
            });
        }, 1500);
    }    
}

function showHiddenParsys() {
    if ( isAuthor() ) {
    setTimeout(
        function() 
        {
    
        $('#CQ').children().each(function() {
            if ($(this).hasClass('hiddenFloatParsys') && $(this).css('width') != '0px') {
                $(this).css('display', 'block');
                $(this).removeClass('hiddenFloatParsys');
            }
        });
    }, 1500);
    }
}

/**
 * Initialize the expand menus
 * Make sure everything has an active or inactive class to begin with.
 * Toggle those classes on click.
 * 
 * Written by Nick D from acquity for CQ 5.2. Moved from ui.js into this js for cq5.4
 * 
 * @returns {void}
 * @author Nick Davison
 */
function initExpandMenus() {
	// Default closed
	parentSections=$('.expand_menu li').each(function(k, v) {
		// If they have neither an active or inactive class, make them inactive by default
		if ( (!$(v).is('active')) && (!$(v).is('inactive')) ) {
			$(v).addClass('inactive');
			$(v).find('.content').css('display', 'none');
		}
	});

	// Bind the clicks for all h3s within expand menus
	$('.expand_menu h3').bind('click', function(e) {
		e.preventDefault();
		// Find their parent li
		parentLis=$(this).parents('li');
		
		// Swap the active/inactive classes
		if ($(parentLis[0]).is('.active')) {
			$(parentLis[0]).removeClass('active');
			$(parentLis[0]).addClass('inactive');
			
			$(parentLis[0]).find('.content').slideUp({duration: 200});
		} else {
			$(parentLis[0]).removeClass('inactive');
			$(parentLis[0]).addClass('active');
			
			$(parentLis[0]).find('.content').slideDown({duration: 200});
		}
	});
	
	// Bind the close links
	$('.expand_menu .close').bind('click', function(e) {
		e.preventDefault();
		// Find their parent li
		parentLis=$(this).parents('li');
		
		$(parentLis[0]).removeClass('active');
		$(parentLis[0]).addClass('inactive');
		
		$(parentLis[0]).find('.content').slideUp({duration: 200});
	});
}



function isAuthor() {
    return isEditMode() || isDesignMode();
}

function isEditMode() {
    var mode = getMode();

    return !mode || mode == 'edit';
}

function isDesignMode() {
    return getMode() == 'design';
}

function getMode() {
    return readCookie("wcmmode");
}

function readCookie(name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';

    for (var i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == name )
        {
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 )
            {
                return unescape( decodeURI(a_temp_cookie[1]).replace(/^\s+|\s+$/g, '').replace(/\+/g,' ') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
        }
    }
    return null;
}
$(document).ready(function(){
    hideFloatingParsys();
    initExpandMenus();
});
