﻿

function fbs_click(url, title) {
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}

// fixes for IE-7 cleartype bug on fade in/out
jQuery.fn.fadeIn = function(speed, callback) {
	return this.animate({ opacity: 'show' }, speed, function() {
		if (jQuery.browser.msie) this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback)) callback();
	});
};

jQuery.fn.fadeOut = function(speed, callback) {
	return this.animate({ opacity: 'hide' }, speed, function() {
		if (jQuery.browser.msie) this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback)) callback();
	});
};

jQuery.fn.fadeTo = function(speed, to, callback) {
	return this.animate({ opacity: to }, speed, function() {
		if (to == 1 && jQuery.browser.msie) this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback)) callback();
	});
};

// liquid <> fixed
function setPageWidth() {
	var currentWidth = jQuery('#page').css('width');
	var COOKIE_NAME = 'imperugo.tostring.it';

	if (currentWidth == "95%") {
		newWidth = "960px";
		$.cookie(COOKIE_NAME, null, options);
	}
	else if (currentWidth == "960px") {
		newWidth = "95%";
	}
	else {
		newWidth = "960px";
		$.cookie(COOKIE_NAME, null, options);
	}

	jQuery("#page").animate({ width: newWidth }, 333).fadeIn("slow");

	var options = { path: '/', expires: 60 };
	jQuery.cookie(COOKIE_NAME, newWidth, options);
}

// minitabs
jQuery.fn.minitabs = function(speed, effect) {
	id = "#" + this.attr('id')
	jQuery(id + ">DIV:gt(0)").hide();
	jQuery(id + ">UL>LI>A:first").addClass("current");
	jQuery(id + ">UL>LI>A").click(
    function() {
    	jQuery(id + ">UL>LI>A").removeClass("current");
    	jQuery(this).addClass("current");
    	jQuery(this).blur();
    	var re = /([_\-\w]+$)/i;
    	var target = jQuery('#' + re.exec(this.href)[1]);
    	var old = jQuery(id + ">DIV");
    	switch (effect) {
    		case 'fade':
    			old.fadeOut(speed).fadeOut(speed);
    			target.fadeIn(speed);
    			break;
    		case 'slide':
    			old.slideUp(speed);
    			target.fadeOut(speed).fadeIn(speed);
    			break;
    		default:
    			old.hide(speed);
    			target.show(speed)
    	}
    	return false;
    }
 );
}

function initTooltips(o) {
	var showTip = function() {
		var el = jQuery('.tip', this).css('display', 'block')[0];
		var ttHeight = jQuery(el).height();
		var ttOffset = el.offsetHeight;
		var ttTop = ttOffset + ttHeight;
		jQuery('.tip', this)
	  .stop()
	  .css({ 'opacity': 0, 'top': 2 - ttOffset })
  	  .animate({ 'opacity': 1, 'top': 18 - ttOffset }, 250);
	};
	var hideTip = function() {
		var self = this;
		var el = jQuery('.tip', this).css('display', 'block')[0];
		var ttHeight = jQuery(el).height();
		var ttOffset = el.offsetHeight;
		var ttTop = ttOffset + ttHeight;
		jQuery('.tip', this)
	  	.stop()
	  	.animate({ 'opacity': 0, 'top': 10 - ttOffset }, 250, function() {
	  		el.hiding = false;
	  		jQuery(this).css('display', 'none');
	  	}
      );
	};
	jQuery('.tip').hover(
	  function() { return false; },
	  function() { return true; }
	);
	jQuery('.tiptrigger, .cat-item').hover(
	  function() {
	  	var self = this;
	  	showTip.apply(this);
	  	if (o.timeout) this.tttimeout = setTimeout(function() { hideTip.apply(self) }, o.timeout);
	  },
	  function() {
	  	clearTimeout(this.tttimeout);
	  	hideTip.apply(this);
	  }
	);
}

function tabmenudropdowns() {
	jQuery(" #tabs li").hover(function() {
		jQuery(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(333);
	}, function() {
		jQuery(this).find('ul:first').css({ visibility: "hidden" });
	});
}

jQuery(document).ready(function() {
	if (document.all && !window.opera && !window.XMLHttpRequest && jQuery.browser.msie) { var isIE6 = true; }
	else { var isIE6 = false; };
	jQuery.browser.msie6 = isIE6;
	if (!isIE6) {
		initTooltips({
			timeout: 6000
		});
	}
	tabmenudropdowns();
	highlightXFNLinks();


	jQuery('#sidebar ul.nav li ul li a').mouseover(function() {
		jQuery(this).animate({ marginLeft: "4px" }, 100);
	});

	jQuery('#sidebar ul.nav li ul li a').mouseout(function() {
		jQuery(this).animate({ marginLeft: "0px" }, 100);
	});

	jQuery("a#toplink").click(function() {
		jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
	});

});


function HighlightWord(key) {
	jQuery('#mid-content').highlight(key);
}

function bbcode_ins(fieldId, tag) {
	field = document.getElementById(fieldId);
	if (tag == 'b' || tag == 'i' || tag == 'u' || tag == 'php' || tag == 'code') {
		if (document.selection) {
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + '][/' + tag + ']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) {
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos) + '[' + tag + '][/' + tag + ']' + field.value.substring(endPos, field.value.length);
		}
	}
}

var TwitThis = {
	pop: function() {
		return window.open("http://twitthis.com/twit?url=" + encodeURIComponent(location.href) + "&title=" + ((document.title) ? encodeURIComponent(document.title.replace(/^\s*|\s*$/g, '')) : ""), "TwitThisPop", "width=600, height=500, location, status, scrollbars, resizable");
	}
}
