var SectionNumber;
var PageNumber;

$(document).ready(function(){

	$(".menutext").hover(function(){
			$(this).css({color: "#000000"});
		},
		function(){
			$(this).css({color: "#555454"});
		});

	$.ajaxSetup({
		url: "../includes/ajaxresponse.php",
		type: "POST"
		});
	
	arrangeContent ();
						   
	/*SectionNumber= 0;
	PageNumber = 0;
	
	SectionNumber = $("body").attr("currentsectionid");
	PageNumber = $("body").attr("pageid");
	
	if (!PageNumber)	{
	
	$.ajax
				({
					data: "place=pagenumberlookup&section="+SectionNumber,
					success: function(html)
					{
						PageNumber = html;
						makeLeftMenu();
						makeContent();
					}
				});
	}
	else {
	makeLeftMenu();
	makeContent();
	
	}
	$(".leftmenutext").livequery('click', function(){
		PageNumber=$(this).attr("pageid");
		makeContent();
	});*/
	
	$("#alumniform, #brochureform, #minorform").livequery('click', function(){
			
			if (!$("#privacy:checked").length) {
				alert ("You must check that you agree with our privacy policy to submit your information.");
				return;
			}
			place = "email" + $(this).attr("id");
			data = $("form").serialize();
			$.ajax
				({
					data: "place="+place+"&"+data,
					success: function(html)
					{
						alert ("Your information has been sent to the Croft Institute.");
						window.history.back();
					}
				});
			
			});
	
	$(".login").livequery('click', function(){
	
	$.ajax
				({
					data: "place=login&referringpage="+escape(document.URL),
					success: function(html)
					{
						window.location = 'http://www.olemiss.edu/apps?appid=1&appParam='+html; 
					}
				});							  
	});
	
	$(".summaryitem").livequery('click', function(){
		if ($(this).children(".full").css("display") == "block")	{
			$(this).children(".full").slideUp();
			$(this).children(".preview").slideDown();
		}
		else{
		$(".full").slideUp();
		$(".preview").slideDown();
		$(this).children(".full").slideDown();
		$(this).children(".preview").slideUp();
		}
	});
	
	$(".croftselector input").livequery('click', function(){
		
		makeCourses();
		makeTheses();
		if (this.checked!=0 && $(this).attr("type")!="radio"){
			$("#selectorlist").append('<div class="selector" value="'+$(this).val()+'">'+$(this).val()+' x </div>');
			}
		else {
			$(".selector[value='"+$(this).attr("value")+"']").remove();	
		}
		
	});

$(".print").livequery('click', function(){
	myWindow=window.open('../includes/print.php?section='+$("body").attr("currentsectionid")+'&page='+$("body").attr("pageid"),'Print','toolbar=no, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, width=1032');
	myWindow.focus();
	});
	
	$(".gallerypicture").livequery('click', function(){
		$(".zoomdiv").remove();		
		ZoomPicture = '<div class="zoomdiv"><img class="zoompicture" src="'+$(this).children("img").attr("src")+'" /><div class="zoomcaption">'+$(this).children(".caption").html()+'</div></div>';
		$(this).after(ZoomPicture);
		ZoomDiv = $(".zoomdiv")
		var cssPos = {
			'left' : $(this).position().left,
			'top' : $(this).position().top
		};
		ZoomDiv.css(cssPos);
		ZoomDiv.show();
		ZoomDiv.fadeTo(100, 0.33);
		var cssNewPos = {
			'left' : 500-(ZoomDiv.width()/2),
			'top' : $(window.self).scrollTop()-$(this).offsetParent().offset().top+25,
			'opacity' : 1.0
		};
		ZoomDiv.animate(cssNewPos, 500);
		});
	
	$(".zoomdiv").livequery('click', function(){
		$(this).remove();
											  });
	
	$(".selector").livequery('click', function(){
				$("input[value='"+$(this).attr("value")+"']").get(0).checked=0;	
				$(".selector[value='"+$(this).attr("value")+"']").remove();	
				makeCourses();	
				makeTheses();
	  });
	
	$(".inselector").livequery('click', function(){
				rightinput = $("input[value='"+$(this).text()+"']").get(0);							 
				if (rightinput.checked==0){												
				rightinput.checked=1;
				$("#selectorlist").append('<div class="selector" value="'+$(this).text()+'">'+$(this).text()+' x </div>');
				makeCourses();	
				makeTheses();
				}
	  });
	
	$(".croftexpander").livequery('click', function() {
	
	if ($(this).attr("toggle")=="collapsed") { 
                $(this).parent().animate({height:'100%'}, 1000); 
				$(this).attr("toggle", "expanded");
				$(this).text("contract");
			}
           else { 
                $(this).parent().animate({height:'125px'}, 500); 
				$(this).attr("toggle", "collapsed");
				$(this).text("expand");
            } 
 
								   });
	$(".croftclear").livequery('click', function() {
													
			$(this).siblings().children(":checked").each(function (){
				this.checked = 0;
				$(".selector[value='"+$(this).attr("value")+"']").remove();	
				});
			
			makeCourses();
			});
	
	});
	
	function makeLeftMenu()
		{
			$.ajax
				({
					data: "place=leftmenu&section="+SectionNumber,
					success: function(html)
					{
						//alert (html.substr(0,4));
						if (html.substr(0,4)!="<div"){
							$("#leftmenucontainer").replaceWith("<div class='leftmenu'><div class='leftmenutext'>Loading...</div></div>");
							$("#contentcontainer").replaceWith("");
							var t=setTimeout("window.location.reload()",3000);
							}
						$("#leftmenucontainer").replaceWith(html);
						
						var itemwidth=new Array();
						var itemcount=new Array();
						var extrawidth=new Array();
						$(".leftmenuitem, .leftmenudivider").each(function (){
						var position = $(this).position();		
						w = Math.floor((position.top>150?6:position.top) /34);
						itemwidth[w] = $(this).width()+(itemwidth[w]?itemwidth[w]:0);
						itemcount[w] = 1 + (itemcount[w]?itemcount[w]:0);
														  });
						
					for (y=0;y<itemwidth.length;y++){
							extrawidth[y] = Math.floor( (945-itemwidth[y]-(itemcount[y]*12))/itemcount[y] );
							//alert (itemwidth.length);
												  }
					
					$(".leftmenuitem, .leftmenudivider").each(function (){
						var position = $(this).position();													
						w = Math.floor((position.top>150?6:position.top) /34);
						$(this).css({marginLeft:(extrawidth[w]/2), marginRight:(extrawidth[w]/2)});
														  });
					
					$(".menubaritem[sectionid='"+$("body").attr("currentsectionid")+"']").css({backgroundColor: "#f2f1ef", border: "1px solid #dfd7cf", borderBottom: "none"  });
					
					$(".leftmenuitem[pageid='"+$("body").attr("pageid")+"']").css({backgroundColor: "#ffffff", border: "1px solid #dfd7cf"});
						
					}
				
				
				
				});
		}
	
	function makeContent()
		{
			
			$.ajax
				({
					data: "place=makeContent&page="+PageNumber,
					success: function(html)
					{
						$("#contentcontainer").replaceWith(html);
						$(".leftmenuheader").html("<a href='"+$(".menubaritem[sectionid="+$("body").attr("currentsectionid")+"] a").attr("href")+"'>"+$(".menubaritem[sectionid="+$("body").attr("currentsectionid")+"]").text())+"</a>";
						$(".leftnormalblocktext, .leftnormalblockpicture, .leftwideblockpicture, .leftnarrowblockpicture, .leftwideblocktext, .leftnarrowblocktext, .facultylistwrapper, .croftselectorwrapper, .leftsummary, .leftfile").each(function (i) {
							$(this).addClass("goesleftblock");
							});
							$(".goesleftblock").appendTo("#leftblock");
						$(".leftnormalblocktext, .leftnormalblockpicture, .leftwideblockpicture, .leftnarrowblockpicture, .leftwideblocktext, .leftnarrowblocktext, .facultylistwrapper, .croftselectorwrapper, .leftsummary, .leftfile").each(function (i) {
							$(this).removeClass("goesleftblock");
							});	
						
						$(".middlenormalblocktext, .middlenormalblockpicture, .middlewideblockpicture, .middlenarrowblockpicture, .middlewideblocktext, .middlenarrowblocktext, .courseheader, .courses, #selectorlist, .middlefile").each(function (i) {
							$(this).addClass("goesmiddleblock");
							});
							$(".goesmiddleblock").appendTo("#middleblock");
						$(".middlenormalblocktext, .middlenormalblockpicture, .middlewideblockpicture, .middlenarrowblockpicture, .middlewideblocktext, .middlenarrowblocktext, .courseheader, .courses, #selectorlist, .middlefile").each(function (i) {
							$(this).removeClass("goesmiddleblock");
							});	
							
						$(".rightnormalblocktext, .rightnormalblockpicture, .rightwideblockpicture, .rightnarrowblockpicture, .rightwideblocktext, .rightnarrowblocktext,  .rightsummary, .rightfile").each(function (i) {
							$(this).addClass("goesrightblock");
							});
							$(".goesrightblock").appendTo("#rightblock");
						$(".rightnormalblocktext, .rightnormalblockpicture, .rightwideblockpicture, .rightnarrowblockpicture, .rightwideblocktext, .rightnarrowblocktext,  .rightsummary, .rightfile").each(function (i) {
							$(this).removeClass("goesrightblock");
							});
							
						$(".eventdynamic").each(function (i) {
							$(this).addClass("goesevents");
							});
							$(".goesevents").appendTo("#events");
						$(".eventdynamic").each(function (i) {
							$(this).removeClass("goesevents");
							});
						
						$(".newsdynamic").each(function (i) {
							$(this).addClass("goesnews");
							});
						$(".goesnews").appendTo("#news");
						$(".newsdynamic").each(function (i) {
							$(this).removeClass("goesnews");
							});
							
					}
				});
			
		}
function makeCourses (){
	i=0;
	j=0;
	k=0;
	l=0;
		Where="";
		
		if ($(".croftselector[category='instructor'] input[type=checkbox]:checked").length>0) {
		Where = "LEFT JOIN facultylookup ON courses.CoursesID = facultylookup.coursesID ";	
		}
		
		$(".croftselector[category='requirements'] input[type=checkbox]:checked").each(function (){																						
			Where = i>0?Where+" OR ":Where + "WHERE (";
			Where = Where + "courses." + $(this).val() + "=1";
			i++;
		});
		Where = i>0?Where + ")":Where;
		$(".croftselector[category='instructor'] input[type=checkbox]:checked").each(function (){
			if (j==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = j>0?Where+" OR ":Where;
			Where = Where +"facultylookup.Instructor='"+ $(this).val() + "'";
			j++;
			i++
		});
		Where = j>0?Where+")":Where;
		$(".croftselector[category='schedule'] input[type=checkbox]:checked").each(function (){
			if (k==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = k>0?Where+" OR ":Where;
			Where = Where + "courses.Schedule" + "='" + $(this).val() + "'";
			k++;
			i++
		});
		Where = k>0?Where+")":Where;
		$(".croftselector[category='language'] input[type=checkbox]:checked").each(function (){
			if (l==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = l>0?Where+" OR ":Where;
			Where = Where + "courses.Language" + "='" + $(this).val() + "'";
			l++;
			i++
		});
		Where = l>0?Where+")":Where;
			
		
		$(".croftselector[category='term'] input[type=radio]:checked").each(function (){																						
			Where = i>0?Where+" AND ":Where + "WHERE ";
			Where = Where + "courses.Term" + "='" + $(this).val() + "'";
			i++;
		});
		Where = Where + " ";
		Where = Where == "WHERE  "?"":Where;
		$.ajax
				({
					data: "place=courseselection&where="+Where,
					success: function(html)
					{
						if (html.substr(0,4)!="<div"){
							$(".courses").replaceWith("<div class='courses'>Loading...</div>");
							return 2;
							}
						$(".courses").replaceWith(html);
					}
				});
	}
	
		
function makeTheses (){
	i=0;
	j=0;
	k=0;
	l=0;
	m=0;
	n=0;
		Where="";
		
		$(".croftselector[category='year'] input[type=checkbox]:checked").each(function (){																						
			Where = i>0?Where+" OR ":Where + "WHERE (";
			Where = Where +"theses.Year='"+ $(this).val() + "'";
			i++;
		});
		Where = i>0?Where + ")":Where;
		$(".croftselector[category='author'] input[type=checkbox]:checked").each(function (){
			if (j==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = j>0?Where+" OR ":Where;
			Where = Where +"theses.Author='"+ $(this).val() + "'";
			j++;
			i++
		});
		Where = j>0?Where+")":Where;
		$(".croftselector[category='region'] input[type=checkbox]:checked").each(function (){
			if (k==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = k>0?Where+" OR ":Where;
			Where = Where + "theses.Region" + "='" + $(this).val() + "'";
			k++;
			i++
		});
		Where = k>0?Where+")":Where;
			
		$(".croftselector[category='theme'] input[type=checkbox]:checked").each(function (){
			if (l==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = l>0?Where+" OR ":Where;
			Where = Where + "theses.Theme" + "='" + $(this).val() + "'";
			l++;
			i++
		});
		Where = l>0?Where+")":Where;
			
		$(".croftselector[category='type'] input[type=checkbox]:checked").each(function (){
			if (m==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = m>0?Where+" OR ":Where;
			Where = Where + "theses.Type" + "='" + $(this).val() + "'";
			m++;
			i++
		});
		Where = m>0?Where+")":Where;
			
		$(".croftselector[category='prize'] input[type=checkbox]:checked").each(function (){
			if (n==0) {
			Where = (i>0)?Where+" AND (":Where + "WHERE (";
			}
			Where = n>0?Where+" OR ":Where;
			Where = Where + "theses.Prize" + "='" + $(this).val() + "'";
			n++;
			i++
		});
		Where = n>0?Where+")":Where;	
			
		Where = Where + " ";
		Where = Where == "WHERE  "?"":Where;

		$.ajax
				({
					data: "place=thesisselection&where="+Where,
					success: function(html)
					{
						$(".theses").replaceWith(html);
					}
				});
	}
	
		
function arrangeContent () {

$(".leftmenuheader").html("<a href='"+$(".menubaritem[sectionid="+$("body").attr("currentsectionid")+"] a").attr("href")+"'>"+$(".menubaritem[sectionid="+$("body").attr("currentsectionid")+"]").text())+"</a>";

$(".leftnormalblocktext, .leftnormalblockpicture, .leftwideblockpicture, .leftnarrowblockpicture, .leftwideblocktext, .leftnarrowblocktext, .facultylistwrapper, .croftselectorwrapper, .leftsummary, .leftfile").each(function (i) {
$(this).addClass("goesleftblock");
							});
							$(".goesleftblock").appendTo("#leftblock");
						$(".leftnormalblocktext, .leftnormalblockpicture, .leftwideblockpicture, .leftnarrowblockpicture, .leftwideblocktext, .leftnarrowblocktext, .facultylistwrapper, .croftselectorwrapper, .leftsummary, .leftfile").each(function (i) {
							$(this).removeClass("goesleftblock");
							});	
						
						$(".middlenormalblocktext, .middlenormalblockpicture, .middlewideblockpicture, .middlenarrowblockpicture, .middlewideblocktext, .middlenarrowblocktext, .courseheader, .courses, .thesisheader, .theses, #selectorlist, .middlefile").each(function (i) {
							$(this).addClass("goesmiddleblock");
							});
							$(".goesmiddleblock").appendTo("#middleblock");
						$(".middlenormalblocktext, .middlenormalblockpicture, .middlewideblockpicture, .middlenarrowblockpicture, .middlewideblocktext, .middlenarrowblocktext, .courseheader, .courses, .thesisheader, .theses, #selectorlist, .middlefile").each(function (i) {
							$(this).removeClass("goesmiddleblock");
							});	
							
						$(".rightnormalblocktext, .rightnormalblockpicture, .rightwideblockpicture, .rightnarrowblockpicture, .rightwideblocktext, .rightnarrowblocktext,  .rightsummary, .rightfile").each(function (i) {
							$(this).addClass("goesrightblock");
							});
							$(".goesrightblock").appendTo("#rightblock");
						$(".rightnormalblocktext, .rightnormalblockpicture, .rightwideblockpicture, .rightnarrowblockpicture, .rightwideblocktext, .rightnarrowblocktext,  .rightsummary, .rightfile").each(function (i) {
							$(this).removeClass("goesrightblock");
							});
							
						$(".eventdynamic").each(function (i) {
							$(this).addClass("goesevents");
							});
							$(".goesevents").appendTo("#events");
						$(".eventdynamic").each(function (i) {
							$(this).removeClass("goesevents");
							});
						
						$(".newsdynamic").each(function (i) {
							$(this).addClass("goesnews");
							});
						$(".goesnews").appendTo("#news");
						$(".newsdynamic").each(function (i) {
							$(this).removeClass("goesnews");
							});
						$(".gallerypicture").each(function (i) {
							$(this).addClass("goesgallery");
							});
						$(".goesgallery").appendTo("#gallery");
						$(".gallerypicture").each(function (i) {
							$(this).removeClass("goesgallery");
							});
						
						var itemwidth=new Array();
						var itemcount=new Array();
						var extrawidth=new Array();
						$(".leftmenuitem, .leftmenudivider").each(function (){
						var position = $(this).position();		
						w = Math.floor((position.top>150?6:position.top) /34);
						itemwidth[w] = $(this).width()+(itemwidth[w]?itemwidth[w]:0);
						itemcount[w] = 1 + (itemcount[w]?itemcount[w]:0);
														  });
						
					for (y=0;y<itemwidth.length;y++){
							extrawidth[y] = Math.floor( (945-itemwidth[y]-(itemcount[y]*12))/itemcount[y] );
							//alert (itemwidth.length);
												  }
					
					$(".leftmenuitem, .leftmenudivider").each(function (){
						var position = $(this).position();													
						w = Math.floor((position.top>150?6:position.top) /34);
						$(this).css({marginLeft:(extrawidth[w]/2), marginRight:(extrawidth[w]/2)});
														  });
					
					$(".menubaritem[sectionid='"+$("body").attr("currentsectionid")+"']").css({backgroundColor: "#f2f1ef", border: "1px solid #dfd7cf", borderBottom: "none"  });
					
					$(".leftmenuitem[pageid='"+$("body").attr("pageid")+"']").css({backgroundColor: "#ffffff", border: "1px solid #dfd7cf"});
					
}
