﻿//TODO: this will need to be configurable by the 'host' site
//urlHome: 'http://myln.stage.ln-trust/Portal/Landing' 
//urlHome: 'http://myln-ci.webdev2.livenation.co.uk/Portal/Landing'
//urlHome: 'http://localhost:1960/MyLn/Portal',

$(document).ready(function() {
/*
    $(window).bind('resize', function() {
        if ($('#TB_window').size() > 0) {
            if ($(window).height() <= 640) {
                $('#TB_window').css('top', '320px');
            } else {
                $('#TB_window').css('top', '50%');
            }
        }
    });


    $('.myLnLogoutLink').click(function() {
        myLn.logout.doLogout();
        return false;
    });
    $('li.LoginLink a').click(function() {
        myLn.loginlayer.loadForm();
        tb_show("", "#TB_inline?height=600&width=700&inlineId=LoginLayer&modal=true", false);
    });

    $('#presaleslogin').click(function() {
        myLn.loginlayer.loadForm();
        tb_show("", "#TB_inline?height=600&width=700&inlineId=LoginLayer&modal=true", false);
    });

    $('.MyLNlogin').click(function() {
        myLn.loginlayer.loadForm();
        tb_show("", "#TB_inline?height=600&width=700&inlineId=LoginLayer&modal=true", false);
    });

    $('.myLnAddToFaves').click(function() {
        var ids = $(this).attr('id').split('_');

        $('#MyLnAddToFaves').html(myLn.loading());
        $.getJSON("/myln/user/AddFaveItem", { faveTypeID: parseInt(ids[1]), faveID: parseInt(ids[2]) }, function(data) {
            if (data.IsAuthenticated == true) {
                if (data.Result == true) {
                    //window.location.reload();
                    window.location.href = window.location.href;
                }
                else {
                    $('#MyLnAddToFaves').html('<p>error</p>');
                }
            }
            else {
                myLn.loginlayer.loadForm();
                tb_show("", "#TB_inline?height=600&width=700&inlineId=LoginLayer&modal=true", false);
            }
        });
    });
*/
});

// Namespacing
var myLn = {
    placeHolder: 'MyLnPlaceholder',
    urlHome: '/myln/portal?page=home', //configurable by host site
    urlLoadingImg: staticUrl + 'Images/ajax-loader.gif', //configurable by host site
    changeEmailTarget: 'ChangeEmailPlaceholder',
    myLnFaveTarget: 'MyLnFavePlaceholder',
    LoginLayerTarget: 'MyLnLogin',
    redirectToHomePage: function() {
        window.location = myLn.urlHome;
    },
    loading: function() {

        //The AMG site variable is located in the master pages of ln and amg shared views      
        if (AMGSite) myLn.urlLoadingImg = staticUrl + '/Images/amg-ajax-loader2.gif';
        var div = document.createElement('div');
        div.setAttribute('id', 'wrap');
        var img = document.createElement('img');
        img.setAttribute('alt', "Loading...");
        img.setAttribute('class', 'loadingImg');
        img.setAttribute('src', myLn.urlLoadingImg);
        div.appendChild(img);
        return div;
    }
};

myLn.activate = {
    url : '/myln/register/activate',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.activate.url);
    },
    submitForm: function(form) {
        myLn.activate.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.activate.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.addNickname = {
url: '/myln/login/addnickname',
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.addNickname.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.addNickname.response
		);
    },
    response: function(resp) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.addNicknameLayer = {
url: '/myln/login/addnicknamelayer',
    loadForm: function() {
        $('#' + myLn.LoginLayerTarget).html(myLn.loading()).load(myLn.addNicknameLayer.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.addNicknameLayer.response
		);
    },
    response: function(resp) {
        scroll(0, 0);
        $('#' + myLn.LoginLayerTarget).html(resp);
    }
};

myLn.changeEmail = {
	url: '/myln/account/ChangeEmail',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.changeEmailTarget).html(myLn.loading()).load(myLn.changeEmail.url);
    },
    submitForm: function(form) {
        myLn.changeEmail.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.changeEmail.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.changeEmailTarget).html(resp);
    }
};

myLn.deactivateAccount = {
	url: '/myln/login/DeactivateAccount',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.changeEmailTarget).html(myLn.loading()).load(myLn.deactivateAccount.url);
    },
    submitForm: function(form) {
        myLn.deactivateAccount.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.deactivateAccount.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.changeEmailTarget).html(resp);
    }
};

myLn.faveButton = {
};

myLn.faves = {
	url: '/myln/user/faves',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.faves.url);
    },
    submitForm: function(form) {
        myLn.faves.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.faves.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.faveSearch = {
    url: '/myln/user/FaveSearch',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.faveSearch.url);
    },
    submitForm: function(form) {
        myLn.faveSearch.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.faveSearch.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.forgotPassword = {
	url: '/myln/login/ForgotPassword',
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.forgotPassword.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.forgotPassword.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.forgotPasswordLayer = {
	url: '/myln/login/ForgotPasswordLayer?height=300&width=300',
    loadForm: function() {
        $('#' + myLn.LoginLayerTarget).html(myLn.loading()).load(myLn.forgotPasswordLayer.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.forgotPasswordLayer.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.LoginLayerTarget).html(resp);
    }
};

myLn.login = {
    url: '/myln/login/landing',
    returnUrl: '',
    onLoad: function() {
        //alert("!");
    },
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.login.url + myLn.login.returnUrl);
    },
    submitForm: function(form) {
        myLn.login.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.login.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.loginlayer = {
    url: '/myln/login/loginlayer',
    formSubmitted: false,
    success: false,
    loadForm: function() {
        $('#' + myLn.LoginLayerTarget).html(myLn.loading()).load(myLn.loginlayer.url);
    },
    submitForm: function(form) {
        myLn.loginlayer.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.loginlayer.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        if (resp.indexOf("LOGINERROR") < 0) {
            window.location.reload(); //stays on current page
        }
        else {
            $('#' + myLn.LoginLayerTarget).html(resp);
        }
    }
};

myLn.logout = {
url: '/myln/login/Logout',
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.logout.url);
    },
    doLogout: function(form) {
        var action = myLn.logout.url;
        $.post(action,
			{},
			myLn.logout.response
		);
    },
    response: function(resp) {
        scroll(0, 0);
        window.location = myLn.urlHome;
    }
};

myLn.home = {
    url: '/myln/user/landing',
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.home.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.login.response
		);
    },
    response: function(resp) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    },
    onLoad: function() {
        $(".myLnPaginateLink").click(function() {
            $('#eventResults').html(myLn.loading());
            var sortId = $('#eventsTable tr th.sortcol a').attr('id').substring(0, $('#eventsTable tr th.sortcol a').attr('id').length - 1);
            var islocal = $('#eventsTable tr th.sortcol a').attr('id').substring($('#eventsTable tr th.sortcol a').attr('id').length - 1, $('#eventsTable tr th.sortcol a').attr('id').length);
            $('#' + myLn.placeHolder).load('/myln/user/paging',
            {
                targetPage: getID($(this).attr('id')),
                sortColumn: sortId,
                sortDirection: $('#eventsTable tr th.sortcol').attr('class').split(' ').slice(-1),
                islocal: islocal
            });
        });
        $(".myLnPaginateLinkInt").click(function() {
            $('#eventResultsInt').html(myLn.loading());
            var sortId = $('#eventsTableInt tr th.sortcol a').attr('id').substring(0, $('#eventsTableInt tr th.sortcol a').attr('id').length - 1);
            var islocal = $('#eventsTableInt tr th.sortcol a').attr('id').substring($('#eventsTableInt tr th.sortcol a').attr('id').length - 1, $('#eventsTableInt tr th.sortcol a').attr('id').length);
            $('#' + myLn.placeHolder).load('/myln/user/paging',
            {
                targetPage: getID($(this).attr('id')),
                sortColumn: sortId,
                sortDirection: $('#eventsTableInt tr th.sortcol').attr('class').split(' ').slice(-1),
                islocal: islocal
            });
        });
        $(".myLnSortLink").click(function() {
            $('#eventResults').html(myLn.loading());
            var sortColumnString = $(this).attr('id').substring(0, $(this).attr('id').length - 1);
            var islocal = $(this).attr('id').substring($(this).attr('id').length - 1, $(this).attr('id').length);
            var sortDir = ($(this).parent().hasClass("ascending")) ? "descending" : "ascending";
            $('#' + myLn.placeHolder).load('/myln/user/paging',
            {
                targetPage: "1",
                sortColumn: sortColumnString,
                sortDirection: sortDir,
                islocal: islocal
            });
        });
    }
};

myLn.myAccount = {
    url: '/myln/account/landing',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.myAccount.url);
    },
    submitForm: function(form) {
        myLn.myAccount.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.myAccount.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.register = {
    url: '/myln/register/landing',
    formSubmitted: false,
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.register.url);
    },
    submitForm: function(form) {
        myLn.register.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.register.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
        myLn.errorcount = 0;
    }
};

myLn.resendEmail = {
    url: '/MyLn/login/ResendEmail',
    loadForm: function() {
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.resendEmail.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.resendEmail.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.resendEmailLayer = {
    url: '/MyLn.ln/login/ResendEmailLayer',
    loadForm: function() {
        $('#' + myLn.LoginLayerTarget).html(myLn.loading()).load(myLn.resendEmailLayer.url);
    },
    submitForm: function(form) {
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.resendEmailLayer.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.LoginLayerTarget).html(resp);
    }
};

myLn.unsubscribe = {
    url: '/UnSubscription/Unsubscribe',
    formSubmitted: false,
    loadForm: function() {
        //$('#' + myLn.placeHolder).load(myLn.unsubscribe.url);
        $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.unsubscribe.url);
    },
    submitForm: function(form) {
        myLn.unsubscribe.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.unsubscribe.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.unsubscribeNonMem = {
url: '/UnSubscription/UnsubscribeNonMem',
    formSubmitted: false,
    loadForm: function() {
        //    $('#' + myLn.placeHolder).html(myLn.loading()).load(myLn.unsubscribeNonMem.url);
        $('#' + myLn.placeHolder).load(myLn.unsubscribeNonMem.url);
    },
    submitForm: function(form) {
        myLn.unsubscribeNonMem.formSubmitted = true;
        var action = form.attr('action');
        var serialized = form.serialize();
        $.post(action,
			serialized,
			myLn.unsubscribeNonMem.response
		);
    },
    response: function(resp, status) {
        scroll(0, 0);
        $('#' + myLn.placeHolder).html(resp);
    }
};

myLn.redirect = {
    redirectUrl: '',
    onLoad: function() {
        window.location = myLn.redirect.redirectUrl;
    }
};



function validateItem(value, name) {

    $.ajax({
        type: "GET",
        url: "/myln/login/validate",
        data: "&Name=" + name + "&Value=" + value,
        cache: false,
        success: function(message) {

            var errors = message.toString().split('*');

            var html = "";

            for (i = 0; i < errors.length; i++) {
                html += errors[i].toString().substring(0, (errors[i].length));
            }

            $('#errorHtml_' + name).html(html);
        }
    });

    return false;
}

function validatePhone(value) {
    if (value.toString().length > 0) {
        $('#smsSub').removeAttr("disabled");
    }
    else {
        $('#smsSub').attr('checked', false);
        $('#smsSub').attr('disabled', 'true');
    }
    return false;
};

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        //            else if (tag == 'select')
        //                this.selectedIndex = -1;

        $('#smsSub').attr('checked', false);
        $('#smsSub').attr('disabled', 'true');
        $('#submit').attr('disabled', 'true');
    });
};


//counts number of checked subscriptions in a tab, then updates tab counter and tooltip
function countCheckedSubscriptions(idIn) {
    var mid = getID(idIn);
    var checkedCount = $('#tabContent_' + mid + ' .cbSub:checked').size();
    if (checkedCount > 0) {
        var tabText = $('#tab_' + mid + ' div.m').html();
        tabText = tabText.replace('<span></span>', '');
        $('#tab_' + mid + ' span').html(' (' + checkedCount + ')');
        $('#tab_' + mid).attr('title', $('#CommunitySubscriptionToolTip1').val() + ' ' + checkedCount + ' ' + $('#CommunitySubscriptionToolTip2').val() + ' ' + tabText);
    }
    else {
        $('#tab_' + mid + ' span').html('');
        $('#tab_' + mid).attr('title', $('#NoCommunitySubscriptionToolTip').val());

    }

    if (mid == 0) {
        //$('#tab_' + mid + ' span').html('');

        $('#tab_' + mid).attr('title', $('#SubscriptionsToolTip').val());
    }

    //    $('#tab_' + mid).tooltip();
}

function showTab(id) {
    $('.tabContent').hide();
    $('.tab').removeClass('selected');
    $('.mapLink').removeClass('selected');
    var mid = getID(id);
    $('#tabContent_' + mid).show();
    $('#tab_' + mid).addClass('selected');
    $('#map_' + mid + ' a').addClass('selected');
    //HACK: 
    //major hack alert, either need to move these into web.config or change sentinel to identify communities as 'asia/europe'
    if ((mid == "8f1cfc20-5b3b-41f8-a188-0111414c9006") || (mid == "21255103-8ec3-4782-b1d3-5614615897eb")) {
        switchToAsia();
    }
    else {
        switchToEurope();
    }
}

function getID(idIn) {
try{
    var idArr = idIn.split('_');
    var id = idArr[1];
    return id;
    }
    catch(err) {
    return 0;
    }
}

//shows the 'save info' box
function showInfo() {
    $('input#btnUpdate').removeAttr("disabled");
    $('input#btnCancel').removeAttr("disabled");
    $('.saveInfo').show();
    return false;
}

//checks for any mailing lists that appear more than one when clicked 
//and updates duplicates to true/false as necessary
function searchForDuplicates(id, isChecked) {
    $('#tabs .cbSub').each(function() {
        if ($(this).attr('id').indexOf('cb_' + getID(id) + '_') == 0) {
            this.checked = isChecked;
            countCheckedSubscriptions($(this).parents('div.tabContent').attr('id'));
        }
    });
}


function setOmnitureAMGPageView(s_account, pageName) {

	var s = s_gi(s_account);
	s.pageName = pageName;


	/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/

	var s_code = s.t(); if (s_code) document.write(s_code)

}

function setOmnitureAMGPageEventView(s_account, pageName, event) {

	var s = s_gi(s_account);
	s.pageName = pageName;
	s.events = event;

	/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/

	var s_code = s.t(); if (s_code) document.write(s_code)

}

function setOmniturePageView(s_account, pageName, channel, prop1, prop3) {

    var s = s_gi(s_account);
    s.pageName = pageName;
    s.channel = channel;
    s.prop1 = prop1;
    s.prop2 = "LN";
    s.prop3 = prop3;

    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/

    var s_code = s.t(); if (s_code) document.write(s_code)

   }

   function setOmniturePageEventView(s_account, pageName, channel, prop1, prop3, event) {

   	var s = s_gi(s_account);
   	s.pageName = pageName;
   	s.channel = channel;
   	s.prop1 = prop1;
   	s.prop2 = "LN";
   	s.prop3 = prop3;
   	s.events = event;

   	/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/

   	var s_code = s.t(); if (s_code) document.write(s_code)

   }

   function setOmnitureEvent(s_account,event) {

   	var s = s_gi(s_account);
   	s.prop2 = "LN";
   	s.events = event;

   	/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/

   	var s_code = s.t(); if (s_code) document.write(s_code)

   }

function switchToEurope() {
    $('#lnMap').removeClass('asia');
    $('#lnMap').addClass('europe');
    $('.europe_item').show();
    $('.asia_item').hide();
}
function switchToAsia() {
    $('#lnMap').removeClass('europe');
    $('#lnMap').addClass('asia');
    $('.europe_item').hide();
    $('.asia_item').show();
}


//these settings are for MVC sites only
//TODO: this will need to be configurable by the 'host' site
myLn.home.url = '/myln/user/landing';
myLn.login.url = '/myln/login/landing';
myLn.loginlayer.url = '/myln/login/loginlayer';
myLn.logout.url = '/myln/login/logout';
myLn.resendEmail.url = '/myln/login/ResendEmail';
myLn.resendEmailLayer.url = '/myln/login/ResendEmailLayer';
myLn.forgotPassword.url = '/myln/login/ForgotPassword';
myLn.forgotPasswordLayer.url = '/myln/login/ForgotPasswordLayer?height=300&width=300';
myLn.register.url = '/myln/register/landing';
// m.g. - throws error, doesnt exist nowhere in the code
//myLn.subscriptions.url = '/myln/subscription/landing';
myLn.myAccount.url = '/myln/account/Landing';
myLn.activate.url = '/myln/register/activate';
myLn.changeEmail.url = '/myln/account/ChangeEmail';
myLn.unsubscribe.url = '/UnSubscription/Unsubscribe';
myLn.unsubscribeNonMem.url = '/UnSubscription/UnsubscribeNonMem';
myLn.addNickname.url = '/myln/login/addnickname';
myLn.addNicknameLayer.url = '/myln/login/addnicknamelayer';
myLn.faves.url = '/myln/user/faves';
myLn.faveSearch.url = '/myln/user/FaveSearch'; //unused?
myLn.deactivateAccount.url = '/myln/login/DeactivateAccount';

