// JavaScript Document
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();
function popPreview(imgid,capvar,vadjust) {
	document.getElementById('preview_window').innerHTML = '<span>'+capvar+'</span><img src="/cutie2010/uploads/'+imgid+'" alt="" width="350"'+(vadjust?' style="margin-top:-'+vadjust+'px;"':'')+' /><div></div>';
	document.getElementById('preview_window').style.visibility = 'visible';
}
function popGoAway() {
	document.getElementById('preview_window').style.visibility = 'hidden';
	document.getElementById('preview_window').innerHTML = '';
}
function whatSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function check_form() {
	var emailFilter=/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var phoneFilter=/\(?[0-9]{3}\)?[ -]?[0-9]{3}-?[0-9]{4}/;
	var ageFilter=/[0-9]+/;
	var myerror = '';
	if(document.forms[0].name.value.length < 5) {
		myerror+="Your first and last name.\r\n";
		$("input[name=name]").css("background","url(images/input_text_red.gif) center center no-repeat");
	}
	if (!(emailFilter.test(document.forms[0].email.value))) { 
		myerror+="A valid email address.\r\n";
		$("input[name=email]").css("background","url(images/input_text_red.gif) center center no-repeat");
	} else if (document.forms[0].email.value=='') {
		myerror+="A valid email address.\r\n";
		$("input[name=email]").css("background","url(images/input_text_red.gif) center center no-repeat");
	}
	if(!(phoneFilter.test(document.forms[0].phone.value))) {
		myerror+= "A valid phone number.\r\n";
		$("input[name=phone]").css("background","url(images/input_text_red.gif) center center no-repeat");
	}
	if (document.forms[0].reader_photo.value=='') {
		myerror+="Attached photograph.\r\n";
		$("#photo_field").css("background","#ffc5c5");
	}
	if(myerror.length > 0) {
		alert("Please provide the following information:\r\n\r\n"+myerror);
	} else {
		document.forms[0].submit();
	}
	return(false);
}
function newReq(action,mytype) {
    http.open('get', 'elements/vote.php?action='+(action?action:""));
	http.onreadystatechange = function() {
		if(http.readyState == 4) {
			var response = http.responseText;
			$("#loadingtxt").html("");
			$("#loadingtxt").css("padding" , "25px 15px");
			if(response!="Okay") {
				$("#loadingtxt").html(response);
			} else {
				$("#loadingtxt").html("Okay, we have your vote"+(imagecaption[action]?" for "+imagecaption[action]:"")+".<br /><br />There is no vote limit, so be sure to vote again!");
			}
			setTimeout('window.location.reload();',3000);
//			setTimeout('$("#loadingtxt").hide(); $("#loading").hide();',2500);
		}	
	}
    http.send(null);
}

var mytimeout;
$(document).ready(function() {
	$(".votebuttons").show('slow');
	$("#deadline").mouseover(function () {
		$("#faq_div").show("slow");
	});
	$("#faq_div").bind("clickoutside", function(event) {
		$("#faq_div").hide("slow");
	});
	$("#close_link").click(function () {
		$("#faq_div").hide("slow");
		clearTimeout(mytimeout);
	});
	$("#reader_photos img.thepic").mouseover(function () {
		var currentId = $(this).attr('id').substring(12);
		popPreview(imagefile[currentId],imagecaption[currentId],imagevalign[currentId]);
	});
	$("#reader_photos img.thepic").mouseout(function () {
		popGoAway();
	});
	$("a").click(function() {
		window.open(this.href,'_blank');
		return false;
	});
	$("body").mousemove(function(event) {
		d=document.getElementById("preview_window").style;
		y=event.clientY;
		x=event.clientX;
		narrowMe=document.getElementById('narrowme');
		scrollOffset = getScrollXY();
		mySize=whatSize();
		d.top = (y + 5 + 350 > mySize[1]? y - 350 + scrollOffset[1] : y + 5 + scrollOffset[1]) + "px";
		d.left = (x + 5 + 450>mySize[0]? x - 400 - 25 : x + 10) + "px";
	});
	$(".votebuttons").click(function() {
		tempvar=$(this).attr('id').substring(4);
//		var answer = confirm  ("Vote for this image"+(imagecaption[tempvar]?" of "+imagecaption[tempvar]:"")+"?");
//		if (answer) {
			$("#loading").show();
			$("#loadingtxt").show();
			newReq(tempvar);
//		}
	});
});
