function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

var sharebar = function(cl, socialfacebook, socialtwitter) {
	this.sb = this;
	this.sbcl = null;
	this.t = null;
	this.tooltip = null;
	this.closebutton = null;
	this.hidden = false;
	
	this.groups = new Array(
		'<span class="sstext">SHARE</span><span class="ssshare"><a href="javascript:;" title="Share via Facebook" class="st_facebook_custom"><img src="/images/facebook.png" /></a><a href="javascript:;" title="Share via Twitter" class="st_twitter_custom"><img src="/images/twitter.png" /></a><a class="st_email_custom" href="javascript:;" title="Share via E-mail" ><img src="/images/email.png" /></a><a href="javascript:;" title="More share options" class="st_sharethis_custom"><img src="/images/more.png" /></a></span>',
		'<span style="margin: 9px 7px 0 0; float: left"><a href="'+socialfacebook+'" title="Visit our Facebook fan page!" style="float: left"><img src="/images/facebook_logo.png" width="67" height="14" style="" /></a> <a href="'+socialtwitter+'" title="Follow us on Twitter!" style="float: left; margin-left: 2px; margin-top: -3px"><img src="/images/twitter.png" width="25" height="20" /></a></span><span style="float: left; width: 80px"><a class="st_fblike_hcount" href="javascript:;" ></a></span>',
		'<span style="float: left"><a href="javascript:;" class="pond-expand"><span class="pond-expand-wrap">&laquo; Pond Calculators &raquo;</span></a><span class="hidden pond-links"><span class="pond-links-wrap">\
		<a href="http://newenglandkoi.com/Pond-Salinity-Calculator.html">Pond Salinity</a><a href="http://newenglandkoi.com/Pond-Volume-Calculator.html">Pond Volume</a><a href="http://newenglandkoi.com/Pump-Cost-Calculator.html">Pump Cost</a><a href="http://newenglandkoi.com/Total-Dynamic-Head-Calculator.html">Total Dynamic Head</a>\
		</span></span></span>',
		'<span style="margin: 5px 0 0 0; float: left; "><form action="/searchResults.php" method="post"><span class="inputwrap"><input class="input" type="text" name="searchFor" value="Search NewEnglandKoi.com" /></span><input type="submit" class="submit" value="" /></form></span>',
		'<span style="margin: 10px 0 0 0; float: left">&copy; 2011 <a href="http://koihosting.com">KoiHosting</a></span>'
	);
	
	this.init = function(cl) {
		var t = this;
		$('body').append('<div class="'+cl+'"><div class="'+cl+'-wrap"></div></div>');
		this.t = $('.'+cl+'-wrap');
		this.sbcl = $('.'+cl);
		
		this.t.append('<div class="ssdelim"></div>');
		for (g in this.groups) {
			this.t.append('<div class="ssgroup">'+this.groups[g]+'</div>');
			this.t.append('<div class="ssdelim"></div>');
		}
		// setup typical stuff
		this.t.append('<a class="sshide" href="javascript:;"></a>');
		this.t.append('<div class="sstooltip"></div>');
		this.close = this.t.find('.sshide');
		this.close.click(function() { t.showhideSharebar(this) });
		this.tooltip = this.t.find('.sstooltip');
		this.initTooltips();
		this.preloadImages();
		
		// custom stuff
		this.t.find('.ssgroup input[type=text]').each(function() {
			var ct = $(this).val();
			$(this).focus(function() {
				if ($(this).val() == ct)
					$(this).val('');
			});
			$(this).blur(function() {
				if ($(this).val() == '')
					$(this).val(ct);
			});
		});
		
		var plw = 335;
		var pew = 98;
		$('.pond-expand').click(function() {
			$('.pond-links').css({width:0}).removeClass('hidden').animate({width:plw});
			$('.pond-expand').animate({width:0}, function() { $(this).addClass('hidden') });
		});
		var to = null;
		$('.pond-links').hover(function() {
			clearTimeout(to);
		}, function() {
			to = setTimeout(function() {
				$('.pond-links').css({width:plw}).removeClass('hidden').animate({width:0}, function() {
					$(this).addClass('hidden');
				});
				$('.pond-expand').removeClass('hidden').css({width:0}).animate({width:pew});
			}, 1000);		
		});
		
		// hidden or not
		var hidden = getCookie('sharebar-hidden');
		if (hidden == "1") {
			this.hidden = true;			
			this.close.addClass('ssshow');
			this.close.css({top:-25});
			h = $(this.sbcl).height();
			this.sbcl.css({bottom:-h});
			
		}
	}
	
	this.preloadImages = function() {
		$('<img />').attr('src', '/images/show.png');
	}
	
	this.initTooltips = function() {
		var t = this;
		this.t.find('*[title]').each(function() {
			var tt = $(this).attr('title');
			$(this).attr('title', '');
			$(this).mousemove(function(e) {
				t.showTooltip(this, tt, e);
				return false;
			});
			$(this).hover(function(){}, function() {
				t.hideTooltip();
			});
		});
	}
	
	this.showhideSharebar = function(target) {
		if (this.sb.hidden) {
			this.sbcl.animate({bottom:0});
			this.hidden = false;
			setCookie('sharebar-hidden', 0, 31); 
			$(target).removeClass('ssshow');
			$(target).animate({top:-13});
		} else {
			h = $(this.sbcl).height();
			this.sbcl.animate({bottom:-h}); 
			this.hidden = true;
			setCookie('sharebar-hidden', 1, 31); 
			$(target).addClass('ssshow');
			$(target).animate({top:-25});
		}
	}
	
	this.hideTooltip = function() {
		this.tooltip.css('display', 'none');
	}
	this.showTooltip = function(target, title, e) {
		var x = e.clientX + 10;// + e.offsetX;
		var y = e.clientY - 20;// + e.offsetY;
		this.tooltip.css('display', 'block').css('top', y).css('left', x).html(title);
	}
	
	this.init(cl);
	
	$(function() {
		var i = setInterval(function() { 
			$('iframe.grelated-iframe').each(function() {
				$(this).prev().css('display', 'none');
				$(this).css('display', 'none');
				
				clearInterval(i);
			});
		}, 500);
	});
};


