$(document).ready(function() {

	$("img.whatisbutton").click(function() {
		if ($("img.whatisbutton").attr("src") == "/images/v2/whatsite_button_hide.png") {
			$("img.whatisbutton").attr("src","/images/v2/whatsite_button_show.png");
			$("#whatis div.body, #whatis div.r").slideUp("fast");
			$("#whattext").animate({
				top: "-28px",
				left: "90px"
			});
			$("#whatis").animate({
				height: '40px'
			});
			Set_Cookie('hideslidebox','1',55920,'/','.filmfights.com');
		} else {
			$("img.whatisbutton").attr("src","/images/v2/whatsite_button_hide.png");
			$("#whatis div.body, #whatis div.r").slideDown("fast");
			$("#whattext").animate({
				top: "37px",
				left: "19px"
			});
			$("#whatis").animate({
				height: '275px'
			});
		}
	});
	
	$("#nlsignup").click(function() {
		var email = $("#nlemail").val();
		
		if (check_email(email)) {
			$.post("/ajax_add_email.php", { email: email });
			$("#nlemail").val("Thanks!");
			$("#nlemail").attr("disabled","disabled");
		} else {
			alert("Please enter a valid email address.");
		}
	});
	
	$("input.url").focus(function() {
		$(this).select();
	});
	
	$("#fightmenu a.read,#fightmenu a.write,#fightmenu a.info").click(function() {
		// which is active?
		var whichact = $("#fightmenu a.active").attr("class");
		// hide everything
		$("#fightmenu a").removeClass("active");
		$("#playing div.submenu:visible,#video div.submenu:visible").slideUp("fast");
		var which = $(this).attr("class");
		// show
		if (which == "read" && whichact != "read active") {
			$("#fight_read").slideDown("fast");
			$(this).addClass("active");
		} else if (which == "write" && whichact != "write active") {
			$("#fight_write").slideDown("fast");
			$(this).addClass("active");
		} else if (which == "info" && whichact != "info active") {
			$("#fight_info").slideDown("fast");
			$(this).addClass("active");
		}
		return false;
	});
	
	$("#vidtabs a").click(function() {
		// hide all tabs
		$("#vidtabs a").removeClass("on");
		$(this).addClass("on");
		var what = $(this).attr("rel");
		$("#vids .vidthumbarea").hide();
		
		if (what == "hw") {
			$("#vidbox div.sub").html('The best of the fight <img src="images/v2/icon_stars.png" width="9" height="9" alt="*" /><img src="images/v2/icon_stars.png" width="9" height="9" alt="*" /><img src="images/v2/icon_stars.png" width="9" height="9" alt="*" />');
			$("#hwvids").fadeIn("fast");
		} else if (what == "mw") {
			$("#vidbox div.sub").html('Solid contenders, with a couple flaws <img src="images/v2/icon_stars.png" width="9" height="9" alt="*" /><img src="images/v2/icon_stars.png" width="9" height="9" alt="*" />');
			$("#mwvids").fadeIn("fast");
		} else if (what == "lw") {
			$("#vidbox div.sub").html('Broke the rules or just not quite there yet <img src="images/v2/icon_stars.png" width="9" height="9" alt="*" />');
			$("#lwvids").fadeIn("fast");
		} else {
			return false;
		}
		return false;
	});
	
	$("#resmenu a").click(function() {
		// reset all
		$("#resmenu a").removeClass("selected");
		$("#results table").hide();
		$(this).addClass("selected");
		// what are we looking at
		var what = $(this).attr("rel");
		if (what == "hw") {
			$("#hwresults").fadeIn("fast");
		} else if (what == "mw") {
			$("#mwresults").fadeIn("fast");
		}
		return false;
	});
	
	$("#lfmenu a.read,#lfmenu a.write,#lfmenu a.info").click(function() {
		// which is active?
		var whichact = $("#lfmenu a.active").attr("class");
		// hide everything
		$("#lfmenu a").removeClass("active");
		$("#lastfight div.submenu:visible").slideUp("fast");
		var which = $(this).attr("class");
		// show
		if (which == "read" && whichact != "read active") {
			$("#lf_read").slideDown("fast");
			$(this).addClass("active");
		} else if (which == "write" && whichact != "write active") {
			$("#lf_write").slideDown("fast");
			$(this).addClass("active");
		} else if (which == "info" && whichact != "info active") {
			$("#lf_info").slideDown("fast");
			$(this).addClass("active");
		}
		return false;
	});
	
	$("#votebutton").click(function() {
		var errors = 0;
		var errortext = '';
		var email = $("#voteemail").val();
		var hw = $("#votehw").val();
		var mw = $("#votemw").val();
		var fight = $("#currentfight").val();
		
		if (hw == 0) {
			errortext = errortext + "Please choose a heavyweight film!\n";
			$("#votehw").css('background-color','yellow');
			errors++;
		}
		
		if (mw == 0) {
			errortext = errortext + "Please choose a middleweight film!\n";
			$("#votemw").css('background-color','yellow');
			errors++;
		}
		
		if (!check_email(email)) {
			errortext = errortext + "Please enter a valid email address!";
			$("#voteemail").css('background-color','yellow');
			errors++;
		}
		
		if (errors > 0) {
			alert(errortext);
			return false;
		} else {
			$.post("/cast_vote.php", { id: hw, lwid: mw, email: email, fight: fight });
			Set_Cookie('voted',fight,55920,'/','.filmfights.com');
			Set_Cookie('votedemail',email,55920,'/','.filmfights.com');
			$("#votebox").css("background","#cc7b1b");
			$("#votebox").html('<div class="voted"><img src="images/v2/voted_almost.png" width="107" height="120" alt="Voted" /><h3>Check Your Email</h3></div>');
		}
	});
	
	$("#votebox select").click(function() {
		$(this).css("background-color","#fff");
	});
	
	$("#morebutton").hover(function() {
		$("#bnot").slideDown("fast");
	});
	
	$("#bnot").hover(function() {
		return true;
	}, function() {
		$("#bnot").slideUp("fast");
	});
});

$(window).scroll(function() {
	if ($(this).scrollTop() > 300 && $('#bbar').hasClass('hidden')) {
		$('#bbar').animate({ top: '+=44px' }, 500);
		$('#bbar').removeClass("hidden").addClass("showing");
	} else if ($(this).scrollTop() <= 300 && $('#bbar').hasClass('showing')) {
		$('#bbar').animate({ top: '-=44px' }, 500);
		$('#bbar').removeClass("showing").addClass("hidden");
		$("#bnot").slideUp("fast");
	}
});

function writeReview(id,user_id,whichbox) {
	if (whichbox == "fight_write") {
		var entry = $("#review_text").val();
	} else {
		var entry = $("#lf_review_text").val();
	}
	var el = entry.length;
	
	if (el > 1) {
		$.post("/ajax_write_review.php", { id: id, user: user_id, entry: entry },
		 function(data) {
			if (data == '0') {
				$("#"+whichbox).html('<h3 class="error">You need to log in to post a review.</h3>');
				$("#fightmenu a,#lfmenu a").removeClass("active");
				setTimeout('$("#playing div.submenu:visible,#lastfight div.submenu:visible").slideUp("fast");',3000);
			} else {
				$("#"+whichbox).html('<h3 class="error">Thanks for your review!</h3>');
				$("#fightmenu a,#lfmenu a").removeClass("active");
				setTimeout('$("#playing div.submenu:visible,#lastfight div.submenu:visible").slideUp("fast");',3000);
			}
		});
	} else {
		alert("You should probably write something before hitting Submit...try again.");
	}
}

function showV2Review(id,movie) {
	var d = new Date();
	var time = d.getTime();
	$("#fight_read").load("/aj_review.php?id="+id+"&movie="+movie+"&gibberish="+time);
}

function showLFReview(id,movie) {
	var d = new Date();
	var time = d.getTime();
	$("#lf_read").load("/aj_lfreview.php?id="+id+"&movie="+movie+"&gibberish="+time);
}

// play a video with AJAX because justin hates me

function playVideo(vidid) {
	// first reset all the play icons to their normal state
	$("img.playbutton").attr("src","/images/v2/vid_playbutton.png");
	// set the new video's play button to what it should be
	$("#playbutton"+vidid).attr("src","/images/v2/vid_now_playing.png");
	
	// load our video first
	var d = new Date();
	var time = d.getTime();
	$("#dyn_vid_area").load("/aj_playvid.php?id="+vidid+"&gibberish="+time, function() {
		$("#fightmenu a.read,#fightmenu a.write,#fightmenu a.info").click(function() {
			// which is active?
			var whichact = $("#fightmenu a.active").attr("class");
			// hide everything
			$("#fightmenu a").removeClass("active");
			$("#playing div.submenu:visible,#video div.submenu:visible").slideUp("fast");
			var which = $(this).attr("class");
			// show
			if (which == "read" && whichact != "read active") {
				$("#fight_read").slideDown("fast");
				$(this).addClass("active");
			} else if (which == "write" && whichact != "write active") {
				$("#fight_write").slideDown("fast");
				$(this).addClass("active");
			} else if (which == "info" && whichact != "info active") {
				$("#fight_info").slideDown("fast");
				$(this).addClass("active");
			}
			return false;
		});
	});
	
	return false;
}

function check_email(e) {
	var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
		}	
	} 

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}

function CountTitleChars() {
	var total_chars = 25;
	var txtbox = document.getElementById('vid_title');
	$("#vid_title").css('background-color','#ffffff');
	var txtlength = txtbox.value.length;
	if (txtlength > total_chars) {
		txtbox.value = txtbox.value.substring(0,total_chars);
	}
	var charstring = txtlength + "/25 characters";
	var charleftstring = (23-txtlength) + " char left";
	$('#titlechars').text(charstring);
	$('#titlecharleft').text(charleftstring);
}

function CountDescChars() {
	var total_chars = 124;
	var txtbox = document.getElementById('vid_desc');
	$("#vid_desc").css('background-color','#ffffff');
	var txtlength = txtbox.value.length;
	if (txtlength > total_chars) {
		txtbox.value = txtbox.value.substring(0,total_chars);
	}
	var charstring = txtlength + "/124 characters";
	var charleftstring = (100-txtlength) + " char left";
	$('#descchars').text(charstring);
	$('#desccharsleft').text(charleftstring);
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
