var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19485343-1']);
_gaq.push(['_trackPageview']);


function suspendBackground() {
	$("#like").css("visibility", "hidden");
	if (document.slides && document.slides.isPlaying()) {
		document.slides.stop();
	}
}

function resumeBackground() {
	$("#like").css("visibility", "visible");
	if (document.slides && !document.slides.isPlaying()) {
		document.slides.start(1000);
	}
}

function time() { var d = new Date(); return d.getHours()+":"+d.getMinutes()+":"+d.getSeconds(); }

function loadScript(url, allowSSL, stdPrefix, SSLPrefix, callback) {
	var isSSL = ("https:" == document.location.protocol);
	var protocol = "";
	if(allowSSL !== undefined && allowSSL && isSSL) {
		protocol = "https://" + ((SSLPrefix === undefined) ? "" : SSLPrefix);
	} else {
		protocol = "http://" + ((stdPrefix === undefined) ? "" : stdPrefix);
	}
	
	$.getScript(protocol+url, callback||function(){});
}

(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var finished = 0;
		var total = 0;
		var args_len = arguments.length;
		var callback = function() {};
		if(typeof(arguments[args_len - 1]) == "function") {
			callback = arguments[--args_len];
		}
		var count = function() {
			if(++finished >= total) {
				callback();
			}
		};
		finished = 0;
		total = args_len;
		var i = args_len;
		while(i--) {
			var arg = arguments[i];
			if(typeof(arg) == "string") {
				var cacheImage = document.createElement('img');
				$(cacheImage).load(count);
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	};
})(jQuery);

function init() {
	loadScript("google-analytics.com/ga.js", true, "www.", "ssl.");
	//loadScript("widgets.digg.com/buttons.js");

	$("#demoContainer").hide();

	initClickStats();
	$("#newsletterForm").submit(newsletter);
}

function initClickStats() {
	$("a").each(function() {
		$(this).click(function() {
			clickStats($(this).attr("id"));
		});
	});
}

function clickStats(id) {
	$.get("click.php", {id: id});
}

function newsletterReset() {
	if($("#newsletter input[type=text]").val() == "your@email.here") {
		$("#newsletter input[type=text]").val("");
	}else if($("#newsletter input[type=text]").val() == "") {
		$("#newsletter input[type=text]").val("your@email.here");
	}
	$("#nlIndicator").removeClass("success fail");
	$("#nlInfoText").removeClass("success fail");
	$("#nlInfoText").text("");
}

function newsletter(event) {
	event.preventDefault();
	newsletterReset();
	$("#nlIndicator").addClass("working");
	$.get("newsletter.php", {action: "register", mail: $("#mail").val()}, function(data) {
		$("#nlIndicator").removeClass("working");
		if(parseInt(data) == 1) {
			$("#nlIndicator").addClass("success");
			$("#nlInfoText").addClass("success");
			$("#nlInfoText").text("Thanks! Delta Strike will bring you joy!");
		}
		else {
			$("#nlIndicator").addClass("fail");
			$("#nlInfoText").addClass("fail");
			$("#nlInfoText").text(parseInt(data) == 0 ? "..oops, this email address is not so awesome." : "..oops, my system's broken :(");
		}
	});
}
