/*!
 * Project: PIECEプロモーションサイト(Ver.2)
 * Shared on All of pages Javascirpt
 * nagisa@shift-jp.net
 * 15:00 2010/05/06 nagisa@shift-jp.net
 * Based on jQuery1.4.2 / jQuery UI 1.8.1
 */

if(!rootPath) var rootPath = './';

var piece2 = {
	include: function(path,target)
	{
		var s = '<script type="text/javascript" src="' + path + '"></script>';
		target ? $(target).append(s) : document.write(s);
	},
	importCSS: function(path)
	{
		$('head').append('<link rel="stylesheet" type="text/css" href="' + path + '" />');
	},
	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('?'));
		} else if(arr[last].indexOf('?')==0) {
			arr.pop();
			last--; 
		}
		if(arr[last].indexOf('#')>0) { 
			arr[last] = arr[last].substring(0,arr[last].indexOf('#'));
		} else if(arr[last].indexOf('#')==0) {
			arr.pop();
			last--; 
		}
		if(arr[last].indexOf('.htm')>0 || arr[last].indexOf('.php')>0){
			vars['file'] = arr[last];
			vars['dir'] = arr[last-1];
		} else {
			vars['file'] = '';
			vars['dir'] = arr[last];
		}
		return vars;
	},
	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 target = $(this.hash);
				target = target.length && target;
				if (target.length) {
					var targetOffset = target.offset().top - 30;
					$('html,body').animate({scrollTop: targetOffset}, {duration: speed, easing: easing});
					return false;
				}
			}
			return target.offset().top;
		});
	},
	externalLink: function(string,type,externalBullet,absolute) {
		if(!type) type = 'class';
		if(type=='class' && absolute) $('a[href^=http]').addClass(string);
		if(type!='class' && absolute) $('a[href^=http]').attr(type,string);
		$('a['+type+'*=' +string+ ']').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
		if(externalBullet) $('a['+type+'*=' +string+ ']').append(externalBullet);
	},
	hoverAlpha: function(target,opacity,easing,speed){
		if(!opacity) opacity = 0.75;
		if(!speed) speed = ['fast'];
		if(speed.length<2) speed[1] = speed[0];
		if(!easing) easing = ['swing'];
		if(easing.length<2) easing[1] = easing[0];
		$(target).hover(function(){
	 	 	 $(this).animate({opacity:opacity}, speed[0], easing[0]);
	 	 },function(){
	 	 	 $(this).animate({opacity:1}, speed[1], easing[1]);
	 	 });
	},
	addCaption: function(parent,img,maxString) {
		var count = 0;
		if(!maxString) var maxString = 30;
		if(!parent) parent = 'body';
		
		if(img) {
			$(parent).find('img').each(function() {
				if(!$(this).parent().is('a')){
					if($(this).attr('title').length<1) $(this).attr('title',$(this).attr('alt'));
					count ++;
				}
			});
		} else {
			$(parent).find('a').each(function() {
				var str;
				if(($(this).attr('title')!='')&&($(this).attr('title')!='undefind')) {
					str = '';
				} 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 ++;
				} else {
					($(this).text().length>maxString) ? str = $(this).text().substring(0,maxString)+'...' : str = $(this).text();
					count ++;
				}
				if(str!='') $(this).attr('title',str);
			});
		}
		return count;
	},
	 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'
}
$(function() {

	var arr = piece2.getURL();
	var $id;
	if(rootPath!='./') {
		$id = arr['dir'];
		if(arr['file'].indexOf('html')>0) var $page = arr['file'].substring(0,arr['file'].indexOf('.html'));
		if(arr['file'].indexOf('php')>0) var $page = arr['file'].substring(0,arr['file'].indexOf('.php'));
		$('body').attr('id',$id);
		($page) ? $('body').addClass($page) : $('body').addClass('index');
	} else {
		$id = 'top';
	}
	piece2.importCSS(rootPath+'assets/styles/' + $id +'.css');
	$('#globalMenu #menu_' + $id).addClass('active');

	/* JavaScriptの読み込み */
	 piece2.include(piece2.jQueryUI,'head');
	 piece2.include(piece2.jQueryEasing,'head');

	 if($('a[rel*="thickbox"]').length>0) {
	 	piece2.include(rootPath+'assets/scripts/jquery.colorbox-min.js','head');
	 	//piece2.importCSS(rootPath+'assets/styles/colorbox/colorbox.css');
	 	$('a[rel*="thickbox"]').colorbox({transition:'fade', speed:'fast',maxWidth:'90%',maxHeight:'100%'});
	 }

	/* リンクのはってある画像にhover時透明度を75% */
	piece2.hoverAlpha('a img');
	/* globalMenu hover処理 */
	piece2.hoverAlpha('#globalMenu a',0.5,['easeOutCubic'],[500,200]);
	/* shiftBar hover処理 */
	piece2.hoverAlpha('#shiftHeader a');
	/* フッタ hover処理 */
	piece2.hoverAlpha('#footerNavi a',0.2);
	/* スムーススクロール */
	piece2.smoothScroll('easeOutQuart');

 	 /* first-child IE対応 */
	 if($.browser.msie && $.browser.version < 7.0) { 
	 	 $('#footer ul li:first-child').addClass('firstChild');
	 	 $('.breadCrumb li:first-child').addClass('firstChild');
	 }
	 $('input[type=text]').addClass('text');

 	 /* PNG24 IE対応 */
	 if($.browser.msie && $.browser.version < 7.0) { 
	 	piece2.include(piece2.png4IE,'head');
	 	DD_belatedPNG.fix('#shiftHeader, #shiftHeader h1 a');
	 }

 	 /* サブメニュー */
	$('#globalMenu li').hover(function(){
		var t = $(this);
		clearTimeout(this._tim2);
		this._tim1 = setTimeout(function(){ t.find('ul:first').fadeIn(); }, 150);
	},function(){
		var t = $(this);
		clearTimeout(this._tim1);
		this._tim2 = setTimeout(function(){ t.find('ul').hide(); }, 300);
	});

	/* title属性のない要素に付加 */
	 if($id!='top') piece2.addCaption('.flipNavigation');
	 piece2.addCaption('#globalMenu');
	 piece2.addCaption('#contentBody');
	 piece2.addCaption('#contentBody',true);
	 piece2.addCaption('#footerNavi');
	 piece2.addCaption('ul#features',true);
	 piece2.addCaption('#contactBanner',true);

	/* 外部リンク */
	var externalBullet = '<img src="' +rootPath+ 'assets/images/shared/bullet_external.gif" width="14" height="13" alt="新しいウィンドウ" style="vertical-align:middle;margin-left:3px;" />';
	piece2.externalLink('external','class',externalBullet,false);

	/* 矢印ボタンでページ移動 */
	if($('link[rel="next"]').length>0&&$('link[rel="prev"]').length>0) {
		$(document).keydown( function (e)  {
			if ( e.which == 39 ) {
			    // press [ Right Arrow ] key
			    window.location.href=$('link[rel="next"]').attr('href');
			} else if ( e.which == 37 ) {
			    // press [ Left Arrow ] key
			    window.location.href=$('link[rel="prev"]').attr('href');
			}
		});
	}

});
