/*@cc_on _d=document;eval('var document=_d')

if(typeof ActiveXObject == "function" && typeof XMLHttpRequest == "undefined"){
	XMLHttpRequest = function(){
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
}

var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),
i = e.length;
while (i--) {
	document.createElement(e[i])
}

@*/

(function(){
	if(/informationarchitects/.test(location.hostname) && !(/_debug_/.test(location.hash))){
		if(typeof(console)!='object'){
			console = {}
		}
		console.log = function(){}
		console.time = function(){}
		console.timeEnd = function(){}
	}
})()

if(!Array.indexOf){
	Array.prototype.indexOf = function(searchElement,fromIndex){
		var i = fromIndex<0 ? this.length+fromIndex : fromIndex || 0;
		for(;i<this.length;i++)
			if(searchElement === this[i]) return i;
		return -1
	}
}

if(!Array.forEach){
	Array.prototype.forEach = function(callback,thisObject){
		for(var i=0,len=this.length;i<len;i++){
			callback.call(thisObject,this[i],i,this)
		}
	}
}

if(!Array.clone){
	Array.prototype.clone = function(){
		return Array.apply(null,this)
	}
}
if(!Array.filter){
	Array.prototype.filter = function(callback,thisObject){
		for(var i=0,res=[],len=this.length;i<len;i++)
			callback.call(thisObject,this[i],i,this) && res.push(this[i]);
		return res
	}
}

Array.prototype.shuffle = function() {
	var i = this.length;
	while(i){
		var j = Math.floor(Math.random()*i);
		var t = this[--i];
		this[i] = this[j];
		this[j] = t;
	}
	return this;
}


$.fn._removeClass = $.fn.removeClass
$.fn.removeClass = function(arg) {
	return this.each(function(){
		if(typeof(arg.test) == 'function'){
			var classes = $(this).attr('class').split(/\s/);
			$.each(classes,function(i){if(arg.test(this)){classes.splice(i,1)}});
			$(this).attr('class',classes.join(' '));
		}else{
			$(this)._removeClass(arg);
		}
	})
};

$.fn.replaceClass = function(RE,STR) {
	return this.each(function(){
		var classes = $(this).attr('class').split(/\s/);
		$.each(classes,function(i){classes[i] = this.replace(RE,STR)})
		$(this).attr('class',classes.join(' '));
	})
};

$.fn.markCurrentLocation = function() {
	var path = window.location.href
	return this.each(function(){
		var self = $(this);
		var href = self.attr('href');
		if(path == href){
			var linkLabel = self.text();
			self
				.before('<em><span class="emcontent">'+linkLabel+'</span></em>')
				.remove()
		}else if(RegExp(href).test(path)){
			self
				.wrap('<em></em>')
		}
	})
};

//expect cite
$.fn.wrapCite = function() {
	return this.each(function(){
		var self = $(this);
		self.parent('p').addClass('cite')
	})
};

//expect blockquote
$.fn.removePoutofBq = function() {
	return this.each(function(){
		var self = $(this);
	})
};

//expect blockquote
$.fn.wrapBq = function() {
	return this.each(function(){
		var self = $(this);
		var orBqtext = self.html();
		var rpBqtext = orBqtext.replace(/^“([^“”]+)”$/,'$1');
		self.html(rpBqtext);
		if($('p',self).size() == 0){
			self.wrapInner('<p>');
		}
	})
};

//expect elements have target text
$.fn.orderedFigure = function() {
	return this.each(function(){
		var self = $(this);
		var cont = self.html();
		var cont2 = cont.replace(/(\d)(st|nd|rd|th)/g,'$1<sup>$2</sup>')
		self.html(cont2);
	})
};

$.fn.notify = function(type) {
	var to = {backgroundColor: "white"};
	var duration = 1000;
	switch(type){
		case 'alert':
			var from = {backgroundColor:'#ff9797'};
			break;
		default:
			var from = {backgroundColor:'#fef794'};
			break;
	}
	return this.each(function(){
		$(this).css(from).stop().animate(to,duration,function(){$(this).removeAttr('style')});
	});
};

/**
 * @param query {RegExp}
 * @wrapElement wrapElement {jQuery}
 **/
$.fn.highlightText = function(query,wrapElement){
	var REtag = '<\\/?[^>]+?\\/?>';
	var REtagDivider = new RegExp(REtag+'|[^<>]*','gi');
	return this.each(function(){
		var elem = $(this);

		var srcs = elem.html().match(REtagDivider);
		for(var i=0,l=srcs.length;i<l;i++){
			var obj = srcs[i];
			if(!RegExp(REtag).test(obj)){
				srcs[i] = obj.replace(query,function(matched){
					return $('<div />').append(wrapElement.html(matched)).html();
				})
			}
		}
		elem.html(srcs.join(''));
	})
}

/**
 * @example $('em.highlight').removeOuterTag()
 * @description will turn 'text1 <em class="highlight">text2</em> text3' into 'text1 text2 text3'
 **/
$.fn.removeOuterTag = function(){
	return this.each(function(){
		$(this).replaceWith($(this).html())
	})
}


var iaMobileNavInit = function(){
	$('body').append('<div id="shield" />')
	$('#shield')
		.css('height',$(document).height()+6)
		.click(function(){
			$(this).toggle();
			$('header nav').hide();
			$('.forRMB').removeClass('focus')
		})
		.hide();
	$('#iMenu')
		.click(function(){
			$('#shield').toggle();
			$(this).toggleClass('focus')
			$('#iLang').removeClass('focus')
			$('.langSelector').hide()
			$('.mainNav').toggle()
			return false;
		})
	$('#iLang')
		.click(function(){
			$('#shield').toggle();
			$(this).toggleClass('focus')
			$('#iMenu').removeClass('focus')
			$('.mainNav').hide()
			$('.langSelector').toggle()
			return false;
		})
}

var require = (function(){
	var jspath = /.+\//.exec($('script[src]:last').attr('src'))[0]
	return function(jsfile,callback){
		document.write('<script type="text/javascript" src="' + jspath + jsfile + '"><\/script>');
		if(callback){
			$('script[src]:last').load(function(){callback()})
		}
	}
})()

require('jquery.browser.addEnvClass.js');
require('jquery.color.js');
require('jquery.initInput.js');
//require('jquery.addFavicon.js');
require('jquery.addFileInfo.js');
require('jquery.fancybox.js');
//require('jquery.waokon.js');
require('jquery.incrementalSearch.js');

$(function(){
	//$(':lang(ja)').waokon();
	$('header nav a').markCurrentLocation();
	$('.contentBody.blogEntry blockquote').removePoutofBq();
	$('.contentBody.blogEntry blockquote').wrapBq();
	$('blockquote + p cite').wrapCite();
	$('form.sitesearch :text').initInput('Enter some keyword');
	$('#darth_vader').initInput('It\'s empty!')
	$('.contentBody .date, .contentBody cite').orderedFigure();
	$('article a').addFileInfo();

	if($.browser.mobile){
		iaMobileNavInit();
	}else{
		$('.contentBody .enlarge').fancybox();
	}

	if($.browser.msie){
		$('.lu:first-child').addClass('first-child')
	}

})
