(function ($) {
    jQuery.fn.powertoggle = function (options) { //console.log('powertoggle START');
	
        var $realy_clicked = 0;
        var opts = $.extend({},
        $.fn.powertoggle.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.powertoggle'); //Bind Click 
            jQuery(e).bind('click.powertoggle', function (event) {
                event.preventDefault();
				
				
                $rel = $(this).attr('rel');
                $href = $t.attr('href');
				
                //ORIG
				//if ($href.match(/^#/) && $href.length > 1) {
                //    $id = $href.substr(1);
                //    $obj = jQuery('#' + $id);                    
                //    _process($this, $t, $obj);
                //} else { 
				//	//console.log('BAD HREF : EXITING');
				//	alert('BAD HREF : EXITING '+$href);
                //    return;
                //}
				//EOF orig
				if ($href.match(/#/) && $href.length > 1) {				
                    //$id = $href.substr(1);
					$_id=$href.split('#');
					$id=$_id[1];				
					
                    $obj = jQuery('#' + $id);
                    _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.powertoggle.defaults = {
        showLabel: 'Preview',
        hideLabel: 'Hide',
		progress_image:'../images/indicator.gif',
		ajaxFile: 'aprobe.php',
		animateBG:false,
		openBGRclass:'_power_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 {
                _bussy_box($obj,opts);
                _ajax_on_show($obj,opts.ajaxFile);
				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 _ajax_on_show($obj,file_to_use) { 
	
	//console.log('Ajax Start');
	//	console.log($obj);
        $.ajax({
            async: true,
            type: "POST",
            url: file_to_use,
            data: "adata=" + $rel,
            success: function (msg) {
                $obj.html(msg);
		$obj.show();
		//console.log($obj);
            },
            error: function (msg) {
                //alert(" Error  : " + msg.responseText);
		$obj.html(msg.responseText);
		$obj.show();
		//console.log($obj);
            }
        });
    }

    function _bussy_box($obj,opts) {
        $obj.html('<h2 style="color:green;text-align:center;">Processing  <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('powertoggle debug: Start');
            window.console.log($obj);
            window.console.log('powertoggle debug: End');
        } else alert($obj);
    };
})(jQuery);
