$(document).ready(function() {
	if($("input[type=text]").attr("placeholder") === undefined) {
		// placeholder attribute NOT selected, so emulate it
		$("input[type=text]").focus(function() {
			$(this).removeClass("inputPlaceholder");
			if(this.value == this.title) {
				this.value = "";
			}
		}).blur(function() {
			if(this.value == "") {
				$(this).addClass("inputPlaceholder");
				this.value = this.title;
			} else {
				$(this).removeClass("inputPlaceholder");
			}
		}).blur().parents("form").submit(function() {
			$(this).find("input").each(function(){
				if(this.title == this.value) {
					this.value = "";
				}
				return true;
			});
		});
	} else {
		// placeholder attribute selected
		$("input[type=text][title]").each(function() {
			this.placeholder = this.title;
			this.title = "";
		})
	}
});


/* Navigation */
$(function() {
	var timeout = false;
	$("#nav ul:first > li:not(.submenu)").mouseenter(function() {
		clearTimeout(timeout);
		$("#nav ul .dropdown").hide();
		var id = $(this).attr("rel");
		var ul = $("#nav li.submenu[rel="+id+"]")
		ul.not(".activated").addClass("dropdown").css({
			top: $(this).position()['top'],
			left: $(this).width() + $(this).position()['left']
		}).each(function() {
			if($(this).find("ul > li").size() > 0) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
		
		if(ul.children().size() == 0) {
			ul.hide();
		}
	});
	$("#nav ul:first").mouseleave(function() {
		clearTimeout(timeout);
		timeout = setTimeout(function() {
			$("#nav ul .dropdown").hide();
		},500);
	}).mouseenter(function() {
		clearTimeout(timeout);
	})
});

function newsletter() {
	var name = document.getElementsByName("fullname")[0]
	var fname = document.getElementsByName("cm-name")[0]
	var lname = document.getElementsByName("cm-name2")[0]
	fname.name = "cm-name1";
	name.name = "cm-name";
	
	name.value = fname.value +" "+ lname.value;
	return true;
}

var homepage = {
	'recip' : [],
	'timeout' : null,
	'displayPeriod' : 4,
	'index' : -1,
	'container' : "#mediapanel",
	'init' : function(recipients) {
		homepage.recip = recipients;
		$(homepage.container).mouseover(homepage.halt)
		$(homepage.container).mouseout(homepage.go)
		for(var m in homepage.recip) {
			$("<img>").attr("src",homepage.recip[m].image);
		}
		homepage.next(true);
	},
	'go' : function() {
		homepage.timeout = setTimeout(homepage.next,homepage.displayPeriod*1000);
	},
	'halt' : function() {
		clearTimeout(homepage.timeout);
	},
	'next' : function(instant) {
		clearTimeout(homepage.timeout);
		homepage.index = (homepage.index+1<homepage.recip.length) ? homepage.index+1 : 0;
		if(instant) {
			$(homepage.container).empty();
		}
		var block = $(homepage.container).append("<a class='link'><img /></a>").children("a:last").hide();
		block.find("img").attr("src",homepage.recip[homepage.index].image).attr("title",homepage.recip[homepage.index].title);
		var link = homepage.recip[homepage.index].link ? homepage.recip[homepage.index].link : "/recipients/?m="+homepage.recip[homepage.index].pageID
		block.attr("href",link).attr("title",homepage.recip[homepage.index].title);
		if(instant) {
			block.show();
			homepage.go();
		} else {
			block.fadeIn("slow",function() {
				$(homepage.container).children().not("a:last").remove();
				homepage.go();
			});
		}
	}
}
$(document).ready(function() {
	homepage.init([
		{
			'image' : '/graphics/recipients/fionaWoods.gif',
			'title' : "Dr Fiona Wood",
			'pageID' : "fiona-wood"
		},
		{
			'image' : '/graphics/recipients/graemeDrew.gif',
			'title' : "Graeme Drew",
			'pageID' : "graeme-drew"
		},
		{
			'image' : '/graphics/recipients/jontyBush.gif',
			'title' : "Jonty Bush",
			'pageID' : "jonty-bush"
		},
		{
			'image' : '/graphics/recipients/mickDodson.gif',
			'title' : "Mick Dodson",
			'pageID' : "michael-dodson"
		},
		{
			'image' : '/graphics/recipients/patLamanna.gif',
			'title' : "Pat LaManna",
			'pageID' : "pat-lamanna"
		},
		{
			'image' : '/graphics/recipients/slimDusty.gif',
			'title' : "Slim Dusty",
			'pageID' : "slim-dusty"
		}
	]);
});
