
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

	Site: WHO DID IT?
	File: common.js
	=====
	Last Update: 2011/09/01
	Create Date: 2011/08/30

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

/*--------------------------------------------------
  Debug
----------------------------------------------------*/
var debugMode = false;
function _debug(_log) {
	if (!debugMode) return;
	if ( window.console ) {
		window.console.log(_log);
	}
}

/*--------------------------------------------------
  Browser Distinction
----------------------------------------------------*/
var isMac		= navigator.platform.indexOf('Mac') != -1;
var isAndroid	= navigator.userAgent.indexOf('Android') != -1;
var isIPhone	= navigator.userAgent.indexOf('iPhone;') != -1;
var isIPad		= navigator.userAgent.indexOf('iPad;') != -1;
var isIPod		= navigator.userAgent.indexOf('iPod;') != -1;
var isMobile	= navigator.userAgent.indexOf('Mobile/') != -1 || isAndroid || isIPhone || isIPad || isIPod;
var isIE6		= navigator.appVersion.indexOf('MSIE 6.0') > -1;
var isIE7		= navigator.appVersion.indexOf('MSIE 7.0') > -1;
var isIE8		= navigator.appVersion.indexOf('MSIE 8.0') > -1;
var isIE9		= navigator.appVersion.indexOf('MSIE 9.0') > -1;
var isChrome	= navigator.userAgent.indexOf('Chrome/') != -1 || navigator.userAgent.indexOf('Mac/') != -1 || navigator.userAgent.indexOf('Win/') != -1 && window.chrome;
var isSafari	= navigator.userAgent.indexOf('Safari/') != -1 && !window.chrome && !navigator.userAgent.indexOf('Mobile/');

/*--------------------------------------------------
  Encode
----------------------------------------------------*/
function encodeHTML(str) {
	return str
				.replace(/&/g, '&amp;')
				.replace(/</g, '&lt;')
				.replace(/>/g, '&gt;')
				.replace(/'/g, '&acute;')
				.replace(/"/g, '&quot;');
}

function encodeJS(str) {
	return str
				.replace(/\\/g, '\\')
				.replace(/"/g, '\"')
				.replace(/'/g, '\'')
				.replace(/\//g, '\/')
				.replace(/&/g, '\&')
				.replace(/</g, '\<')
				.replace(/>/g, '\>')
				.replace(/0x0D(CR)/g, '\r')
				.replace(/0x0A(LF)/g, '\n');
}

/*--------------------------------------------------
  alphaOver
----------------------------------------------------*/
function alphaOver() {
	$('div.thumbnail a img', '#archives').hover(
		function() {			
			$(this).stop().fadeTo(250, .75);
		},
		function() {
			$(this).stop().fadeTo(500, 1);
		}
	);
}
$(document).ready(alphaOver);

/*--------------------------------------------------
  動画を中央配置
----------------------------------------------------*/
function setVideoPosition() {
	$('.mainImg', '#main').each(function() {
		var
			$this = $(this),
			$video = $this.find("#video"),
			w = $this.outerWidth(),
			h = $this.outerHeight(),
			vW = 0,
			vH = 0;
		
		$this.filter('object').each(function() {
			vW = $(this).outerWidth();
			vH = $(this).outerHeight();
		});
		$this.find('embed').each(function() {
			vW = $(this).outerWidth();
			vH = $(this).outerHeight();
		});
		$this.find('iframe').each(function() {
			vW = $(this).width();
			vH = $(this).height();
		});
		$video.css({ position:'absolute', top:(h - vH)/2 + 'px', left:(w - vW)/2 + 'px', visibility:'visible' });
	});
}
$(document).ready(setVideoPosition);

/*--------------------------------------------------
  setContactLink
----------------------------------------------------*/
function setContact () {
	document.write('<a href="mailto:contact&#64;whodidit.jp">contact&#64;whodidit.jp</a>');
}

/*--------------------------------------------------
  setAdItLink
----------------------------------------------------*/
function setAdIt() {
	$('p.adit', 'body.adit').html('<a href="mailto:adit&#64;whodidit.jp">adit&#64;whodidit.jp</a>');
}

/*--------------------------------------------------
  maxLength
----------------------------------------------------*/
function maxLength(str,maxLength,more) {
	function setMaxLength (thisStr) {
		var tx = str.slice(0,maxLength);
		if (tx.match(/[0-9a-zA-Z.]/g) == null) {
			var tx2 = 0;
			var tx3 = -2;
		} else {
			var tx2 = tx.match(/[0-9a-zA-Z.]/g).length/2;
			var tx3 = Math.ceil(tx2);
		}
		
		if (navigator.userAgent.indexOf('Mac') >= 0) {
			//for Mac
			maxLength = tx3 - 3 + parseInt(maxLength);
		} else {
			//for Win
			maxLength = tx3 - 2 + parseInt(maxLength);
		}
		return maxLength;
	}
	
	if (isMac) { maxLength = maxLength-2; }
	if (str.length > maxLength) {
		maxLength = setMaxLength(str);
		if (str.length < maxLength)	{
			return str;
		} else {
			return str.slice(0,maxLength)+more;
		}
	} else {		
		return str;
	}
}

/*--------------------------------------------------
  sliceText
----------------------------------------------------*/
function sliceText() {
	var $title = $('h1.title', '#archives');
	var $table = $('table.credits,table.profiles', '#archives');
	var $td = $table.find('td');
	//console.time("hoge");
	for (var i=0; i < $title.length; i++) {
		if ($title.eq(i).find('a').length) {
			$title.eq(i).find('a')[0].innerHTML = maxLength($title.eq(i).text(),18,'...');
		} else {
			$title[i].innerHTML = maxLength($title.eq(i).text(),18,'...');
		}
	}
	for (var i=0; i < $td.length; i++) {
		$td[i].innerHTML = maxLength($td.eq(i).text(),17,'<a href="" title="more">...</a>');
	}
	$('.article', '#archives').each(function() {
		$(this).find('td a').attr('href',$(this).find('.thumbnail a').attr('href'));
	});
	//console.timeEnd("hoge");
	
	$title.css('visibility','visible');
	if (isIE6 || isIE7) {
		$table.css('display','block');
	} else {
		$table.css('display','inline-table');
	}
}
$(document).ready(sliceText);

/*--------------------------------------------------
  getKeyCode
----------------------------------------------------*/
function getKeyCode(e) {
	var shift, ctrl;
	
	if (e != null) {
		// Firefox, Safari, Chrome, Opera
		keycode = e.which;
		ctrl    = typeof e.modifiers == 'undefined' ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK;
		shift   = typeof e.modifiers == 'undefined' ? e.shiftKey : e.modifiers & Event.SHIFT_MASK;
		e.preventDefault();
		e.stopPropagation();
	} else {
		// IE
		keycode = event.keyCode;
		ctrl    = event.ctrlKey;
		shift   = event.shiftKey;
		event.returnValue = false;
		event.cancelBubble = true;
	}
	
	// キーコードの文字取得
	keychar = String.fromCharCode(keycode).toUpperCase();
	
	if (ctrl) {
		// Ctrl同時押しの場合
		return 'ctrl';
	} else if (shift) {
		// Shift同時押しの場合
		return 'shift';
	} else {
		// 通常のキーダウン時の場合
		return keychar;
	}
}

/*--------------------------------------------------
  onKeyAction
----------------------------------------------------*/
function onKeyAction(kc) {
	if ($('#search_box').hasClass('isFocus')) { return; }
	$('li.btnPrev:eq(0) a').each(function() {
		//D, J を押した場合
		if (kc == 'D' || kc == 'J') { location.href = $(this).attr('href'); }
	});
	$('li.btnHome:eq(0) a').each(function() {
		//G, H を押した場合
		if (kc == 'G' || kc == 'H') { location.href = $(this).attr('href'); }
	});
	$('li.btnNext:eq(0) a').each(function() {
		//F, K を押した場合
		if (kc == 'F' || kc == 'K') { location.href = $(this).attr('href'); }
	});
}

/*--------------------------------------------------
  smoothScroll
----------------------------------------------------*/
var smoothScroll = {};
smoothScroll.scrolling = false;
smoothScroll.start = function(targetId,easing,callbackFunc) {
	smoothScroll.scrolling = true;
	var $element = $('#'+targetId)[0];
	var easing = easing || 0.14;
	var interval = 30;
	var st = $(document).scrollTop();
	var sl = $(document).scrollLeft();
	var ww = $(window).width();
	var wh = $(window).height();
	var dw = $(document).width();
	var dh = $(document).height();
	var tX = 0; var tY = 0;
	var myTimer, moveX, moveY;
	while($element) {
	   tX += $element.offsetLeft;
	   tY += $element.offsetTop;
	   $element = $element.offsetParent;
	}
	if (dw > (tX + ww)) {
		var toX = tX - sl;
	} else {
		var toX = dw - sl - ww;
	}
	if (dh > (tY + wh)) {
		var toY = tY - st;
	} else {
		var toY = dh - st - wh;
	}
	function windowScroll() {	
		if (0 > toX) {
			moveX = Math.floor(toX*easing);
		} else {
			moveX = Math.ceil(toX*easing);
		}		
		if (0 > toY) {
			moveY = Math.floor(toY*easing);
		} else {
			moveY = Math.ceil(toY*easing);
		}		
		sl += moveX;
		st += moveY;
		window.scrollTo(sl, st);
		toX -= moveX;
		toY -= moveY;
		myTimer = setTimeout(windowScroll,interval);
		if (moveX == 0 && moveY == 0) {
			smoothScroll.scrolling = false;
			clearTimeout(myTimer);
			if ($.isFunction(callbackFunc)) { callbackFunc; }
		}
	}
	windowScroll();	
};
smoothScroll.setup = function() {
	var
		allLinks = document.links,
		lnk = "",
		targetId;
	for(var i = 0 ; i < allLinks.length; i++) {
		lnk = allLinks[i];
		if ((lnk.href && lnk.href.indexOf('#') != -1) && ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname)) && $(lnk).hasClass("scroll")) {
			lnk.onclick = function() {
				if (smoothScroll.scrolling) { return; }
				targetId = this.hash.replace(/#/g,"");
				if (document.getElementById(targetId)) {
					smoothScroll.start(targetId);
					return false;
				}
			};
		}
	}
};
$(document).ready(smoothScroll.setup);

/*--------------------------------------------------
  window.load
----------------------------------------------------*/
$(document).ready(function() {
	$('#search_box').bind({
		focus: function() { $(this).addClass('isFocus'); },
		blur: function() { $(this).removeClass('isFocus'); }
	});
	
	// キーボードによるページ遷移
	document.onkeyup = function(e) {
		var k = getKeyCode(e);
		if (k == 'ctrl' || k == 'shift') {
			return;
		} else {
			onKeyAction(k);
		}
	 	return false;
	};
	
	// set YouDidIt link
	$('#btnYDI').html('<a href="mailto:youdidit&#64;whodidit.jp?subject=TELL%20ME%20WHAT%20YOU%20DID%21&body=YOUR%20NAME%20%3a%20%0d%0a%0d%0aYOUR%20MESSAGE%20%3a%20%0d%0a%0d%0a%0d%0a%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%0d%0a%0d%0aTITLE%20%3a%20%0d%0aURL%20%3a%20%0d%0aCATEGORIES%20%3a%20%0d%0a%28WEB%2cGRAPHIC%2cTVCM%2cPV%2fMV%2cPRODUCT%2cOTHER%29%0d%0a%0d%0aCLIENT%20%3a%20%0d%0aAGENCY%20%3a%20%0d%0aPRODUCTION%20%3a%20%0d%0aCREATIVE%20DIRECTOR%20%3a%20%0d%0aART%20DIRECTOR%20%3a%20%0d%0aPLANNER%20%3a%20%0d%0aPRODUCER%20%3a%20%0d%0aCOPY%20WRITER%20%3a%20%0d%0aDIRECTOR%20%3a%20%0d%0aDESIGNER%20%3a%20%0d%0aPROGRAMER%20%3a%20%0d%0aOTHER%20STAFF%20%3a%20%0d%0aAWARD%20%3a%20%0d%0a%0d%0a%5battached%20image%20file%5d%0d%0a%2d%20attach%20or%20omakase%20%2d%0d%0atop%20thumbnail%20%3a%20200px%2a149px%0d%0adetail%20main%20%3a%20817px%2a517px%0d%0adetail%20small%20%28you%20can%20upload%206files%29%20%3a%20398px%2a298px%0d%0a%0d%0a%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d">YOU DID IT!</a>');
});
