﻿function charLimit(sndr, limitNum) {
    if (sndr.val().length > limitNum) {
        sndr.val(sndr.val().substring(0, limitNum));
    }
}
function centerWindow(page, varWidth, varHeigth) {
     var xMax;
     if (document.all)
        xMax = screen.width, yMax = screen.height;
     else
        if (document.layers)
            xMax = window.outerWidth, yMax = window.outerHeight;
        else
            xMax = 640, yMax=480;

     var xOffset = (xMax - varWidth)/2, yOffset = (yMax - varHeigth)/2;

     window.open(page,'','width='+varWidth+',height='+varHeigth+',screenX='+xOffset+',screenY='+yOffset+', top='+yOffset+',left='+xOffset+'');
 }
 var ct = 0;
 function emailDialog(sku, desc) {
     var frm = '<iframe id=\'em_' + ct + '\' src="/Tell-a-Friend.aspx?sku=' + sku + '&desc=' + desc + '" frameBorder=\'0\' border=\'0\'  ></iframe>';
     $('body').append(frm);
     $('#em_' + ct).dialog({ modal: true, title:'Share the Joy of Element Bars', width: 500, height: 500, autoOpen: false, autoResize: true });
     $('#em_' + ct).dialog('open').width(450).height(480);


 }
 function showEmailWithNote(sku, desc) {
     emailDialog(sku, desc);
 }
 function showEmail() {
     emailDialog('', '');
 }
var taggingItemId;
var taggingItemType = 1;
function TagItPreset(ingredientId) {
    $('#tagIt').dialog(
        { buttons:
            { "Tag It":
                function() {
                    $.ajax({
                        type: "POST",
                        url: '/Admin/TaggingWebService.asmx/TagItem',
                        data: "{'itemType':'1','itemId':'" + ingredientId + "','tagId':'" + $('#tagIt select').val() + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) {
                            alert('yeah');
                        },
                        error: function(e) {
                            alert('boo');
                        }
                    });
                    $(this).close();
                }
            }, modal: true
        }).open();
    }
    
    var isFindingTags = false;
    function GetTags(findText, listEl) {
        if (isFindingTags == false) {
        isFindingTags = true;
        
        $.ajax({
            type: "POST",
            url: '/Admin/TaggingWebService.asmx/GetTags',
            data: "{'tagText':'" + findText + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var ara = msg.d;
                var listHtml = ''
                if (ara != null && ara != 'undefined') {                        
                    for (var i = 0; i < ara.length; i++) {
                        //suggest values
                        listHtml += '<li><a href="javascript:void(0)" onclick="ApplyTag(' + taggingItemType + ',' + taggingItemId + ',\'' + ara[i] + '\')">' + ara[i] + '</a></li>';
                    

                    }
                }
                listEl.html(listHtml);
                isFindingTags = false;
            },
            failure : function() {
                isFindingTags = false;
            }
            
        });
            
        }
        

    }
    function ApplyTag(itemType, itemId, tagText) {
    
                    $.ajax({
                        type: "POST",
                        url: '/Admin/TaggingWebService.asmx/TagItemByText',
                        data: "{'itemType':'" + itemType + "','itemId':'" + itemId + "','tagText':'" + tagText + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) {
                            $('#msg').html('Tagged!').effect("highlight", {}, 3000);
                        },
                        error: function(e) {
                            alert('oops tagging failed!');
                        }
                    });
                    
                
    }
    var dia = null;
function TagIt(itemType, itemId) {
    taggingItemId = itemId;
    taggingItemType = itemType;
    
    if (dia == null) {
        dia = $('<div><p id="msg" style="color:#ff0000;"></p>Tag: <input type="text" id="tagName" /><ul></ul></div>');
        $(dia).dialog(
        { title: "Tag Ingredients", buttons:
            { "Tag It":
                function() { ApplyTag(taggingItemType, taggingItemId, $(this).find('input').val()); }
            }, modal: true, autoOpen: false, beforeclose: function() { $(this).find('ul').html(''); }
        });
    }
    $(dia).dialog("open").find('input').keyup(
            function(eventObject) {
                var txt = $(this).val();
                if (txt.length >= 3) {
                    //put a timeout in...
                    GetTags(txt, $(this).parent().find('ul'));

                }
            }
        ).focus();
}
function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
}

(function($) {
    $.fn.BubbleItUp = function(options) {
        var opts = $.extend({}, $.fn.BubbleItUp.defaults, options);
        return this.each(function() {            
            $(this).mouseover(
                function() {
                    $(this).clone()
                        .removeAttr('style')
                        .css({'position':'absolute','z-index':100,'top':0,'left':0,'width': 48})
                        .appendTo($(this).parent())
                        .addClass(opts.cssClass)
                        .animate({
                            left: "-=14",
                            top: "-=14",
                            width: 76                            
                            }, 'fast')                                                
                        .mouseout(
                            function() {
                                $(this).stop().fadeOut(400, function() { $(this).remove() } ).removeClass(opts.cssClass);
                                
                            });
                });
           });
      
    };
       // default spinner options
    $.fn.BubbleItUp.defaults = {
        cssClass: ''       // class for item
        
    };
})(jQuery);

(function($) {
    var methods = {
        init: function(options) {
            return this.each(function() {
                var opts = $.extend({}, $.fn.DefTexter.defaults, options);
                var $this = $(this),
                    data = $this.data('DefTexter');
                $this.addClass(opts.cssClass);
                if ($this.val().length <= 0) { $this.val(opts.defText); }
                $this.focus(function() {
                    if ($this.val() == opts.defText) {
                        $this.val('');
                    }
                }).submit(function() {
                    if ($this.val() == opts.defText) {
                        $this.val('');
                    }
                }).blur(function() {
                    if ($this.val() == '') {
                        $this.val(opts.defText);
                    }
                });
                if (!data) {
                    $this.data('DefTexter', {
                        target: $this,
                        defText: opts.defText
                    });
                }
            });
        },
        value: function() {
            var data = $(this).data('DefTexter');
            if ($(this).val() == data.defText) {
                return '';
            }
            return $(this).val();
        }
    };
    $.fn.DefTexter = function(options) {
        if (methods[options]) {
            return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof options === 'object' || !options) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.DefTexter');
        }
    };

    // default spinner options
    $.fn.DefTexter.defaults = {
        cssClass: 'defText'       // class for input
        , defText: '' // default text        
    };
})(jQuery);

(function($) {
    $.fn.BubbleItUpA = function() {
        return this.each(function() {
            $(this).mouseover(
                function() {
                    $(this).stop();
                    $(this).css({'z-index':100,'top':0,'left':0,'width': 48}).animate({
                        left: "-=14",
                        top: "-=14",
                        width: 76                                
                    }, 'fast');
                }).mouseout(
                function() {
                    $(this).stop();
                    $(this).animate({
                        left:0,
                        top:0,
                        width:48                                
                    },'fast',function() {
                        $(this).css({'z-index':0});
                    });
                });
        });
    };
})(jQuery);
        
        
        
