﻿var no_flash_yet = false;
// placeholder for cookie value
var cookieValue = "";
// used in conjunction with debugging
var debug;

var videoTourId = "VideoTourLink";
/*********************************************************/
// loads the js environment  for flash object
/*********************************************************/
function LoadEnvironment(value) {
    if (value) {
        SetCookieValue(value);
    }
}
var loginUrl = "login.aspx";
var logoutUrl = "logout.aspx";
var prayerTree = "prayertree/";

function RedirectToLogin() {
    RedirectToUrl(loginUrl + "?ReturnUrl=" + parseUri(location)["relative"]);
}
function RedirectToLogout() {
    RedirectToUrl(logoutUrl);
}
function RedirectToSignup() {
    RedirectToUrl(loginUrl + "?signup=1" + "&ReturnUrl=" + parseUri(location)["relative"]);
}
function RedirectToPrayerTree()
{
    RedirectToUrl(prayerTree);
}
function RedirectToUrl(url)
{
    document.location = url;
}
/*********************************************************/
// gets the page cookie, should be called before making
// a call to the web-services
/*********************************************************/
function GetCookieValue() {

    return cookieValue;
}
/*********************************************************/
// sets cookie value
/*********************************************************/
function SetCookieValue(value) {
    cookieValue = value;
}
/*********************************************************/
// sets cookie value
/*********************************************************/
function UpdateConnect() {
    if (cmdId) UpdateClick(cmdId);
}
/*********************************************************/
// simulates update button being clicked by a user
/*********************************************************/
function UpdateClick(id) {
    var ele = document.getElementById(id);
    if (ele) {
        LinkClick(ele);
    }
    if (no_flash_yet) {
        setTimeout("UpdateClick('" + id + "')", 30 * 60 * 1000);
    }

}
/*********************************************************/
// called by aspx page after request is complete
// sets the cookie value
/*********************************************************/
function EndRequestHandler(sender, args) {
    try {
        SetCookieValue(document.getElementById(cmdId).attributes["cookie"].value);
        ShowCookie();
    } catch (e) {
    }
}
/*********************************************************/
// used in conjunction with debugging/development
// display cookie to the user
// "smart" enough to be left in in a release build
/*********************************************************/
function ShowCookie(id) {

    if (id) {
        debug = document.getElementById(id);
    }
    if (!debug) {
        return;
    }
    debug.innerHTML = GetCookieValue();
}

function StartVideoTour() {
    window.open("VideoTour.html", "videoTour", 
        "status=1,toolbar=0,menubar=0,directories=0,resizable=1,scrollbars=0,width=896,height=480");
}
function ShowExternalLink(topic)
{
var re = /^https?:\/\//;
    if(re.test(topic))
    {
    }
    else{
        if(hub)
            hub(topic);
    }
}
function hub(topic)
{
    switch(topic)
    {
    case "Home":
        RedirectToUrl("../");
    break;
    case "PrayerTree":
        RedirectToUrl("");
    break;
    default:
        alert('you are not going anywhere');
    break;
    }

}
function LinkClick(ele)
{
    if (ele.click)
            ele.click();
    else {
            var evtObj = document.createEvent('MouseEvents');
            evtObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            ele.dispatchEvent(evtObj);
    }
}
function WhatIsPrayerTree()
{
    var lnk ;
    try{
        lnk = document.getElementById(videoTourId);
        if(lnk)LinkClick(lnk);
    }catch(e)
    {
        window.status = e.message;
    }
    
}

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
