$(document).ready(function() {
	attachEvents("nav");
	$(window).unload(function() {
		// alert(window.location.href);
	})
});

function attachEvents(which) {
	switch(which) {
		case "nav":
			$(".nav_item").click(function() {
				
				//Google analytics
				pageTracker._trackPageview(id);
				var id = $(this).attr("id");
				$("h2").css({ background: "none" });
				transition("ajax/" + id + ".php", "slider", id, $(this));
			});
			break;
		case "tickets_mobile":
			$(".internal_nav .nav_item").click(function(e) {
				pageTracker._trackPageview("ticket_content_"+id);
				$("h2").css({ background: "none" });
				var id = $(this).attr("id");
				transition("ajax/ticket_content.php?id=" + id, "slider", "tickets_content", $(this));
				e.stopPropagation();
			});
			break;
		case "tickets_content":
			$(".internal_nav .sub_nav_item").click(function(e) {
				pageTracker._trackPageview("ticket_sub_content_"+id);
				var id = $(this).attr("id");
				$("h2").css({ background: "none" });
				transition("ajax/ticket_sub_content.php?id=" + id, "slider", "tickets_content", $(this));
				e.stopPropagation();
			});
			break;
		case "music":
			$(".internal_nav .sub_nav_item").click(function(e) {
				pageTracker._trackPageview("music_content_"+id);
				var id = $(this).attr("id");
				$("h2").css({ background: "none" });
				transition("ajax/music_content.php?id=" + id, "slider", "clear", $(this));
				
				e.stopPropagation();
			});
			break;
		case "about":
			$("#VIDEO h2").click(function(e) {
			pageTracker._trackPageview("about");
				//window.location.href = "http://blueman-mobile.mway.situationdev.net/media/video/embed.php?media=promo_reel-iPhone.3gp&poster=attention-poster-sm.jpg&width=400&height=300";
			});
			
		case "clear":
		
		break;
	}
}

function backHome(local) { 
	$.ajax({
		url: "ajax/"+local+".php",
		cache: false,
		success: function(html){
			$("#content_area").html(html);
			attachEvents("nav");
		}
	});
}

var cur = 'home';
function transition(mixed, target, eventId, $el, direction) {
	direction = direction ? direction : "ltr";
	target_child = $("#" + target).children("div:first-child").attr("id");
	if(mixed.match(/^ajax\//)) {
		
		$("#loading").fadeOut(200, function() {	$(this).remove(); });
		if(!!$el && $el.length) {
			$el.css({ color: "#000" });
			$el.append("<img src=\"img/icon_loading.gif\" id=\"loading\" />");
		}
		loc = mixed.match(/ajax\/(\w+)\.php/) ? mixed.replace(/ajax\/(\w+)\.php/, '$1') : mixed.replace(/ajax\/(ticket_content)\.php\?id=(\w+)/, '$1_$2');
		if(cur == loc) { return; } else { cur = loc; }
		
		$.ajax({
			url: mixed,
			cache: false,
			success: function(response) {
				switch(direction) {
					case "ltr":
						$("#loading").fadeOut(200, function() {	$(this).remove(); });
						$("#clip").css({ height: $("#content_area").height() });
						$("#" + target).append("<div id=\"temp_" + target + "\"></div>");
						$("#" + target_child).next().html(response);
						$("#clip").animate({ height: $("#" + target_child).next().height() }, { queue: false, duration: 500 }, function() {
							$(this).css({ height: $(this).height() });
						});
						$("#" + target_child).css({ position: "relative" }).animate({ left: -($(window).width()) }, 500, function() {
							$(this).remove();
						});
						$("#" + target_child).next().css({ position: "relative" }).animate({ left: -($(window).width()) }, 500, function() {
							$(this).attr("id", target_child).animate({ left: 0 }, 1, function() {
								$(this).animate({ left: 50 }, 100, function() {
									$(this).animate({ left: 0 }, 50);
								});
							});
							attachEvents(eventId);
						});
						break;
					case "rtl":
						$("#loading").fadeOut(200, function() {	$(this).remove(); });
						$("#clip").css({ height: $("#content_area").height() });
						$("#" + target).prepend("<div id=\"temp_" + target + "\"></div>");
						$("#" + target_child).prev().html(response).css({ position: "relative", left: -($(window).width()) });
						$("#" + target_child).css({ position: "relative", left: -($(window).width()) });
						$("#clip").animate({ height: $("#" + target_child).prev().height() }, { queue: false, duration: 500 }, function() {
							$(this).css({ height: $(this).height() });
						});
						$("#" + target_child).animate({ left: 0 }, 500, function() {
							$(this).remove();
						});
						$("#" + target_child).prev().animate({ left: 0 }, 500, function() {
							$(this).attr("id", target_child).animate({ left: 0 }, 1, function() {
								$(this).animate({ left: -50 }, 100, function() {
									$(this).animate({ left: 0 }, 50);
								});
							});
							attachEvents(eventId);
						});
						break
				}

			}
		});
	} else {
		$("#clip").css({ height: $("#" + target_child).height() });
		$("#" + target).append("<div id=\"temp_" + target + "\"></div>");
		$("#" + target_child).next().html(mixed);
		$("#clip").animate({ height: $("#" + target_child).next().height() }, { queue: false, duration: 500 }, function() {
			$(this).css({ height: $(this).height() });
		});
		$("#" + target_child).css({ position: "relative" }).animate({ left: -($(window).width()) }, 500, function() {
			$(this).remove();
		});
		$("#" + target_child).next().css({ position: "relative" }).animate({ left: -($(window).width()) }, 500, function() {
			$(this).attr("id", target_child).animate({ left: 0 }, 1);
			attachEvents(eventId);
		});
	}
}