
/* Global Variables */
var timeouts = [];

function init_site(){
	clearClick();
	setBrowserClass();
	
	Cufon.replace('#nav > li > a', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	Cufon.replace('#alt_nav > li > a', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	
	Cufon.replace('#tabs_nav a', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	
	Cufon.replace('.green_btn, .grey_btn', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	Cufon.replace('.home_widget h3', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	Cufon.replace('#footer h2', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	
	Cufon.replace('.box .title', { fontFamily: 'GothamBook', letterSpacing: "-0.75px", hover: true });
	
	Cufon.replace('#page_title h1', { fontFamily: 'GothamBook', letterSpacing: "-0.75px", hover: true });
	
	Cufon.replace('#content h2', { fontFamily: 'GothamBook', letterSpacing: "-0.75px", hover: true });
	Cufon.replace('h3:not(#html_notes h3)', { fontFamily: 'HelveticaNeueMedium', letterSpacing: "-0.75px", hover: true });
		Cufon.replace('h4', { fontFamily: 'HelveticaNeueMedium', letterSpacing: "-0.75px", hover: true });
	Cufon.replace('h5, h6', { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	
	Cufon.replace("#subnav li li a", { fontFamily: 'GothamMedium', letterSpacing: "-0.50px", hover: true });
	
	Cufon.replace('.text_btn', { fontFamily: 'GothamMedium', hover: true });
	
	Cufon.replace('#ccb_events_list .event_date', { fontFamily: 'GothamMedium', hover: true });
	
	Cufon.replace('.series_item .series_title', { fontFamily: 'GothamMedium', hover: true });
	
	Cufon.replace('.sermon_info .sermon_title', { fontFamily: 'GothamMedium', hover: true });


		
	//slide the nav line around
	var start = $("#navigation .current:last").position();
	var width = $("#navigation .current:last a").width();
	//$("#nav_slider").css("opacity", 1).css("left", start.left+9).css("width", width+1).css("display", "block");
	
	/*$("#nav > li > a").hover(function() {
		var offset = $(this).position();
		var width = $(this).width();
		$("#nav_slider").stop().animate({opacity: 1, width: width+1, left: offset.left+9}, 300, function() {
			
		});
	}, function() {
		var start = $("#navigation .current").position();
		$("#nav_slider").stop().animate({left: start.left+9, opacity: 0}, 400, function() {
			var start = $("#navigation").position();
			$("#nav_slider").css("left", start.left).css("width", "45px");
		});
	});*/
	if ($("body").hasClass("home")) { //if we're on the home page
		var howmanyTabs = $(".home_tab").length;
		$("#tabs_container").width(600*howmanyTabs);
		
		//slide the tab background around
		var start = $("#tabs_nav").position();
		$("#tab_slider").css("left", start.left+40).css("margin-left", "0px");
		
		$("#tabs_nav a").click(function(e) {
			e.preventDefault();
			if ($(this).hasClass("current")) {
				//do nothing
			} else {
				$("#tabs_nav a").removeClass("current");
				Cufon.refresh("#tabs_nav a");
				
				$(".home_tab.active").animate({marginLeft: -600}, 600, function() {
					$(this).css("margin-left", "600px");
					$(".home_tab").removeClass("active");
				});
				
				
				$(this).addClass("current");
				var thetab = $(this).attr("rel");
				var offset = $(this).position();
				$("#tab_slider").stop().animate({opacity: 1, left: offset.left}, 300, function() {
					
				});
				
				$("#"+thetab+"_tab").stop(true, true).animate({marginLeft: "0"}, 600, function() {
					$(this).addClass("active");
					$(".home_tab:not(.active)").css("margin-left", "600px");
					Cufon.refresh(".home_tab");
				});
			}
		});
		
		var width = $("#gallery .slide:visible").width();
		$("#gallery").after('<div class="container"><div id="paging_conatiner"><div id="paging"></div></div></div>').cycle({ 
			fx:				'scrollLeft',
			speed:			1000,
			easeIn:			"easeOutSine",
			easeOut:		"easeInSine",
			timeout: 		8000,
			animOut:		{
								width: "100%",
								left: "-"+width
							},
			cssAfter:		{
								width: "100%",
								left: "-"+width
							},
			pager:			'#paging',
			onPagerEvent:	goToGalleryBox
		});
		
		$("#gallery_boxes").after('<div id="box_paging"></div>').cycle({
			fx:			'scrollLeft',
			speed:		900,
			easeIn:			"easeInOutQuad",
			easeOut:		"easeOutCubic",
			timeout:	8000,
			animOut:	{
							width: "100%",
							left: "-"+width
						},
			cssAfter:	{
							width: "100%",
							left: "-"+width
						},
			pager:		'#box_paging'
		});
		
	}
	
	$(".green_btn, .grey_btn").before("<span class='clear'></span>").after("<span class='clear'></span>");
	$(".text_btn").after("<span class='clear'></span>");
	$(".home_widget img").each(function() {
		$(this).wrap("<div class='homeWidgetImage'></div>");
		var height = $(this).height();
		$(this).parents(".homeWidgetImage").css("height", height);
	});
	
	$("#subnav li li a:last").css("border-bottom", "0px none");
	
	$("img.rounded_corners_left").each(function() {
		$(this).bind("load", function() {
			var width = $(this).width();
			var height = $(this).height();
			var src = $(this).attr("src");
			$(this).removeClass("rounded_corners_left").wrap("<div class='rounded_corners_wrap alignleft' style='background-image: url("+src+"); width:"+width+"px; height:"+height+"px;'></div>");
		});
	});
	$("img.rounded_corners_right").each(function() {
		$(this).bind("load", function() {
			var width = $(this).width();
			var height = $(this).height();
			var src = $(this).attr("src");
			$(this).removeClass("rounded_corners_right").wrap("<div class='rounded_corners_wrap alignright' style='background-image: url("+src+"); width:"+width+"; height:"+height+";'></div>");
		});
	});
	
	
	$("a.rounded_corners_link img").each(function() {
		$(this).bind("load", function() {
		
			var width = $(this).width();
			var height = $(this).height();
			var src = $(this).attr("src");
			
			$(this).parent('a').attr("style", "background: url("+src+") no-repeat; width:"+width+"px; height:"+height+"px;");
			//$(this).parent('a').height(height);
			//$(this).parent('a').width(width);
			$(this).css('visibility','hidden');
			       
			//$(this).wrap("<div class='rounded_corners_wrap alignright' style='background-image: url("+src+"); width:"+width+"; height:"+height+";'></div>");
		});
	});
	
	
	
	$(".series_item:nth-child(3n+3)").css("margin-right","0px");
	
	$("#design_by_am a").hover(function() {
		$(this).stop(true, true).animate({rotate: "+=360deg"}, 500);
	}, function() {
		$(this).stop(true, true).animate({rotate: "-=360deg"}, 500);
	});
	$("#sermons_list .paging").width($("#sermons_list .paging").children(".prev").outerWidth() + $("#sermons_list .paging").children(".info").outerWidth() + $("#sermons_list .paging").children(".next").outerWidth());

	// dropdown menu
	$("ul#nav li").hover(function(){
        $(this).addClass("hover");
       // $('ul:first',this).css('dispay', 'block');
		$('ul:first',this).show();
    }, function(){
        $(this).removeClass("hover");
        //$('ul:first',this).css('dispay', 'none');
		$('ul:first',this).hide();
    });
    
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
	
	// to any class of .btn_contact to open a new window
	$('.btn_contact').click(function() {
		openNewWindow($(this).attr('href'), 'window', 740, 600);
		return false;
	});

}

function css_tweaks(){

}

function goToGalleryBox(zeroBasedSlideIndex, slideElement) {
	var theslide = zeroBasedSlideIndex+1;
	$("#box_paging a:contains('"+theslide+"')").trigger("click");
}


// Opens a new window in the middle on the browser
function openNewWindow(theURL, winName, winWidth, winHeight){
	var width;
	var height;
	var leftdist;
	var topdist;
	width = screen.width;
	height = screen.height;
	leftdist = Math.round((width - winWidth) / 2);
	topdist = Math.round((height - winHeight) / 2);
	window.open(theURL, winName,'width='+winWidth+',height='+winHeight+',top='+topdist+',left='+leftdist+',noresize,resize=0,menubar=0,location=no,scrollbars=1btn_contact,toolbar=no')
}
