var container = "#container";
var foot = "#foot";
var foot_height;
var binded = false;

if(navigator.userAgent.search(/iPhone/) != -1 ) {
	//iPhone
	
	$("html").css("height", "auto");
	$("body").css("height", "auto");
	$(container).css("height", "auto");
	
}
else {
	
	$(function () {
		foot_height = $(foot).height();
		
		$(window).resize(adjustFoot);
		$(window).scroll(adjustFoot);
		adjustFoot();
	});
	
	function adjustFoot () {
		//alert("adjustFoot ()");
		
		var y = $(window).height() - foot_height + $(window).scrollTop();
		y = Math.max(y, 330);
		$(foot).css("top", y);
	}
	
}


function setSocialLinks (url, title) {
	
	document.write('<div id="social"></div>');
	var social = document.getElementById('social');
	var head = document.getElementsByTagName("head")[0] || document.documentElement;
	
	var login = 'francfranc';
	var apiKey = 'R_469973867742805109b34f4652bb6e36';
	var bitly_url = 'http://api.bit.ly/shorten?version=2.0.1&format=json&callback=bitlyCallback&login=' + login + '&apiKey=' + apiKey + '&longUrl=' + encodeURIComponent(url);
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = bitly_url;
	social.appendChild(script);
	
	this.bitlyCallback = function (json) {
		
		var short_url = encodeURIComponent(json.results[url]['shortUrl']);
		var anchor;
		var icon;
		var count;
		var text;
		var script;
		
		//Title
		var lead = document.createElement('strong');
		text = document.createTextNode("この記事をシェアする");
		lead.appendChild(text);
		text = document.createTextNode("  ");
		lead.appendChild(text);
		social.appendChild(lead);
		
		//Twitter
		anchor = document.createElement('a');
		anchor.className  = 'twitter';
		anchor.rel = 'nofollow';
		anchor.href = 'http://twitter.com/home/?status=' + title + ' ' + short_url;
		anchor.target = '_blank';
		anchor.title = 'この記事をTwitterでつぶやく';
		icon = document.createElement('img');
		icon.src = '/images/icon_twitter.gif';
		icon.alt = 'Twitter';
		icon.width = 16;
		icon.height = 16;
		text = document.createTextNode(" ");
		anchor.appendChild(icon);
		social.appendChild(anchor);
		social.appendChild(text);
		
		//Topsy
		anchor = document.createElement('a');
		anchor.id = 'tweet_count';
		anchor.rel = 'nofollow';
		anchor.href = 'http://topsy.com/tb/' + escape(url.replace('http://', ''));
		anchor.target = '_blank';
		text = document.createTextNode(" ");
		social.appendChild(anchor);
		social.appendChild(text);
		
		script = document.createElement("script");
		script.src = 'http://otter.topsy.com/trackbacks.js?callback=topsyCallback&url=' + encodeURIComponent(url);
		script.charSet = "utf-8";
		head.appendChild(script);
		
		//Hatena Bookmark
		anchor = document.createElement('a');
		anchor.className = 'hatenabookmark';
		anchor.rel = 'nofollow';
		anchor.href = 'http://b.hatena.ne.jp/entry/' + url;
		anchor.target = '_blank';
		anchor.title = 'この記事をはてなブックマークでシェア';
		icon = document.createElement('img');
		icon.src = '/images/icon_hatenabookmark.gif';
		icon.alt = 'はてなブックマーク';
		icon.width = 16;
		icon.height = 16;
		count = document.createElement('img');
		count.src = 'http://b.hatena.ne.jp/entry/image/' + url;
		text = document.createTextNode(" ");
		anchor.appendChild(icon);
		anchor.appendChild(count);
		social.appendChild(anchor);
		social.appendChild(text);
		
		//livedoor clip
		anchor = document.createElement('a');
		anchor.className = 'livedoorclip';
		anchor.rel = 'nofollow';
		anchor.href = 'http://clip.livedoor.com/page/' + url;
		anchor.target = '_blank';
		anchor.title = 'この記事をlivedoorクリップでシェア';
		icon = document.createElement('img');
		icon.src = '/images/icon_livedoorclip.gif';
		icon.alt = 'livedoorクリップ';
		icon.width = 16;
		icon.height = 16;
		count = document.createElement('img');
		count.src = 'http://image.clip.livedoor.com/counter/small/' + url;
		text = document.createTextNode(" ");
		anchor.appendChild(icon);
		anchor.appendChild(count);
		social.appendChild(anchor);
		social.appendChild(text);
		
		//Yahoo! Bookmark
		anchor = document.createElement('a');
		anchor.className = 'yahoobookmark';
		anchor.rel = 'nofollow';
		anchor.href = 'http://bookmarks.yahoo.co.jp/url/' + url;
		anchor.target = '_blank';
		anchor.title = 'この記事をYahoo!ブックマークに登録';
		icon = document.createElement('img');
		icon.src = '/images/icon_yahoobookmark.gif';
		icon.alt = 'Yahoo!ブックマーク';
		icon.width = 16;
		icon.height = 16;
		count = document.createElement('img');
		count.src = 'http://num.bookmarks.yahoo.co.jp/image/small/' + url;
		text = document.createTextNode(" ");
		anchor.appendChild(icon);
		anchor.appendChild(count);
		social.appendChild(anchor);
		social.appendChild(text);
		
		//Facebook
		anchor = document.createElement('a');
		anchor.className = 'facebook';
		anchor.name = 'fb_share';
		anchor.type = 'icon';
		anchor.share_url = url;
		anchor.title = 'この記事をfacebookでシェア';
		script = document.createElement("script");
		script.type = "text/javascript";
		script.src = 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
		text = document.createTextNode(" ");
		social.appendChild(anchor);
		social.appendChild(script);
		social.appendChild(text);
	}
	
	this.topsyCallback = function (json) {
		
		var response = json.response;
		
		if (!response.total) {return false;}
		
		var element = document.getElementById('tweet_count');
		if (element) {
			var suffix = response.total > 1 ? "tweets" : "tweet";
			element.innerHTML = response.total + suffix + "&nbsp;";
			element.style.display = "inline";
		}
	}
	
}



