var t,
		t2;

// cufon replaces 
Cufon.replace('.futurastd', {hover:true, fontFamily: 'FuturaStd'});

$(document).ready(function(){
	// footer attachment
	$('a.footer-toggle').live('click',function(event){event.preventDefault();toggleFooter()})
	// overlay attachments
	$('#overlay-button a').live('click',function(event){event.preventDefault();$('#overlay').hide()});
	$('#overlay-box').bind("clickoutside",function(event){$('#overlay').hide()});
	$(document).keypress(function(event){if(event.keyCode==27)$('#overlay').hide()})
	// attach events to primary nav 
	$('#primary-nav li a').mouseover(function(){controlPrimary(this)});
	$('#primary-nav li a').mouseout(function(){t=setTimeout(function(){if($('#primary-nav li.selected a').length){offsetPrimary($('#primary-nav li.selected a').attr('rel'))}else{$('#navigation-pri-flare').fadeOut("fast")}},500)});
	// attach ajax call to ajax forms
	$('form.ajax').submit(function(event){event.preventDefault();ajaxForm(this)});
	// get form elements with a title attr to bind a infield labe to it
	$(':input').each(function(index){if($(this).attr('title')){infieldLabel(this)}});
	// attach events to delete links
	$('.delete-link').live('click',function(event){deleteAlert(this,event)});
	//attach events to overlay text links
	$('.text-overlay').click(function(event){event.preventDefault();textOverlay($(this).attr('title'),$(this).attr('data-body'))});
	// attach event to tabs
	$('#tabs a').live('mouseover',function(){animateTab(this,'over')});
	$('#tabs a').live('mouseout',function(){animateTab(this,'out')});
	// scrolls
	$('.scroller').live('click',function(event){event.preventDefault();scroller(this)});
});

// array funcs
function arrayNumber(a,b){return a - b}
function arrayInteger(array){for(x in array){array[x]=parseInt(array[x])};return array}

// footer toggle
function toggleFooter(){
	if($('#guest-call-abs').css('bottom')=='-191px'){
		// closed 
		var settings={
			open:0,
			newpos:0,
			text:'Hide'
		}
	}else{
		// open
		var settings={
			open:1,
			newpos:'-191px',
			text:'Sign up today'
		}
	}
	$('#guest-call-abs').animate({
		bottom:settings.newpos
	},250,function(){
		$('#footer-toggle').fadeOut(200,function(){
			$('#footer-toggle').text(settings.text);
			$('#footer-toggle').fadeIn(200)
		})
	})
}

// animate the listing tabs
function animateTab(el,state){
	if(!$(el).hasClass('selected')){
		if(state=='over'){var offset='0'}else{var offset='12px'}
		$(el).animate({'margin-top':offset},100);
	}
}

// overlay popup callers 
function alertOverlay(title,body){
	$('#overlay-inject').load('/overlay/alert/', {'overlay.title':title,'overlay.body':body},function(){
		$('#overlay').show()
	});
}
function textOverlay(title,body){
	$('#overlay-inject').load('/overlay/text/', {'overlay.title':title,'overlay.body':body},function(){
		$('#overlay').show()
	});
}

// infield label for forms 
function infieldLabel(el){
	// setup the onload label 
	if(el.value==''){$(el).attr('value',el.title)}else if(el.value != el.title){$(el).addClass('active')}
	// set the focus event
	$(el).focus(function(){
		if(el.value==el.title){
			// blank it
			$(el).attr('value','')
			// class it
			$(el).addClass('active')
			}
	});
	// set the blur event
	$(el).blur(function(){
		if(el.value==''){
			// title it
			$(el).attr('value',el.title)
			// remove class
			$(el).removeClass('active')			
		}
	});
}

// scroller controller
function scroller(el){$.scrollTo($(el).attr('href'),400)}

// delete link popup alert
function deleteAlert(el,ev){
	if(confirm(el.title)){
		return true
	}else{
		ev.preventDefault();
		return false
	}
}

// animate controller for the primary nav 
function controlPrimary(el){
	clearTimeout(t);
	offsetPrimary(el.rel)
}
// offset the flare 
function offsetPrimary(offset){
	$('#navigation-pri-flare').animate({
		left:offset+'px'
	},function(){if(!$('#navigation-pri-flare').is(":visible"))$('#navigation-pri-flare').fadeIn("fast")});
}

// homepage flash
if($('#home-video')){
	var flashvars = {};
	var params = {};
			params.allowFullScreen = "true";
			params.allowScriptAccess="always";
			params.wmode="opaque";
	var attributes = {};
			attributes.id = "bg_flash";
			attributes.name = "bg_flash";
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id=14057463&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1", "home-video", "546", "308", "9.0.0", "", flashvars, params, attributes);
}





// contact us ajax form 
function ajaxForm(form){
	$('#indicator_'+form.id).show();
	$('#ajax-form').load($(form).attr('action'),$(form).serializeArray(),function(){
		// hide indicator
		$('#indicator_'+form.id).hide();
	});
}
