/* JS TITLE 属性取得
-----------------------------------------------------------------------*/
function getScriptTitleTag(e){
	if(e.nodeName.toLowerCase() == 'script'){
		return e.getAttribute('title');
	}else{
		return arguments.callee(e.lastChild);
	}
}

/* RETWEET
-----------------------------------------------------------------------*/
function retweet(data){
	retweet.setDomain(); // ドメイン判定
	retweet.setData(data); // 引数を変数に代入
	retweet.setKey(); // ID と APIKEY を変数に代入
	retweet.setApi(); // ID と APIKEY を APIURL に代入し変数に代入
	retweet.setVariable(); // 変数セット フラグ
	retweet.setHashtag(); // ハッシュタグありなし
	retweet.setTitlecut(); // タイトルをカットするかしないか
	retweet.thispageORindexes(); // location か index か判別
/*	retweet.addScript();*/
	setTimeout('retweet.addScript()',100); // JSON コール
}

retweet.setDomain = function(){ // ドメイン判定
	var a = location.pathname;
	if(a.match(/^\/(makoto|bizid|d-style|bizmobile)\//)){
		var b = 'http://bizmakoto.jp';
	}else if(a.match(/^\/(pcuser|pcupdate|mobile|lifestyle|plus|navi)\//)){
		var b = 'http://plusd.itmedia.co.jp';
	}else if(a.match(/^\/dc\//)){
		var b = 'http://camera.itmedia.co.jp';
	}else if(a.match(/^\/games\//)){
		var b = 'http://gamez.itmedia.co.jp';
	}else if(a.match(/^\/executive\//)){
		var b = 'http://mag.executive.itmedia.co.jp';
	}else if(a.match(/^\/tt\//)){
		var b = 'http://techtarget.itmedia.co.jp';
	}else if(a.match(/^\/ebook\//)){
		var b = 'http://ebook.itmedia.co.jp';
	}else{
		var b = 'http://www.itmedia.co.jp';
	}
	this.mydomain = b;
}

retweet.setData = function(data){ // 引数を変数に代入
	this.data = data;
}

retweet.setKey = function(){ // ID と APIKEY を変数に代入
	if(!this.data['id'] || !this.data['apikey']){
		this.bitly_id  = 'itmedia';
		this.bitly_key = 'R_736fc703c85d8539ea61ad3758938118';
	}else{
		this.bitly_id  = this.data['id'];
		this.bitly_key = this.data['apikey'];
	}
}
retweet.setApi = function(){ // ID と APIKEY を APIURL に代入し変数に代入
	this.api = 'http://api.bit.ly/shorten' + '?version=2.0.1' + '&format=json' + '&callback=retweetCallback' + '&login=' + this.bitly_id + '&apiKey=' + this.bitly_key + '&longUrl=';
}
retweet.setVariable = function(){ // 変数セットフラグ
	if(!this.data['variable']){
		this.variable = false;
	}else{
		this.variable = true;
	}
}
retweet.setHashtag = function(){ // hashtag セット
	if(!this.data['hashtag']){
		this.hashtag = '';
	}else{
		this.hashtag = this.data['hashtag'];
	}
}
retweet.setTitlecut = function(){ // Title カット
	if(!this.data['titlecut']){
		this.titlecut = false;
	}else{
		this.titlecut = this.data['titlecut'];
	}
}
retweet.thispageORindexes = function(){ // location か index か判別
	if(!this.data['elem']){
		retweet.thispage();
	}else{
		retweet.indexes();
	}
}
retweet.addScript = function(){
	document.getElementsByTagName('head')[0].appendChild(this.script);
}

retweet.thispage = function(){
	if(!this.titlecut == false){
		if(document.title.length < this.titlecut){
			this.tit = document.title;
		}else{
			this.tit = document.title.slice(0,this.titlecut) + '...';
		}
	}else{
		this.tit = document.title;
	}
	this.script = document.createElement('script');
	this.script.setAttribute('type','text/javascript');
	this.script.setAttribute('title','{"title":"' + this.tit + '","link":"' + this.mydomain + location.pathname + '","hashtag":"' + this.hashtag + '","variable":' + this.variable + '}');
	this.script.setAttribute('src',this.api + encodeURIComponent(this.mydomain + location.pathname));
}
retweet.indexes = function(){
	if(!this.data['blockclassname']){
		this.blockclassname = 'index';
	}else{
		this.blockclassname = this.data['blockclassname'];
	}
	if(!this.data['titleclassname']){
		this.titleclassname = 'txTitle';
	}else{
		this.titleclassname = this.data['titleclassname'];
	}
	if(!this.data['urlclassname']){
		this.urlclassname = 'URL';
	}else{
		this.urlclassname = this.data['urlclassname'];
	}
	this.btn = this.data['elem'];
	while(this.btn.className != this.blockclassname){
		this.btn = this.btn.parentNode;
	}
	this.box = this.btn;
	this.chl = this.box.childNodes;
	for(var i = 0; i < this.chl.length; i++){
		if(this.chl[i].className == this.titleclassname){
			var a = this.chl[i].getElementsByTagName('a');
			for(var j = 0; j < a.length; j++){
				if(a[j].className == this.urlclassname){
					this.lnk = a[j].getAttribute('href');
					this.tit = a[j].innerHTML;
					break;
				}
			}
		}
	}
	if(this.lnk.match(/^http/)) this.mydomain = '';
	this.script = document.createElement('script');
	this.script.setAttribute('type','text/javascript');
	this.script.setAttribute('title','{"title":"' + this.tit + '","link":"' + this.mydomain + this.lnk + '","hashtag":"' + this.hashtag + '","variable":' + this.variable + '}');
	this.script.setAttribute('src',this.api + encodeURIComponent(this.mydomain + this.lnk));
}

/* bitly API
-----------------------------------------------------------------------*/
function retweetCallback(json){
	var sel = '';
	if(document.selection){
		sel = document.selection.createRange().text;
	}else if(window.selection){
		sel = window.selection.createRange().text;
	}else if(document.getSelection){
		sel = document.getSelection();
	}else if(window.getSelection){
		sel = window.getSelection();
	}

	eval('var attr = ' + getScriptTitleTag(document.getElementsByTagName('head')[0]) + ';');

	var tit = attr['title']; // タイトル
	var lnk = attr['link']; // リンク
	if(!attr['hashtag'] || attr['hashtag'] == ''){ // ハッシュタグ
		var has = '';
	}else{
		var has = ' ' + attr['hashtag'];
	}
	if(attr['variable'] == true){
		shorturl_bitly = json.results[lnk]['shortUrl'];
	}else{
		var f = 'http://twitter.com/intent/tweet?text=' + encodeURIComponent(tit + ' ' + json.results[lnk]['shortUrl'] + has);
		if(navigator.userAgent.indexOf('Chrome') != -1 || navigator.userAgent.indexOf('Safari') != -1){
			location.href = f;
		}else{
			window.open(f,'retweet');
		}
	}
/*	document.removeChild(document.getElementsByTagName('head')[0].lastChild);*/
}

