/*
 * web / web.js
 * Modify: Wed, 31 August 2011 08:29:21 GMT
 * Required jQuery Library 1.6
 */

/*
 * js path defined
 */
var path = (function (root) {

    'use strict';

    var js = root + 'Public/Js/';
    var jquery = js + 'jQuery/';
    var paths = {
        ajax: root + 'webajax/',
        css: root + 'Public/css/',
        script: js,
        pdf: root + 'Public/pdf/',
        upload: root + 'Public/pic/',
        ckeditor: js + 'ckeditor/',
        tinymce: js + 'tiny_mce/',
        plugin: {
            s               : jquery + 'standalone/',
            anythingslider  : jquery + 'anythingslider/',
            blockui         : jquery + 'blockui/',
            contextmenu     : jquery + 'contextmenu/',
            daterange       : jquery + 'daterange/',
            dynatree        : jquery + 'dynatree/',
            fancybox        : jquery + 'fancybox/',
            fgmenu          : jquery + 'fgmenu/',
            highcharts      : jquery + 'highcharts/',
            imgareaselect   : jquery + 'imgareaselect/',
            multiselect     : jquery + 'multiselect/',
            swfupload       : jquery + 'swfupload/',
            timeentry       : jquery + 'timeentry/',
            tipsy           : jquery + 'tipsy/',
            treeview        : jquery + 'treeview/',
            wysiwyg         : jquery + 'wysiwyg/',
            zclip           : jquery + 'zclip/',
            ui              : jquery + 'ui/'
        }
    };
    return paths;
}(root));

/*
 * jQuery selector shortcut
 *
 * @param: selector / string or object / DOM selector
 */
var jQ = function (selector) {
    return jQuery(selector) || jQuery('#' + selector) || jQuery('input[name=' + selector + ']');
};

/*
 * does the element exists or not
 *
 * @param: selector / string or object / DOM selector
 */
var exist = function (selector) {
    return 0 === jQ(selector).length ? false : true;
};

var htmlspecialchars_decode = function (string, quote_style) {
    var optTemp = 0, i = 0, noquotes = false;
    if (typeof quote_style === 'undefined') {
        quote_style = 2;
    }
    string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE': 1,
        'ENT_HTML_QUOTE_DOUBLE': 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE': 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i = 0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            } else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
        // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
    }
    if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded
    string = string.replace(/&amp;/g, '&');
    return string;
};

/*
 * home class
 */
var home = function () {

    'use strict';

    (function () {
        if (jQuery.isFunction(jQuery.blockUI)) {
            jQuery(document).ajaxStop(
                function () {
                    jQuery.unblockUI();
                }
            );
        }
    }());
    
    /*
     * jQuery - timeentry setting
     */
    (function () {
        if (exist('input.timeentry')) {
            jQ('input.timeentry').timeEntry({show24Hours: true});
        }
    }());

    /*
     * tab control
     */
    (function () {

        var panel = (location.hash || '').replace(/^#/, '');
        var prefix = '#panel-';
        var url = location.href.toLowerCase();

        if (exist(prefix + panel)) {
            jQuery('div.tabs').hide();
            jQuery('a[href*=#], a.tab').removeClass('selected');
            jQuery('a[href*=#' + panel + ']').addClass('selected');
            jQuery(prefix + panel).show();
        }

        jQuery('body').delegate('a[href*=#]', 'click', function () {
            var obj = jQuery(this);
            var href = obj.attr('href');
            var rel = (obj.attr('rel') || '').toString().toLowerCase();
            var pos = href.indexOf('#');
            var hash = href.substring(pos, href.length);
            var target = hash.replace(/^#/, '');


            if (-1 === url.indexOf(rel)) {
                location.href = href;
                return false;
            }

            if (-1 === pos) {
                location.href = href;
                return false;
            }

            if (0 === target.length) {
                return;
            }

            jQuery('div.tabs').hide();
            jQuery('a[href*=#]').removeClass('selected');
            jQuery('a[href*=' + hash + ']').addClass('selected');
            jQuery(prefix + target).show(0, function() {
                jQuery(window).resize();
            });
            location.hash = target;
            return false;
        });
        
    }());
    
    /*
     * linkAction
     */
    (function () {

        jQ('a.external').click(function () {
            window.open(jQ(this).attr('href'), '_blank');
            return false;
        });

        jQ('a.back,button.back,input.back').click(function () {

                var obj = jQ(this);

                if ('undefined' !== typeof obj.val()) {
                    if (0 !== obj.val().length) {
                        location.href = obj.val();
                    }
                    else {
                        history.back();
                    }
                }
                else {
                    history.back();
                }
                return false;
            }
        );

    }());
    
    (function () {
        var msg = jQuery('.float-message');
        msg.animate({'opacity': 1}, 3000, function () {
            jQuery(this).fadeOut();
        });
    }());

};

/*
 * binding onload
 */
jQuery(function () {
    'use strict';
    home();
});

