﻿var hcpgpSelection = '';
var userLink = '';
var isUSA = '';

$(document).ready(function () {
    $(".VillageLink").click(function (e) {
    
        userLink = $(this).attr('href');



        showGPHCPSelector();
        return false;
    });
});

function showGPHCPSelector() {
    centerPopup();
    loadPopup();

    $("#popupClose").click(function () {
        disablePopup();
    });

    $("#backgroundPopup").click(function () {
        disablePopup();
    });

    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });
}

function showTermsGP() {    
    hcpgpSelection = 'gp';
    $('#popupTermsGP').show();
    $('#popupHCPGP').hide();
}

function showTermsHCP() {
    hcpgpSelection = 'hcp';
    $('#popupTermsHCP').show();
    $('#popupHCPGP').hide();
}

function selectUSAYes() {
    isUSA = '1';
    window.location.href = "http://www.itpscience.com/";
}

function selectUSANo() {
    isUSA = '0';
    redirectUser()
}

function redirectUser() {

    var redirectLink = userLink;

    if (hcpgpSelection == 'gp') {
        redirectGPToHtml();
        return;
    }

    if (redirectLink.indexOf('?') == -1) {
        redirectLink += '?';
    }
    else {
        redirectLink += '&';
    }

    redirectLink += 'hcpgp=' + hcpgpSelection + '&isUSA=' + isUSA;

    document.location.href = redirectLink;

    return false;
}

function getHtmlLinkFromIdent(ident) {

    var link = 'library.aspx';

    switch (ident) {

        case "74":
            link = 'library-all-about-platelets.aspx';
            break;
        case "73":
            link = 'library-at-a-glance.aspx';
            break;
        case "76":
            link = 'library-caring-for-yourself.aspx';
            break;
        case "71":
            link = 'library-emergency-bleeding.aspx';
            break;
        case "69":
            link = 'library-essentials.aspx';
            break;
        case "70":
            link = 'library-inital-ongoing-testing.aspx';
            break;
        case "67":
            link = 'library-pregnancy-fertility-menstruation.aspx';
            break;
        case "66":
            link = 'library-splenectomy-and-surgery.aspx';
            break;
        case "68":
            link = 'library-symptoms-and-tests.aspx'
            break;
        case "81":
            link = 'library-treatments.aspx';
            break;
        case "64":
            link = 'library-types-of-itp.aspx';
            break;
    }

    return link;
}

function redirectGPToHtml() {

    var requestRegex = new RegExp('request=([a-zA-Z0-9]*)');
    var requestM = requestRegex.exec(userLink);
    var request = requestM[1];

    var identRegex = new RegExp('ident=([0-9]*)');
    var identM = identRegex.exec(userLink);
    var ident = '';
    if(identM != undefined)
        ident = identM[1];

    var redirect = '/HTML/GP/';

    switch (request) {
        case "library":
            if (ident == undefined) {
                redirect += 'library.aspx';
            } else {
                redirect += getHtmlLinkFromIdent(ident);
            }
            break;
        case "market":
            redirect += 'market-square.aspx';
            break;
        case "cinema":
            redirect += 'cinema.aspx';
            break;
        case "postoffice":
            redirect += 'postoffice.aspx';
            break;
        case "station":
            redirect += 'station.aspx';
            break;
        case "cafe":
            redirect += 'cafe.aspx';
            break;
        case "council":
            redirect += 'council.aspx';
            break;
    }

    document.location.href = redirect;
    
}

var popupStatus = 0;

function loadPopup() {
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popup").fadeIn("slow");
        popupStatus = 1;
    }
}


function disablePopup() {
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popup").fadeOut("slow");
        popupStatus = 0;
    }
}

function centerPopup() {

    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popup").height();
    var popupWidth = $("#popup").width();
    
    $("#popup").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}

