﻿function flipImage(hideID, showID) {
    var objHide = document.getElementById(hideID);
    var objShow = document.getElementById(showID);
    hide(objHide);
    show(objShow);
}
function hide(control) {
    control.style.display = "none";
}
function show(control) {
    control.style.display = "inline";
}
function hideById(controlID) {
    document.getElementById(controlID).style.display = "none";
}
function loadbackground() {
    var screenX = screen.width;
    var screenY = screen.height;

    var bgImagePath = "../_images/";

    //var bgImage = "bg3280x2187.jpg";
    var bgImageWidth = 1920;
    var bgImageHeight = 1080;

    // lazy coding, but it's cleaner than the alternative
    //            if (screenX <= 2560 && screenY <= 1707) {
    //                bgImageWidth = 2560;
    //                bgImageHeight = 1707;
    //                if (screenX <= 1920 && screenY <= 1280) {
    //                    bgImageWidth = 1920;
    //                    bgImageHeight = 1280;
    //                    if (screenX <= 1680 && screenY <= 1120) {
    //                        bgImageWidth = 1680;
    //                        bgImageHeight = 1120;
    //                        if (screenX <= 1536 && screenY <= 1024) {
    //                            bgImageWidth = 1536;
    //                            bgImageHeight = 1024;
    //                        }
    //                    }
    //                }
    //            }
    document.body.style.backgroundImage = "url(" + bgImagePath + "bg" + bgImageWidth + "x" + bgImageHeight + ".jpg)";

    var offsetX = (screenX - bgImageWidth);

    offsetX = (offsetX > 0) ? 0 : offsetX;
    document.body.style.backgroundPosition = offsetX + "px top";
}
function loadFooter() {
    meshNet.resize.add(loadFooter);
    var height = screen.height;
    if (window.innerHeight) {
        height = window.innerHeight;
    } else {
        if (document.body && document.body.clientHeight) {
            height = document.body.clientHeight;
        } else {
            if (document.documentElement) {
                height = document.documentElement.clientHeight;
            }
        }
    }
    var footer = document.getElementById("footer");
    height -= footer.clientHeight;

    var screenX = screen.width;
    var offsetX = (screenX - 1920);
    offsetX = (offsetX > 0) ? 0 : offsetX;
    var bgImagePath = "../_images/";
    var bgImageWidth = 1920;
    var bgImageHeight = 1080;
    footer.style.backgroundImage = "url(" + bgImagePath + "bg" + bgImageWidth + "x" + bgImageHeight + ".jpg)";
    footer.style.backgroundPosition = offsetX + "px -"+height+"px";
}

function launchVideo(id, title, playerType, height) {
    var p = new meshNet.modal("1024px");
    var i = document.createElement("iframe");
    var src;
    if (playerType = "vimeo") {
        src = "http://player.vimeo.com/video/" + id + "?title=0&byline=0&portrait=0&color=e49d50";
    } else {
        src = "http://www.youtube.com/embed/" + id;
    }
    i.src = src; i.style.border = "none"; i.style.width = "100%"; i.style.height = (height ? height : "576") + "px";
    p.appendChild(i);
    var titleDiv = document.createElement("h2");
    titleDiv.style.margin = "5px";
    titleDiv.innerHTML = title;
    p.appendChild(titleDiv);
    p.open();
}

function trim(string) {
    string = string.replace(/^\s+/, '');
    for (var i = string.length - 1; i >= 0; i--) {
        if (/\S/.test(string.charAt(i))) {
            string = string.substring(0, i + 1);
            break;
        }
    }
    return string;
}
function validatePhoneNumber(txtBox) {
    var text = txtBox.value;
    var output = "";
    if (text.length > 0) {
        for (var i = 0, len = text.length; i < len; i++) {
            var char = text.charAt(i);
            if (i == 0) {
                if (char != "(") {
                    output = "(";
                    if (isNumber(char)) {
                        output += char;
                    }
                } else {
                    output += char;
                }
            } else if (i == 4) {
                if (char != ")") {
                    output += ")";
                    if (isNumber(char)) {
                        output += char;
                    }
                } else {
                    output += char;
                }
            } else if (i == 5) {
                if (char != " ") {
                    output += " ";
                    if (isNumber(char)) {
                        output += char;
                    }
                } else {
                    output += char;
                }
            } else if (i == 9) {
                if (char != "-") {
                    output += "-";
                    if (isNumber(char)) {
                        output += char;
                    }
                } else {
                    output += char;
                }
            } else {
                if (isNumber(char)) {
                    output += char;
                }
            }
        }
    }
    txtBox.value = output;
}
function isNumber(number) {
    var numbers = "0123456789";
    return numbers.indexOf(number) > -1;
}

var meshNet = { path: null, sessionID: null};
meshNet.External = { javascript: ['//www.youtube.com/player_api', '//platform.twitter.com/widgets.js', 'https://apis.google.com/js/plusone.js', '//connect.facebook.net/en_US/all.js', '//platform.twitter.com/anywhere.js?id=BcGY2M6NS3nsA0lFBS27Dg&v=1'] };
meshNet.External.Facebook = { initiated: false, applicationID: null };
meshNet.External.Facebook.User = { loggedIn: false, ID: null, accessToken: null, firstName: null, lastName: null, displayName: null, url: null, username: null, lastUpdate: null };
meshNet.External.Twitter = {  };

// meshNet
meshNet.load = function () {
    this.div = document.getElementById("meshNet-div");
    this.External.load();
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(meshNet.AsyncPostback);
    meshNet.resize.add(meshNet.resizeModal);
}
meshNet.AsyncPostback = function (sender, args) {
    meshNet.resizeModal();
}
meshNet.resizeModal = function () {
    var docWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : (document.body.clientWidth ? document.body.clientWidth : 0));
    var docHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : (document.body.clientHeight ? document.body.clientHeight : 0));

    var resize = function (modalControl) {
        var style = modalControl.style;
        var width = modalControl.clientWidth;
        var height = modalControl.clientHeight;
        var posX = (docWidth - width) / 2;
        var posY = (docHeight - height) / 2;
        if (width > docWidth) {
            style.width = docWidth;
        }
        if (height > docHeight) {
            style.height = docHeight;
        }
        style.left = posX > 0 ? posX + "px" : 0 + "px";
        style.top = posY > 0 ? posY + "px" : 0 + "px";
    };

    if (docWidth > 0 && docHeight > 0) {
        var controlList = document.getElementsByName("meshNet-modal");
        for (var i = 0, len = controlList.length; i < len; i++) {
            resize(controlList[i]);
        }
    }
}
meshNet.loginSuccess = function () { };
meshNet.loginError = function (message) { };

meshNet.callback = function () {
    var f = [];
    var fst = null;
    this.add = function (callback) {
        f[f.length] = callback;
    };
    this.complete = function (lastCallback) {
        if (fst != null) {
            fst();
        }
        var len = f.length;
        if (len > 0) {
            for (var i = 0; i < len; i++) {
                if (f[i] != null) {
                    f[i]();
                }
            }
        }
        if (lastCallback && (typeof lastCallback === 'function')) {
            lastCallback();
        }
    };
    this.first = function (firstCallback) {
        fst = firstCallback;
    };
};
meshNet.resize = new meshNet.callback();
window.onresize = meshNet.resize.complete;

meshNet.modal = function (width, height) {
    var table = document.createElement("table");
    table.setAttribute("class", "ModalPanelWrapper");
    table.setAttribute("cellpadding", "0");
    table.setAttribute("cellspacing", "0");
    var tr = document.createElement("tr"); table.appendChild(tr);
    var td = document.createElement("td"); tr.appendChild(td);
    td.setAttribute("align", "center");
    var div = document.createElement("div");
    div.setAttribute("name", "meshNet-modal");
    div.setAttribute("class", "ModalPanel");
    if (width) {
        div.style.width = width;
    }
    if (height) {
        div.style.height = height;
    }
    this.open = function () {
        document.body.appendChild(table);
        document.body.appendChild(div);
        this.resize();
        td.onclick = this.close;
        meshNet.resize.complete(this.resize);
    };
    this.close = function () {
        document.body.removeChild(table);
        document.body.removeChild(div);
    };
    this.resize = function () {
        var docWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : (document.body.clientWidth ? document.body.clientWidth : 0));
        var docHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : (document.body.clientHeight ? document.body.clientHeight : 0));

        var style = div.style;
        var width = div.clientWidth;
        var height = div.clientHeight;
        var posX = (docWidth - width) / 2;
        var posY = (docHeight - height) / 2;
        if (width > docWidth) {
            style.width = docWidth;
        }
        if (height > docHeight) {
            style.height = docHeight;
        }
        style.left = posX > 0 ? posX + "px" : 0 + "px";
        style.top = posY > 0 ? posY + "px" : 0 + "px";
    };
    this.appendChild = function (element) {
        div.appendChild(element);
    };
    //this.innerHTML = div.innerHTML;
    this.setInnerHTML = function (html) {
        div.innerHTML = html;
    };
    var d = document.createElement("div");
    d.style.width = "100%";
    d.style.textAlign = "right";
    var a = document.createElement("a");
    a.href = "javascript:;"; a.onclick = this.close; a.innerHTML = "Close";
    a.style.padding = "5px"; a.style.display = "inline-block";
    d.appendChild(a);
    this.appendChild(d);
};
// meshNet.External
meshNet.External.load = function () {
    // define async methods...
    // facebook
    window.fbAsyncInit = function () { meshNet.External.Facebook.load() };
    // load javascript files
    this.loadJavascript();
}
meshNet.External.loadJavascript = function () {
    for (var i = 0, len = this.javascript.length; i < len; i++) {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = this.javascript[i];
        meshNet.div.appendChild(po);
    }
}

// meshNet.External.Facebook
meshNet.External.Facebook.load = function () {
    if (!this.initiated && this.applicationID) {
        // add facebook div
        var fbDiv = document.createElement("div"); fbDiv.id = "fb-root"; meshNet.div.appendChild(fbDiv);
        // init facebook
        FB.init({
            appId: this.applicationID,
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true,
            logging: false
        });
        // get login information, if it exists
        this.checkLoginStatus();
        // subscribe to events
        FB.Event.subscribe('edge.create', this.like);
        this.initiated = true;
    }
};
meshNet.External.Facebook.User.refresh = function (authResponse) {
    if (authResponse) {
        meshNet.External.Facebook.User.loggedIn = true;
        meshNet.External.Facebook.User.ID = authResponse.userID;
        meshNet.External.Facebook.User.accessToken = authResponse.accessToken;
        this.retrieveDetails();
    }
}
meshNet.External.Facebook.User.retrieveDetails = function () {
    FB.api('/me', this.fillDetails);
}
meshNet.External.Facebook.User.fillDetails = function (data) {
    meshNet.External.Facebook.User.firstName = data.first_name;
    meshNet.External.Facebook.User.lastName = data.last_name;
    meshNet.External.Facebook.User.displayName = data.name;
    meshNet.External.Facebook.User.url = data.link;
    meshNet.External.Facebook.User.lastUpdate = data.updated_time;
}
meshNet.External.Facebook.login = function () {
    FB.login(function(response) {
        meshNet.External.Facebook.User.refresh(response.authResponse);
    });
};
meshNet.External.Facebook.checkLoginStatus = function () {
    FB.getLoginStatus(function (response) {
        meshNet.External.Facebook.User.refresh(response.authResponse);
    });
};
meshNet.External.Facebook.like = function (response) {
    meshNet.External.Facebook.checkLoginStatus();
    var user = meshNet.External.Facebook.User;
    if (user.loggedIn) {
        jQuery.ajax({ type: "POST", url: meshNet.path + "/handlers/fb/like", data: { url: response, userID: user.ID }, success: function (data) {  } });
    }
};
meshNet.External.Facebook.ProfilePicture = function (callback) {
    FB.login(function (response) {
        if (response.authResponse) {
            meshNet.External.Facebook.User.refresh(response.authResponse);
            FB.api('/me/albums', function (response) {
                for (album in response.data) {
                    // Find the Profile Picture album
                    if (response.data[album].name == "Profile Pictures") {
                        // Get a list of all photos in that album.
                        FB.api(response.data[album].id + "/photos?return_ssl_resources=1", function (response) {
                            //The image link
                            callback(response.data[0].images[0].source);
                        });
                        break;
                    }
                }
            });
        } else {
            alert("Access to profile picture not granted.");
        }
    }, { scope: 'user_photos' });
};
// meshNet.External.Twitter
meshNet.External.Twitter.Linkify = function (controlID) {
    twttr.anywhere(function (T) {
        T("#" + controlID).linkifyUsers();
    });
};
