$(document)
.ready(
	function(){
		$("#gallery img").attr("width", "960");
		$("#gallery img").attr("height", "325");
		$("#sponsors-div img").attr("width", "930");
		$("#sponsors-div img").attr("height", "186");
		
		$('#feedburner_email_widget_sbef_email').attr('value','name@email.com')
		Cufon
			.replace('#header_right a, #sample h2, #content h2,#sponsors-div h2, #signup h2,#primary .xoxo h3', { fontFamily: 'Rockwell Extra Bold' })
			.replace('h1, h2, .nav .page_item a,#gallery .tab_container .tab_content .txt, #sponsors-div .tab_container .tab_content .txt', { fontFamily: 'Rockwell' })
			.replace('.spread li.font, button,.extra-information .comment-box, .extra-information .day,.extra-information .month', { fontFamily: 'Rockwell' });
			
		//$("#gallery .tab_content, #sponsors-div .tab_content").hide();
		$("#gallery ul.tabs li:first, #sponsors-div ul.tabs li:first").addClass("active").show();
		$("#gallery .tab_content:first, #sponsors-div .tab_content:first").show();

		$('.formBuilderCaptcha input').val('Enter word displayed above.');

		$('input')
		.focus(
			function(){
				if ($(this).data('org')==undefined) {
					$(this).data('org', $(this).val());
				}
				var val = $(this).val(),
					org = $(this).data('org');
				if( val == org ){
					$(this).val('');
				}
			}
		)
		.blur(
			function(){
				if ($(this).data('org')==undefined) {
					$(this).data('org', $(this).val());
				}
				var val = $(this).val(),
					org = $(this).data('org');
				if( val == '' ){
					$(this).val(org);
				}
			}
		)
		//.data('org', $(this));
		
		var tmpw = 0;
		$('#gallery .tab_content')
		.each(
			function(){
				var el = $(this);
				el.css('left', tmpw );
				tmpw += parseInt( el.width(),10);
			}
		);
		
		var tmpw = 0;
		$('#sponsors-div .tab_content')
		.each(
			function(){
				var el = $(this);
				el.css('left', tmpw );
				tmpw += parseInt( el.width(),10);
			}
		);
		
		var gtabcur = 1,
			gtabto = null,
			gfn = null,
			gli = $('#gallery ul.tabs li'),
			gtabpause = false;
		
		gfn = function(){
			gtabto = setTimeout(
				function(){
					if( gtabcur == gli.size() ){
						gtabcur = 0;
					}
					gli.eq(gtabcur++).click();
					
				},
				10000
			);
		};
			
		gli
		.click(
			function(){
				clearTimeout( gtabto );
				
				
				$("#gallery ul.tabs li").removeClass("active");
				$(this).addClass("active");
				//$("#gallery .tab_content").hide();
				var i = $('#gallery ul.tabs li').index(this);
				
				gtabcur = i+1;
				var movedistance = $("#gallery img").attr("width");
				
				$("#gallery .tab_container_wrap")
				.animate({
					left:-(i*movedistance)
				},500);
				
				gfn();

				return false;
			}
		);
		
		$("#gallery .tab_container_wrap .tab_content")
		.mouseenter(
			function(){
				clearTimeout( gtabto );
			}
		)
		.mouseleave(
			function(){
				gfn();
			}
		);
		
		
		gfn();
		
		var stabcur = 1,
			stabto = null,
			sfn = null,
			sli = $('#sponsors-div ul.tabs li'),
			stabpause = false;
		
		sfn = function(){
			stabto = setTimeout(
				function(){
					if( stabcur == sli.size() ){
						stabcur = 0;
					}
					sli.eq(stabcur++).click();
				},
				10000
			);
		};
			
		sli
		.click(
			function(){
				clearTimeout( stabto );
				
				$("#sponsors-div ul.tabs li").removeClass("active");
				$(this).addClass("active");
				
				var i = $('#sponsors-div ul.tabs li').index(this);
				var movedistance = $("#sponsors-div .tab_container_wrap").width();
				
				$("#sponsors-div .tab_container_wrap")
				.animate({
					left:-(i*movedistance)
				},500);
				
				sfn();

				return false;
			}
		);
		
		$("#sponsors-div .tab_container_wrap .tab_content")
		.mouseenter(
			function(){
				clearTimeout( stabto );
			}
		)
		.mouseleave(
			function(){
				sfn();
			}
		);
		
		
		sfn();
		
		//$('#container, #primary')
		$('.innerwrap').equalHeights();
		
		$('.spread .l3 a')
		.click(
			function(e){
				e.preventDefault();
				$("#sample")
					.hide()
					.css({
						'position':'',
						'left':'',
						'top':''
					})
					.modal({persist:true,overlayClose:true});
			}
		);

		$('#form-message')
		.submit(
			function( e ){
				e.preventDefault();
				
				if( $.trim( $('#feedburner_email_widget_sbef_email').val() ) == '' ){
					alert('Please enter an email address');
					$('#feedburner_email_widget_sbef_email').focus();
					return;
				}
				
				$.post(
					$(this).attr('action'),
					$(this).serialize(),
					function( data ){
						$('#form-notice-message').show();
						$('#feedburner_email_widget_sbef_email').val('');
						$('#to-message').val('');
						setTimeout(
							function(){
								$('#form-notice-message').hide();
							},
							2000
						);
					}
				);
			}
		);
	}
);

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};
Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
	//set defaults
	settings = jQuery.extend({
		scope: 'body',
		reverse: false
	}, settings);
	
	var pxVal = (this == '') ? 0 : parseFloat(this);
	var scopeVal;
	var getWindowWidth = function(){
		var de = document.documentElement;
		return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
	};	
	
	/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
		For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
		When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
		to get an accurate em value. */
				
	if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){		
			return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
		};
		scopeVal = calcFontSize();
	}
	else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };
			
	var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
	return result;
};
