var ccSelectionId = null;
var ccdata = null;
var ccparamEntryPath = null;
var cctabSelectionIndex = 0;
var CC_INDEX_ALL_CHEFS = 0;
var ccallChefEntries = null;
var ccallChefRecipeEntries = new Array();
var ccrecipeCatEntries = null;
var cctablist = new Array("/allchefs", "/recipe_cat_appetizers", "/recipe_cat_soupsalad", "/recipe_cat_entrees", "/recipe_cat_desserts", "/recipe_cat_drinks", "/recipe_cat_sides");
var ccisIE = ((navigator.appVersion.indexOf("MSIE") != -1) ? true : false);
var ccisFirstLoad = true;
var ccshowPopUp = false;
var cccbkRecipe = false;
var ccShowAnimation = true;
var ccloadComplete = false;
var ccisWindowActive = true;
var ccbackToRecipeList = false;

var ccbaseUrl = hostUrl[campaignidSA] + "/mc/templates/macys/guide/xml.jsp?jsonresponse=1&campaign_id="+campaignidSA+"&channel_id=1&bundle_entryPath=";

var CC_RESPONSE_CODE_SUCCESS = 0;
var CC_OP_GET_VOTES = 1;
var CC_OP_DO_VOTE = 2;

var ccchefIndex, ccrecipeIndex;
var cclikeCount = 0;
var ccrecipeDeepLink = null;
var myCallbackSwf= 
{
    onOpCancelled: function()
    {
        //closePage();
    },
    onOpCompleted: function(code, msg, opcode, retval1, retval2) 
    {
        if(code == CC_RESPONSE_CODE_SUCCESS)
        {
            if(opcode == CC_OP_GET_VOTES)
            {
                if(retval1)
                {
                    cclikeCount = retval1;
                }
                jx.load(ccbaseUrl + ccparamEntryPath, afterGetVotes, "application/x-www-form-urlencoded", "GET", aftergetvoteserrcallback);
            }
            else if(opcode == CC_OP_DO_VOTE)
            {
                if(retval1)
                {
                    cclikeCount = retval1;
                }
                ccShowAnimation = false;
                
                var numpeoplelikes = document.getElementById("numpeoplelike");
                if(numpeoplelikes)
                {
                    numpeoplelikes.innerHTML = cclikeCount+' people like the recipe!';
                }
                //ccshowRecipeCB(ccChefId, ccRecipeId);
            }
        }
        else if(showAlerts)
        {
            alert("error while getting voting details response code = " + code + " response msg = " + msg + " opcode = " + opcode);
        }
    },
    PercentLoaded : function() {return 100;}
};

// TODO: change entry_fileurl to entry_cdnfileurl
/*recipe image and product image both currently taken from entry_fileurl*/

function ccselectLink(el)
{
    el.style.color="red" ;
}

function setInnerHtmlForShowingRecipeList(innerHtml)
{
    var ccrlcontent = document.getElementById("ccrlcontent");
    if(ccrlcontent)
    {
        ccrlcontent.innerHTML = innerHtml;
    }
    else
    {
        innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries);
        platformShowPopup(innerHtml, 586, 634, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
        ccshowPopUp = false;
    }
}

function ccsetSelected(el)
{
    var curElementId = el.id;
    if(ccSelectionId != curElementId)
    {
        for(var i = 1; i <= 7; i++)
        {
            var element = document.getElementById("recipe" + i);
            if(curElementId == "recipe"+i)
            {
                cctabSelectionIndex = i -1;
                element.src = VERSION_DIR+ "img/tab_recipe_by_sel" + i + ".png";    
                ccSelectionId = curElementId;
                
                var callback = function(ccdata)
                {
                    setInnerHtmlForShowingRecipeList(ccdata);
                }
                ccshowRecipeList(cctabSelectionIndex);
            }
            else
            {
                element.src = VERSION_DIR+ "img/tab_recipe_by_desel" + i + ".png";
            }
        }
    }
}


function ccshowRecipeList(tabIndex)
{
    if(showAlerts)
    {
        alert("ccshowRecipeList called tabIndex=" + tabIndex);
    }

    ccisWindowActive = true;
    var pageids = ['ca-so-cc-recipes.chef', 'ca-so-cc-recipes.appt', 'ca-so-cc-recipes.soups', 
        'ca-so-cc-recipes.entrees', 'ca-so-cc-recipes.desserts', 'ca-so-cc-recipes.drinks', 'ca-so-cc-recipes.sides'];

    if(tabIndex < pageids.length)
    {
        platformRegisterPageView(pageids[tabIndex], 'ca-so-cc-recipes.cat');
    }
    
    ccSelectionId = "recipe" + (tabIndex + 1);
    cctabSelectionIndex = tabIndex;    
    if(ccisFirstLoad && !ccloadComplete)
    {
        doFirstLoad(tabIndex, ccshowRecipeListCB);
    }
    else
    {
        ccshowRecipeListCB(tabIndex);
    }
}

function ccshowRecipe(chefId, recipeId, link, backToRecipeList)
{
    ccbackToRecipeList = backToRecipeList;    
    
    if(showAlerts)
    {
        alert("ccshowRecipe called chefId=" + chefId + " recipeId=" + recipeId + " link=" + link);
    }
    ccisWindowActive = true;
    platformRegisterPageView('ca-so-cc-recipes.' + recipeId, 'ca-so-cc-recipes.list'); 
    
    ccrecipeDeepLink = link;

    if(ccisFirstLoad && !ccloadComplete)
    {
        cccbkRecipe = true;
        doFirstLoad(CC_INDEX_ALL_CHEFS, ccshowRecipeCB, chefId, recipeId);
    }
    else
    {
        ccshowRecipeCB(chefId, recipeId);
    }

}

function ccshowRecipeListCB(tabIndex)
{
    if(tabIndex != CC_INDEX_ALL_CHEFS)
    {
        cctabSelectionIndex = tabIndex;
        var afterCategoryLoad = function(result)
        {
            jsonObject = JSON.parse(result); 
            ccrecipeCatEntries[tabIndex - 1] = jsonObject.root.bn_c;
            var innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);
                setInnerHtmlForShowingRecipeList(innerHtml);
            ccisFirstLoad = false;
        };

        var errcallback1 = function()
        {
            if(showAlerts)
            {
                alert("error while connecting......" + (ccbaseUrl + ccparamEntryPath) + " index = " + index);
            }
        };
        
        if(ccloadComplete)
        {
            if(ccrecipeCatEntries && ccrecipeCatEntries[tabIndex - 1])
            {
                var innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);            
                setInnerHtmlForShowingRecipeList(innerHtml);
                ccisFirstLoad = false;
            }
            else
            {
                if(!ccrecipeCatEntries)
                {
                    ccrecipeCatEntries = new Array();
                }
        ccparamEntryPath = cctablist[cctabSelectionIndex];        
                //to draw the loading gif
                createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);            
        jx.load(ccbaseUrl + ccparamEntryPath, afterCategoryLoad, "application/x-www-form-urlencoded", "GET", errcallback1);    
    }
        }        
        else if(ccisWindowActive)
    {        
            var innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);
            if(ccisFirstLoad || ccshowPopUp)
            {  
                platformShowPopup(innerHtml, 586, 634, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));                 
                ccshowPopUp = false;
            }
            else
            {
                setInnerHtmlForShowingRecipeList(innerHtml);
            }
            ccisFirstLoad = false;
        }
        
    }
    else if(ccisWindowActive)
    {        
        cctabSelectionIndex = tabIndex;
        var innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries);
        if(ccisFirstLoad || ccshowPopUp)
        {
            platformShowPopup(innerHtml, 586, 634, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
            ccshowPopUp = false;
        }
        else
        {
            setInnerHtmlForShowingRecipeList(innerHtml);
        }
        ccisFirstLoad = false;        
    }
}

var ccChefId, ccRecipeId;

function doFirstLoad(tabIndex, callback, chefId, recipeId)
{  
    var index = 0;
    ccdata = null;
    cctabSelectionIndex = tabIndex;
//    ccSelectionId = "recipe" + (tabIndex + 1);
    ccChefId = chefId;
    ccRecipeId = recipeId;
    var afterLoad = function(result)
    {  
        var jsonObject = JSON.parse(result); 
        var afterRecipeLoad = function(result)
        {
            jsonObject = JSON.parse(result);
            ccallChefRecipeEntries[index] = jsonObject.root.bn_c;
            index++;            
            if(!cccbkRecipe && (cctabSelectionIndex == CC_INDEX_ALL_CHEFS || index == 1))//loading anim must be called only once
            {
                callback(cctabSelectionIndex);
            }
            
            if(index == ccallChefEntries.length)
            {
                ccloadComplete = true;                
                if(cccbkRecipe)
                {
                    cccbkRecipe = false;
                    callback(ccChefId, ccRecipeId);
                }
                else
                {
                    callback(cctabSelectionIndex);
                }
            }
            else
            {
                ccparamEntryPath = "/recipe_chef_" + ccallChefEntries[index].entry_entryprops.ct_prop_1;                
                jx.load(ccbaseUrl + ccparamEntryPath, afterRecipeLoad, "application/x-www-form-urlencoded", "GET", errcallback1);
            }
        };
        
        var errcallback1 = function()
        {
            if(showAlerts)
            {
            alert("error while connecting......" + (ccbaseUrl + ccparamEntryPath) + " index = " + index);
            }
        };
        
            ccallChefEntries = jsonObject.root.bn_c;
            if(!cccbkRecipe)
            {
                callback(cctabSelectionIndex);
            }
            ccparamEntryPath = "/recipe_chef_" + ccallChefEntries[index].entry_entryprops.ct_prop_1;            
            jx.load(ccbaseUrl + ccparamEntryPath, afterRecipeLoad, "application/x-www-form-urlencoded", "GET", errcallback1);
    };
    
    var errcallback = function()
    {
        if(showAlerts)
        {
        alert("error while connecting......" + (ccbaseUrl + ccparamEntryPath) + " index = " + index);
        }
    };

    if(cccbkRecipe)
    {
       ccshowWaitAnimForRecipe();
    }

    ccparamEntryPath = cctablist[(ccisFirstLoad) ? 0 : cctabSelectionIndex];
        jx.load(ccbaseUrl + ccparamEntryPath, afterLoad, "application/x-www-form-urlencoded", "GET", errcallback);
    }


function ccshowWaitAnimForRecipe()
{
    if(ccisWindowActive)
    {
        var innerHtml = ('<table id="header" class="ccrDisplayArea ccrDrawBorder" cellspacing="0" cellpadding="0" valign="top" >'+ 
                                    '<tr><td><table align="right" cellspacing="0" cellpadding="0">'+ 
                                        '<tr class="ccrButtonClose">'+ 
                                            '<td valign="top">'+ 
                                                '<img class="ccrlCursorHand ccCloseAndPrintButtonWH" id="ccidCloseButton" src="'+VERSION_DIR+'img/button_close.png" onclick="closePage();return false;"></img>' + 
                                                    '</td>'+ 
                                                        '</tr>'+ 
                                                            '</table></td></tr>'+
                                                                '<tr><td width="746px" height="632px" align="center" valign="middle"><img  border="none" align="center" src="'+VERSION_DIR+'img/loading_anim.gif"></img></td></tr>' +
                                                                    '</table>');
        platformShowPopup(innerHtml, 746, 632, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
    }
}

function ccshowRecipeCB(chefId, recipeId)
{
    var entryIdArray = new Array();
    ccChefId = chefId;
    ccRecipeId = recipeId;
    ccchefIndex = getChefIndex(ccallChefEntries, chefId);
    ccrecipeIndex = getRecipeIndex(ccallChefRecipeEntries, ccchefIndex, recipeId);
    ccparamEntryPath = "/pdt_recipe_" + ccallChefRecipeEntries[ccchefIndex][ccrecipeIndex].entry_entryprops.ct_prop_1;    
    entryIdArray[0] = ccallChefRecipeEntries[ccchefIndex][ccrecipeIndex].entry_id;
    ccshowWaitAnimForRecipe();
    getVotesFor(myCallbackSwf, entryIdArray);
}

var afterGetVotes = function(result)
{
    var jsonObject = JSON.parse(result);
    var productEntries = jsonObject.root.bn_c;
    if(ccisWindowActive)
    {
        var innerHtml = createHTMLForRecipe(ccallChefEntries[ccchefIndex], ccallChefRecipeEntries[ccchefIndex][ccrecipeIndex], productEntries);
        platformShowPopup(innerHtml, 746, 632, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
        ccShowAnimation = true;
        ccisFirstLoad = false;
    }
};

var aftergetvoteserrcallback = function()
    {
        if(showAlerts)
        {
        alert("error while connecting...... pdt_recipe_" + ccallChefRecipeEntries[chefIndex][recipeIndex]);
        }
    };

function ccshowStoreEvent()
{
    if(showAlerts)
    {
        alert("ccshowStoreEvent called");
    }

    platformRegisterPageView('ca-so-cc-events', 'ca-so-culinary council'); 
    
    var afterLoad = function(result)
    {
        var jsonObject = JSON.parse(result);
        var storeEventEntries = jsonObject.root.bn_c;
        var innerHtml = createHTMLForStoreEvent(storeEventEntries);
        platformShowPopup(innerHtml, 730, 582, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
    };
    
    var errcallback = function()
    {
        if(showAlerts)
        {
        alert("error while connecting......" + (ccbaseUrl + ccparamEntryPath));
        }
    };
    
    ccparamEntryPath = "/allevents";    
    jx.load(ccbaseUrl + ccparamEntryPath, afterLoad, "application/x-www-form-urlencoded", "GET", errcallback);    
}
    
function getChefAndRecipesList()
{
    var chef_recipe = ( ''+
                '<table class="ccrlDisplayAreaTable" cellpadding="0" cellspacing="0">'+
                                '<tr>' + 
                        '<td valign="top">'+
                        '<div class="ccrlChefListOverFow">'+
                            '<table class="ccrlPaddingLeft10 ccrlDrawBorder ccrlChefListHeight" cellspacing="0" cellpadding="0">' +                     
                                '<tr>');
chef_recipe +=  generateChefListByTabIndex(cctabSelectionIndex, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);                                    
                               chef_recipe+= (''+
                                            '</tr>' + 
                                        '</table>' + 
                                        '</div>'+
                            '<div class="ccrlDivScrollContainer">' +
                            '<div id="ccrlDivContainer" class="ccrlDivScroll">' + 
                                            '<a name="top"/>' +
                                '<table  class="ccrlPaddingLeft10 ccrlPaddingTop10" cellspacing="0" cellpadding="0">');
chef_recipe +=  generateChefRecipeListByTabIndex(cctabSelectionIndex, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);
                                   chef_recipe+=  (''+
                                                    '<tr>' + 
                                            '<td height="3px">'+
                                                        '</td>' + 
                                                    '</tr>' + 
                                                '</table>' + 
                                '</div>'+
                            '</div>' + 
                                            '</td>' + 
                                        '</tr>' + 
                '</table>');
    return chef_recipe;
}
                            
function generateChefListByTabIndex(tabIndex, allChefEntries, allChefRecipeEntries, recipeCatEntries)
{
    var chefNames = "";
    var chefNamesList = [];
    var defaultNoOfColumns = 4;
    var defaultNoOfRow = 4;
    if(tabIndex == CC_INDEX_ALL_CHEFS)
    {
        if(allChefEntries)
        {
            var noOfDataPerColumn = Math.ceil(allChefEntries.length / defaultNoOfColumns);
            noOfDataPerColumn = noOfDataPerColumn < defaultNoOfRow ? defaultNoOfRow : noOfDataPerColumn;
            var noOfChfNameToShow = noOfDataPerColumn * defaultNoOfColumns;
            for(var i=0; i < noOfChfNameToShow; i++)//allChefEntries.length
            {
                if(i%noOfDataPerColumn ==0)
                {
                    chefNames += ''+
                                    '<td class="ccrlChefColCls ">' ;
                }
                if(i < allChefEntries.length)
                {
                    chefNames += ''+
                                    '<a class="ccrlFontStyleVerdanaTitle" href="#" onclick="ccselectLink(this); platformAnchorJump(\''+allChefEntries[i].entry_label1+'\',\'ccrlDivContainer\');return false;">'+allChefEntries[i].entry_label1+'</a><br/>';
                }
                else
                {
                    chefNames += ''+
                                        '<font class="ccrlFontStyleVerdanaTitle">&nbsp;</font><br/>' ;
                }

                if(i > 0 && ((i + 1) % noOfDataPerColumn) ==0)// || i == allChefEntries.length - 1)
                {
                    chefNames +=  ''+
                                    '</td>';
                }
            }
        }
    }
    else
    {
        var len = 0; //length associated with chefNamesList
        if(allChefEntries && recipeCatEntries && recipeCatEntries[tabIndex - 1])
        {
            for(var i = 0; i < recipeCatEntries[tabIndex - 1].length; i++)
            {
                for(var k = 0; k < allChefEntries.length; k++)
                {
                    if(((recipeCatEntries[tabIndex - 1][i].entry_entryprops.ct_prop_2).toLowerCase()) == ((allChefEntries[k].entry_entryprops.ct_prop_1).toLowerCase()))
                    {
                        //need to call addUniqueEntries function
                        if(len == 0)
                        {
                            chefNamesList[len++] = allChefEntries[k].entry_label1;                            
                        }
                        else
                        {
                            var matchFound = false;
                            for(var m = 0; m < len; m++)
                            {
                                if(allChefEntries[k].entry_label1 == chefNamesList[m])
                                {  
                                    matchFound = true;
                                }
                            }

                            if(!matchFound)
                            {
                                chefNamesList[len++] = allChefEntries[k].entry_label1;
                            }
                        }
                    }
                }
            }

            var noOfDataPerColumn = Math.ceil(chefNamesList.length / defaultNoOfColumns);
            noOfDataPerColumn = noOfDataPerColumn < defaultNoOfRow ? defaultNoOfRow : noOfDataPerColumn;
            var noOfChfNameToShow = noOfDataPerColumn * defaultNoOfColumns;

            for(var n = 0; n < noOfChfNameToShow; n++)//len
            {
                if(n%noOfDataPerColumn ==0)
                {
                    chefNames += ''+
                        '<td class="ccrlChefColCls ">' ;
                }
                if(n < len)
                {
                    chefNames += ''+
                            '<a class="ccrlFontStyleVerdanaTitle" href="#" onclick="ccselectLink(this); platformAnchorJump(\''+chefNamesList[n]+'\',\'ccrlDivContainer\');return false;">'+chefNamesList[n]+'</a><br/>';                    
                }
                else
                {
                    chefNames += ''+
                            '<font class="ccrlFontStyleVerdanaTitle">&nbsp;</font><br/>' ;
                }

                if(n > 0 && ((n + 1) % noOfDataPerColumn) ==0)// || n == len - 1)
                {
                    chefNames +=  ''+
                        '</td>';
                }
            }
        }
    }    
    return chefNames;
}

function generateChefRecipeListByTabIndex(tabIndex, allChefEntries, allChefRecipeEntries, recipeCatEntries)
{
     var chefRecipeList = "";
     if(tabIndex == CC_INDEX_ALL_CHEFS)
     {
        if(allChefEntries && allChefRecipeEntries)
        {
            for(var i = 0; i < allChefEntries.length; i++) 
            {
                chefRecipeList += ( ''+
                                        '<tr>' + 
                        '<td class="ccrlSpaceForEveryLine">'+
                            '<a id="'+allChefEntries[i].entry_label1+'"/>' + 
                            '<font class="ccrlFontStyleVerdana14px">'+allChefEntries[i].entry_label1+'<br/></font>');
                            
                if(allChefRecipeEntries[i])
                {
                for(var k = 0; k < allChefRecipeEntries[i].length; k++) 
                {
                    chefRecipeList +=  ''+
                            '<a class="ccrlFontStyleVerdana" href="#" onclick="ccshowRecipe(\''+allChefEntries[i].entry_entryprops.ct_prop_1+'\',\''+ allChefRecipeEntries[i][k].entry_entryprops.ct_prop_1+'\',\''+defaultWebURL+'\',\''+true+'\');return false;">' +allChefRecipeEntries[i][k].entry_label1+'</a> <br/>';
                }

                 chefRecipeList += (''+
                                                '<table class="ccrlBackToTopWidthPlusRightPadding" cellspacing="0" cellpadding="0" >' + 
                                                    '<tr>' + 
                                                        '<td align="right">' + 
                                                                '<a class="ccrlFontStyleVerdanaTitle" href="#" onclick="platformScrollToTop(\'ccrlDivContainer\');return false;">back to top</a><br/>'+
                                                         '</td>' + 
                                                    '</tr>' + 
                                                '</table>' + 
                                            '</td>' + 
                    '</tr>');
            }
        }
}
}
    else
    {
        if(allChefEntries && allChefRecipeEntries && recipeCatEntries && recipeCatEntries[tabIndex - 1])
        {  
            var chefNameList = [];            
            for(var i = 0; i < recipeCatEntries[tabIndex - 1].length; i++) 
            {
                var recipeNames = [];
                var chefName = getChefNameByChefId(recipeCatEntries[tabIndex - 1][i].entry_entryprops.ct_prop_2);
                var chefIndex = getChefIndex(allChefEntries, recipeCatEntries[tabIndex - 1][i].entry_entryprops.ct_prop_2);
                var matchFound = addUniqueEntries(chefName, chefNameList);
                if(!matchFound)
                {
                    chefRecipeList += ( '<tr>' + 
                                 '<td class="ccrlSpaceForEveryLine">'+
                                    '<a id="'+allChefEntries[chefIndex].entry_label1+'"/>' + 
                                    '<font class="ccrlFontStyleVerdana14px">'+chefName+'<br/></font>');                               


                    for(var k = 0; k < recipeCatEntries[tabIndex - 1].length; k++)
                    {
                        if(chefName == getChefNameByChefId(recipeCatEntries[tabIndex - 1][k].entry_entryprops.ct_prop_2))
                        {
                            var recipeIndex = getRecipeIndex(ccallChefRecipeEntries, chefIndex, recipeCatEntries[tabIndex - 1][k].entry_entryprops.ct_prop_1);
                            chefRecipeList +=  '<a class="ccrlFontStyleVerdana" href="#" onclick="ccshowRecipe(\''+allChefEntries[chefIndex].entry_entryprops.ct_prop_1+'\',\''+ allChefRecipeEntries[chefIndex][recipeIndex].entry_entryprops.ct_prop_1+'\',\''+defaultWebURL+'\',\''+true+'\');return false;">' +recipeCatEntries[tabIndex - 1][k].entry_label1+'</a> <br/>';
                        }
                    }

                    chefRecipeList += ('<table class="ccrlBackToTopWidthPlusRightPadding" cellspacing="0" cellpadding="0" >' + 
        '<tr>'+
                                                        '<td align="right">' + 
                                                            '<a class="ccrlFontStyleVerdana" href="#" onclick="platformScrollToTop(\'ccrlDivContainer\');return false;"> back to top</a> <br/>' + 
                                        '</td>'+
                                    '</tr>'+
                                '</table>'+
                            '</td>'+
                                        '</tr>');
                }
            }
        }
        else
        {
            chefRecipeList = '<tr><td width="586px" height="502px" align="center" valign="middle"><img  border="none" align="center" src="'+VERSION_DIR+'img/loading_anim.gif"></img></td></tr>';
        }        
    }    
    return chefRecipeList;
}

function getChefNameByChefId(chefId)
{
    var chefName="";
    if(ccallChefEntries)
    {
        for(var k = 0; k < ccallChefEntries.length; k++)
        {
            if(chefId == ccallChefEntries[k].entry_entryprops.ct_prop_1)
            {
                chefName = ccallChefEntries[k].entry_label1;
                break;
            }
        }
    }
    return chefName;
}

function getChefIndex(allchefs, chefId)
{
    var chefIndex = 0;
    for(var i = 0; i < allchefs.length; i++)
    {
        if(chefId == allchefs[i].entry_entryprops.ct_prop_1)
        {
            chefIndex = i;
            break;
        }
    }
    return chefIndex;
}

function getRecipeIndex(allChefRecipes, chefIndex, recipeId)
{
    var recipeIndex = 0;
    if(allChefRecipes && allChefRecipes[chefIndex])
    {
    for(var i = 0; i < allChefRecipes[chefIndex].length; i++)
    {
        if(recipeId == allChefRecipes[chefIndex][i].entry_entryprops.ct_prop_1)
        {
            recipeIndex = i;
            break;
        }
    }
    }
    return recipeIndex;
}

function addUniqueEntries(value, container)
{
    var matchFound = false;
    if(container.length == 0)
    {
        container[0] = value;
    }
    else
    {
        for(var i =0; i < container.length; i++)
        {
            if(container[i] == value)
            {
                matchFound = true;
                break;
            }        
        }

        if(!matchFound)
        {
            container[container.length] = value;
        }
    }
    return matchFound;
}

function replaceAll(str, find, replace, inc)
{
    var index = -1;
    var fromIndex = 0;
    if(str)
    {
        while((index = str.indexOf(find, fromIndex)) != -1)
        {
            str = str.replace(find, replace);
            fromIndex = index+inc;
        }
    }
    return str;
}



function createHTMLForRecipeList(isFirstLoad, allChefEntries, allChefRecipeEntries, recipeCatEntries)
{  
    var ccrlcontent = document.getElementById("ccrlcontent");
    if(isFirstLoad || ccshowPopUp || !ccrlcontent)
    {
        ccdata = ('<table id="header" class="ccrlDisplayArea" cellspacing="0" cellpadding="0">' + 
                        '<tr>'+
            '<td valign="top">' + 
                '<table class="ccrlTabsHeightAndPadding" cellspacing="0" cellpadding="0">' + 
                                    '<tr>'+
                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe1" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 0 ? 'tab_recipe_by_sel1.png' : 'tab_recipe_by_desel1.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' +                            
                                        '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe2" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 1 ? 'tab_recipe_by_sel2.png' : 'tab_recipe_by_desel2.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' +
                            '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe3" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 2 ? 'tab_recipe_by_sel3.png' : 'tab_recipe_by_desel3.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' + 
                                        '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe4" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 3 ? 'tab_recipe_by_sel4.png' : 'tab_recipe_by_desel4.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' + 
                            '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe5" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 4 ? 'tab_recipe_by_sel5.png' : 'tab_recipe_by_desel5.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' + 
                                        '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img  class="ccrlCursorHand" id="recipe6" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 5 ? 'tab_recipe_by_sel6.png' : 'tab_recipe_by_desel6.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' + 
                            '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHand" id="recipe7" src="'+VERSION_DIR+'img/' +(cctabSelectionIndex == 6 ? 'tab_recipe_by_sel7.png' : 'tab_recipe_by_desel7.png') +'" onclick="ccsetSelected(this);return false;"></img>' + 
                            '<font class="'+(ccisIE ? 'ccrltabSpaceIE">' : 'ccrltabSpaceOthers">&nbsp;')+'</font>' +                             
                                        '</td>'+

                        '<td class="ccrlTabCls">' + 
                                '<img class="ccrlCursorHandForClose" id="ccidCloseButton" src="'+VERSION_DIR+'img/button_close.png" onclick="closePage();return false;" ></img>' + 
                                        '</td>'+
                                    '</tr>'+
                                '</table>'+
                '<div id= "ccrlcontent">'+getChefAndRecipesList()+'</div>'+
            '</td>'+
        '</tr>'+
    '</table>');
}
    else
    {
        ccdata = getChefAndRecipesList();
    }
    return ccdata;
}

function closePage()
{
    ccisFirstLoad = (ccloadComplete) ? ccisFirstLoad : true;
    ccshowPopUp = true;
    if(ccbackToRecipeList)
    {
        ccbackToRecipeList = false;
        //cancel the recipe popup
//        platformOnCancelled( 1, 'platformShowPopup');
        //show the recipelist popup
        var innerHtml = createHTMLForRecipeList(ccisFirstLoad, ccallChefEntries, ccallChefRecipeEntries, ccrecipeCatEntries);
        platformShowPopup(innerHtml, 586, 634, true, false, createDelegate(platformOnCancelled, this, [1, "platformShowPopup"]));
        ccshowPopUp = false;
    }
    else
    {
        platformOnCancelled( 1, 'platformShowPopup');
        ccisWindowActive = false;
        ccSelectionId = null;
        cctabSelectionIndex = 0;
        ccrecipeDeepLink = null;
    }
}

function createHTMLForRecipe(chef, recipe, productEntries)
{ 
    //setting values for sharing
    var comment = (recipe.entry_comment) ? replaceAll(recipe.entry_comment, "'", "\'", 2) : " ";
    comment = replaceAll(comment, '"', '\"', 2);
    var webUrl = (ccrecipeDeepLink && ccrecipeDeepLink.length>0) ? ccrecipeDeepLink : defaultWebURL;
    
    var recipeData =  ('<table id="header" class="ccrDisplayArea ccrDrawBorder '+((ccisIE) ? " ccrIEDisplayArea" : " ccrBreakHere")+'" cellspacing="0" cellpadding="0" valign="top" >'+ 
        '<tr>'+ 
            '<td>'+ 
                '<table align="center" class="ccrBreakHere ccrChefDetImgTableWidth ccrChefImgTableHeight ccrDrawBorder'+((ccisIE) ? " ccrIEChefImgTableWidth" : "")+'" cellspacing="0" cellpadding="0">'+ 
                    '<tr>'+ 
                        '<td  class="ccrChefImageWidth ccrValign">'+ 
                            '<img class="ccrChefImageWidthHeight ccrValign" src="'+chef.entry_entryprops["vc.iconurl"]+'" ></img>'+ 
                        '</td>'+ 
                        '<td width="10px">&nbsp;</td>'+ 
                        '<td class="ccrChefName">'+ 

                        '<font class="ccrFontVerdana18 ">'+(chef.entry_label1? chef.entry_label1.replace(' ', '<br/>') : '')+'</font>'+ 
                        '</td>'+
                        
                        '<td class="ccrMailPhoneShareLikeCls"  align="right">'+
                            '<table class="ccrSocialTableCls">'+
                                '<tr>'+
                                    '<td id="idccrShareIt" class="ccrShareIt '+(ccisIE ? '' : 'ccrShareItPaddingNotIE')+'">');

recipeData += (shareItControlElmts('<img class="ccrShareItImgWH" border="none" align="middle" src="'+VERSION_DIR+'img/button_share_it.png">', recipe.entry_label1, comment, recipe.entry_entryprops["vc.iconurl"], webUrl, defaultWebURL)+
                        '</td>'+ 
                                    '<td class="ccrEmailIt" align="right">');
                    recipeData += ('<a href="#" class="ccrNoTextDecoration ccrImgHighlight ccMozOutlineStyle" onclick=\'showEmail(myCallbackSwf, "'+sanitizeStr(recipe.entry_label1)+'", "'+sanitizeStr(recipe.entry_comment)+'", "'+ recipe.entry_fileurl+'", "'+ (ccrecipeDeepLink ? ccrecipeDeepLink : defaultWebURL) +'");return false;\'><img class="ccrSendToMailImgCls" title="Email this" style="border:none" class="ccrNoTextDecoration" src="'+VERSION_DIR+'img/button_email.png"></img></a>'+ 
                        '</td>'+ 
                        '<td class="ccrSmsIt" align="right">'+ 
                                        '<a href="#" class="ccrNoTextDecoration ccrImgHighlight ccMozOutlineStyle" onclick="sendtoPhone(myCallbackSwf, \''+recipe.entry_id+'\');return false;"> <img class="ccrSendToPhoneImgCls" title="Send to Phone" style="border:none" class="ccrNoTextDecoration" src="'+VERSION_DIR+'img/button_sms.png"></img></a>'+ 
                        '</td>'+ 
                        '<td class="ccrShareVoteIt" align="right">'+ 
                                         '<a href="#" class="ccrNoTextDecoration ccrImgHighlight ccMozOutlineStyle" onclick="voteFor(myCallbackSwf, \''+recipe.entry_id+'\');return false;"> <img class="ccrVotItImgWH" style="border:none" class="ccrNoTextDecoration" src="'+VERSION_DIR+'img/button_likeit.png" ></img></a>'+ 
                        '</td>'+ 
                                    '<td id="numpeoplelike" class="ccrLikeIt ccrLikeTheRecipe '+(ccisIE ? '': 'ccrLikeTheRecipeDiv')+'" align="left">'+ 
                                cclikeCount+' people like the recipe!' +
                        '</td>'+ 
                                '</tr>'+
                            '</table>'+
                        '</td>'+
                        '<td width="10px">&nbsp;</td>'+ 
                        '<td class=ccrClosePrintTable" valign="top">'+ 
                            '<table align="right" cellspacing="0" cellpadding="0">'+ 
                                '<tr class="ccrButtonClose">'+ 
                                    '<td valign="top">'+ 
                                        '<img class="ccrlCursorHand ccCloseAndPrintButtonWH" id="ccidCloseButton" src="'+VERSION_DIR+'img/button_close.png" onclick="closePage();return false;"></img>' + 
                                    '</td>'+ 
                                '</tr>'+ 
                                '<tr>'+ 
                                    '<td class="ccrGapBwCloseAndPrint">'+ 
                                        '&nbsp;' + 
                                    '</td>'+ 
                                '</tr>'+                             
                                '<tr class="ccrButtonPrint">'+ 
                                    '<td valign="top">'+ 
                                        '<a href="javascript:window.print()" class="ccrNoTextDecoration ccrImgHighlight ccMozOutlineStyle"><img class="ccCloseAndPrintButtonWH" style="border:none" class="ccrNoTextDecoration" src="'+VERSION_DIR+'img/button_print.png"></img>'+ 
                                    '</td>'+ 
                                '</tr>'+ 
                            '</table>'+ 
                        '</td>'+                     
                    '</tr>'+                 
                '</table>'+                 
            '</td>'+ 
        '</tr>'+ 
        '<tr>'+ 
        '<td valign="top">'+ 
            '<table class="ccrChefImgTableWidth'+((ccisIE) ? " ccrIEChefImgTableWidth" : "")+'" cellspacing="0" cellpadding="0">'+ 
                    '<tr>'+ 
                        '<td>'+ 
                            '<div class="ccrDivScroll'+((ccisIE) ? " ccrIEDivScroll" : "")+'">'+ 
                            '<table class="ccrChefImgTableWidth'+((ccisIE) ? " ccrIEChefImgTableWidth" : "")+'" cellspacing="0" cellpadding="0">'+
                                '<tr>'+
                                    '<td class= "ccrRecipeDetailWidth ccrlPaddingLeft10 ccrPrintLeft" valign="top">'+                                
                                        '<table class="ccrRecipeDetailWidth ccrlPaddingLeft10" cellspacing="0" cellpadding="0">'+ 
                                    '<tr>'+ 
                                                '<td class="ccrRecipeDetailWidth">'+ 
                                                    '<font class="ccrFontVerdana">'+recipe.entry_label1+'</font><br/>'+
                                                    '<center class="ccrSpacebw">&nbsp;</center>'+
                                                    '<font class="ccrFontVerdana14"> Serves '+replaceAll(recipe.entry_entryprops.ct_prop_6, "\\n", "<br/>", 0)+'</font><br/>'); 
                                                    if(recipe.entry_comment)
                                                    {
                                                        recipeData += ('<font class="ccrFontIngredients">'+replaceAll(recipe.entry_comment, "\\n", "<br/>", 0)+'</font><br/>');
                                                    }
                                                    recipeData += ('<br/>'+ 
                                                        '<font class="ccrFontVerdana14"> Ingredients</font>&nbsp;<br/>'+
                                                        '<font class="ccrFontIngredients">'+replaceAll(recipe.entry_entryprops.ct_prop_7, "\\n", "<br/>", 0)+'</font>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 

                                    '<tr>'+ 
                                                '<td>'+ 
                                                '<br/>'+ 
                                                    '<font class="ccrFontVerdana14"> Directions</font>&nbsp;<br/>'+ 
                                                    '<font class="ccrFontIngredients">'+replaceAll(recipe.entry_entryprops.ct_prop_8, "\\n", "<br/>", 0)+'</font>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 

                                    '<tr>'+ 
                                                '<td>' + 
                                                    '<font class="ccrFooter">'+
                                                    '<br/>');                                                        
                                                        if(recipe.entry_entryprops.ct_prop_9)
                                                        {
                                                            recipeData += recipe.entry_entryprops.ct_prop_9 + '<br/><br/>';
                                                        }
                                                    recipeData += ('</font>'+
                                        '</td>'+ 
                                    '</tr>'+ 
                                        '</table>'+
                                    '</td>'+
                
                                    '<td class="ccrRecipeDummyWidth">'+
                                        '&nbsp;'+
                                        '</td>'+ 
                                    '<td valign="top" class="ccrRecipeImgWidth ccrPrintRight">'+
                                        '<table  cellspacing="0" cellpadding="0" class="ccrRecipeImgWidth" align="left">'+ 
                                    '<tr class="ccrRecipeImgWidth">'+ 
                                                '<td class="ccrRecipeImg ccrRecipeImgWidth">'+ 
                                                    '<img class = "ccrrecipeImageWH" src="'+recipe.entry_fileurl+'" alt="Recipe Image"></img>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 
                                    '<tr class="ccrRecipeImgWidth">'+ 
                                                '<td class="ccrRecipeImg ccrRecipeImgBg ccrRecipeImgWidth" align="center">'+ 
                                                    '&nbsp;<br/>'+ 
                                                    '<font class="ccrFontVerdana14 ccrRecipeImgWidth">what you\'ll need:</font><br/>&nbsp;' +
                                        '</td>'+ 
                                            '</tr>');

                                   for(var i =0; i < productEntries.length; i++)
                                   {
                                       recipeData +=  ('<tr class="ccrRecipeImgWidth">'+ 
                                                '<td class="ccrRecipeImg ccrRecipeImgBg ccrRecipeImgWidth" align="center">'+ 
                                                    '<img class="ccrProductImageWH" src="'+productEntries[i].entry_fileurl+'" alt="Product Image"></img><br/>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 
                                    '<tr class="ccrRecipeImgWidth">'+ 
                                                '<td class="ccrRecipeImg ccrRecipeImgBg ccrRecipeImgWidth" align="center">'+ 
                                                    '<a target="_blank" href="'+productEntries[i].entry_itemurl1+'" class="ccrFontProductName ccrRecipeImgWidth ccrWordWrap">'+productEntries[i].entry_label1+'</a>'+ 
                                                '</td>'+ 
                                            '</tr>');     
                                   }
                        recipeData +=    ('</table>'+
                                        '</td>'+ 
                                    '</tr>'+ 
                                    '<tr class="ccrRecipeImgWidth">'+
                                        '<td class="ccrRecipeImgWidth">'+
                                            '<br/>'+
                                        '</td>'+
                                    '</tr>'+
                                '</table>'+ 
                        '</div>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 
            '</table>'+ 
                                        '</td>'+ 
                                    '</tr>'+ 
'</table> ');

return recipeData;
}

function createHTMLForStoreEvent(storeEventEntries)
{
   var storeEventData = ( '<table id="id_mainTable" class="ccsMainTableWidth ccsBorder1" cellspacing="0" cellpadding="0">'+
                                    '<tr>'+ 
            '<td class="ccsEndButtonCls" valign="top" align="right">'+                
                '<img class="ccrlCursorHandForClose ccCloseAndPrintButtonWH" id="ccidCloseButton" src="'+VERSION_DIR+'img/button_close.png" onclick="closePage();return false;"></img>' + 
                                        '</td>'+ 
                                    '</tr>'+ 
                                            
        '<tr class="ccsTitleTRHeight">'+
            '<td class="ccsTitleTDCls ccsAlignCenter" valign="top">'+                
                '<font class="ccsFontStyleCenturyGothicTitle">STORE EVENTS</font><br class="ccsBRLineHeight"/>'+
                '<font class="ccsFontStyleCenturyGothicSub">See Culinary Council chefs live & in-person, preparing their favorite recipes and sharing valuable tips!</font><br/>'+
                                        '</td>'+ 
                                    '</tr>'+ 
                                    '<tr>'+
                                        '<td>' + 
                '<div id="store" class="ccsContentDivHeight ccsScrollY">'+                    
                    '<table class="ccsSubTableCls" cellpadding="0px" cellspacing="0px">'+
                        '<tr class="ccsTRHCls" >'+
                            '<td class="ccsTD1Width ccsTHPadding ccsColumnTD ccsTHPaddingBottom">LOCATION</td>'+
                            '<td class="ccsTD2Width ccsTHPadding ccsColumnTD ccsTHPaddingBottom"> TIME</td>'+
                            '<td class="ccsColumnTD ccsTHPadding ccsTHPaddingBottom">EVENT DETAILS</td>'+
                        '</tr>');                       
                        
                   for(var i = 0; i < storeEventEntries.length; i++)
                   {
                       var drawBottomBorder = (i == storeEventEntries.length - 1) ? "ccsTdBorderBottom" : "";
                       var drawBgndColor = (i % 2 == 0) ? "ccsBgColorGray" : "";
                       storeEventData += ('<tr class="'+drawBgndColor+'">'+
                       '<td class="ccsTD1Width ccsBorderRightForTD ccsPadding '+ drawBottomBorder +'" valign="top">'+
                                '<font class="ccsFontStyleVerdana">' + (storeEventEntries[i].entry_entryprops.ct_prop_2 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_2, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal">' + (storeEventEntries[i].entry_entryprops.ct_prop_3 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_3, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal">' + (storeEventEntries[i].entry_entryprops.ct_prop_4 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_4, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal">' + (storeEventEntries[i].entry_entryprops.ct_prop_5 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_5, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal">' + (storeEventEntries[i].entry_entryprops.ct_prop_6 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_6, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal">' + (storeEventEntries[i].entry_entryprops.ct_prop_7 ? replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_7, "\\n", "<br/>", 0) : "") + '</font><br/>'+
                            '</td>'+
                            
                            '<td class="ccsTD2Width ccsBorderRightForTD ccsPadding '+ drawBottomBorder +' " valign="top">'+
                                '<font class="ccsFontStyleVerdanaNormal">'+replaceAll(storeEventEntries[i].entry_entryprops.ct_prop_1, "\\n", "<br/>", 0)+'</font><br/>'+                                
                            '</td>'+
                            
                            '<td class="ccsPadding '+ drawBottomBorder +'" valign="top">'+
                                '<font class="ccsFontStyleVerdana">'+storeEventEntries[i].entry_label1+'</font><br/>'+
                                '<font class="ccsFontStyleVerdanaNormal ccsLineHeightForDescription">'+replaceAll(storeEventEntries[i].entry_comment, "\\n", "<br/>", 0)+'</font><br/>'+
                                        '</td>'+
                        '</tr>');
                   }
                    storeEventData +=  ('<table>'+
                            '</div>'+ 
                        '</td>'+ 
                    '</tr>'+ 
    '</table> ');

    return storeEventData;
}

