/*===========================================

URL短縮

===========================================*/

var LOGIN = "diamor";
var API_KEY = "R_3c32edbbf0e5fb7019a0f64f926f95a9";

$(function(){
	
	//mixiカレンダー
	var mixiCalURL = $("a#ShareBtnMixiCal").attr("title");
	if ( mixiCalURL ){
		shortenURL( mixiCalURL, function( i_shortURL ){
			var href = $("a#ShareBtnMixiCal").attr("href");
			href = href.replace(decodeURIComponent(mixiCalURL),i_shortURL);
			$("a#ShareBtnMixiCal").attr("href",href);
		} );
		$("a#ShareBtnMixiCal").attr("title","");
	}
	
	//mixiボイス
	var mixiVoiceURL = $("a#ShareBtnMixiVoice").attr("title");
	if ( mixiVoiceURL ){
		shortenURL( mixiVoiceURL, function( i_shortURL ){
			var href = $("a#ShareBtnMixiVoice").attr("href");
			href = href.replace(decodeURIComponent(mixiVoiceURL),i_shortURL);
			$("a#ShareBtnMixiVoice").attr("href",href);
		} );
		$("a#ShareBtnMixiVoice").attr("title","");
	}
	
	//facebook
	var facebookURL = $("a#ShareBtnFacebook").attr("title");
	if ( facebookURL ){
		shortenURL( facebookURL, function( i_shortURL ){
			var href = $("a#ShareBtnFacebook").attr("href");
			href = href.replace(decodeURIComponent(facebookURL),i_shortURL);
			$("a#ShareBtnFacebook").attr("href",href);
		} );
		$("a#ShareBtnFacebook").attr("title","");
	}	
	
	function shortenURL( i_url, i_callback ) {
		$.ajax({
		    type: "GET",
		    url: "http://api.bit.ly/shorten?version=2.0.1&longUrl=" + i_url + "&login=" + LOGIN + "&apiKey=" + API_KEY,
		    dataType: "jsonp",
		    success: function(data) {
		    	var url = decodeURIComponent(i_url)
		        if (data.results && data.results[url]) {
		            i_callback( data.results[url]["shortUrl"] );
		        }
		    }
		});
	}
	
});


/*===========================================

queueを消すstop

===========================================*/
$.fn.exStop = function() {
	return this.queue([]).stop();
}


/*===========================================

opacity:1までフェードイン

===========================================*/
$.fn.exFadeIn = function( i_time ) {
	this.exStop();
	return this.each(function(){
		var $this = $(this);
		if ( $this.css("display") == "none" ) {
			$this.show();
			$this.css( {opacity:0} );
		}
		$this.animate( { opacity:1 }, i_time );
	});
}

//フェードアウト
$.fn.exFadeOut = function( i_time ) {
	this.exStop();
	return this.fadeOut( i_time );
}

/*===========================================

遅延実行プラグイン

===========================================*/
$.fn.startTimer = function( i_function, i_time, i_key ) {
	return this.each(function(){
		$(this).stopTimer( i_key );
		var timers = $(this).data("timers") || {};
		timers[i_key] = setTimeout( i_function, i_time );
		$(this).data("timers",timers);
	});
}

$.fn.stopTimer = function( i_key ) {
	return this.each(function(){
		var timers = $(this).data("timers") || {};
		if ( timers[i_key] ) clearTimeout( timers[i_key] );
	});
}

/*
$.fn.initRollOver = function() {
	return this.each(function(){
		var $img = $(this).children("img");
		if ( !$img.length ) return;
		var up = $img.attr("src");
		var over = up.replace(/＼.([a-zA-Z0-9]+)$/,"-over.$1");
		$("<img />").attr("src",over);
		$(this).data("img",$img);
		$(this).data("up",up);
		$(this).data("over",over);
	});
}
*/
$.fn.over = function() {
	return this.each(function(){
		if ( $(this).data("img") ) $(this).data("img").attr("src",$(this).data("over"));
	});
}

$.fn.up = function() {
	return this.each(function(){
		if ( $(this).data("img") ) $(this).data("img").attr("src",$(this).data("up"));
	});
}


/*===========================================

mixiのボックス表示

===========================================*/

$(function(){
	
	$("div#MixiBox").hover(
		function(){
			var $this = $(this);
			
			$this.stopTimer();
			$this.exFadeIn(200);

		},
		function(){
			var $this = $(this);
			$this.startTimer( function() { $this.exFadeOut(200) }, 300 );
		}
	);
	
	$("strong#ShareBtnMixi").hover(
		function(){
			//alert(1);		
			$( "div#MixiBox" ).mouseover();
		},
		function(){
			//alert(2);
			$( "div#MixiBox" ).mouseout();
		}
	);
});







