$(document).ready(function() {

	$("img.hover,input.hover").hover(function() {
		var curimage = $(this).attr("src");
		filelength = curimage.length;
		imagefilename = curimage.substring(7,filelength-4);
		var lastpart = imagefilename.substr(-4);
		if (lastpart == "over") {
			var newfilename = "/images/"+imagefilename+".png";
		} else {
			var newfilename = "/images/"+imagefilename+"_over.png";
		}
		$(this).attr("src",newfilename);
	}, function() {
		var curimage = $(this).attr("src");
		filelength = curimage.length;
		imagefilename = curimage.substring(7,filelength-4);
		var lastpart = imagefilename.substr(-4);
		if (lastpart == "over") {
			imagefilename = curimage.substring(7,filelength-9);
			var newfilename = "/images/"+imagefilename+".png";
		} else {
			var newfilename = "/images/"+imagefilename+"_over.png";
		}
		$(this).attr("src",newfilename);
	});
	
	$("#sponsor").click(function() {
		var whaturl = $(this).attr("rel");
		document.location.href=whaturl;
	});
	
	$('#searchterm').liveSearch({url: '/search.php?st='});

	$("img.bubble").hover(function() {
	var curpos = $(this).offset();
	var newleft = Math.ceil(curpos.left - 93);
	var newtop = Math.ceil(curpos.top - 107);
	$(this).next("em").css("top",newtop);
	$(this).next("em").css("left",newleft);
	$(this).next("em").stop(true, true).fadeIn("fast");
	}, function() {
	$(this).next("em").fadeOut("fast");
	});
	
	$("div.selectbox-wrapper").click(function() {
		var age = $("#sort_ages").val();
		var prize = $("#sort_prizes").val();
		var judge = $("#sort_judge").val();
		var locat = $("#sort_loc").val();
		var sort = $("#sort_sort").val();
		
		var cookieval = age+","+prize+","+judge+","+locat+","+sort;
		Set_Cookie('sort',cookieval,1440,'/','.onlinevideocontests.com');
		var newurl = "http://www.onlinevideocontests.com/page/1";
		document.location.href=newurl;
	});
	
	$("#resendconf").click(function() {
		var userid = $("#resendconf").attr("rel");
		if (!userid) return false;
		
		$.post("/ajax_resendconf.php", { id: userid });
		alert("Your confirmation email has been resent.");
	});
	
});

$(function() {
	$('a.lightbox').lightBox();
});

jQuery(document.body).click(function(event) {
	var clicked = jQuery(event.target);

	if (!(clicked.is('#searchbox') || clicked.is('#searchsubmit') || clicked.parents('#sresults').length || clicked.is('#searchterm'))) {
		$("#sresults").hide();
	}

});

function boxChecker(which) {
	var current = $("#"+which).attr("checked");
	if (current == true) {
		$("#"+which).attr("checked",false);
	} else {
		$("#"+which).attr("checked",true);
	}
}

// mailing list functions

function addMailList() {
	var embox = document.getElementById('nlemail');
	var email = embox.value;
	
	if (check_email(email)) {
	
		$.post("http://www.onlinevideocontests.com/addmail.php", { email: email });
		embox.value = "Thanks!";
		embox.disabled = true;
	} else {
		alert ("Please enter a valid email address.");
	}
}

function doSort() {
	// load values
	var age = $("#sort_ages").val();
	var prize = $("#sort_prizes").val();
	var judge = $("#sort_judge").val();
	var locat = $("#sort_loc").val();
	var sort = $("#sort_sort").val();
	
	var cookieval = age+","+prize+","+judge+","+locat+","+sort;
	Set_Cookie('sort',cookieval,1440,'/','.onlinevideocontests.com');
	var newurl = "http://www.onlinevideocontests.com/";
	document.location.href=newurl;
}

function cbwc(what) {
	$(what).slideUp('fast');
	// set a cookie so it doesn't show again
	
	var cookieval = "whatsthis";
	Set_Cookie('whatsthis',cookieval,43200,'/','.onlinevideocontests.com');
}

function cbwa(what,where) {
	$(what).slideUp('fast');
	
	$.post("/ajax_saveboxes.php", { field: where });
}

function hideBugBar() {
	$('#menuguest').slideUp('fast');
	var cookieval = 1;
	Set_Cookie('bugbar',cookieval,1440,'/','.onlinevideocontests.com');
}

function checkSearchVal() {
	var searchterm = $("#searchterm").val();
	if (searchterm == "Search contests") {
		$("#searchterm").val("");
	}
}
	
function posSearchRes() {
	var tmpOffset	= $("#searchterm").offset();
	var inputDim	= {
		left:		tmpOffset.left, 
		top:		tmpOffset.top, 
		width:		$("#searchterm").outerWidth(), 
		height:		$("#searchterm").outerHeight()
	};

	inputDim.topPos		= inputDim.top + inputDim.height;
	inputDim.totalWidth	= 275;

	$("#sresults").css({
		position:	'absolute', 
		left:		inputDim.left + 'px', 
		top:		inputDim.topPos + 'px',
		width:		inputDim.totalWidth + 'px'
	});
};

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" : "" );
}

function check_email(e) {
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 hideBub() {
	$("img.bubble").attr('src','/images/icon_question_off.png');
}

function showBub(what) {
	$(what).attr('src','/images/icon_question_on.png');
}