function searchField(){
	$('.searchField').focus(function(){
		if($(this).val() == 'Search the site'){
			$(this).val('');
		}
	});
	$('.searchField').blur(function(){
		if($(this).val().length == 0){
			$(this).val('Search the site');
		}
	});
}

function tables(){
	$('.tableStyle tr:even').addClass('even');
}

function seperate(){
	jQuery('#categorysurround ul.callouts li:nth-child(4n)').addClass('split').show();
}

function videos(){
	$('.videos li').each(function(){
		var imgSrc = $(this).find('img').attr('src');
		$(this).find('img').attr('src', '/static/images/video_overlay.png').css('background', 'url(' + imgSrc + ')');
	});
	$('.videos a').click(function(){
		var videoURL = $(this).attr('href');
		$('#tv').flash({swf:'/static/flash/video_player.swf', width:'656', height:'372', wmode:'transparent', expressInstaller:'/static/flash/expressInstall.swf', flashvars:{filesrc:videoURL}});
		$("html,body").stop().animate({
			scrollTop:($('#tv').offset().top - 20)
		},600);
		return false;
	});
	if($('#tv').size() > 0){
		$(window).load(function(){
			setTimeout(function(){
				if(($('.videos:first').offset().top - 40) > ($(window).height() + $("html,body").scrollTop())){
					$("html,body").stop().animate({
						scrollTop:($('.videos:first').offset().top - 40)
					},1300);
				}
			},200);
		});
	}
}

function addLinks(){
		setTimeout(function() {
        	$("ul#leftNav li a[href^='http://www.schuelke.com']").attr('target','_blank');
        }, 0);
}

function sortList(){
	$('<div class="column2"></div>').insertAfter('.column1');
	$('.column1 .item:gt(' + Math.floor($('.column1 .item').size() / 2) + ')').appendTo('.column2');
	$('#video').prependTo('.column2');
}

function removeRed(){
	$('.redBlock:empty').css('display','none');	
}

$(function(){
	sortList();
	addLinks();
	searchField();
	tables();
	videos();
	seperate();
	removeRed();
});

// COPYRIGHT WICKEDWEB LTD

function calendar() {
	doneAni = true;
	itemExists = 0;
	totalMonths = $("#calendar ul").size();
	var d = new Date();
	$("#calendar li:odd").css("background", "#F3F8FC");
	$("#calendar ul").hide();
	$("#calendar div").hide();
	$("#calendarNav").show();
	$(".month").hide();
	$("#calendar h2").addClass("open");
	
	$("#calendar li h2").click(function() {
		//$("#calendar div").not($(this).parent().find("div")).slideUp(400);
    	$(this).parent().find("div").toggle(400,function(){
			if($(this).parent().find("h2").attr("class") != "close"){
				$(this).parent().find("h2").addClass("close").removeClass("open");
			}
			else{
				$(this).parent().find("h2").removeClass("close").addClass("open");
			}
		});
  	});
	
	$("#calendar li h2").hover(function() {
		$(this).css("background-color", "");
	},function(){
		$(this).css("background-color", "");
	});
	
	monthArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	$("#calendar ul").each(
		function( intIndex ){
			if($(this).find(".month").html() == (monthArray[d.getMonth()] + " " + d.getFullYear()) || $(this).find(".month").html() == (monthArray[d.getMonth()] + " " + d.getFullYear() + " ")){
				$(this).show();
				currentMonth = (intIndex + 1);
				$("#monthTitle").html($(this).find(".month").html());
				if((intIndex + 1) == 1){
					$("#previousMonth").css("color", "#999999");
				}
				if((intIndex + 1) == totalMonths){
					$("#nextMonth").css("color", "#999999");
				}
				date = d.getDate();
				if(date > 0){
					if(date == 1 || date == 21 || date == 31){
						date = date + "st";
					}
					if(date == 2 || date == 22){
						date = date + "nd";
					}
					if(date == 3 || date == 23){
						date = date + "rd";
					}
					else{
						date = date + "th";
					}
				}
				searchItem = $(this).find("h2:contains(' " + date + "')").parent().find("div");
				if($(searchItem).size() != 0){
					//$(searchItem).show();
					$(searchItem).toggle(400);
					$(searchItem).parent().next("li").find("div").toggle(400);
					$(searchItem).parent().next("li").find("div").parent().next("li").find("div").toggle(400);
					$(searchItem).parent().find("h2").addClass("close").removeClass("open");
					$(searchItem).parent().next("li").find("div").parent().find("h2").addClass("close").removeClass("open");
					$(searchItem).parent().next("li").find("div").parent().next("li").find("div").parent().find("h2").addClass("close").removeClass("open");
				}
				if($(searchItem).size() == 0){
					itemToCheck = $(this);
					itemExists = 1;
				}
			}
		}
	);
	if(itemExists == 1){
		datesRemaining = (31 - d.getDate());
		$(itemToCheck).find("li").each(
			function( intIndex ){
				for(i=0; i<=datesRemaining; i++){
					date = d.getDate() + i;
					if(date > 0){
						if(date == 1 || date == 21 || date == 31){
							date = date + "st";
						}
						else if(date == 2 || date == 22){
							date = date + "nd";
						}
						else if(date == 3 || date == 23){
							date = date + "rd";
						}
						else{
							date = date + "th";
						}
					}
					if($(this).find("h2:contains(' " + date + "')").size() == 1){
						datesRemaining = -1;
						//$(this).find("h2:contains(' " + date + "')").parent().find("div").show();
						$(this).find("div").toggle(400);
						$(this).next("li").find("div").toggle(400);
						$(this).next("li").next("li").find("div").toggle(400);
						$(this).find("h2").addClass("close");
						$(this).next("li").find("h2").addClass("close");
						$(this).next("li").next("li").find("h2").addClass("close");
					}
				}
			}
		)
	}
	if($("#monthTitle").html() == ""){
		$("#calendar ul:first").show();
		currentMonth = 1;
		$("#monthTitle").html($("#calendar ul:first .month").html());
		//$("#calendar ul:first").find("div:first").show();
		$("#calendar ul:first").find("div:first").toggle(400);
		$("#previousMonth").css("color", "#999999");
	}
	$("#nextMonth").click(function(){
		if(doneAni == true){
			if(currentMonth < totalMonths){
				doneAni = false;
				currentMonth += 1;
				$("#calendar ul:nth-child(" + (currentMonth) + ")").slideUp(400,function(){
					$("#calendar ul:nth-child(" + (currentMonth + 1) + ")").slideDown(400);
					$("#monthTitle").html($("#calendar ul:nth-child(" + (currentMonth + 1) + ") .month").html());
					doneAni = true;
				});
				$("#previousMonth").css("color", "#FFFFFF");
			}
			if(currentMonth == totalMonths){
				$("#nextMonth").css("color", "#999999");
			}
		}
		return false;
	});
	
	$("#previousMonth").click(function(){
		if(doneAni == true){
			if(currentMonth > 1){
				doneAni = false;
				$("#calendar ul:nth-child(" + (currentMonth + 1) + ")").slideUp(400,function(){
					$("#calendar ul:nth-child(" + (currentMonth + 1) + ")").slideDown(400);
					$("#monthTitle").html($("#calendar ul:nth-child(" + (currentMonth + 1) + ") .month").html());
					doneAni = true;
				});
				currentMonth -= 1;
				$("#nextMonth").css("color", "#FFFFFF");
			}
			if(currentMonth == 1){
				$("#previousMonth").css("color", "#999999");
			}
		}
		return false;
	});
}
$(document).ready(calendar);

function calendarEvents() {
	/*$("#calendarEvents div div").hide();*/
	$("#calendarEvents div").find("p:first").css("padding-top","5px");
	$("#calendarEvents div").find("p:last").css("padding-bottom","10px");
	/*$("#calendarEvents div:first div").toggle(400);
	$("#calendarEvents h2").click(
		function() {
		$(this).parent().find("div").toggle(400); 
	});*/
}
$(document).ready(calendarEvents);

function hiddenInput(){
	if ($('.hidden').length > 0) {
		var text = $('.hidden').text();
		var input = $('input.hiddenUsername');
		input.val(text);
	}
}

$(document).ready(function(){
	hiddenInput();
})

