tooltipTemplate = function(text){
	return '<div class="ttip"><div class="ttl"></div><div class="ttc"></div><div class="ttr"></div><div class="tm">' + text + '</div><div class="tbl"></div><div class="tbc"></div><div class="tbr"></div></div>';
};

show_custom = function(obj, ths){
	obj.nx = obj.position().left + obj.width() + obj.toffset[0];
    obj.ny = obj.position().top - $(ths).height() + obj.toffset[1];
    obj.nw = $(ths).width();
    obj.nww = $(window).width() + $(window).scrollLeft();
    if(obj.nx + obj.nw > obj.nww){
        obj.nx = obj.nww - obj.nw;
        if(obj.nx < 0){
            obj.nx = 0;
        }
    }
	obj.ttip.setPos(obj.nx, obj.ny);
	$(ths).show();
}

tooltipCache = {}

tooltipAjax = function(obj, url, params, cache, current, offset){
	obj.tcache = cache;
	obj.toffset = offset;
	if(!obj.toffset){
		obj.toffset = [-35, 17];
	}
	obj.simpletip({
		content: current,
		hideTime: 0,
		showEffect: 'custom',
		onBeforeShow: function(){
			obj.ttip = this;
			
			if(!this.loading){
				this.loading = true;
				obj.tupdate = function(){
					obj.tcv = $(obj.ttip.getTooltip()).filter(':visible').length > 0;
					obj.ttip.hide();
					obj.ttip.getTooltip().html(tooltipTemplate(obj.tdata));
					if(obj.tcv){
						obj.ttip.show();
					}
				}
				if(!tooltipCache[obj.tcache]){
					$.post(url, params, function(data){
						obj.tdata = data;
						tooltipCache[obj.tcache] = obj.tdata;
						obj.tupdate();
					});
				}else{
					obj.tdata = tooltipCache[obj.tcache];
					obj.tupdate();
				}
			}
		},
		showCustom: function(){
			show_custom(obj, this);
		}
	});
};

confirmTemplate = function(text, url_yes){
	return tooltipTemplate('<div class="middle big blue" style="width: 300px">' +
	       text + '<br /><br />' +
	       '<form action="' + url_yes + '" method="post"><button type="submit" class="button-yes"></button> &nbsp;&nbsp; ' +
	       '<a class="tooltip-no" href="#" onclick="' + "$(this).parents('.tooltip').hide(); " + 'return false;"><button type="button" class="button-no"></button></a></form>' + 
	       '</div>');
};

jsConfirmTemplate = function(text){
	return tooltipTemplate('<div class="middle big blue" style="width: 300px">' +
	       text + '<br /><br />' +
	       '<button type="button" class="button-yes"></button> &nbsp;&nbsp; ' +
	       '<button type="button" class="button-no" onclick="' + "$(this).parents('.tooltip').hide();" + '"></button>' + 
	       '</div>');
};

okTemplate = function(text){
	return tooltipTemplate('<div class="middle" style="width: 120px">' +
	       text + '<br />' +
	       '<a class="tooltip-ok" href="" onclick="' + "$('.tooltip').hide(); " + 'return false;"><strong>ok</strong></a>' + 
	       '</div>');
};

tooltipConfirm = function(obj, options, offset){
	obj.toffset = offset;
	if(!obj.toffset){
		obj.toffset = [-35, 17];
	}
	defaults = {
		content: confirmTemplate(''),
		persistent: true,
		focus: true,
		hideTime: 0,
		showEffect: 'custom',
		onBeforeShow: function(){
			obj.ttip = this;
			if(!this.loading){
				this.loading = true;
    			obj.ttip.show();
			}
		},
		showCustom: function(){
			show_custom(obj, this);
		}
	}
	opts = $.extend({}, defaults, options);
	
	obj.eq(0).parent().simpletip(opts);
    var api = obj.eq(0).parent().simpletip();
    obj.click(function() {
        api.show();
        return false;
    });    
};

tooltipNormal = function(obj, ctt, offset){
	obj.toffset = offset;
	if(!obj.toffset){
		obj.toffset = [-35, 17];
	}
	obj.eq(0).parent().parent().simpletip({
		content: tooltipTemplate(ctt),
		hideTime: 0,
		showEffect: 'custom',
		onBeforeShow: function(){
			obj.ttip = this;
		},
		showCustom: function(){
			show_custom(obj, this);
		}
	});
};

comments_form = function(el){
    var cform = el.find('.comment-form form');
    var posting = false;
    cform.submit(function(){
        var textarea = cform.find('textarea');
        
        if(!posting && textarea.val() != 'Loading...' && textarea.val() != 'Please enter your comment.'){
            if(textarea.val() == ''){
                textarea.val('Please enter your comment.').one('focus', function(){
                    textarea.val('');
                });
                
                return false;
            }
            
            posting = true;
            
            $.post(cform.attr('action'), cform.serialize(), function(data){
                el.html(data);
                comments_form(el);
            });
            
            textarea.val('Loading...').one('focus', function(){
                textarea.val('');
            });
            
        }
        
        return false;
    });
};

$.fn.outside = function(el){
    return $(el).has(this).length == 0 && this != $(el)[0];
}