
var init;
var secondInit;
var indexInit;

$(init);

function init () {
	//alert("init ()");
	
	var scroll = new SmoothScroll();
		
	if (secondInit) secondInit();
	if (indexInit) indexInit();
}

function delegate (this_object, method) {
	//trace("delegate" + [this_object, method] + ")");
	
	return function () { return method.apply(this_object, arguments); }
};

function delegateWithArgs (this_object, method, args, join_args) {
	//trace("fac.delegateWithArgs(" + [this_object, method, args, join_args] + ")");
	
	return function () {
		if (join_args) {
			var joined_args = new Array();
			var i;
			var length = arguments.length;
			for (i = 0; i < length; i++) {
				joined_args.push(arguments[i]);
			};
			joined_args = joined_args.concat(args);
			return method.apply(this_object, joined_args);
		}
		else {
			return method.apply(this_object, args);
		};
	};
};

function printPage () {
	
	$("conceptline").stop();
	
	print();
}


//EventDispatcher
function EventDispatcher () {
	//alert("EventDispatcher (" + [] + ")");
	
} 

EventDispatcher.initialize = function (object) {
	//alert("EventDispatcher.initialize (" + [object] + ")");
	
	object.__ed_eventContainer = new Object();
	object.addEventListener = this._addEventListener;
	object.removeEventListener = this._removeEventListener;
	object.dispatchEvent = this._dispatchEvent;
}

EventDispatcher._addEventListener = function (eventName, object) {
	//alert("EventDispatcher._addEventListener (" + [eventName, object] + ")");
	
	if ( this.__ed_eventContainer[eventName] == null) {
		this.__ed_eventContainer[eventName] = new Array();
	}
	this.removeEventListener(eventName, object);
	this.__ed_eventContainer[eventName].push(object);
}

EventDispatcher._removeEventListener = function(eventName, object) {
	//alert("EventDispatcher._removeEventListener (" + [eventName, object] + ")");
		
	var listener_ar = this.__ed_eventContainer[eventName];
	if(listener_ar == undefined) return;
	
	var imax = listener_ar.length;
	for (var i = 0; i < imax; i++) {
		var listener = listener_ar[i];
		if (listener == object) {
			listener_ar.splice(i, 1);
			return;
		}
	}
}

EventDispatcher._dispatchEvent = function (event){
	//alert("EventDispatcher._dispatchEvent (" + [event] + ")");
	
	if(event.target==null) event.target = this;
	
	var eventName = event.type;
	if(this.__ed_eventContainer[eventName] == null) return;
	
	var imax = this.__ed_eventContainer[eventName].length;
	for (var i = 0; i < imax; i++) {
		var listener = this.__ed_eventContainer[eventName][i];
		if (typeof(listener) == "object") {
			listener[eventName].apply(listener, new Array(event));
			return;
		}
		else {
			listener(event);
			return;
		}
	}
}


/* SmoothScroll */
function SmoothScroll (elements) {
	//console.log("SmoothScroll(" + [elements] + ")");
	
	$('a[href^=#]').click(this.scroll);
	$(document).mousewheel(this.onMouseWheel);
}

SmoothScroll.prototype.scroll = function () {
	//console.log("SmoothScroll.scroll(" + [] + ")");
	
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		if ($target.length) {
			var position = $target.offset().top;
			position = position == 0 ? position : position + anchor_offset;
			$('html, body').stop().animate({scrollTop:position}, 600, "easeOutQuart");
			return false;
		}
	}
}

SmoothScroll.prototype.onMouseWheel = function () {
	//console.log("SmoothScroll.onMouseWheel(" + [] + ")");
	
	$('html, body').stop();
}


/* StickyElement */
function StickyElement (target, min_y, offset) {
	//console.log("StickyElement(" + [target, min_y, offset] + ")");
	
	this.target = $(target);
	this.original_style = {position:this.target.css("position"), top:this.target.css("top")};
	this.min_y = min_y != undefined ? min_y : this.target.offset().top;
	anchor_offset = offset || -this.target.height();
	
	this.check();
	$(window).scroll(delegate(this, this.check));
}

StickyElement.prototype ={
	
	check : function () {
		//console.log("StickyElement.check(" + [] + ")");
		
		if ($($.browser.safari ? 'body' : 'html').scrollTop() >= this.min_y) this.target.css({position:"fixed", top:0});
		else this.target.css(this.original_style);
	}
}


//SocialLinks
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 = 'exelco';
	var apiKey = 'R_47bd149e77cb39f849c0003bc9fe8541';
	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";
		}
	}
	
}


/* fac */
if (typeof fac == "undefined") fac = function() {};

fac.delegate = function  (this_object, method, args, join_args) {
	//fac.log("fac.delegate(" + [this_object, method, args, join_args] + ")");
	
	return function () {
		if (join_args) {
			var joined_args = new Array();
			var i;
			var length = arguments.length;
			for (i = 0; i < length; i++) {
				joined_args.push(arguments[i]);
			};
			joined_args = joined_args.concat(args);
			return method.apply(this_object, joined_args);
		}
		else if (args && args.length >= 1) {
			return method.apply(this_object, args);
		}
		else {
			return method.apply(this_object, arguments);
		};
	};
};

fac.log = function (args) {
	//console.log.apply(this, arguments);
	
	if ($("#fac_console_log").length == 0) {
		$("body").append('<div id="fac_console_log" style="width:200px; height:20em; position:fixed; _position:absolute; left:0; top:0; z-index:10000; background:#fff; border:1px solid #000; color:#000; text-align:left; overflow:auto;">::Log::</div>'); 
	};
	
	var console = $("#fac_console_log");
	
	console
		.append('<div>' + String(args) + '</div>')
		.scrollTop(console.scrollHeight);
};





