var e;

$(document).ready(function() {
	$(".aeresize230").aeImageResize({ width: 230 });
	$(".aeresize120").aeImageResize({ width: 120 });
	$(".entry-media-container img").aeImageResize({ width: 408 });

	$("#entry-recipe-index .recipe-over").hide();
	
	$("#entry-recipe-index li").each(function() {
		$(this).hover(function() {
			$(this).children(".recipe-over").stop(true, true).fadeIn();
		}, function() {
			$(this).children(".recipe-over").stop(true, true).fadeOut();
		})
	});
	
	// take care of ie6 users
	if ( typeof e == 'function' ) {
		e("frontend/js/ie6/");
	}
	
	// recipe magic
	$("#recipe-method").children("ol").children('li').click(function(){
		//alert('Hi :)\n' + $(this).contents());
		if ( $(this).hasClass('rm-item-selected') ) {
			$(this).removeClass('rm-item-selected');
		} else {
			$(this).addClass('rm-item-selected');
		}
	});
	
	$("#recipe-method").children("ol").children('li').hover(
		function() { // over
			if ( !$(this).hasClass('rm-item-selected') ) {
				$(this).addClass('rm-item-hover');
			}
		},
		function() { // out
			if ( !$(this).hasClass('rm-item-selected') ) {
				$(this).removeClass('rm-item-hover');
			}
		}
	);
	
	// contest display tips
	$('.contest-form-helper').each(function() {
		var formhelper = $(this);
		formhelper.css({'opacity' : '0','left' : '429px'})
		.siblings('input').focus(function() {
			formhelper.show().animate({
				'opacity' : '1',
				'left' : '445px'
			}, 400);
		})
		.blur(function() {
			formhelper.animate({ 
				'opacity' : '0',
				'left' : '429px'
			}, 200, function() {
				formhelper.hide();
			});
		});
		
	});
	
	// contest upload button submit
	$('#form-video-upload').submit(function(ev) {
		if ($('#browser-upload-ff').val()) {
			$('#spinner-overlay').css('opacity','0.9');
			$("#spinner-container").fadeIn(200);
		} else {
			ev.preventDefault();
			$(this).children('.error').show();
		}
	});
	
	// contest slideshow in phase 3
	if ( $('#contest-intro-slideshow-slides').length ) {
		setInterval(showNextSlideInPhase3SlideShow, 5000);
	}
	
});

// Cufon... More like CuFUN!
Cufon.replace('.page-heading', {fontFamily:'Avenir'});
Cufon.replace('.content-heading', {fontFamily:'Avenir'});
Cufon.replace('.panel h2', {fontFamily:'avenirBlack'});
Cufon.replace('.subheading', {fontFamily:'avenirBlack'});

// Sharing Tools
function shareTwitter(link, title) {
	var url = 'http://twitter.com/share?url=' + link;
	url += '&via=bettertogether';
	url += '&text=' + title;
	window.open(url,'twitterShareWindow','width=550,height=450,location=no,status=no,menubar=no');
	
	return false;
}

function shareFacebook(link) {
	var url = 'http://www.facebook.com/share.php?u=' + link;
	window.open(url,'facebookShareWindow','width=620,height=450,location=0,status=0,menubar=0');
	
	return false;
}

function printPage() {
	window.print();
	return false;
}

// Contest Functions

function browseTagListMadeSelection(obj) {
	var section = '/contest/browse/';
	var tag = obj.options[obj.selectedIndex].value;
	
	var URL = section + tag;
	
	if (obj.selectedIndex != 0) { 
		location.href = URL;
	}
	else {
		location.href = '/contest/browse/';
	}
}

function browseReferencesListMadeSelection(obj) {
	var option = obj.options[obj.selectedIndex].value;
	
	if ( option == 'Other' ) {
		$('#contest-other-reference').toggle(true);
	}
	else {
		$('#contest-other-reference').toggle(false);
	}
}

function showNextSlideInPhase3SlideShow() {
	var maxSlides = $('#contest-intro-slideshow-slides').children('li').size();
	var slideWidth = $('#contest-intro-slideshow-slides').children('li').width();
	var width = slideWidth * maxSlides;
	var offset = parseInt($('#contest-intro-slideshow-slides').css('left'));
	var slideNumber = maxSlides - ((width + offset) / slideWidth);
	var newLeft = ((slideNumber + 1) * slideWidth ) * -1;
	
	if ( slideNumber == maxSlides - 1 ) {
		newLeft = 0;
	}
	
	$('#contest-intro-slideshow-slides').animate({
		left: newLeft + 'px'
	}, 
	500);
}
