(function ($) {
    jQuery.fn.justtoggle = function (options) {
        //		console.log('justtoggle START');
       
        var opts = $.extend({},
        $.fn.justtoggle.defaults, options);
        var $this = $(this);
        $this.data('opts', opts);
        //console.log($(this));
        return this.each(function (i, e) {
            var $t = jQuery(this);
            if (!$t.is('a')) {
                alert('NOT A tag exiting');
                return;
            }
            //IMPORTANT !!!
            //Prevent MULTIPLE Binding !!
            //IN case when they was already Binded!
            jQuery(e).unbind('click.justtoggle'); //Bind Click 
            jQuery(e).bind('click.justtoggle', function (event) {
                event.preventDefault();
                
                $href = $t.attr('href');
                if ($href.match(/#/) && $href.length > 1) {
                    //$id = $href.substr(1);
                    $_id = $href.split('#');
                    $id = $_id[1];
                    $obj = jQuery('#' + $id);
					//console.log($obj);
                    _process($this, $t, $obj);
                } else {
                    //console.log('BAD HREF : EXITING');
                    alert('BAD HREF : EXITING');
                    return;
                }
				
            });
			if(opts.startOpened==true){
					//console.log($obj);
					$(this).trigger('click');
				}
			
        });
    } //defaults
    jQuery.fn.justtoggle.defaults = {
        showLabel: '[+]',
        hideLabel: '[-]',
        progress_image: '../images/indicator.gif',
		animateBG:true,
		openBGRclass:'_just_is_opened',
		startOpened:false,
        onShow: function () {},
        onHide: function () {}
    }; //private


    function _process($this, $t, $obj) { 
		//alert('DO Show');
        var opts = $this.data('opts');
        var visible = 'unknown';
        if ($obj.length > 0) {
            if ($($obj).attr('offsetHeight') > 0) {
                visible = true;
            } else {
                visible = false;
            }
            if (visible == true) { //console.log('Hiding');
				if(opts.animateBG==true){
					//console.log($obj);
					$obj.removeClass(opts.openBGRclass);
				}
                $obj.hide();
                $t.html(opts.showLabel);
                opts.onHide.call(this);
            } else {
                if(opts.animateBG==true){
					//console.log($obj);
					$obj.addClass(opts.openBGRclass);
				}				
                $obj.show();
                $t.html(opts.hideLabel);
                opts.onShow.call(this);
            }
        } else { //console.log('No Object - Skipping');
            return;
        }
    }
    function _bussy_box($obj, opts) {
        //console.log(opts);
        $obj.html('<h2 style="color:green;text-align:center;">Loading ... <img src="' + opts.progress_image + '" alt="" border="0"></h2>');
    }
    function _load_missing_js($file_name) {
        $.getScript($file_name, function () {
            return true;
        });
    }
    function debug($obj) {
        if (window.console && window.console.log) {
            window.console.log('justtoggle debug: Start');
            window.console.log($obj);
            window.console.log('justtoggle debug: End');
        } else alert($obj);
    };
})(jQuery);
