
function LMS () {}

LMS.ResourceCenter = new ResourceCenter(Environment);
LMS.QueryString    = new PMQueryString();

LMS.webId        = LMS.QueryString.get("MM_webID");
LMS.keyCode      = LMS.QueryString.get("keyCode");
LMS.restrictions = LMS.QueryString.get("aff_res");
LMS.stateCode    = LMS.QueryString.get("state");

// NOTE:
// not sure these are even used
// - SL
LMS.customQueryString        = LMS.QueryString.getQueryString([ "MM_webID", "src" ]);
LMS.libertyMutualQueryString = LMS.QueryString.getQueryString();

LMS.urlLookup = function (key, skip_personalization) {
	if (skip_personalization == true) {
		return LMS.ResourceCenter.urlLookup(key);
	}
    return LMS.addPersonalizationToUrl(LMS.ResourceCenter.urlLookup(key));
};

LMS.addPersonalizationToUrl = function (url, options) {
	if (options == undefined) {
		options = {};
	}
    var extra_query = [];
    if (LMS.webId) {
        extra_query.push("MM_webID=" + LMS.webId);
    }
    if (LMS.keyCode) {
        extra_query.push("keyCode=" + LMS.keyCode);
    }
    if (options['include_aff_res'] && LMS.restrictions) {
		extra_query.push("aff_res=" + LMS.restrictions);
    }

    if (extra_query.length) {
        var questionIndex = url.indexOf("?");
    	if (questionIndex == -1) {
            url += '?';
    	}
    	else {
    		url += '&';
    	}
        url += (extra_query.join('&'));
    }

    return url;
};

LMS.fixPolicyTypeForRestrictions = function (policyTypeSelect) {
    
    var find_index = function (value_to_match) {
        for (var i = 0; i < policyTypeSelect.options.length; i++) {
            if (policyTypeSelect.options[i].value == value_to_match) {
                return i;
            }
        }
        return -1;
    };
    
    // auto restricted
    if (LMS.restrictions.indexOf('a') != -1) {
        policyTypeSelect.options[ find_index("Auto") ] = null;
        policyTypeSelect.options[ find_index("Motorcycle") ] = null;        
    }
    // home restricted
    if (LMS.restrictions.indexOf('h') != -1) {
        policyTypeSelect.options[ find_index("H3") ] = null; // home
        policyTypeSelect.options[ find_index("H6") ] = null; // condo
        policyTypeSelect.options[ find_index("H4") ] = null; // renters
        policyTypeSelect.options[ find_index("Mobile Home") ] = null;
    }    
    // life restricted
    if (LMS.restrictions.indexOf('l') != -1) {
        policyTypeSelect.options[ find_index("Life") ] = null;
    }    
};

// Omniture traking stuff

LMS.MangeYourPoliciesTracking = function () {
	var s=s_gi(LMS.ResourceCenter.getOmnitureReportSuiteId());
	s.linkTrackVars="events";
	s.linkTrackEvents='event7';
	s.events='events7';
	s.tl("button","o","Manage Your Policies login button");
};

LMS.SupplementOfferTracking = function () {
	var s=s_gi(LMS.ResourceCenter.getOmnitureReportSuiteId());
	s.linkTrackVars="prop29";
	s.prop29= s.pageName? s.pageName : "articlePageName";
	s.tl("link","o","Supplements");
};

LMS.OpenSearchResultsInNewWindow = function () {
	var url = $(this).attr('href');
	
	// anything not local ...
	if (url.indexOf(document.domain) == -1) {
		$(this).attr({ 'target' : '_blank' });
	}
	
	if (url.indexOf("libertymutual.com") != -1) {
		$(this).attr({ 'href' : LMS.addPersonalizationToUrl(url) });
	}
	else if (url.indexOf("libertymutualonline.com/your-protection") != -1 ||
			 url.indexOf("youcovered.com")                          != -1 ){
		$(this).attr({ 'href' : LMS.addPersonalizationToUrl(url, { 'include_aff_res' : true }) });
	}
};

LMS.findAnAgentSubmit = function (form) {
    var url = "http://www.libertymutual.com/sales-representative-search-results";
    form.action = url;
    form.submit();
};

// setup all the URLs from the ResourceCenter
$(document).ready(function () {

    $('#AutoLanding_footer').attr({ 'href' : LMS.urlLookup('AutoLanding') });
    $('#AutoLanding_header').attr({ 'href' : LMS.urlLookup('AutoLanding') });

    $('#HomeLanding_header').attr({ 'href' : LMS.urlLookup('HomeLanding') });
    $('#HomeLanding_footer').attr({ 'href' : LMS.urlLookup('HomeLanding') });
    $('#HomeLanding_offer104').attr({ 'href' : LMS.urlLookup('HomeLanding') });

    $('#CorpLifeLanding_header').attr({ 'href' : LMS.urlLookup('CorpLifeLanding') });
    $('#CorpLifeLanding_footer').attr({ 'href' : LMS.urlLookup('CorpLifeLanding') });

    $('#ClaimsLanding_header').attr({ 'href' : LMS.urlLookup('ClaimsLanding') });

    $('#LibertyMutualGroup_header').attr({ 'href' : LMS.urlLookup('LibertyMutualGroup') });
    $('#LibertyMutualGroup_footer').attr({ 'href' : LMS.urlLookup('LibertyMutualGroup') });

    $('#Careers_header').attr({ 'href' : LMS.urlLookup('Careers') });

    $('#InvestorRelations_header').attr({ 'href' : LMS.urlLookup('InvestorRelations') });

    $('#InsuranceForBusiness_header').attr({ 'href' : LMS.urlLookup('InsuranceForBusiness') });
    $('#InsuranceForBusiness_footer').attr({ 'href' : LMS.urlLookup('InsuranceForBusiness') });

    $('#ContactUs_header').attr({ 'href' : LMS.urlLookup('ContactUs') });

    $('.personalized_url').each(function () {
        $(this).attr({ 'href' : LMS.addPersonalizationToUrl( $(this).attr('href') ) });
    });

	$('#rightcolumn .offer a').each(function () { $(this).click(LMS.SupplementOfferTracking) });
	
	$('.breadcrumb a').each(function () {
		var url = $(this).attr('href');
		if (url && url.indexOf("libertymutual.com") != -1) {
			$(this).attr({ 'href' : LMS.addPersonalizationToUrl(url) });
		}
	});

});
