﻿//populates the MSegment variable with the value of the .MemberSegment cookie
var MSegment = "";

var found = 1;

var dc = document.cookie;
var prefix = ".MemberSegment=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) found = 0;
} else {
    begin += 2;
}

if (found != 0) {
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    MSegment = unescape(dc.substring(begin + 15, end));
}

var sliding = false;
var latestSlide = "";
var inSubMenu = false;
var needHide = false;


var choiceThumbs;
var curThumb = 0;
var thumbClicked = false;
var delay = 5000;



function swapImage(image) {
    var temp = $(image).attr('src');

    $(image).attr('src', $(image).attr('swap'));
    $(image).attr('swap', temp);
}

function showChoice(choice) {
    var id = "#" + $(choice).attr("name");

    var position = $(choice).position();

    $('#bannerBorder').animate({
        left: position.left
    }, 500, function(e) {
        if ($(id + ":visible").length == 0) {
            $("#banners #top div:visible").hide();
            $(id).show();
        }
    });
}

function showNextThumb() {

    if (!thumbClicked) {

        curThumb = (curThumb + 1) % choiceThumbs.length;

        showChoice(choiceThumbs[curThumb]);
        setTimeout(function() { showNextThumb(); }, delay);
    }
}

function hideOvers() {
    $('div#menus div img[over]').each(function() {
        $(this).attr('src', $(this).attr('orig'));
    });
}

// Hides all open menus and sets variable so that after any animations complete, they will be hidden too
function HideStore() {
    $('div#menus div ul').hide();
    needHide = true;
    hideOvers();
}


$(function() {

    $('div#menus div img[over]').each(function() {
        $(this).attr('orig', $(this).attr('src'));
    });

    $('div#menus div').hover(function(e) {
        var curId = $(this).attr('id')

      
            hideOvers();
            $(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('over'));
      

        if ($(this).children('ul:visible').length == 0) {
            needHide = false;
            latestSlide = curId;

            $('div#menus div ul').hide();


            sliding = true;
            $(this).children('ul').slideDown('fast', function(e) {
                if (latestSlide != curId || needHide) {
                    $(this).hide();
                    $(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('orig'));
                    needHide = false;

                }
                sliding = false;

            }

        );
        }
    },
    function(e) {
        if ($('#holder').length = 0) {
            HideStore();
        } else {
            needHide = true;
        }
    }
    );



    $('#mainHeader').hover(function() {
        // Hide if the user backs up from a menu div
        HideStore();
    }, function() { });


    $('div#menus div ul').hover(function(e) { },
    function(e) {
        if (!sliding) {
            HideStore();
        }
    });


    $('div#menus li').hover(function(e) {
        $(this).children("ul").show();
    }, function(e) {
        if (!inSubMenu) {
            $(this).children("ul").hide();
        }
    });

    $('div#menus li ul').hover(function(e) {
        inSubMenu = true;
    }, function(e) {
        inSubMenu = false;
        $(this).hide();
    });


    $('td#tabarea ul li').click(function(e) {
        var rel = $(this).attr('rel');

        $('td#tabarea div').hide();
        $('td#tabarea div#' + rel).show();

        $('td#tabarea ul li').removeClass('selected');
        $(this).addClass('selected');
    });

    $('img[swap]').hover(function(e) {
        swapImage(this);
    }, function(e) { swapImage(this); });


    choiceThumbs = $("#banners #thumbs div.thumb");


    if (choiceThumbs.length > 0) {
        var position = $(choiceThumbs[0]).position();


        $('#bannerBorder').click(function(e) {
            window.location = $("#banners #top div:visible").children('a').attr('href');
        });



        $("#banners #thumbs div").bind("click", function(ex) {
            thumbClicked = true;
            showChoice(this);
            return false;
        });

        if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
            showChoice(choiceThumbs[0]);
        }
        setTimeout(showNextThumb, delay);
    }



});

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}

function MM_jumpMenu(targ, selObj, restore) { //v3.0
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) selObj.selectedIndex = 0;
}

function MM_jumpMenuGo(selName, targ, restore) { //v3.0
    var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ, selObj, restore);
}


/* Email List Dialog */
var mainList;

function isArray(obj) {
    return obj.constructor == Array;
}

function GetSelectedLists() {
  
   

    var selectedLists = Array();
    var unselectLists = Array();

    if (mainList != null) {
        selectedLists.push(mainList.ListId)
    } 

    $('ul#lists input').each(function() {
        var id = $(this).attr("listid");

        if ($(this).attr("checked") == true) {
            selectedLists.push(id);
        } else {
        unselectLists.push(id);
        }
    });

    if (selectedLists.length > 0) {
        return "&optin=" + selectedLists.toString() + "&unlists=" + unselectLists.toString();
    } else {
        return null;
    }
}

function validateEmail(email) 
{
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
  return email.match(re) 
}

function ShowMailError(message) {
    $('#dialog #error').remove();
    $('#mainDesc').append("<p id=\"error\" class=\"errorcontent\">" + message + "</p>");
}

function OnGetListsComplete(result) {
    $('#dialog').remove();
    
    if (mainList == null && result.length == 1) {
        mainList = result[0];
        result = null;
    }

    var body = '<div id="dialog">';

    if (mainList != null) {
        body += '<h2>' + mainList.Name + '</h2><p id="mainDesc"><img height="50" hspace="10" src="/Members/images/editemailprefsicon.gif" width="83" align="left" vspace="2"/>' + mainList.Description + '<p><label for="mail">E-mail:</label><input type="mail" id="mail" name="mail" /></p>';

        if (result != null && result.length > 0) {
            body = body + '<h3>Additional E-mail Offerings:</h3><ul id="lists"></ul>';
        }

    } else {
        body += '<p id="mainDesc"><img height="50" hspace="10" src="/Members/images/editemailprefsicon.gif" width="83" align="left" vspace="2"/>Maplesoft publishes a variety of mailing lists designed to help you get the most out of your software, and to keep informed            about events and special promotions.</p>            <h3>Select or or more of the following:</h3>            <ul id="lists">            </ul>            <label for="mail">                E-mail:</label><input type="mail" id="mail" name="mail" />';
    }

    body = body + '<input type="submit" id="subscribe" value="Subscribe" /><p class="disclaimer">After filling out this form, you will receive an e-mail to confirm your subscription. You can <a href="/subscribers/global_unsubscribe.aspx" class="plainlink">unsubscribe</a> at any time.</p>        </div>';

    $('body').append(body);

    var title = "Subscribe to a Maplesoft Mailing List";

    if (mainList != null) {
        title = "Subscribe to " + mainList.Name;
    }
    
    $('#dialog').dialog({
        title: title,
        modal: true,
        resizable: false,
        width: 400
    });

    
    if (result != null && result.length > 0) {
        $.each(result, function() {
            var checked = "";
            if (this.Subscribed) {
                checked = " checked=\"checked\"";
            }
            
            $('ul#lists').append("<li><input type=\"checkbox\" listid=\"" + this.ListId + "\"" + checked + "/><label>" + this.Name + "</label>");
        });
    }

    $('#subscribe').click(function() {
        var mail = $('#mail').val();
        var selectedLists = GetSelectedLists();

        if (selectedLists == null) {
            ShowMailError("Please select at least one mailing list");
        } else if (mail.length == 0) {
            ShowMailError("Please enter your e-mail address");
        } else if (!(validateEmail(mail))) {
            ShowMailError("You have entered an invalid e-mail address");
        } else {

            window.location = "/subscribers/Subscribe.aspx?mail=" + mail + selectedLists + "&pagename=" + s.pageName;
        }


    });
 

    $.get('/subscribers/services/CurrentEmail.ashx', null, function(data) {
        $('#mail').val(data);
    });

}

function ShowSubscribeDialog(ids, extraIds) {
    mainList = null;
    
    if (arguments.length == 1) {
        var listIds;

        if (isArray(ids)) {
            listIds = ids;
        } else {
            listIds = [ids];
        }

        $.getJSON('/subscribers/services/GetLists.ashx?ids=' + listIds.toString(), null, OnGetListsComplete);

    } else {
        // Additional offerings configuration
        $.getJSON('/subscribers/services/GetList.ashx?id=' + ids.toString(), null, function(result) {
            mainList = result;
            $.getJSON('/subscribers/services/GetLists.ashx?ids=' + extraIds.toString(), null, OnGetListsComplete);
        });
    }
}
