/*
 * SHIFT 共通 JavaScript Liblary (Based on jQuery1.4.2)
 * Ver. 0.1.1
 * 21:59 2010/05/11 nagisa@shift-jp.net
 
 ----------
 使い方：
 ・head内で【jQreryの後に】呼んでください
 ・HTMLがルートパスにない場合、rootPathにルートパスまでの相対パスを渡してください
	 ex.) <script type="text/javascript">var rootPath = '../../'; </script>
・変数を呼ぶときの基本は、shift.functionName({varName1:var1,varName2:var2,varName3:var3...)という具合です
・！！【納品時にはshift.min.jsを使用して下さい】！！
----------- 
*/

if(!rootPath) { var rootPath = ''; }

var shift = {
	/* 
	  * 外部JavaScript読み込み関数
	  * [引数] path ※必須 : HTMLからjsファイルまでの相対or絶対パス
	  * [戻り値] なし
	*/
	include: function(path)
	{
		var $s = $('<script>');
		$s.attr('type','text/javascript').attr('src',path);
		$('head').append($s);
	},
	/* 
	  * 外部CSS読み込み関数
	  * [引数] path ※必須 : HTMLからjsファイルまでの相対or絶対パス
	  * [戻り値] なし
	*/
	importCSS: function(path,target)
	{
		$('head').append('<link rel="stylesheet" type="text/css" href="' + path + '" />');
	},
	/* 
	  * URLからディレクトリ名・ファイル名を取得する関数
	  * [引数] なし
	  * [戻り値] vars連想配列 / vars['file'] : ファイル名・vars['dir'] : ディレクトリ名
	  * [サンプル] var path = shift.getURL(); alert('ファイル名:'+path['file']); alert('ディレクトリ名:'+path['dir'])
	*/
	getURL: function()
	{
		var vars =[];
		var url = window.location.href;
		var arr = url.split('\/');
		var last = arr.length-1;
		if(arr[last]==='') { arr.pop(); last--; }
		if(arr[last].indexOf('?')>0) { arr[last] = arr[last].substring(0,arr[last].indexOf('?')); }
		if(arr[last].indexOf('#')>0) { arr[last] = arr[last].substring(0,arr[last].indexOf('#')); }
		if(arr[last].indexOf('.htm')>0 || arr[last].indexOf('.php')>0){
			vars['file'] = arr[last];
			vars['dir'] = arr[3];
			vars['parent'] = arr[last-1];
		} else {
			vars['file'] = '';
			vars['dir'] = arr[3];
			vars['parent'] = arr[last];
		}
		return vars;
	},
	getQuery :function() {
		var qs=location.search;
		if (qs) {
			var qs_arr = qs.substring(1).split('&');
			var params = {};
			for(var i=0; i<qs_arr.length; i++) {
				var pair=qs_arr[i].split('=');
				if (pair[0]) {
					params[pair[0]]=decodeURIComponent(pair[1]);
				}
			}
			return params;
		} else {
			return null;
		}
	},
	/* 
	  * スムーススクロール関数
	  * [引数] easing : 'liner' or 'swing' (jQuery easing plug-in使用の場合、全てのeasingが使用可) 省略時 swing
	  * [引数] speed : 文字列またはミリ秒での指定 / 省略時 1200
	  * [戻り値] なし
	*/
	smoothScroll: function(easing,speed) {
		if(!easing) { easing = 'swing'; }
		if(!speed) { speed = 1200; }
		$('a[href*=#]').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
				var anchorName = this.hash.replace(/#/, '');
				var target;
				if(anchorName.length == 0){
				    target = $("#header");
				} else {
				    target = $(this.hash).length ? $(this.hash) : $("a[name="+anchorName+"]").eq(0);
				}
				target = (target.length && target);
				if (target.length) {
					var targetOffset = target.offset().top - 30;
					$('html,body').animate({scrollTop: targetOffset}, {duration: speed, easing: easing});
					return false;
				}
			}
		});
	},
	/* 
	  * 外部リンク関数
	  * [引数] string : 外部リンクを示す文字列 / 省略時 external
	  * [引数] type : 'class', 'rel' などの属性を示す文字列 / 省略時 rel
	  * [引数] bullet : アイコンなどを付与する場合の文字列
	  * [引数] absolute : httpから始まるリンクも別ウィンドウフラグ / 省略時 false
	  * [戻り値] なし
	*/
	externalLink: function() {
		$('a[rel*=external]').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
		if($.bullet) { $('a['+$.type+'*=' +$.string+ ']').append($.bullet); }
	},
	/* jQueryPluginの場所（ルートから） */
	 jQueryUI: rootPath+'assets/scripts/jquery-ui-1.8.1.custom.min.js',
	 jQueryEasing: rootPath+'assets/scripts/jquery.easing.1.3.js',
	 png4IE: rootPath+'assets/scripts/DD_belatedPNG_0.0.8a-min.js'
};
$.fn.extend({
	/* 
	  * hover時に透明度を下げる関数
	  * [引数] target ※必須 : 対象オブジェクト(jQueryセレクタ指定が可能 - ex.) 'a img' )
	  * [引数] opacity : hover時の透明度(0~1) 省略時 0.75
	  * [引数] easing : 動きを配列で指定可能 ['hover時のeasing','mouseout時のeasing'] / 省略時 swing
	  * [引数] speed : スピードを配列で指定可能 [1000,200] <- hoverした時1秒・mouseout時0.2秒 / 省略時 fast
	  * [戻り値] なし
	*/
	hoverAlpha: function(options){
		var target,opacity,easing,speed;
		$.extend(options);
		// opacity指定なしの場合
		(!opacity) ? opacity = 0.5: opacity = $.opacity;
		// speed指定なしの場合
		(!speed) ?  speed = ['fast']: speed = $.speed ;
		// speedの指定がひとつだった場合mouseout時も同じspeed
		if(speed.length<2) speed[1] = speed[0];
		// easing指定なしの場合
		(!easing) ? easing = ['swing']: easing = $.easing;
		// easingの指定がひとつだった場合mouseout時も同じeasing
		if(easing.length<2) easing[1] = easing[0];

		$(this).find('a').hover(function(){
			$(this).animate({opacity:opacity}, speed[0], easing[0]);
		},function(){
			$(this).animate({opacity:1}, speed[1], easing[1]);
		});
		$(this).find('button').hover(function(){
			$(this).animate({opacity:opacity}, speed[0], easing[0]);
		},function(){
			$(this).animate({opacity:1}, speed[1], easing[1]);
		});
		return opacity + speed;
	},
	/* 
	  * title属性を自動的に追加する関数
	  * [引数] parent : 親要素(狭い範囲での設定推奨) / 省略時 'body'
	  * [引数] img : 画像モードフラグ(true:img要素に対して処理/false・省略時:a要素に対して処理)
	  * [引数] maxString : a要素に画像が含まれない場合の最大文字数 / 省略時 半角30文字
	  * [戻り値] 処理した要素の総数
	*/
	addCaption: function(options) {
		var img,maxString;
		var count = 0;

		$.extend(options);

		(!maxString) ? maxString = 30: maxString = $.maxString;
		if ($.img) img = true;
		
		if(img) {
			$(this).find('img').each(function() {
				if(!$(this).parent().is('a')){
					if($(this).attr('title').length<1) $(this).attr('title',$(this).attr('alt'));
					count ++;
				}
			});
			$('.insteadImage').each(function() {
				$(this).attr('title',$(this).text());
				count ++;
			});
		} else {
			$(this).find('a').each(function() {
				var str;
				// a要素にtitleの指定がすでにある場合は何もしない
				if(($(this).attr('title')!=='')&&($(this).attr('title')!='undefind')) {
					str = '';
				// a要素にimgがある場合、titleがあればそれをaにセット・なければaltをセット
				} else if($(this).find('img').length>0) {
					($(this).find('img').attr('title')) ? str = $(this).find('img').attr('title') : str = $(this).find('img').attr('alt');
					count ++;
				// imgがない場合はaに含まれるテキストをセット/maxStringを超えるときは「...」で省略
				} else {
					($(this).text().length>maxString) ? str = $(this).text().substring(0,maxString)+'...' : str = $(this).text();
					count ++;
				}
				if(str!=='') { $(this).attr('title',str); }
			});
		}
		return count;
	}
});
