// var resizeTimer = null;
// function autoPosition() {
// 	var windowWidth = $(window).width();
// 	var imgWidth = $('.autoPos').width();
// 	var left = (imgWidth - windowWidth) / 2;
// 	
// 	var windowHeight = $(window).height();
// 	var imgHeight = $('.autoPos').height();
// 	var top = (imgHeight - windowHeight) / 2;							
// 	 
// 	$('.autoPos').animate(
// 		{
// 		"left":"-"+left+"px",
// 		"top":"-"+top+"px"
// 		},500);
// 
// }

jQuery.fn.addLineBreaks = function() {  
	var regexp = /\s/;  
	this.each(function() {  
		$(this).html(  
			$(this).html().replace(regexp,"<br>")  
		);  
	});  
	return $(this);  
}

jQuery.ajaxSetup({
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Accept", "text/javascript");
    }
});

//DISPLAY FLASH NOTICES
function notify(notice) { 
	$('#flash_notice').html(notice);	
	$('#flash_notice').show().fadeTo(600,1,function(){
		//delay and fadeout
		$(this).fadeTo(2500,1,function(){$(this).fadeTo(600,0,function(){$(this).hide();})});
	});
}

$(document).ready(function(){
	
	$(document).pngFix();
	
	// autoPosition;
	
	// $(window).bind('resize', function() {
	// 	    if (resizeTimer) clearTimeout(resizeTimer);
	// 	    resizeTimer = setTimeout(autoPosition, 100);
	// 	});	
	
	// ============
	// = Main Nav =
	// ============
	//show submenu on click
	$('#nav li a.hasChildren').click(function(){
		$(this).parents('li').addClass('hover');
		$('#nav').addClass('inUse');
		$(this).parents('li').siblings('li').removeClass('hover');
		return false;
	});
	
	//remove inUse state from nav on mouseOut
	$('#nav').hover(function() { }, function() { $(this).removeClass('inUse'); });
	
	//show submenu on mouseIn if nav is already in use. 
	//hide submenus on mouseOut
	$('#nav li').hover(function() { 
		if ( $('#nav').hasClass('inUse') || $(this).siblings('li').hasClass('active') ) { $(this).addClass('hover'); }
		$(this).siblings('li.active').addClass('inactive');
	}, function() { 
		$(this).removeClass('hover'); 
		$(this).siblings('li.active').removeClass('inactive');
	});
	
	//vip text area focus
	$('#n5 a').click(function() { $('#login').focus(); });
	$('#n5 a').hover(function() { $('#login').focus(); });
	
	// =============
	// = Buzz Tabs =
	// =============
	var scroll_enabled = "false";
	
	$('#buzz_tabs').tabs({ fx: { opacity: 'toggle' }, show: function(event, ui) {
		if (ui.index == 1) {
			if (scroll_enabled == "false") {
				$('#roiomg').animate({ opacity: 1.0 }, 500, false, function(){
						$('#upcoming_scroller').autoscroll(AUTOSCROLL_Y);
						scroll_enabled = "true";
				});
			}
		}
	} 
	});
	
	$('#buzz_tabs ul.tabs li a').click(function(){
		if ( $('#buzz_tabs').hasClass("open") && $(this).parents('li').hasClass("ui-state-active") ) {
			$('#buzz_tabs').addClass("closed").animate({"right": "-670px"}, 500).removeClass("open");
		} else {
			$('#buzz_tabs').removeClass("closed").animate({"right": "0px"}, 500).addClass("open");
		}		
	});
	
	$('div.scroller').autoscroll(AUTOSCROLL_Y);
	
	// ==================
	// = Buzz/Blog Page =
	// ==================
	$('#buzz_switcher').tabs({ fx: { opacity: 'toggle' } });
	
	// ==============
	// = What We Do =
	// ==============
	$('#what_we_do a.roi-omg, #what_we_do .roi a').click(function(){
		if ( $('#what_we_do a.roi-omg').hasClass('omg-selected') ) {
			$('#what_we_do a.roi-omg').removeClass('omg-selected');
			$('#what_we_do .omg').fadeOut("slow", function(){ $('#what_we_do .roi').fadeIn("slow") });
		} else {
			$('#what_we_do a.roi-omg').addClass('omg-selected');
			$('#what_we_do .roi').fadeOut("slow", function(){ $('#what_we_do .omg').fadeIn("slow") });
		}
	});
	
	// ==================
	// = Complete Works =
	// ==================
	$('#complete_works').tabs();
	$('#complete_works > div.category').tabs();

	// ================
	// = Fundamentals =
	// ================
	$('#fundamentals').tabs({ fx: { opacity: 'toggle' } });
	
	// ==========
	// = People =
	// ==========
	$('#people').tabs();
	
	// $('#people ul li a').addLineBreaks();
	
	// ======================================
	// = Admin - Input / Textarea Countdown =
	// ======================================
	var countdown = {
		init: function() {
			countdown.remaining = countdown.max - $(countdown.obj).val().length;
			if (countdown.remaining > countdown.max) {
				$(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
			}
			$(countdown.obj).siblings("span.countdown_message").html(countdown.remaining + " characters remaining.");
		},
		max: null,
		remaining: null,
		obj: null
	};
	$(".countdown").livequery(function(){
		$(this).each(function() {
			$(this).focus(function() {
				var c = $(this).attr("class");
				countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
				countdown.obj = this;
				iCount = setInterval(countdown.init,1000);
			}).blur(function() {
				countdown.init();
				clearInterval(iCount);
			});
		});
	});
	
});


