YAHOO.namespace ("SaucyPlateClient");

YAHOO.SaucyPlateClient.InPlaceEditor = function(element, id, method, useTextarea, path)
{
    this._element = element;
    this._editBox;
    this._containerElement;
    this._id = id;
    this._method = method;
    this._useTextarea = useTextarea;
    this._path = path;

    YAHOO.util.Event.addListener(this._element, 'mouseover', this.highlight, this);
    YAHOO.util.Event.addListener(this._element, 'mouseout', this.unhighlight, this);
    YAHOO.util.Event.addListener(this._element, 'click', this.handleClick, this);
   
    
}

YAHOO.SaucyPlateClient.InPlaceEditor.prototype = {
    
    handleBlur:function(e, objContext)
    {
       YAHOO.util.Dom.setStyle(this, 'display', 'none'); 
       YAHOO.util.Dom.setStyle(objContext._element, 'display', ''); 
      
       if(objContext._element.innerHTML != this.value){ 
           
           objContext._element.innerHTML = "Saving...";
           
           var loc
           if(objContext._path == null)
           {
            loc = "EditHandler.ashx";
           }
            else
           {
             loc = objContext._path + "EditHandler.ashx";
           }
           
           var postData = "m=" + objContext._method + "&value=" + this.value + "&id=" + objContext._id;  
           var request = YAHOO.util.Connect.asyncRequest('POST', loc, 
           {
			    success: function(o){
	                    objContext._element.innerHTML = this.value;
                        var anim = new YAHOO.util.ColorAnim (objContext._element, {backgroundColor:{to:'#ffffff'}}, .5, YAHOO.util.Easing.easeOut);
	                    anim.animate();
			    },
			    failure: function(o){
				        alert(o.statusText );
	                    objContext._element.innerHTML = this.value;
			    },
			    scope: this
		    },
            postData);  
        }else
        {
             var anim = new YAHOO.util.ColorAnim (objContext._element, {backgroundColor:{to:'#ffffff'}}, .5, YAHOO.util.Easing.easeOut);
	         anim.animate();
        }
    },
    
    handleClick:function(e, objContext)
    {  

       if(!objContext._containerElement)
       {
            objContext._containerElement = document.createElement('SPAN');
            this.parentNode.insertBefore(objContext._containerElement, this);
            objContext._containerElement.appendChild(this);
            
            if(objContext._useTextarea)
            {            
                objContext._editBox = document.createElement('TEXTAREA');
                objContext._editBox.cols = "70";
                objContext._editBox.rows = "5";
            }else
            {
                objContext._editBox = document.createElement('INPUT');
            }
            
            objContext._containerElement.appendChild(objContext._editBox);
            YAHOO.util.Event.addListener(objContext._editBox, 'blur', objContext.handleBlur, objContext);
       }
       else
       {
           YAHOO.util.Dom.setStyle(objContext._editBox, 'display', ''); 
       }
      
      objContext._editBox.value = this.innerHTML;
      objContext._editBox.focus();
      
      region = YAHOO.util.Region.getRegion(this);
      //alert(region.toString());
      
      YAHOO.util.Dom.setStyle(objContext._editBox, 'height', region.bottom - region.top); 
      YAHOO.util.Dom.setStyle(objContext._editBox, 'width', (region.right - region.left) + 25); 
      YAHOO.util.Dom.setStyle(objContext._editBox, 'position', 'absolute'); 
      YAHOO.util.Dom.setStyle(this, 'display', 'none'); 
  
    },
    
    highlight:function()
    {
        var anim = new YAHOO.util.ColorAnim (this, {backgroundColor:{to:'#ffffd3'}}, .5, YAHOO.util.Easing.easeOut);
	    anim.animate();
    },
    
    unhighlight:function()
    {
        var anim = new YAHOO.util.ColorAnim (this, {backgroundColor:{to:'#ffffff'}}, .5, YAHOO.util.Easing.easeOut);
	    anim.animate();
    }

}
// center: function () {
//    
//            var scrollX = Dom.getDocumentScrollLeft(),
//                scrollY = Dom.getDocumentScrollTop(),
//    
//                viewPortWidth = Dom.getClientWidth(),
//                viewPortHeight = Dom.getClientHeight(),
//                elementWidth = this.element.offsetWidth,
//                elementHeight = this.element.offsetHeight,
//                x = (viewPortWidth / 2) - (elementWidth / 2) + scrollX,
//                y = (viewPortHeight / 2) - (elementHeight / 2) + scrollY;
//            
//            this.cfg.setProperty("xy", [parseInt(x, 10), parseInt(y, 10)]);
//            
//            this.cfg.refireEvent("iframe");
//    
//        },


YAHOO.namespace("SaucyPlateClient");  

YAHOO.SaucyPlateClient.RecipePreviewScripts = function()
{
    var $E = YAHOO.util.Event;
    var $D = YAHOO.util.Dom;
    var previewPanel;
    var container;
    var servicePath;
    var creatingPanel;
    var loading = false;
    
    return {
        init:function()
        {

            var recipePreview = $D.get("recipe-preview-container");
            if(recipePreview)
            {
                servicePath = "../";
                container = recipePreview;
                $E.addListener(recipePreview, 'click', YAHOO.SaucyPlateClient.RecipePreviewScripts.recipeClickHandler);
          
                creatingPanel= new YAHOO.widget.Panel("creatingPanel", { width:"170px", fixedcenter: true, modal:false, close:false, visible:false, draggable:false } );
                creatingPanel.setBody("Creating Recipe Preview <img src=\"../images/spinner.gif\"/>");
                creatingPanel.render(document.body);
            }
            
        },
        hideShowCreatePanel:function(show)
        {
            if(show)
            {
                loading = true;
                creatingPanel.show();
            }
            else
            {   
                loading = false;
                creatingPanel.hide();
            }  
        },
        getPanel:function()
        {
        
            if(!previewPanel)
            {
                previewPanel= new YAHOO.widget.Panel("previewPanel", { width:"400px", x:5, y:5, fixedcenter: false, modal:false, close:true, visible:false, draggable:false } );
                previewPanel.setBody("Preview Recipe");
                previewPanel.render(document.body);
                previewPanel.body.style.textAlign = "left";
                previewPanel.show();
            }
            else
            {
                previewPanel.setBody("");
                previewPanel.show();
            }
            return previewPanel;
        
        },
        getHeight:function(elm)
        {
            var elm = $D.get(elm);
            var h=elm.style.height;
            if(h=='auto')
            {
                elm.style.zoom=1;
                h=elm.clientHeight;
            }
            return h;
        },
        recipeClickHandler:function(e)
        {
            var elTarget = $E.getTarget(e);
            
            while (elTarget.id != container.id)
            {
                if(elTarget.nodeName.toUpperCase() == "DIV")
                {
                    if(elTarget.className == "div-recipe-preview")
                    {
                      
                      $E.preventDefault(e);
                      var recipeTest = /rp-(.+)/;
                      //var recipeTest = /Recipes\/(.+).aspx/;

                        
                        if(recipeTest.test(elTarget.id))
                        {
                            var idRegEx = recipeTest.exec(elTarget.id);
                            var recipeId = idRegEx[1];
                            YAHOO.SaucyPlateClient.RecipePreviewScripts.showPreview(recipeId);
                        }
                    }
                    break;
                }
                else
                {
                    elTarget = elTarget.parentNode;
                }
            }
            
        },
        showPreview:function(recipeId)
        {
            
             if(!loading)
             {
             YAHOO.SaucyPlateClient.RecipePreviewScripts.hideShowCreatePanel(true);
             var request = YAHOO.util.Connect.asyncRequest('GET', servicePath + "Blank.aspx?rid=" + recipeId, 
               {
			        success: function(o){
			        
			            var st = $D.getDocumentScrollTop() + 10;
			        
	                    YAHOO.SaucyPlateClient.RecipePreviewScripts.getPanel().cfg.setProperty("y", st);
	                    YAHOO.SaucyPlateClient.RecipePreviewScripts.getPanel().setBody(o.responseText);
	                    YAHOO.SaucyPlateClient.RecipePreviewScripts.hideShowCreatePanel(false);    
			        },
			        failure: function(o){
				            alert("We're sorry.  We could not create your recipe preview.  Please try again.");
				            YAHOO.SaucyPlateClient.RecipePreviewScripts.hideShowCreatePanel(false); 
			        },
			        scope: this
		        }); 
		       }
		       else
		       {
		       //alert('already loading');
		       } 
 
        }
    }
    
}();

YAHOO.util.Event.onDOMReady(YAHOO.SaucyPlateClient.RecipePreviewScripts.init);
var defaultPath;

YAHOO.namespace("SaucyPlateClient");  


YAHOO.SaucyPlateClient.ShoppingListsScripts = function()
{

    var $E = YAHOO.util.Event;
    var $D = YAHOO.util.Dom;

    return {
        init:function()
        {
            defaultPath = "";
            var listId = $D.get('ctl00_mainContent_shoppingListIdHid').value;
            new YAHOO.SaucyPlateClient.InPlaceEditor(YAHOO.util.Dom.get('listNameP'), listId, 'sln');
            
 	        var elements = $D.getElementsByClassName('ilEdit');
	        
	        for(i = 0; i < elements.length; i++)
	        {
	            var elm = elements[i];
	            var id = elements[i].id.replace("listItemP", "");
                new YAHOO.SaucyPlateClient.InPlaceEditor(elm, id, 'sli');
	        }
	        
            Nifty("div#add-item-container", "normal");
            
        }
    }
}();


YAHOO.util.Event.addListener(window, "load", YAHOO.SaucyPlateClient.ShoppingListsScripts.init);


