/*
 * JQuery SimpleUI
 * @author MOGRA DESIGN ,ltd.
 * @url http://www.mogra.co.jp
 * @version 0.8
 */


var scrolling = false;

(function($) {

var suffix = "_f2";
var activeClass = 'active';

$.fn.extend({
	swapImage:function(src0, src1){
		var dir = this.attr("src").split("/");
		
		dir.push(dir.pop().replace(src0, src1));
		this.attr("src", dir.join("/"));
	},
	swapOnImage:function(suffix){
		$(this).each(function(){
			var img = $(this).find('img');
			//オーバー
			var reg = eval('/'+suffix+'$/');
			if(img.attr('src').match(reg)){
				return;
			}
			var ext = img.attr('src').match(/(.gif|.jpg|.png)$/)[1];
			img.swapImage(ext , suffix+ext);
		});
	},
	swapOffImage:function(suffix){
		$(this).each(function(){
			var img = $(this).find('img');
			var ext = img.attr('src').match(/(.gif|.jpg|.png)$/)[1];
			img.swapImage(suffix+ext, ext);
		});
	}
});

$.extend({
	hoverImage:function(obj, opt){
		
		if(Boolean(opt)){
			suffix =  opt.suffix;
			activeClass =  opt.activeClass;
		}
		//グローバルメニュー
		obj.parent('a').hover(
		function(){
			$(this).swapOnImage(suffix);
		},
		function(){
			//アウト
			if(!$(this).parent().hasClass(activeClass)){
				$(this).swapOffImage(suffix);
			}
		})
	}
});


$.simple = {
  smoothScroll:function(obj, opt) {
    	obj.click(function(){
    		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
    			var tgt = $(this.hash);
    			if(this.hash != ''){
      			tgt = tgt.length && tgt || $('[name="' + this.hash.slice(1) +'"]');
      			$.simple.scrollTo(tgt, opt);
      			return false;
    			}
    		}
    })
  },
  scrollTo:function(tgt, opt){
    
    if(typeof(tgt) == 'string'){
      tgt = $(tgt);
    }
    
    if (tgt.length) {
      var tgt_o = tgt.offset().top;
      var page_top = $('body').scrollTop() ? $('body').scrollTop() : $('html').scrollTop();
      var t = Math.abs(page_top - tgt_o) / 5;
      var e = 'swing';
      var m = tgt_o;
      var c = function(){};
      if(Boolean(opt)){
        	t = Boolean(opt.duration) ? opt.duration : t;
        	e = Boolean(opt.easing) ? opt.easing : e;
        	m = Boolean(opt.margin) ? m - opt.margin : m;
        	c = Boolean(opt.complete) ? opt.complete : c;
      }
      //alert(t)
      scrolling = true;
      $('html,body').animate({scrollTop: m}, {duration: t, easing: e, complete:function(){
        	c();
        	scrolling = false;
      }});
    }
  }
}

$.tooltip = function(obj){
  $('body').mousemove(function(e){
    //console.log(e)
    if($('#tooltip').size()){
      $('#tooltip').css({
        top: e.pageY - 50,
        left: e.pageX - 10
      })
    }
  })
  
  obj.hover(function(e){
    if($('#tooltip').size()){
      return;
    }
    var tit = $(this).data('title');
    var $tooltip = $('<div id="tooltip">'+tit+'</div>');
    
    $('body').append($tooltip);
  },function(){
    $('#tooltip').remove();
  })
}

})(jQuery);
