﻿/**************************************************************************************
    Marcia: Opening categories saved in the cookie
            
    Duplicating the expand category function in raw jquery ($j) because ajax content
    requires livequery which binds click event to the a tag when it is clicked (on demand), 
    so the click function is not defined yet. 
            
    If I include tabs.js to define the click event, then the event is bound mutliple times,
    so you would see the loading happing mulitiple times (you'll see multiple loading graphics).
        
**************************************************************************************/
// Had to set the current activePageId here since this load before parent page with the HiddenPageId
//alert('SubPage:  set active page id cookie ' + $jqInner('#ct100_HiddenSubPageId').val());
//m2SetActivePageToCookie('activePageId', $jqInner('#ct100_HiddenSubPageId').val());

var openCatArray = new Array();
openCatArray = m2GetCatArrayFromCookie();

// Marcia:  This was causing Sitecore Preview 'unterminated string' issue as it was returning a function{ instead of the id
//var x;
//for (x in openCatArray) {
for (var x = 0; x < openCatArray.length; x++) {
    if (openCatArray[x] != 'SJM') {
        // Async calls getting out of sync with wrong openCatArray[x], so building the string and then executing;
        var jsStr = '';
        jsStr =
                "$jq('#" + openCatArray[x] + " .content').load(" +
                "	$jq('#a" + openCatArray[x] + "').attr('href')," +
			    "	function() {" +
			    "   $jq('#" + openCatArray[x] + " .content').css('display', 'block'); " +
			    "	    $jq('#" + openCatArray[x] + " .ctrl a').append('<img class=\"loader\" alt=\"loading\" src=\"" + uri_imgpath + "ajax-loader.gif\" />');" +
			    "	    $jq('#" + openCatArray[x] + "').slideDown(" +
			    "			animspeed," +
			    "			function() {" +
                "			    $jq('#" + openCatArray[x] + "').removeClass('closed').removeClass('not-loaded').addClass('opened').addClass('loaded');" +
			    "			    $jq('#" + openCatArray[x] + " img.loader').remove();" +
			    "			}" +
			    "		);" +
			    "	}" +
                ");";

        //alert('jsStr ' + jsStr);
        eval(jsStr);
    }
};