//Function to add minimize and maximize functionality to MyCrestron Widgets
$(document).ready(
    function addMyCrestronMinimizeMaximizeListeners() {
        //minimize actions on click
        $(".myCrestronMinimizer").click(
             function(){
                var relatedDiv = $(this).attr("id").replace(/imgMin/, "div");       //Get the related div's id from the id of the minimize image
                var maximizer = $(this).attr("id").replace(/imgMin/, "imgMax");     //Get the maximize image's id from the id of the minimize image
                $("#" + relatedDiv + " > .myCrestronList").slideUp(300);    //Hide the list that contains the div content
                $(this).hide();                                             //Hide the minimize image
                $("#" + maximizer).show();                                  //Show the maximize image
                updateMCMinMaxPreferencesCookie(relatedDiv,true);
             } 
        );      
        
        //maximize actions on click
        $(".myCrestronMaximizer").click(
            function(){
                var relatedDiv = $(this).attr("id").replace(/imgMax/, "div");       //Get the related div's id from the id of the maximize image
                var minimizer = $(this).attr("id").replace(/imgMax/, "imgMin");     //Get the minimize image's id from the id of the maximize image
                $("#" + relatedDiv + " > .myCrestronList").slideDown(300);  //Show the list that contains the div content
                $(this).hide();                                             //Hide the maximize image
                $("#" + minimizer).show();                                  //Show the minimize image
                updateMCMinMaxPreferencesCookie(relatedDiv,false);
            }
        );
        
        //set the default min max levels
        setDefaultMCMinMaxLevels();
    }
);

//function to set the default min max levels based on the users stored cookie
function setDefaultMCMinMaxLevels() {
    if ($.cookie("MyCrestronMinmaxPreferences") != null)
    {
        var minmaxString = $.cookie("MyCrestronMinmaxPreferences").replace(/\+/g,"");
        var minmaxArray = minmaxString.split(",");
        $(".divMyCrestronContent").each(
            function() {
                var widgetId = $(this).attr("id");
                if (widgetId != "")
                {
                    var minmaxIndex = minmaxArray.find("(" + widgetId + ":0)");
                    if (minmaxIndex.toString() == "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(/div/, "imgMin");    //Get the minimize image's id from the id of the content div
                            var maximizer = $(this).attr("id").replace(/div/, "imgMax");    //Get the maximize image's id from the id of the content div
                            $("#" + relatedDiv + " > .myCrestronList").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 prefernces cookie when they minimize or maximize a widget
function updateMCMinMaxPreferencesCookie(widgetId,hideIt) {
    if ($.cookie("MyCrestronMinmaxPreferences") != null)
    {
        var newMinMaxString = "";
        var minmaxString = $.cookie("MyCrestronMinmaxPreferences").replace(/\+/g,"");
        var minmaxArray = minmaxString.split(",");
        var minmaxIndex = minmaxArray.find("(" + widgetId + ":0)");
        if (minmaxIndex.toString() == "false")
        {
            minmaxIndex = minmaxArray.find("(" + widgetId + ":1)"); 
        }
        if (minmaxIndex != false)
        {
            if(hideIt == true)
            {
                minmaxArray[minmaxIndex] = "(" + widgetId + ":" + "0)";
            }
            else
            {
                minmaxArray[minmaxIndex] = "(" + widgetId + ":" + "1)";    
            }
            newMinMaxString = minmaxArray.toString();
            $.cookie("MyCrestronMinmaxPreferences", newMinMaxString, { expires: 365, path: "/"}); 
        }
    }       
}

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;
}
