//Function to add minimize and maximize funcitonality to Homepage Widgets
$(document).ready(
    function addHomepageMinimizeMaximizeListeners() {
        //minimize actions on click
        $(".pageContentSectionMinimizer").click(
             function(){
                var relatedDiv = $(this).attr("id").replace(/imgPageContentSectionMin/, "divPageContentSection");       //Get the related div's id from the id of the minimize image
                var maximizer = $(this).attr("id").replace(/imgPageContentSectionMin/, "imgPageContentSectionMax");     //Get the maximize image's id from the id of the minimize image
                $("#" + relatedDiv + " > .pageContentList").slideUp(300);   //Hide the list that contains the div content
                $(this).hide();                                             //Hide the minimize image
                $("#" + maximizer).show();                                  //Show the maximize image
                var widgetId = $(this).attr("id").replace(/imgPageContentSectionMin/, "");
                updateMinMaxPreferencesCookie(widgetId,true);
             }
        );       
        
        //maximize actions on click
        $(".pageContentSectionMaximizer").click(
            function(){
                var relatedDiv = $(this).attr("id").replace(/imgPageContentSectionMax/, "divPageContentSection");       //Get the related div's id from the id of the maximize image
                var minimizer = $(this).attr("id").replace(/imgPageContentSectionMax/, "imgPageContentSectionMin");     //Get the minimize image's id from the id of the maximize image
                $("#" + relatedDiv + " > .pageContentList").slideDown(300); //Show the list that contains the div content
                $(this).hide();                                             //Hide the maximize image
                $("#" + minimizer).show();                                  //Show the minimize image
                var widgetId = $(this).attr("id").replace(/imgPageContentSectionMax/, "");
                updateMinMaxPreferencesCookie(widgetId,false);
            }
        );
        
        //set the default min max levels
        setDefaultMinMaxLevels();
    }
);

//Function to set the default min max levels based on the users stored cookie
function setDefaultMinMaxLevels() {
    if ($.cookie("HomepageMinmaxPreferences") != null)
    {
        var minmaxString = $.cookie("HomepageMinmaxPreferences").replace(/\+/g,"");
        var minmaxArray = minmaxString.split(",");
        $(".divPageContentSection").each(
            function() {
                var widgetId = $(this).attr("id").replace(/divPageContentSection/, "");
                var minmaxIndex = minmaxArray.find("(" + widgetId + ":0)");
                if (minmaxIndex == false)
                {
                    minmaxIndex = minmaxArray.find("(" + widgetId + ":1)"); 
                }
                if (minmaxIndex != false) 
                {
                    var idValueDividerLocation = minmaxArray[minmaxIndex].indexOf(":") + 1;
                    var endOfValueLocation = minmaxArray[minmaxIndex].length - 1;
                    var showValue = minmaxArray[minmaxIndex].substring(idValueDividerLocation,endOfValueLocation);
                    if (showValue == "0")                                           // we want this  widget to be minimized
                    {
                        var relatedDiv = $(this).attr("id").toString();                                                     //Get the id of the content div
                        var minimizer = $(this).attr("id").replace(/divPageContentSection/, "imgPageContentSectionMin");     //Get the minimize image's id from the id of the content div
                        var maximizer = $(this).attr("id").replace(/divPageContentSection/, "imgPageContentSectionMax");    //Get the maximize image's id from the id of the content div
                        $("#" + relatedDiv + " > .pageContentList").slideUp(300);   //Hide the list that contains the div content
                        $("#" + minimizer).hide();                                  //Hide the minimize image
                        $("#" + maximizer).show();                                  //Show the maximize image                
                    }
                }
            }
        ); 
    }   
}

//Function to update the users min max preferences cookie when they minimize or maximize a widget
function updateMinMaxPreferencesCookie(widgetId,hideIt) {
    if ($.cookie("HomepageMinmaxPreferences") != null)
    {
        var newMinMaxString = "";
        var minmaxString = $.cookie("HomepageMinmaxPreferences").replace(/\+/g,"");
        var minmaxArray = minmaxString.split(",");
        var minmaxIndex = minmaxArray.find("(" + widgetId + ":0)");
        if (minmaxIndex == false)
        {
            minmaxIndex = minmaxArray.find("(" + widgetId + ":1)"); 
        }
        if(hideIt == true)
        {
            minmaxArray[minmaxIndex] = "(" + widgetId + ":" + "0)";
        }
        else
        {
            minmaxArray[minmaxIndex] = "(" + widgetId + ":" + "1)";    
        }
        newMinMaxString = minmaxArray.toString();
        $.cookie("HomepageMinmaxPreferences", newMinMaxString, { expires: 365});    
    } 
}

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}
