/*
 * these functions are only for debug purpuse
 */
function debug(s, append){
    if(typeof(s) != 'undefined'){
        if((typeof(append)=='undefined')||append==true||append==null)
            $("#debug").html(JSON.stringify(s)+ "<br/>"+ $("#debug").html());
        else
            $("#debug").html(JSON.stringify(s));
    }
}

function ddebug(){}
function dconfirm(){}
function dalert(){}

/**
 * Generate jQuery html object
 */
function doMakeElement(id, cls, html, attr, tag){
    if(typeof(tag) == 'undefined' && tag == null) tag = 'div';
    var $r = jQuery("<"+tag+"/>");
    if(typeof(id) != 'undefined' && id != null)
        $r.attr('id', id);
    if(typeof(cls) != 'undefined' && cls != null)
        $r.addClass(cls);
    if(typeof(html) != 'undefined' && html != null)
        $r.html(html);
    if(typeof(attr) != 'undefined' && attr != null)
        $r.attr(attr);
    return $r;
}

/**
 * function for centering an HTML element
 *
 * NOTE: has to set width for the element before center it
 * if using relative position, parent element width should also be set
 * 
 * usage:
 * $(element).center({
 *  vertical: 50%,  // distance from page top
 *  horizontal: 50%,// distance from page left
 *  relative: false // position is absolute
 * })
 *
 * or
 *
 * $(element).center({
 *  vertical: -30,  // element will move -30px lower than vertical center
 *  horizontal: 0,  // element will move 0px righter than horizontal center
 *  relative: true  // position is relative
 * })
 * 
 */
jQuery.fn.center = function(params) {
    // default options
    var options = {
        vertical: "50%",
        horizontal: "50%",
        relative: false
    }

    op = jQuery.extend(options, params);

    return this.each(function(){
        //initializing variables
        var self = jQuery(this);
        var parent = self.parent();
        //get the type of positioning
        var positionType = parent.css("position");
        // get the half minus of width and height
        var halfWidth = (self.width()/2)*(-1);
        var halfHeight = (self.height()/2)*(-1);

        // define the css properties
        var cssProp;
        if(op.relative){
            // get the half minus of width and height for parent
            var halfParentWidth = parent.width()/2;
            var halfParentHeight = parent.height()/2;

            cssProp = {
                "position":"relative",
                "margin-top":halfParentHeight+halfHeight+op.vertical,
                "margin-left":halfParentWidth+halfWidth+op.horizontal
            };
        }else{
            cssProp = {
                "position":"absolute",
                "top":op.vertical,
                "margin-top":halfHeight,
                "left":op.horizontal,
                "margin-left":halfWidth
            };
        }

        //check the current position
        if(positionType == 'static') {
            parent.css("position","relative");
        }
        //aplying the css
        self.css(cssProp);
    });
};

function doInputHint(node, value){
    node.val(value);
    var inactive_color = "#999";
    var active_color = "#000";
    node.css("color", inactive_color);
    var default_values = new Array();
    node.focus(function() {
        if (!default_values[this.id]) {
            default_values[this.id] = this.value;
        }
        if (this.value == default_values[this.id]) {
            this.value = '';
            this.style.color = active_color;
        }
        $(this).blur(function() {
            if (this.value == '') {
                this.style.color = inactive_color;
                this.value = default_values[this.id];
            }
        });
    });
}

function doAutoComplete(node, type){
    var options = {
        serviceUrl:commonUrl,
        minChars:2,
        delimiter: /(,|;)\s*/, // regex or character
        maxHeight:400,
        deferRequestBy: 0, //miliseconds
        zIndex: 9999
    }
    var ac = node.autocomplete(options);
    ac.setOptions({
        params: {
            mode:type
        }
    });
    ac.setOptions({
        onSelect: function(value, data){
            dalert('You selected: ' + value + ', ' + data);
        }
    });
}

