query = jQuery.noConflict();
query(document).ready(init);

Cufon.replace(
	"h1, h2, h3, h4, #side-area .interactive",
	{ hover : true }
);

function init() {
	var menus = query("#site-nav .page");
	
	menus.hover(function() {
		var menu     = query(this),
		    children = menu.find(".children");
		
		if (children.html().length != 0)
			query(this).find(".children").show()
			
	}, function() {
		query(this).find(".children").hide()
	});
	
	query("img").each(function() {
		query(this).removeAttr("title");
	});
	
	var start = query("#start");
	
	if (start.length != 0)
		initStartPage(start);
	
	var overview = query("#overview");
	
	if (overview.length != 0)
		initOverview(overview);
	
	var contact = query("#contact");
	
	if (contact.length != 0)
		initContact(contact);
}

function initStartPage(start) {
	function run() {
		var first, second;
		start.find(".row").each(function() {
			if (first == null)
				first  = query(this);
			else
				second = query(this);
		});
		
		var height = first.height();
		second.css("overflow", "visible");
		
		for (var i = 0; i < 2; i++) {
			var parent = first.find(".widget").eq(i + 2),
			    offset = height - parent.outerHeight() - 24;
			
			second.find(".column").eq(i + 1)
				.css("margin-top", -(offset));
		}
	}
	run();
	setInterval(run, 500);
	
	initSlideshow();
}

function initSlideshow() {
	var slideshow = query("#slideshow");
	
	slideshow.find("img:first")
		.addClass("active");
	
	function nextImage() {
		var current = slideshow.find(".active"),
		    next    = current.next();
		
		if (next.length == 0)
			next = slideshow.find("img:first");
					
		next.addClass("active").css("z-index", "2").siblings().removeClass("active");
		next.fadeIn(2000, function() {
			current.hide();
			next.css("z-index", "1");
		});
	}
	
	setInterval(nextImage, 5000);
}

function initOverview(overview) {
	var items  = overview.find(".item");
	
	var index = 0;
	items.each(function() {
		index++;
		
		if (index % 2 == 0)
			return true;
		else
			query(this).css("clear", "both");
		
		var first  = query(this),
		    second = first.next();
		
		if (second.length == 0)
			return true;
		
		if (first.height() > second.height())
			second.height(first.height());
		else
			first.height(second.height());
	});
}

function initContact(contact) {
	contact.submit(validate);
	
	function validate() {
		if (contact.find("#name").val().length == 0) {
			alert("Namn saknas.");
			return false;
		}
		
		if (contact.find("#mail").val().length == 0) {
			alert("E-mail saknas.");
			return false;
		}
		
		if (! validMail(contact.find("#mail").val())) {
			alert("Den e-mail du angav är felaktiv.");
			return false;
		}
		
		if (contact.find("#message").val().length == 0) {
			alert("Du måste skriva ett meddelande.");
			return false;
		}
		
		return true;
	}
}

function popup(elem) {
	var popup = query(elem).show(),
	    shade = query("#shade");
	    
	var view  = query(window);
	
	popup.show();
	shade.fadeTo(100, 0.2);
	
	popup.css(
		"left",
		view.width() / 2 - (popup.width() / 2)
	);
	
	var offset = 50;
	
	popup.css(
		"top",
		view.scrollTop() + offset
	);
	
	shade.click(popupClose);
	view.resize(popupClose);
}

function popupClose() {
	query(".popup").hide();
	query("#shade").fadeOut(400);
}

function vote(id) {
	var poll = query("#poll");
	
	query.ajax({
		url : location.pathname + "?vote=" + id,
		success : response
	});
	
	function response(html) {
		var poll = query("#poll");
		poll.replaceWith(html);
		
		Cufon.refresh();
	}
}

function validMail(str) {
	return (str.indexOf(".") > 2)
	    && (str.indexOf("@") > 0);
}
