﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="jQuery/jquery.cookie-vsdoc.js" />
/// <reference path="Modal.js" />

// Go Slow
//#region
var g_speed = (function () {
	var c_cookie = "slow";
	return {
		isSlow: function () {
			return $.cookie(c_cookie) == "1";
		},
		goSlow: function (slow) {
			if (slow === undefined)
				slow = true;
			$.cookie(c_cookie, slow ? "1" : "0", { path: "/" });
		}
	};
})();

function isiPhone() {
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        return true;
    } else {
        return false;
    }
}

function isIDevice(device) {
    switch (device) {
        case "iphone":
            return (navigator.userAgent.match(/iPhone/i));
            break;
        case "ipod":
            return (navigator.userAgent.match(/iPod/i))
            break;
        case "ipad":
            return (navigator.userAgent.match(/iPad/i))
            break;
        default:
            return false;
    }
}

//#endregion

// Document Load
//#region
function updateFooterForiPhone() {
    if (isiPhone()) {
        //alert("Orientation change detected");
        setTimeout(function () {
            $("#footer").css("top", $(document).height() - ($("#footer").height() + 23) + "px");
        }, 2000);
    }
}

$(document).ready(function () {

    if (isiPhone()) {
        if (isIDevice("ipod") || isIDevice("iphone")) {
            $("#footer p").css("font-size", "0.6em");
            $("#footer p.secondp").css("font-size", "0.9em");
        }
        window.onorientationchange = updateFooterForiPhone;
        updateFooterForiPhone();
    }

    $('a[href^="http://"]')
	  .attr({
	      target: "_blank",
	      title: "Opens in a new window"
	  })
    .append(' [^]');

    $.modal(".modal");

    var versionText = g_speed.isSlow() ? "High version" : "Low version";
    $("#footer .privacy").before("<a class=\"low-version\" href=\"#\">" + versionText + "</a><div class=\"heart\"></div> |&nbsp;");

    $("#footer").css("overflow", "visible");

    var $lowVersion = $("#footer .low-version");
    var $heart = $("#footer .heart");

    var lowPosition = $lowVersion.position();

    $heart.css({
        bottom: lowPosition.top + 12,
        left: (lowPosition.left + ($lowVersion.width() / 2)) - ($heart.width() / 2),
        display: g_speed.isSlow() ? "none" : "block"
    });

    $("#footer").css("overflow", "visible");

    var timeout = setTimeout(function () {
        $heart.fadeOut();
        timeout = null;
    }, 10000);

    $lowVersion.hover(function () {
        if (timeout != null)
            clearTimeout(timeout);

        if (!g_speed.isSlow()) {
            $heart.fadeIn();
            $("#footer").css("overflow", "visible");
        }
    },
	function () {
	    if (timeout != null)
	        clearTimeout(timeout);

	    $heart.fadeOut();
	    $("#footer").css("overflow", "hidden");
	});

    $lowVersion.click(function (evt) {
        evt.preventDefault();
        var slow = !g_speed.isSlow();
        g_speed.goSlow(slow);

        if (slow)
            $lowVersion.text("High version");
        else $lowVersion.text("Low version");

        location.reload();
    });
});
//#endregion
