if (typeof tgtr == 'undefined') {
	tgtr = new Object();
	tgtr.jQuery = null;

	tgtr.url = "http://togetter.com",
	tgtr.ListWidget = function(s){
		document.open();
		document.write("<iframe width='"+s.width+"' height='"+s.height+"' src='"+tgtr.url+"/ipage/iframe/"+s.id+"?bc="+s.color+"' style='border:solid 1px #CCC;' frameborder='0'></iframe>");
		document.close();
	}
	tgtr.InfoWidget = function(s){
		document.open();
		document.write("<iframe width='"+s.width+"' height='"+s.height+"' src='"+tgtr.url+"/ipage/iinfo/"+s.id+"' style='border:solid 1px #CCC;' frameborder='0'></iframe>");
		document.close();
	}
	tgtr.ExtendWidget = function(s){

		document.open();
		document.write("<div id='ChpStry_Widget_"+s.id+"' class='ChpStry_Widget'></div>");
		document.close();
		
		if(window.jQuery === undefined){
			var script = document.createElement('script');
			script.setAttribute("type", "text/javascript");
			script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js");
			script.onload = function(){tgtr.onLoad(s)};
			script.onreadystatechange = function(){
				if (this.readyState == 'complete' || this.readyState == 'loaded') {
					tgtr.onLoad(s);
				}
			};
			(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script);
		} else {
			tgtr.jQuery = window.jQuery.noConflict(true);
			tgtr.StartWidget(s);
		}
	},
	tgtr.onLoad = function(s){
		tgtr.jQuery = window.jQuery.noConflict(true);
		tgtr.StartWidget(s);
	},
	tgtr.StartWidget = function(s){
		
		tgtr.jQuery.getScript("http://b.st-hatena.com/js/bookmark_button.js", function(){
			tgtr.jQuery.getJSON(tgtr.url+"/ipage/jsonp/"+s.id+'?callbackname=?',function (text){
				tgtr.jQuery("#ChpStry_Widget_"+s.id).html(text);
				tgtr.init();
			});
		});
		
		var link = document.createElement('link');
		link.setAttribute("rel", "stylesheet");
		link.setAttribute("href", tgtr.url+"/css/parts.css");
		link.setAttribute("type", "text/css");
		(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(link);
		
	},
	tgtr.init = function(){
		
		tgtr.jQuery(".tweet_box .balloon_body").live("mouseover",function(e){
			tgtr.jQuery(e.currentTarget).find(".status_left").show();
		});
		tgtr.jQuery(".tweet_box .balloon_body").live("mouseout",function(e){
			tgtr.jQuery(e.currentTarget).find(".status_left").hide();
		});
		
	},
	tgtr.sid = function(elm){
		var m = tgtr.jQuery(elm).parents(".balloon_module").attr("id").match(/tweet_id_([0-9]+)_([0-9]+)/);
		return m[2];
	},
	tgtr.reply = function(elm,idx){
		var href = "http://twitter.com/intent/tweet?in_reply_to="+tgtr.sid(elm);
		this.Intent(href);
	}	
	tgtr.retweet = function(elm,idx){
		var href = "http://twitter.com/intent/retweet?tweet_id="+tgtr.sid(elm);
		this.Intent(href);
	}	
	tgtr.favorite = function(elm,idx){
		var href = "http://twitter.com/intent/favorite?tweet_id="+tgtr.sid(elm);
		this.Intent(href);
	}
	tgtr.Intent = function(href){
		var width = 550,height = 420,winHeight = screen.height,winWidth = screen.width;
		
        var left = Math.round((winWidth / 2) - (width / 2));
        var top = 0;

        if (winHeight > height) {
          top = Math.round((winHeight / 2) - (height / 2));
        }
		window.open(href, 'intent', 'scrollbars=yes,resizable=yes,toolbar=no,location=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
	}
	tgtr.more = function(id,page){
		tgtr.jQuery.getJSON(tgtr.url+'/ipage/moreTweets/'+id+'?callbackname=?',{page:page},function (text){
			tgtr.jQuery("#more_tweet_box_"+id).replaceWith(text);
		});
	}
}
