﻿(function () {
    // remove layerX and layerY
    var all = $.event.props,
        len = all.length,
        res = [];
    while (len--) {
        var el = all[len];
        if (el != 'layerX' && el != 'layerY') res.push(el);
    }
    $.event.props = res;
} ());

//Set the facebook stream
$(document).ready(function(){
    $('#facebook-wall').fbWall({
        id: 'wvama',
                accessToken: 'AAAC7gAoWDQ0BAAkoRL2eisVwVaGrXv5NkZA8V1Ussx6zjU0wZBot8WOkwkPIAPoitUX5PCv4h1AoemeFPfJblUSeDgwIWBv1sA0jPjZBQZDZD',
                showGuestEntries: false,
                showComments: false,
                max: 5,
                timeConversion: 12
     });
});


//Main function control
$(document).ready(function () {

    setNavigation();

    setSocialNetworks();
});

function setNavigation() {

    //Get the the object containing the current page 
    var pageName = $(location).attr('pathname').split("/");
    var curPage = $("#navigation li [href^='/" + pageName[1] + "/']");
    var curChild = $("#" + pageName[2]);

    curPage.parent().parent().css("display", "block");
    curPage.addClass("green_border");

    var hover = "";
    var curHoverObj = "";
    $("#navigation li").last().css("padding-right", "0px");

    $("#navigation li").hover(function () {

        if ($(this).children().attr("id") != curPage.attr("id") && $(this).parent().parent().attr("id") != curChild.parent().parent().attr("id") && curPage.attr("id") != $(this).parent().parent().attr("id")) {

            hover = true;
            if (curPage.attr("id") == "Jobs Form" && $(this).attr("id") != "JOBS" && $(this).attr("id") != "Job Postings")
                curPage.parent().parent().css("display", "none")
            else
                curPage.parent().children().children().css("display", "none");
        }

       
    }, function () {

        if (curPage.attr("id") == "Jobs Form" && $(this).attr("id") != "JOBS" && hover == false)
            curPage.parent().parent().css("display", "block");

        hover = false;
    });

    $("#navigation a").hover(function () {

        if ($(this).attr("id") == curPage.attr("id"))
            curPage.parent().children().children().css("display", "block");


        if ($(this).parent().parent().parent().attr("id") == "navigation") {
           if (curHoverObj != "")
                curHoverObj.removeClass("green_border");

            curHoverObj = $(this);
        }

        curHoverObj.addClass("green_border");


    }, function () {
    });

    $(document).mousemove(function (e) {

        var rightPos = $("#navigation").position().left + $("#navigation").width();

        if (e.pageY < 68 || e.pageY > 118 || e.pageX < $("#navigation").position().left || e.pageX > rightPos) {
            curPage.parent().children().children().css("display", "block");
            curPage.parent().parent().css("display", "block");
            if(curHoverObj != "")
            curHoverObj.removeClass("green_border");
        }
    }); 
    
}

function setSocialNetworks() {

    //If an item is clicked get the target which was clicked on
    $("body").click(function (event) {

        //Set up the content pages of the Social Networks so they can be changed accordingly
        var fb_content = $("#fb_content");
        var twitter_content = $("#twitter_content");
        var blog_content = $("#blog_content");

        //pass in the target id of the clicked element
        switch (event.target.id) {
            case 'fb_icon':
                setContent(fb_content, twitter_content, blog_content);

                break;
            case 'twitter_icon':
                setContent(twitter_content, fb_content, blog_content);
                break;
            case 'blog_icon':
                setContent(blog_content, twitter_content, fb_content);
                break;
        };
    });

}
function setContent(target, prev1, prev2) {
    target.css("display", "block");
    target.children().css("display", "block");

    prev1.css("display", "none");
    prev2.css("display", "none");
    prev1.children().css("display", "none");
    prev2.children().css("display", "none");
}

function setIconImages(iconIndex) {

    var fb_icon = $("#fb_icon");
    var twitter_icon = $("#twitter_icon");
    var blog_icon = $("#blog_icon");

    switch (iconIndex) {
        case 0:
            fb_icon.attr("src", "/Content/images/fb_icon_on.png");
            twitter_icon.attr("src", "/Content/images/twitter_icon_off.png");
            blog_icon.attr("src", "/Content/images/blog_icon_off.png");
            break;
        case 1:
            twitter_icon.attr("src", "/Content/images/twitter_icon_on.png");
            fb_icon.attr("src", "/Content/images/fb_icon_off.png");
            blog_icon.attr("src", "/Content/images/blog_icon_off.png");
            break;
        case 2:
            blog_icon.attr("src", "/Content/images/blog_icon_on.png");
            twitter_icon.attr("src", "/Content/images/twitter_icon_off.png");
            fb_icon.attr("src", "/Content/images/fb_icon_off.png");
            break;
    };
}

//----------- Twitter Plugin ---------------//
function setTwitter() {

    new TWTR.Widget({
        version: 2,
        type: 'profile',
        rpp: 6,
        interval: 30000,
        width: 363,
        height: 280,
        theme: {
            shell: {
                background: '#FFFFFF',
                color: '#ffffff'
            },
            tweets: {
                background: '#FFFFFF',
                color: '#000000',
                links: '#61A40F'
            }
        },
        features: {
            scrollbar: false,
            loop: false,
            live: false,
            behavior: 'all'
        }
    }).render().setUser('wvama').start();
}


// ------------   Linked in API ---------------

// 2. Runs when the JavaScript framework is loaded
var resetLogin = true;

function onLinkedInLoad() {
    IN.Event.on(IN, "auth", onLinkedInAuth);
}

// 2. Runs when the viewer has authenticated
function onLinkedInAuth() {
    IN.API.Profile("me").result(displayProfiles);
    IN.API.Raw("/people/~/group-memberships?membership-state=member").result(getMembersGroup);
    IN.API.Raw("/people/~/group-memberships?membership-state=moderator").result(getMembersGroup);
    IN.API.Raw("/people/~/group-memberships?membership-state=manager").result(getMembersGroup);
    IN.API.Raw("/people/~/group-memberships?membership-state=owner").result(getMembersGroup);
}

//Get the groups which the member is a part of
function getMembersGroup(membersGroup) {

    //loop the array of groups associated with the member
    for (var i = 0; i < membersGroup._total; i++) {
        curGroup = membersGroup.values[i];
        //console.log(curGroup);
        //check if the member is a part of the AMA group
        if (curGroup.group.id == "663407") //Willamette Valley AMA
        {
            //console.log("cur group is === ", curGroup);
            setLogin();
            resetLogin = false;
            break;
        }

    }
}


// 2. Runs when the Profile() API call returns successfully
function displayProfiles(profiles) {
    member = profiles.values[0];

    if (resetLogin == true) {
        //alert("You need to be a member of the AMA group to sign in");
        $("#profiles").html(" <div id =\"linkedin_text\"> <a target = \" _blank \" href =  \"http://www.linkedin.com/groups?home=&gid=663407\">You are not a member of the AMA Group Join Today </a></div>");
        $("#profiles2").html(" <div id =\"linkedin_text2\"> <a target = \" _blank \" href =  \"http://www.linkedin.com/groups?home=&gid=663407\">You are not a member of the AMA Group Join Today </a></div>");
    
    }
    else {
        document.getElementById("profiles").innerHTML =
               "<p id =\"linkedin_text\" >Hello " + member.firstName + " " + member.lastName + ", " + "<a style = \" text-decoration:underline; \" href =  \" /MediaRoom/ \"> WVAMA Member Area  </a></p>";
    }
}

function setLogin() {

    //use ajax to lookup username/email in account table & login
    $.ajax({
        url: "/LinkedIn/Login/",
        type: "POST",
        success: function (result) {
            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //alert('Error getting Linked In Login');
        }
    });
}

