var IC_UI = {
	Support: {
		_fixed: function() {
			var container = document.body;
			
			if (document.createElement && container && container.appendChild && container.removeChild) {
				var el = document.createElement('div');
				
				if (!el.getBoundingClientRect) return null;
				
				el.innerHTML = 'x';
				el.style.cssText = 'position:fixed;top:100px;';
				container.appendChild(el);
				
				var originalHeight = container.style.height,
				originalScrollTop = container.scrollTop;
				
				container.style.height = '3000px';
				container.scrollTop = 500;
				
				var elementTop = el.getBoundingClientRect().top;
				container.style.height = originalHeight;
				
				var isSupported = (elementTop === 100);
				container.removeChild(el);
				container.scrollTop = originalScrollTop;
				
				return isSupported;
			}
			return null;
		},
		_ios: function(){
			var ua = navigator.userAgent;
			return ua.match(/iPhone/i) || ua.match(/iPod/i) || ua.match(/iPad/i);
		},
		init: function() {
			for(var i in this) {
				if(i.substring(0,1) == '_') {
					this[i.replace('_','')] = this[i]();
				}
			}
		}
	},
	
	Toggleable: {
		_p: {
			ctx: null,
			hd: null,
			tg: null,
			speed: 150,
			tclass: 'toggleable',
			tgclass: 'toggleable-target',
			opclass: 'toggleable-open',
			hidecond: true,
			title: ''
		},
		hide: function()
		{
			$(this._p.hd, this._p.ctx).slideUp(this._p.speed);
			$(this._p.ctx).removeClass(this._p.opclass);
		},
		show: function()
		{
			$(this._p.hd, this._p.ctx).slideDown(this._p.speed);
			$(this._p.ctx).addClass(this._p.opclass);
		},
		_setEvents: function()
		{
			if(this._p.hidecond) this.hide();
			var self = this;
			$(this._p.ctx).addClass(this._p.tclass);
			$(this._p.tg, this._p.ctx).click( function(e){
				e.preventDefault();
				if( $(self._p.hd, self._p.ctx).is(':hidden') ) self.show();
				else self.hide();
			}).attr('title', this._p.title).addClass(this._p.tgclass);
		},
		init: function(params) {
			var toggler = $.extend(true, {}, IC_UI.Toggleable, {_p: params});
			toggler._setEvents();
			return toggler;
		}
	},
		
	Staff: {
		prefs: {
			autoHeight: false,
			navigation: true,
			collapsible: true,
			active: false
		},
		init: function() {
			if($('.post-4590').length > 0) {
				$('.post-4590 .entry-content').accordion(IC_UI.Staff.prefs);
			}
		}
	},
	
	iOS: {
		init: function() {
			if (IC_UI.Support.ios) {
				$('body').addClass('platform-ios');
				if ($('body').hasClass('home')) {
					document.title = 'insertcredit';
				}
			}
		}
	},

	Search: {
		_s: '#searchform',
		_state: {
			elemfocus: false,
			hover: false
		},
		_checkstate: function(){
			if (IC_UI.Search._state.elemfocus || IC_UI.Search._state.hover) {
				IC_UI.Search._s.addClass('focused');
			}
			else {
				IC_UI.Search._s.removeClass('focused');
			}
		},
		init: function(){
			IC_UI.Search._s = $(IC_UI.Search._s);
			IC_UI.Search._s
				.mouseover(function(){ IC_UI.Search._state.hover = true; IC_UI.Search._checkstate(); })
				.mouseout(function(){ IC_UI.Search._state.hover = false; IC_UI.Search._checkstate(); });
			$('input', IC_UI.Search._s)
				.focus(function(){ IC_UI.Search._state.elemfocus = true; IC_UI.Search._checkstate(); })
				.blur(function(){ IC_UI.Search._state.elemfocus = false; IC_UI.Search._checkstate(); });
		}
	},

	Paginator: {
		_p: '.entry-paginator',
		_ec: '.entry-content',
		_t: 0,
		_s: function() {
			if ($(window).scrollTop() > IC_UI.Paginator._t) {
				IC_UI.Paginator._p.addClass('paginator-float');
				if (IC_UI.Support.fixed) {
					IC_UI.Paginator._p.css({
						position: 'fixed',
						top: '0px'
					});
				} 
				else {
					IC_UI.Paginator._p.css({
						position: 'absolute',
						top: $(window).scrollTop() + 'px'
					});
					IC_UI.Paginator._ec.css({
						paddingTop: (IC_UI.Paginator._p.height() + 10) + 'px'
					});
				}
			}
			else {
				IC_UI.Paginator._p.css({
					position: 'static',
					top: '0px'
				}).removeClass('paginator-float');
				
				if (!IC_UI.Support.fixed) {
					IC_UI.Paginator._ec.css({
						paddingTop: '0px'
					});
				}
			}
		},
		init: function() {
			IC_UI.Paginator._p = $(IC_UI.Paginator._p);
			IC_UI.Paginator._ec = $(IC_UI.Paginator._ec);
			
			if (IC_UI.Paginator._p.length > 0) {
				IC_UI.Paginator._t = $('.entry-paginator').offset().top;
				$(window).scroll( IC_UI.Paginator._s );
			}
		}
	},
	
	Antimoire: {
		_to: null,
		_t: '#featured-posts a .overlay',
		_speed: 400,
		_am: function() {
			IC_UI.Antimoire._t.addClass('antimoire');
			IC_UI.Antimoire._to = setTimeout(function(){
				IC_UI.Antimoire._t.removeClass('antimoire');
				removeTimeout(IC_UI.Antimoire._to);
			}, IC_UI.Antimoire._speed);
		},
		init: function() {
			IC_UI.Antimoire._t = $(IC_UI.Antimoire._t);
			
			if (IC_UI.Antimoire._t.length > 0) {
				$(window).bind({
					scroll: IC_UI.Antimoire._am,
					mousewheel: IC_UI.Antimoire._am
				});
			}
		}
	},
	
	Prank: {
		_pd: null,
		_ps: '#prankspace',
		_obj: [],
		
		_area: function() {
			$(this._ps).remove();
			$('<div id="prankspace"></div>').hide().appendTo('body');
			this._ps = $(this._ps);
		},
		_ctr: function() {
			return (($(window).width()) - (IC_UI.Prank._ps.width())) / 2;
		},
		_pos: function(vert) {
			IC_UI.Prank._ps.css({
				top: '',
				bottom: '',
				left: '',
				right: '',
				zIndex: 100000
			});
			if (vert == undefined) {
				vert = 'top';
			}
			var positioning = {
				position: 'fixed'
			};
			positioning[vert] = '0px';
			
			if (IC_UI.Support.fixed) {
				positioning['left'] = '0px';
				IC_UI.Prank._ps.css(positioning);
			}
			else {
				var OnScroller = function(){
					var positioning = {
						position: 'absolute'
					};
					positioning[vert] = $(window).scrollTop() + 'px';
					IC_UI.Prank._ps.css(positioning);
				};
				$(window).unbind('scroll', OnScroller).scroll(OnScroller);
				OnScroller();
			}
			IC_UI.Prank._ps.css({
				left: IC_UI.Prank._ctr() + 'px',
				'background-color': $('body').css('background-color')
			})
		},
		_showarea: function(content, callback, noremove, vert) {
			var ShowMe = function() {
				if (!noremove) {
					IC_UI.Prank._ps.children().remove();
				}
				IC_UI.Prank._pos(vert);
					IC_UI.Prank._ps.append(content).css({
					left: IC_UI.Prank._ctr() + 'px'
				}).slideDown(function(){
					if ($.isFunction(callback)) {
						callback();
					}
				});
			};
			if (!IC_UI.Prank._ps.is(':hidden')) {
				IC_UI.Prank._hidearea(ShowMe);
			}
			else {
				ShowMe();
			}
		},
		_hidearea: function(callback) {
			IC_UI.Prank._ps.slideUp(function(){
				$(this).children().remove();
				if ($.isFunction(callback)) {
					callback();
				}
			});
		},
		
		_trigevents: function(param) {
			var resp = {};
			
			if (parseInt(param)+'' == param) {
				resp = this._pd[param].response;
			}
			else if (typeof param == 'object') {
				resp = param;
			}
			
			for (var j in resp) {
				for (var k in resp[j]) {
					if (k == 'init' || k.substring(0,1) == '_') continue;
					
					var value = resp[j][k];
					
					if (!!this[k] && $.isFunction(this[k])) {
						this[k](value);
					}
				}
			}
		},
		_exec: function() {
			var self = this;
			for (var i in this._pd) {
				var resp = self._pd[i].response;
				if (this._pd[i].action == 'konami') {
					var k = new Konami();
					if (this._pd[i].pattern) {
						k.pattern = this._pd[i].pattern;
					}
					k.code = function() {
						self._trigevents(resp);
					};
					k.load();
					this._obj.push(k);
				}
				else if (this._pd[i].action == 'click') {
					$(this._pd[i].target).attr('data-pindex', i).css({cursor: 'help'}).click( function(e){
						e.preventDefault;
						self._trigevents( $(this).attr('data-pindex') );
					});
				}
			}
		},
		
		alert: function(text) {
			alert(text);
		},
		image: function(url) {
			var img = $('<img src="' + url + '" />');
			img.click(IC_UI.Prank._hidearea);
			IC_UI.Prank._showarea(img);
		},
		audio: function(url) {
			var aud = $('<audio id="prankaudio" src="' + url + '" /><a href="#" id="closeprankaudio">Close</a>');
			IC_UI.Prank._showarea(aud, function(){
				$('audio').mediaelementplayer();
				$('#closeprankaudio').css({
					display: 'block', 
					width: '100%',
					height: '16px',
					textAlign: 'center'
				}).click(IC_UI.Prank._hidearea);
				IC_UI.Prank._pos();
			});
		},
		video: function(url) {
			var aud = $('<video id="prankvideo" src="' + url + '" /><a href="#" id="closeprankvideo">Close</a>');
			IC_UI.Prank._showarea(aud, function(){
				$('video').mediaelementplayer();
				$('#closeprankvideo').css({
					display: 'block', 
					width: '100%',
					height: '16px',
					textAlign: 'center'
				}).click(IC_UI.Prank._hidearea);
				IC_UI.Prank._pos();
			});
		},
		
		init: function() {
			if (Prank != undefined && !!Prank) {
				this._area();
				if (Prank.length > 0) {
					this._pd = jQuery.parseJSON(Prank);
				}
				if (!!this._pd) {
					this._exec();
				}
			}
		}
	}
};

$(document).ready( function(){
	
	var Comments = IC_UI.Toggleable.init({
		ctx: '#comment-container',
		hd: '#comments, .commentlist, .comment-note, #respond, form',
		tg: '#comment-container-title',
		title: 'Toggle comments',
		opclass: 'toggleable-open',
		hidecond: ( window.location.hash.indexOf('#comment-') < 0 )
	});
	
	var PageToggle = IC_UI.Toggleable.init({
		ctx: '.entry-paginator',
		hd: 'ul',
		tg: 'h3',
		title: 'Toggle paginator display',
		opclass: 'toggleable-open'
	});
	
	var ArchToggle = IC_UI.Toggleable.init({
		ctx: '#archives',
		hd: 'ul',
		tg: 'h3',
		title: 'Toggle monthly archives',
		opclass: 'toggleable-open'
	});
	
	IC_UI.Support.init();
	IC_UI.Staff.init();
	IC_UI.Paginator.init();
	IC_UI.iOS.init();
	IC_UI.Search.init();
	if (!!Prank) {
		IC_UI.Prank.init();
	}
	
});
