// Homepage image rotator settings

// Set rotation speed - this is the amount of time spent on each image before it rotates (in seconds)
var rotationSpeed = 8;

// Set transition speed - this is the amount of time it takes to transition from one image to the next (in seconds)
var transitionSpeed = 1.5;

$(document).ready(function() {	
	// Initialize image rotation and apply additional settings 
	$('#images').before('<div id="img-nav">').cycle({ 
		fx:     'fade', 
		speed:  transitionSpeed * 1000, 
		timeout: rotationSpeed * 1000, 
		pager:  '#img-nav',
		pause: true,
		before: onAfter 
	});
	
	var captions = $("#captions").find("span");
	var captionheight = 0;
	for (var i = 0; i < captions.length; i++) {
		if ($("#" + captions[i].id).height() > captionheight) {
			captionheight = $("#" + captions[i].id).height();
		}
	}
	$("#captions").height(captionheight);
	
	function onAfter() { 
	  $("#captions span").fadeOut();
	  $("#captions span#caption" + this.id).fadeIn();
	} 
// End home page image rotation code

	$("#login input").focus(function() {
		if (this.id != "submit") {
			$(this).css("background-position", "-900px");
		}
	});
	$("#login input").blur(function() {
		if (this.value == "" && this.id != "submit") {
			$(this).css("background-position", "center");
		}
	});
	
	$("#regionalName").focus(function() {
        if ($(this).val() == "Name") {
            $(this).val("");
        }
	});
	$("#regionalName").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Name");
        }
    });
	$("#regionalEmail").focus(function() {
        if ($(this).val() == "Enter Email Address") {
            $(this).val("");
        }
	});
	$("#regionalEmail").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Enter Email Address");
        }
    });
	
	$("#submit-poll").click(function() {
		if (getRadioCheckedValue("poll")) {
		$(".poll form").css("min-height", $(".poll form").height());
		$.ajax({
			type: "POST",
			url: $(".poll form").attr("action") + "vote.php",
			data: "vote=" + getRadioCheckedValue("poll") + "&pollid=" + $("#pollid").val(),
			success: function(html) {
				$(".poll form").fadeOut("normal",
					function() {
						if (html != "Error: Could not complete your vote.") {
							createCookie("voted","true",1);
						}
						$(".poll form").html(html); 
						$(".poll form").fadeIn("normal",
							function() {
								var arrResults = new Array();
								arrResults = $(".poll form").find("p");
								for (var j = 0; j < arrResults.length; j++) {
									$("#bar" + j).css("opacity", "0");
									$("#bar" + j).animate({
										width: $("#percent" + j).html(),
										opacity: 1
									}, "slow");	
								}
							}
						);
					}
				);
			}
		});
		}
		return false;
	});
	
	if ($("#alumni-news").length) {
		loadReader("http://www.sandiego.edu/rss/", "alumni-news");
	}
	$(".news ul li a").click(function() {
		var url = "";
		if (this.id == "alumni") {
			url = "http://www.sandiego.edu/rss/";
		} else if (this.id == "inside-usd") {
			url = "http://www.sandiego.edu/insideusd/?feed=rss2";
		} else {
			url = "http://usdtoreros.cstv.com/headline-rss.xml";
		}
		loadReader(url, this.id + "-news");
		$(".news ul li a").removeClass("current");
		$(this).addClass("current");
		$(".news div div").css("display", "none");
		$("#" + this.id + "-news").css("display", "block");
		return false;
	
	});
	
	
	// Featured Alumni Admin pages
	$("#title").blur(function() {
		/*if ($("#slug input").val() == "" && this.value.length > 0) {
			$("#slug").slideDown();
			formatSlug(this.value);
		}*/
		if (this.value.length > 0) {
			formatSlug(this.value);
		}
	});
	$("#slug a").click(function() {
		if ($(this).html() == "Edit") {
			$("#slug span").css("display", "none");
			$("#slug input").css("display", "inline");
			$(this).html("Save")
		} else {
			$("#slug span").css("display", "inline");
			$("#slug input").css("display", "none");
			formatSlug($("#slug input").val());
			$(this).html("Edit");
		}
		return false;
	});
	$("#image a").click(function() {
		if ($(this).html() == "Change") {
			$("#image input").css("display", "inline");
			$("#image img").css("display", "none");
			$(this).html("Cancel");
		} else {
			$("#image input").css("display", "none");
			$("#image img").css("display", "inline");
			$(this).html("Change");
		}
		return false;
	});

	$("#jqGSContainer").jqGalScroll();
	
	$("div.school").css("display", "none");
	
	$("a.school-headline").click(function() {
		$("#" + this.id + "-links").slideToggle();
		$(this).toggleClass("school-headline-down");
		return false;
	});

});

function loadReader(url, id) {
	if ($("#" + id).html().length < 1) {
		$("#news-items").height($("#news-items").height());
		$("#" + id).html('<img src="images/ajax-loader.gif" />');
		$.ajax({
			type: "POST",
			url: "feed-reader/reader.php",
			data: "url=" + url,
			success: function(html) {
				$("#" + id).html(html);
				$("#news-items").css("height", "auto");
			}
		});
	}
}
	
function getRadioCheckedValue(radio_name) {
	//var oRadio = document.forms[1].elements[radio_name];
	var oRadio = $(".poll form input[name='" + radio_name + "']");
	for(var i = 0; i < oRadio.length; i++) {
		if(oRadio[i].checked) {
			return oRadio[i].value;
		}
	}
	return '';
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


var fields = 1;
function addField(field) {
	if (field.value) {
		var arrOptions = new Array();
		arrOptions = $("#options p").children(".option-field");
		var count = 0;
		for (i = 0; i < arrOptions.length; i++) {
			if (arrOptions[i].value == "") {
				count += 1;
			}
		}
		if (count == 0) {
			$("#options").append('<p id="' + fields + '"><input type="text" class="option-field" name="option[]" onblur="addField(this)" />\n<input type="text" name="votes[]" value="0" /> <a href="#" onclick="removeOption(' + fields + '); return false;">Remove</a></p>');
			$("#options #" + (fields - 1) + " a").css("display", "inline");
			fields++;
		}
	}
}

function removeOption(field) {
	$("#" + field).html("");
	$("#" + field).css("display", "none");
}

function formatSlug(val) {
		var slug = val.toLowerCase();
		var regex=/[^a-zA-Z0-9_\s\-]/g;
		slug = slug.replace(regex, "");
		slug = slug.replace(/^\s+|\s+$/g, "")
		slug = slug.replace(/ /g, "-");
		//$("#slug span").html(slug);
		$("#slug").val(slug);
}

function OpenPopup (URL,wd,ht) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=' + wd + ',height=' + ht + ',left = 250,top = 100');");
}


