root = 0;
checkSkip();

$(document).ready( function () {
	startQueue();
});

function startQueue() {
	root = $("#root");
	
	//1
	root.queue("root", function(next) {
		root.fadeOut(function() {
			clearStage();
			root.css('backgroundImage', 'url("/intro/library/img/1.png")');
			root.fadeIn();
			next();
		});
	});
	
	root.queue("root", function(next) {
		flashy = addBox(645, 560, 70, 20);
		speed = 800;
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1).delay(speed);
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1).delay(speed);
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1, function() {
			next();
		});
	});
	
	//2
	root.queue("root", function(next) {
		root.fadeOut(function() {
			clearStage();
			root.css('backgroundImage', 'url("/intro/library/img/2.png")');
			root.fadeIn();
			next();
		});
	});

	root.queue("root", function(next) {
		flashy = addBox(782, 497, 70, 20);
		speed = 800;
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1).delay(speed);
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1).delay(speed);
		flashy.animate({opacity:0}, 1).delay(speed);
		flashy.animate({opacity:1}, 1, function() {
			next();
		});
	});
	
	//3
	root.queue("root", function(next) {
		root.fadeOut(function() {
			clearStage();
			root.css('backgroundImage', 'url("/intro/library/img/3.png")');
			root.fadeIn();
			next();
		});
	});

	root.queue("root", function(next) {
		flashy = addBox(493, 370, 70, 20);
		speed = 800;
		flashy.animate({opacity:0}, 1).delay(3000);
		flashy.animate({opacity:1}, 1, function() {
			next();
		});
	});

	root.queue("root", function(next) {
		$("#frame").fadeOut(function() {
			window.location = "/aboutus";
		});
	});
	
	root.dequeue("root");
}

function showNext(element, delay, callbk) {
	element.show();
	var next = element.next();
	if (next.length == 1) {
		setTimeout(function(){showNext(next, delay, callbk)}, delay);
	} else {
		if (callbk != null) {
			callbk.call();
		}
	}
}

function addBox(x, y, width, height) {
	var new_box = $("<div class='box'></div>");
	new_box.css({
		left: x,
		top: y,
		width: width,
		height: height
	});
	root.append(new_box);
	return new_box;
}

function clearStage() {
	$("#root").children().remove();
}

function checkSkip() {
	var skip = getCookie("ep_skip_intro", "0");
	if (skip == "1") {
		window.location = "/aboutus";
	}
}

function alwaysSkip() {
	setCookie("ep_skip_intro", "1");
	window.location = "/aboutus";
}

function setCookie(name, value) {
	var cookie_string = name + "=" + escape (value);
	cookie_string += "; path=" + escape ("/");
	document.cookie = cookie_string;
}

function getCookie(name, default_value) {
	var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
	if (results) {
		return (unescape (results[2]));
	} else {
		setCookie(name, default_value);
		return default_value;
	}
}
