(function($) {
	var vibation = window.vibation = {
	data: {
		// js data
		caseStudyTimer: null,
		homeQuoteFadeTime: 1,
		whatWeBelieveFadeTime: 1
	},
	func: {
		alignLinedBgs: function() {
			$('div.callouts').each(function(){
				var yPosition = $(this).offset().top;
				var offset = 7 -(yPosition % 6) ;
				//console.debug("alignLinedBgs: " + yPosition  + " - "+ yPosition % 6 + " - " + offset);
				$(this).css("background-position", "center " + offset + "px");
			});
			jQuery.each(jQuery.browser, function(i, val) {
			   if(i=="mozilla"){
					$(".centeredContent").addClass("centeredContentFF");
				}
			     
			 });
		},
		activateCallouts: function() {
			$("#calloutPopover").jqm({
				overlay: 20,
				trigger: ".callouts li a",
				ajax: '@href',
				onHide: function(h) {
			        h.w.slideUp(400,function() { if(h.o) h.o.remove(); }); 
					$(".callouts li").removeClass("current");
					$(".formError").remove();
				},
				onShow: function(h) {
					h.w.slideDown(400);
				}
			});
			$(".callouts li a").click(function (){
				var marginLeft = 403;
				if ($(this).parent().hasClass("first")){
					marginLeft = -10;
				} else if ($(this).parent().hasClass("second")){
					marginLeft = 228;
				} else if ($(this).parent().hasClass("third")){
					marginLeft = 167;
				}
				$("#calloutPopover").css("margin-left", marginLeft);

				$(".callouts li").removeClass("current");
				$(this).parent().addClass("current");
			});

		},
		caseStudyDetailsHovers: function() {
			$('#caseStudiesNav li').hover(function() {
				$(".caseStudyDetailsBox").hide();
				var id = $(this).attr("id");
				var thisLeftPos = $(this).position().left;
				var navLeftPos = $("#caseStudiesNav").position().left;
				var hoverPos = thisLeftPos-navLeftPos-10;
				$("#"+id+"Hover").css("margin-left", hoverPos+"px");
				$("#"+id+"Hover").fadeIn(300);
				vibation.data.caseStudyTimer = setTimeout("vibation.func.hideCSHoversOnTimeout()",10*1000);
			},function(){
				// do nothing
			});
			$(".caseStudyDetailsBox").hover(function() {
				// do nothing
			},function(){
				$(this).fadeOut(300);
				if (vibation.data.caseStudyTimer){
					clearTimeout(vibation.data.caseStudyTimer);
					vibation.data.caseStudyTimer = null;
				}
			});
		},
		hideCSHoversOnTimeout: function() {
			vibation.data.caseStudyTimer = null;
			$(".caseStudyDetailsBox").hide();
		},
		initHomeQuotes: function() {
			vibation.data.currentQuote = Math.round(2*Math.random());
		//	$(".homeQuote").hide();
		//	$("#homeQuote"+vibation.data.currentQuote).show();
			//setInterval("vibation.func.loopHomeQuotes()", 10*1000);
		},
		loopHomeQuotes: function() {
			$(".homeQuote").fadeOut(vibation.data.homeQuoteFadeTime*1000);
			setTimeout("vibation.func.fadeInNextHomeQuote()",vibation.data.homeQuoteFadeTime*1000);
			
			vibation.data.currentQuote++;
			if($(".homeQuote").size() <= vibation.data.currentQuote){
				vibation.data.currentQuote = 0;
			}
		},
		fadeInNextHomeQuote: function() {
			$("#homeQuote"+vibation.data.currentQuote).fadeIn(vibation.data.homeQuoteFadeTime*1000);
		},
		initWhatWeBelieve: function() {
			var orderIds = shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
			var i = 0;
			$(".favQuote").each(function (){
				$(this).attr("id", "favQuote"+orderIds[i]);
				i++;
			});

			vibation.data.currentQuote = 0;
			$(".favQuote").hide();
			$("#favQuote"+0).show();
			setInterval("vibation.func.loopWhatWeBelieve()", 8*1000);
		},
		loopWhatWeBelieve: function() {
			$(".favQuote").fadeOut(vibation.data.whatWeBelieveFadeTime*1000);
			setTimeout("vibation.func.fadeInNextWhatWeBelieve()",vibation.data.whatWeBelieveFadeTime*1000);
			
			vibation.data.currentQuote++;
			if($(".favQuote").size() <= vibation.data.currentQuote){
				vibation.data.currentQuote = 0;
			}
		},
		fadeInNextWhatWeBelieve: function() {
			$("#favQuote"+vibation.data.currentQuote).fadeIn(vibation.data.homeQuoteFadeTime*1000);
		},
		initExpandList: function() {
			$(".expandListBody").each(function(){
				$(this).css("height", $(this).height());
			});
			$(".expandListBody").hide();
			$(".expandList li.current .expandListBody").show();
			$(".expandList ul li h2").click(function(){
				// do nothing if current					
				if ($(this).parent().hasClass("current")) return;

				$("li.current div.expandListBody").slideUp(500);
				$(".expandList ul li").removeClass("current");
				$(this).parent().addClass("current");
				$("li.current div.expandListBody").slideDown(500);
			});			
		},
		initWhoWeAre: function() {
			$(".whoWeAreCopy").hide();
			$("#whoWeAre_Craftsman").show();
			$('#whoWeAreMap area').hover(function(e) {
				e.preventDefault();
				var whoWeAreId = $(this).attr("href").replace("#", "");

				$(".whoWeAreCopy").hide();
				$("#whoWeAre_"+whoWeAreId).show();

				$("#whoWeAreGraphic").attr("class", "wwa_"+whoWeAreId);

			},function(){
				// do nothing
			});
		},
		oddCaseStudies: function() {
			$(".caseStudyDetailsBox:even").addClass("odd");
		},
		// IE 6 Fixes
		// ------------------------------------------------
		ie6Fixes: function() {
		}
	}
};
})($);

// on body load
$(function() {
	// aling all the diagnal bgs
	vibation.func.alignLinedBgs();

	// activate the callout links
	if($("#calloutPopover").size() > 0){
		vibation.func.activateCallouts();
	}
	// Run the Case Study Functions
	if($("#caseStudiesNav").size() > 0){
		vibation.func.caseStudyDetailsHovers();
	}
	if($(".homeQuoteSect").size() > 0){
		vibation.func.initHomeQuotes();
	}
	if($(".expandList").size() > 0){
		vibation.func.initExpandList();
	}
	if($(".whatWeBelieve").size() > 0){
		vibation.func.initWhatWeBelieve();
	}
	if($("#whoWeAre").size() > 0){
		vibation.func.initWhoWeAre();
	}
	if($(".caseStudyList").size() > 0){
		vibation.func.oddCaseStudies();
	}
});

function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


