$(document).ready(function() {
	// add the top and bottom <b> tags to the mod
	
	$('.mod').prepend('<b class="top"><b class="tl"></b><b class="tr"></b></b>');
	$('.mod').append('<b class="bottom"><b class="bl"></b><b class="br"></b></b>');
	
	
	//Makes the footer positioned below all the content, regardless of absolute positioning.
	if($("#contentCenter"))
	{
		var height = $("#contentCenter").height();
		var tallest = $("#contentRight").height() > $("#contentLeft").height() ? $("#contentRight").height() : $("#contentLeft").height();
		if(tallest > height)
			$("#footer").css("top", tallest - height - 12);
	}
	if($("#content2Right"))
	{
		var height = $("#content2Left").height();
		var tallest = $("#content2Right").height();
		if(tallest > height)
			$("#footer").css("top", tallest - height - 12);
	}
	
	//Hovering over DDL items...
	$("div.ddl li").hover(
		function() { $(this).css("background-color", "dddddd"); },
		function() { $(this).css("background-color", "ffffff"); }
	);	
	$("div.ddlNav li").hover(
		function() { $(this).css("background-color", "2f3126"); },
		function() { $(this).css("background-color", "575b44"); }
	);

	//Advertiser website drop down list
	$("#headerLoginWebsiteArea").hover(hoverIn, hoverOut).hover(
		function() { $(this).children("img").css("height", 6); },
		function() {}
	);
	
	//Handles rollovers for all images with the .hover class.
	//Image names formatted as such: name.gif, name_selected.gif, ro_name.gif
	$("img.hover").hover(
		function() {
			if($(this).attr("src").indexOf("_selected") == -1)
			{
				var path = $(this).attr("src").substr(0, $(this).attr("src").lastIndexOf("/") + 1);
				var name = $(this).attr("src").substr($(this).attr("src").lastIndexOf("/") + 1);
				$(this).attr("src", path + "ro_" + name);
			}
		},
		function() {
			if($(this).attr("src").indexOf("_selected") == -1)
				$(this).attr("src", $(this).attr("src").replace("/ro_", "/"));
		}
	);
	
	//Main navbar drop down menus.
	$("#headerNavBar li.ddlNav, .dropDownMenu").hover(hoverIn, hoverOut);
	
	//Handling help text... any img element with the .altText class will have its alt text shown in the box.
	$("img.altText").hover(helpIn, helpOut);
	
	//Select All, Deselect All
	$("input.checkboxSelectAll").click(function() {
		if(this.checked)
			$(this).parents("table.data:first").find("input.checkboxIndividual").attr("checked", "checked");
		else
			$(this).parents("table.data:first").find("input.checkboxIndividual").removeAttr("checked");
	});
	$("input.checkboxSelectAllTotal").click(function() {
		if(this.checked)
			$(this).parent().parent().find("table.data:first").find("input[@type='checkbox']").attr("checked", "checked");
		else
			$(this).parent().parent().find("table.data:first").find("input[@type='checkbox']").removeAttr("checked");
	});
	$(".checkboxSelectAll").click(function() {
		$(this).parent().parent().find("table.data:first").find("input[@type='checkbox']").attr("checked", "checked");
	});
	$(".checkboxDeselectAll").click(function() {
		$(this).parent().parent().find("table.data:first").find("input[@type='checkbox']").removeAttr("checked");
	});
	//Indivudual checkbox selected, toggle Select All checkbox or not...
	$("input.checkboxIndividual").click(function() {
		if(!this.checked)
			$(this).parents("table.data:first").find("input.checkboxSelectAll").removeAttr("checked");
		/*
		else if(this.checked)
		{
			var allChecked = true;
			for(var i = 0; i < $(this).parents("table.data:first").find("input.checkboxIndividual").length; i++)
			{
				if(!$(this).parents("table.data:first").find("input.checkboxIndividual").eq(i).attr("checked"))
					allChecked = false;
			}
			if(allChecked)
				$(this).parents("table.data:first").find("input.checkboxSelectAll").attr("checked", "checked");
		}
		*/
	});
	//Delete button to delete selected checkboxs
	$(".checkboxDelete").click(function() {
		var checked = true;
		for(var i = 0; i < $("input.checkboxIndividual").length; i++)
		{
			if($("input.checkboxIndividual").eq(i).attr("checked"))
			{
				$("input.checkboxIndividual").eq(i).parent().parent().remove();
				i = i - 1;
			}
		}
	});
	//Individual delete link on each row.
	$(".checkboxIndividualDelete").click(function() {
		$(this).parent().parent().parent().remove();
	});
});

//Handles DDL hovers...
function hoverIn() {
	$(this).append($("<div></div>").addClass("ddlGeneratedDiv").css({ width: $(this).width(), height: $(this).children("div.ddl:first").height() }).css({top:$(this).offset().top + $(this).height(), left:$(this).offset().left}));
	$(this).children("div.ddl:first").css({top:$(this).offset().top + $(this).height(), left:$(this).offset().left});
	$(this).children("div.ddl:first").show("fast");
}

//Handles DDL hovers...
function hoverOut() {
	$(this).children("div.ddl:first").hide("fast");
	$(this).children("div.ddlGeneratedDiv").remove();
}

//Help message Hover In
function helpIn() {
	$("body").append($("<div></div>").addClass("helpMessage").hide().text($(this).attr("alt")));
	$(this).attr("alt", "");
	if($(".helpMessage").width() > 350)
		$(".helpMessage").width(350);
	var left = $(this).offset().left;
	if((left + $(".helpMessage").width()) > $(document).width())	//In case popup would go outside the window size, reposition it.
		left = $(document).width() - $(".helpMessage").width() - 50;
	$(".helpMessage").css({top:$(this).offset().top + 20, left:left + 20}).show("fast");
}

//Help message Hover Out
function helpOut() {
	$(this).attr("alt", $(".helpMessage").text());
	$(".helpMessage").remove();
}

//Popup creator
function popup(url, width, height)
{
	window.open(url, "_new", "width="+width+",height="+height+",status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function getSelectedElement(e) {
	// setup the selElement
	if (e.srcElement) {
		return e.srcElement; // IE
	} else {
		return e.currentTarget;
	}
}

function ucfirst(str) {
    var firstLetter = str.substr(0, 1);
    return firstLetter.toUpperCase() + str.substr(1);
}

//return cookie value by name
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

