var fp_mastheadAmt = 0;
var fp_mastheadCur = 0;
var fp_int = 0;
var fp_flag = 0;
var np_mastheadAmt = 0;
var np_mastheadCur = 0;
var np_int = 0;
var np_flag = 0;
var ms_pos = 1;
var ms_panels = 0;
//timer


//Navigation
__navigationAddDivider = function(clip,str) {
	var clipli = clip.size();
	for(var i=1;i<clipli;i++){
		clip.eq(i-1).after(str);
	}
}
//frontpage
__setupFrontpageMasthead = function() {
	if($('#fp_mastheadcontainer').size()>0){
		fp_mastheadAmt = $('.fp_projpanel').size();
		if(fp_mastheadAmt > 1) {
			var buttons = '<div id="fp_mastheadbuttons"><span id="fp_mhbutleft"><!-- button left --></span><span id="fp_mhbutright"><!-- button right --></span></div>';
			$('#fp_mastheadbuttoncontainer').append(buttons);
			$('#fp_mhbutleft,#fp_mhbutright').hover(function(){
				$(this).css('background-position','top right');
			},function(){
				$(this).css('background-position','top left');
			});
			$('#fp_mhbutleft').click(function(){
				fp_flag = 0;
				__shiftMasthead(-1);
				
			});
			$('#fp_mhbutright').click(function(){
				fp_flag = 0;
				__shiftMasthead(1);
				
			});
			fp_mastheadCur = 1;
			fp_flag = 1;
			fp_int = setTimeout(__timerfp,6000);
			$('.fp_projpanel').eq(0).fadeIn('700');
			
		} else if (fp_mastheadAmt == 1) {
			$('.fp_projpanel').eq(0).show();
		}
	}
}
__shiftMasthead = function(pos){
	clearTimeout(fp_int);
	var newpos = fp_mastheadCur + pos;
	if(newpos < 1 && pos == -1){
		newpos = fp_mastheadAmt;
	}
	if(newpos > fp_mastheadAmt && pos == 1){
		newpos = 1;
	}
	fp_mastheadCur = newpos;
	$('.fp_projpanel').hide();
	$('.fp_projpanel').eq(newpos-1).fadeIn('700');
	if(fp_flag == 1) {
		fp_int = setTimeout(__timerfp,6000);
	}
}
__timerfp = function(){
	__shiftMasthead(1);
}


//normal pages & project
__setupNormalPageMasthead = function(){
	if($('#page_mastheadcontainer').size()>0){
		np_mastheadAmt = $('.mastheadpanel').size();
		//height set	
		if(np_mastheadAmt > 1) {
			var butappend = '<div id="np_mastheadbutpanel">';
			for(var i=1;i<=np_mastheadAmt;i++){
				butappend += '<div class="np_mastheadbutton"><!-- button --></div>'
			}
			butappend += '</div><div class="clear"></div>';
			$('#page_mastheadcontainer').after(butappend);
			$('.np_mastheadbutton').click(function(){
				np_flag = 0;
				$('.np_mastheadbutton').removeClass('butactive');
				$(this).addClass('butactive');
				var pos = $(this).index();
				np_mastheadCur = pos+1;
				$('.mastheadpanel').hide();
				$('.mastheadpanel').eq(pos).fadeIn();
			});
			np_flag = 1;
			np_mastheadCur = 1;
			np_int = setTimeout(__timernp,6000);
			$('.np_mastheadbutton').eq(0).addClass('butactive');
			$('.mastheadpanel').eq(0).fadeIn();
		} else if(np_mastheadAmt == 1) {
			$('.mastheadpanel').eq(0).fadeIn();
		}
	}
}
__setupProjectPageMasthead = function(){
	if($('#project_mastheadcontainer').size()>0){
		np_mastheadAmt = $('.mastheadpanel').size();
		if(np_mastheadAmt > 1) {
			var butappend = '<div id="np_mastheadbutpanel">';
			for(var i=1;i<=np_mastheadAmt;i++){
				butappend += '<div class="np_mastheadbutton"><!-- button --></div>'
			}
			butappend += '</div>';
			$('#project_mastheadcontainer').after(butappend);
			$('.np_mastheadbutton').click(function(){
				np_flag = 0;
				$('.np_mastheadbutton').removeClass('butactive');
				$(this).addClass('butactive');
				var pos = $(this).index();
				np_mastheadCur = pos+1;
				$('.mastheadpanel').hide();
				$('.mastheadpanel').eq(pos).fadeIn();
			});
			np_flag = 1;
			np_mastheadCur = 1;
			np_int = setTimeout(__timernp,6000);
			$('.np_mastheadbutton').eq(0).addClass('butactive');
			$('.mastheadpanel').eq(0).fadeIn();
		} else if(np_mastheadAmt == 1) {
			$('.mastheadpanel').eq(0).fadeIn();
		}
	}
}
__timernp = function(){
	clearTimeout(np_int);
	
	var newpos = np_mastheadCur+1;
	
	if(newpos > np_mastheadAmt) {
		newpos = 1;
	}
	
	$('.np_mastheadbutton').removeClass('butactive');
	$('.np_mastheadbutton').eq(newpos-1).addClass('butactive');
	$('.mastheadpanel').hide();
	$('.mastheadpanel').eq(newpos-1).fadeIn();
	
	np_mastheadCur = newpos;
	
	if(np_flag == 1) {
		np_int = setTimeout(__timernp,6000);
	}
}

__setupAccordion = function() {
	if($('.page_accordionbutton').size()>0){
		$('.page_accordionbutton').click(function(){
			$(this).next('.page_accordionpanel').slideToggle('fast');
		});
	}	
}
__setupExposedFilters = function() {

	if($('#edit-projtype').size() > 0){

		$('#edit-projtype').children('option').eq(0).text('Project Type');
		$('#edit-projtype').append('<option value="All">View All</option>');
	}
	if($('#edit-date').size() > 0){
		$('#edit-date').children('option').eq(0).text('Completion Date (Chronological Order)');
		$('#edit-date').append('<option value="All">View All</option>');
	}
	if($('#edit-loc').size() > 0){
		$('#edit-loc').children('option').eq(0).text('Location');
		$('#edit-loc').append('<option value="All">View All</option>');
	}
}
__footerAdjust = function(){
	var windowHt = $(window).height();
	var docuHt = $(document).height();
	var pageHt = $('#wrapper').outerHeight() + $('#footer').outerHeight();
	//alert(windowHt+":"+pageHt);
	var htDiff = windowHt- pageHt;
	//alert(htDiff);
	
	if(htDiff >0){
		var footerHtDiv = $('#footer').outerHeight() - $('#footer').height();
		var newFooterHt = $('#footer').height() + htDiff - footerHtDiv-20;
		$('#footer').css('height',newFooterHt);
		
	}
	
}

__msshift = function(pos){
	var newpos = ms_pos + pos;
	if(newpos < 1){
		newpos = 1;
	}
	if(newpos > ms_panels) {
		newpos = ms_panels;
	}
	var newlocation = ((newpos-1)*935);
	$('#milestone_topcontainer .milestone_scroll').animate({
		left: -newlocation
	}, 1000, function() {
		//
	});
	$('#milestone_bottomcontainer .milestone_scroll').animate({
		left: -newlocation
	}, 800, function() {
		//
	});
	ms_pos = newpos;
}

$(window).load(function(){
	__setupFrontpageMasthead();

	__setupNormalPageMasthead();
	__setupProjectPageMasthead();
	if($('#page_mastheadcontainer').size()>0){
		var newHt = $('.mastheadpanel').eq(0).height();
		$('#page_mastheadcontainer').css('height',newHt);
		//alert(newHt);		
	}
	
	
});


$(function() {
	//Exposed filters
	__setupExposedFilters();
	//Cufon
	Cufon.replace(".topbutton",{fontFamily:'ocrf',hover:true});
	Cufon.replace("#topquote h1",{fontFamily:'ocrf',hover:true});
	Cufon.replace("h1.projectlisttitle",{fontFamily:'ocrf',hover:true});
	Cufon.replace(".page_leftpanel h1",{fontFamily:'ocrf',hover:true});
	Cufon.replace(".proj_info h3",{fontFamily:'ocrf',hover:true});
	//navigation
	$('.lvl1navi').hover(function(){
		$(this).css('background-color','#20413a');
		$(this).children('ul').show();
	},function(){
		$(this).css('background-color','');
		$(this).children('ul').hide();
	});
	$('.lvl1navi').each(function(){
		if($(this).has('ul').length) {
			$(this).click(function(){
				$('.lvl1navi').css('background-color','');
				$('.lvl1navi').children('ul').hide();
				$(this).css('background-color','#20413a');
				$(this).children('ul').show();
			});
		}
	});
	
	
	//frontpage
	
	__setupAccordion();
	
	//webforms
	if($('.page_accordionpanel').size()>0){
		//check for links to application-process
		$('.page_accordionpanel').each(function(){
			//alert($(this).next('.page_accordionpanel').find('a').attr('href'));
			if($(this).find('a').attr('href')== 'application-process'){
				var positionapplied = escape($(this).prev('.page_accordionbutton').find('h3').text());
				//$(this).find('a').attr('href', 'application-process?position='+positionapplied);
				$(this).find('a').attr('href', '?q=/join-us/application-process&position='+positionapplied);
			}
		});
	}
	if($('#edit-submitted-phone-number').size()>0){
		$('#edit-submitted-phone-number').numeric();
	}
	if($('#applicationtitle').size()>0) {
		var inputposition = $('#edit-submitted-position').val();
		inputposition = unescape(inputposition);
		$('#edit-submitted-position').val(inputposition);
		var position = $.getUrlVar('position');
		if(position == '' || position == undefined) {
			//check input
			position = inputposition;
		}
		
		if(position != '' && position != undefined){
			position = unescape(position);
			$('#applicationtitle').html('Application for position of');
			$('#applicationjobtitle').html(position);
		} else {
			
		}
	}
	
	//milestones
	if($('#milestonewrapper').size()>0){
		//setup hovers
		$('.milestonebut').hover(function(){

			$(this).css('background-position','top right');
		},function(){
			$(this).css('background-position','top left');
		});
		$('#milestone_left').click(function(){
			__msshift(-1);
		});
		$('#milestone_right').click(function(){
			__msshift(1);
		});
		//amount
		ms_panels = $('#milestone_topcontainer .milestone_scroll .milestone_panel').size();
		$('#milestone_topcontainer .milestone_scroll').css('width',ms_panels*935);
		$('#milestone_bottomcontainer .milestone_scroll').css('width',ms_panels*935);
		
	}
	
	
	//pager
	if($('ul.pager').size()>0){
		
		$('ul.pager li').each(function(){
			var nint;
			if($(this).find('a').size()>0){
				nint = parseInt($(this).find('a').html());
				if(nint < 10) {
					$(this).find('a').html('0'+nint);
				}
			} else {
				nint = parseInt($(this).html());
				if(nint < 10) {
					$(this).html('0'+nint);
				}
			}
			
		});
		$('ul.pager li').eq(0).before('<li>Page: </li>');
	}
	//footer
	__footerAdjust();
	
	
	
});


$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
