if (typeof (Enable) === 'undefined') Enable = {};
(function(){//enable.modules.js requires enable.js, enable.views.js

//regular expession references (Dec 17, noticed to fail on facebook.)
Enable.patterns = {
    //required     : /^[^\s]+$/,
    //email        : /^[\w]+[\w.-+]*\@[\w]+[\w.-]*\.[\w]+$/,
    //phone_number : /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}(x\d+)?$/,
    //credit_card  : /(^(4|5)\d{3}-?\d{4}-?\d{4}-?\d{4}|(4|5)\d{15})|(^(6011)-?\d{4}-?\d{4}-?\d{4}|(6011)-?\d{12})|(^((3\d{3}))-\d{6}-\d{5}|^((3\d{14})))/,
    //ccv          : /^\d{3,4}$/,
    //zip_code     : /^[0-9]{5}([- \/]?[0-9]{4})?$/
};

/*****************************************************************************\
    Enable.documentLocation
    Creates a new layer at the location of a given element.
******************************************************************************/
Enable.documentLocation = function(element) {
    //insert into the document before target element
    var locationTip = Enable('<div class="local_position"></div>').css({
        display  : 'inline',
        position : 'absolute'
    }).insertBefore(element);

    //create an element with no area
    var locationPin = Enable('<div class="local_pin"></div>').css({
        position : 'absolute',
        width    : 0,
        height   : 0,
        top      : 0,
        left     : 0,
        zIndex   : 10
    }).appendTo(locationTip);

    //create a div to control content positioning
    var locationDiv = Enable('<div class="local_content"></div>').css({
        position : 'absolute'
    }).appendTo(locationPin);

    return locationPin;
};

/*****************************************************************************\
    Enable.addHint
    Creates a floating hint above items that appears on focus or mouseover.
******************************************************************************/
Enable.addHint = function(element,hintInnerHTML,switches) {
    hintInnerHTML = hintInnerHTML || '';
    switches      = switches      || ['focus','blur'];
    Enable(element).each(function(){

        var ele = Enable(this);
        var hint = Enable.view.tip(hintInnerHTML).css({
            opacity  : 0,
            position : 'absolute',
            bottom   : 0,
            left     : -20
        }).hide();

        Enable.documentLocation(this).append(hint);

        function show(){
                hint.show().animate({
                    opacity: 1
                }, 550);
        }
        function hide() {
                hint.animate({
                    opacity: 0
                }, 150, null, true).hide();
        }

        ele.bind(switches[0],show);
        ele.bind(switches[1],hide);

    });
};

/*****************************************************************************\
    Enable.addLiveEdit
    Creates hidden fields to edit text on the page. The edited text may also
    be posted to the server to update database records.
******************************************************************************/
Enable.addLiveEdit = function(element,name,value,action){
    value = value || '';
    Enable(element).each(function(){

        var display = Enable(this);
        var pos = display.offset();

        var textbox = Enable('<textarea>'+value+'</textarea>').css({
            color         : '#000000',
            background    : '#FFFBE2',
            border        : '0px none',
            fontSize      : display.css('fontSize'),
            lineHeight    : display.css('lineHeight'),
            wordSpacing   : display.css('wordSpacing'),
            letterSpacing : display.css('letterSpacing'),
            width         : display.width()+6,
            height        : display.height(),
            margin        : '0px',
            padding       : '0px',
            overflow      : 'hidden',
            outline       : '0px',
            resize        : 'none'
        });
        var confirm = Enable('<input type="button" value="Confirm" />').css({
            position  : 'absolute',
            right     : 110,
            bottom    : -28,
            zIndex    : 16,
            width     : 100
        });
        var cancel  = Enable('<input type="button" value="Cancel" />').css({
            position  : 'absolute',
            right     : 0,
            bottom    : -28,
            zIndex    : 16,
            width     : 100
        });

        var wedgeDiv = Enable('<div class="locationPin"></div>').css({
            position   : 'absolute',
            zIndex     : 15
        }).insertBefore(display);

        var edit = Enable('<div></div>').css({
            position: 'absolute',
            zIndex: 15,
            padding: 5,
            top: -6,
            left: -6,
            display: 'none',
            opacity: 0,
            background : '#FFFBE2',
            border     : '1px solid #FFE222',
            borderLeft : '0px none',
            borderRight : '0px none'
        }).append(confirm).append(cancel).append(textbox).appendTo(wedgeDiv);

        function adjustEdit() {
            var spacer = textbox.val();
            display.text(spacer);
            textbox.css({
                height: display.height()
            });
        }

        var editing = Enable.interval(adjustEdit,250);

        function startEdit(){
            edit.show().animate({opacity:1},550);
            textbox[0].focus();
            editing.start();
        }
        function stopEdit(){
            display.text(value);
            textbox.val(value);
            edit.animate({opacity:0},400,null,true).hide();
            editing.stop();
        }

        display.bind('click',startEdit);
        confirm.bind('click',function() {
            value = textbox.val() || value;
            stopEdit();
        });
        cancel.bind('click',function() {
            stopEdit();
        });

    });
};

/*****************************************************************************\
    Enable.toQueryString
    Turns a hash (Object) into a query string -- e.g. x=1&y=2

    Warning: doesn't URL encode keys or values.
******************************************************************************/
Enable.toQueryString = function(hash) {
  var ret = [];
  for (var key in hash)
  {
    if (hash.hasOwnProperty(key)) {
      // TODO: urlencode keys and values
      ret.push(key +"="+ hash[key]);
    }
  }
  return ret.join('&');
};

/*****************************************************************************\
    Enable.arrayToHash
    Turns an array into a hash (Object) with keys set to the array values and
    values set to true.
******************************************************************************/
Enable.arrayToHash = function(array) {
  var hNames = {};
  for (var i=0; i < array.length; i++) {
    hNames[array[i]] = true;
  }
  return hNames;
};

})();// end requires