// Browser object for mobile device detection
var Browser = Browser || {};
	Browser.ua = navigator.userAgent.toLowerCase();
	Browser.ie6 = Browser.ua.indexOf('windows nt 5.1') > -1;

try {
	document.createEvent('TouchEvent');
	Browser.supportsTouch = true;
} catch(e) {
	Browser.supportsTouch = false;
}

// Markup based unobtrusive comprehensive dom ready execution http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
var ADAPTIVEPATH = ADAPTIVEPATH || {};
ADAPTIVEPATH = {
	common : {
		init : function() {
			// Common scripts here
		},
		finalize : function() {
		}
	},
	home : {
		init : function() {
			ADAPTIVEPATH.home.rotator.init();
		},
		rotator : {
			init : function() {
				// Cycle Uberlite
				var index  = $.cookie('ap_homepage_slide') ? parseInt($.cookie('ap_homepage_slide')) : 0,
					speed = $('.lte8').length ? 0 : 500,
					$divs = $('#rotator > div'), $next = $('#rotator-nav #next'), $prev = $('#rotator-nav #prev');

				$('#rotator-nav').css({'z-index': 210});
				$('#rotator > div iframe').each(function(i, e) {
					$(e).attr('data-src', $(e).attr('src'));
					$(e).attr('src', 'about:blank');
				});
				$divs.css({position: 'absolute', opacity: 0});;
				function precache(i) {
					var prev = $('#rotator > div:eq('+(index+1)+')'),
						next = $('#rotator > div:eq('+(index-1)+')');
					if(prev) { prev.css({'background-image': 'url('+prev.attr('data-background-image')+')'}); }
					if(next) { next.css({'background-image': 'url('+next.attr('data-background-image')+')'}); }
				}
				function stopVideo(i) {
					$($divs[index]).find('iframe').attr('src', '');
				}
				function updateNav(i) {
					index = i;
					var o = $('#rotator > div:eq('+index+')'),
						next = $($divs[index == $divs.length - 1 ? 0 : index + 1]),
						prev = $($divs[index == 0 ? $divs.length - 1 : index - 1]);

					(function(f) {
						f.attr('src', f.attr('data-src'));
					}(o.find('iframe')));

					o.fadeTo(speed, 1).css({'background-image': 'url('+o.attr('data-background-image')+')', 'z-index': 200})
						.siblings().fadeTo(speed, 0).css({'z-index': 100});
					setTimeout(precache, 1000, i);	
					$.cookie('ap_homepage_slide', index);
					if(!Browser.supportsTouch) {
						$prev.text(prev.attr('data-nav-text'));
						$next.text(next.attr('data-nav-text'));
					}
				}
				$prev.click(function() {
					stopVideo(index);
					updateNav(index == 0 ? $divs.length - 1 : index - 1); return false;
				});	
				$next.click(function() {
					stopVideo(index);
					updateNav(index == $divs.length - 1 ? 0 : index + 1); return false;
				});	
				updateNav(index);
			}
		}
	},
	work : {
		init : function() {
			var top, y;
			if($('#bar').length) {
				top = $('#bar').offset().top - parseFloat($('#bar').css('marginTop').replace(/auto/, 0));
				$(window).scroll(function (event) {
					y = $(this).scrollTop();
					if(y >= top) { $('#bar').addClass('fixed'); } else { $('#bar').removeClass('fixed'); }
				});
			}
		}
	},
	ideas : {
		init : function() {
			$('#archives').delegate('dt', 'click', function(e) {
				$(this).next().slideToggle('fast');
				$(this).toggleClass('expanded');
			});
		},
		finalize : function() {
			$('input, textarea').placeholder();
		}
	},
	about : {
		init : function() {
			$('#team-filter ul').prepend('<li id="filter-all" class="selected"><a href="">All</a></li>');
			$('#team-filter li').click(function(e) {
				$.scrollTo($('#team-filter').offset().top - 50, 500);
				var context = $(this),
					key = context.find('a').html().toLowerCase().split(' ').join('-');
				context.siblings().removeClass('selected'); //.css({opacity: 0.5});
				context.addClass('selected'); //fadeTo('fast', 1);
				if(key === 'all') {
					$('#team-view li').fadeTo('slow', 1);
				} else {
					$('#team-view li[data-studio!="' + key + '"]').fadeTo('slow', 0.2);
					$('#team-view li[data-studio="' + key + '"]').fadeTo('slow', 1);
				}
				return false;
			});
		}
	},
	team : {
		init : function() {
			$('#social-icons > li > a').tipTip({ defaultPosition: 'top' });
		}
	},
	contact : {
		init : function() {
			$("#freeform").slideLock({
				// set the options - all are given, not all are required
				labelText: "Slide to Unlock:",
				noteText: "Proves you're a human.",
				lockText: "Locked",
				unlockText: "Unlocked",
				iconURL: "/assets/images/chrome/arrow_right.png",
				inputID: "sliderInput",
				onCSS: "#333",
				offCSS: "#aaa",
				inputValue: 1,
				saltValue: 9,
				checkValue: 10,
				js_check: "js_check",
				submitID: "#submit"
			});
		}
	}
};

UTIL = {
	fire : function(func,funcname, args){
		var namespace = ADAPTIVEPATH;
		
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		}
	},
	loadEvents : function() {
		var bodyId = document.body.id;
		
		// Fire common scripts first.
		UTIL.fire('common');
		
		// Fire scripts based on body class and id.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
			UTIL.fire(classnm,bodyId);
		});
		
		// Fire low priority scripts last.
		UTIL.fire('common','finalize');
	}
};

$(function() { UTIL.loadEvents() });

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);

