﻿function makeFakeSEOObj() { };
makeFakeSEOObj.prototype.ShouldAlert = function() {
    return false; /// Set this to true only when testing...
}    
makeFakeSEOObj.prototype.t = function() {
    if (this.ShouldAlert()) {
        alert("Fake SEO called t(); " + Object.toJSON(this));
    }
};
makeFakeSEOObj.prototype.tl = function() {
    if (this.ShouldAlert()) {
        alert("Fake SEO called tl(); " + Object.toJSON(this));
    }
};
// Capture FakeSEOObj in closure so there will be only one FakeSEOObj instance returned from getSEOobj
getSEOobj = (function() {
    var FakeSEOObj = new makeFakeSEOObj();
    //function getSEOobj () {
    return function() {
        // if s undefined  and undeclared (upon invocation) will throw error.
        try {
            if (s == undefined) {
                return FakeSEOObj;
            } else {
                return s;  //Omniture obj
            }
        } catch (e) {
            return FakeSEOObj;
        }
    }
})();

function SEOError(seo,e){
    //FIXME: I'm not sure what to do here..
    //This should never happen.
    //However, we should notify SOMETHING if the impossible happens.
    alert("SEO An Error Occured that should never happen");
}

//SearchSEODataContract.cs
function SearchSEO(data) {
    var seo = getSEOobj();
    try {
        seo.events = "event1";
        seo.prop4 = data.State;
        seo.prop5 = data.City;
        seo.prop6 = data.ZipCode;
        seo.prop7 = data.SearchRadius;
        seo.prop8 = data.Price;
        seo.prop9 = data.Bedrooms;
        seo.prop10 = data.PetFriendly;
        seo.prop11 = data.MoveInDate;
        seo.prop12 = data.CurrentDate;
        seo.prop13 = data.Remember;
        seo.prop14 = "apartment basic search";
        seo.prop15 = data.NumResults;
        //Fire of SEO script
        seo.t();
    } catch(e){SEOError(seo,e)}
}

function AddApartmentToComparisonListSEO(ApartmentID) {
    var seo = getSEOobj();
    try {
        seo.eVar15 = ApartmentID;
        seo.events = "event2";
        seo.tl();
    } catch(e){SEOError(seo,e)}
}
function ApartmentDetailActionSEO(action) {
    var seo = getSEOobj();
    try {
        switch (action){
            case "photo":
                seo.pageName = seo.pageName.replace(/\w*-.*$/, '') + " - Photos";
                break;
            case "map":
                seo.pageName = seo.pageName.replace(/\w*\-.*$/, '') + " - Map";
                break;
            case "siteplan":
                seo.pageName = seo.pageName.replace(/\w*-.*$/, '') + " - Site Plan";
                break;
            case "video":
                seo.pageName = seo.pageName.replace(/\w*-.*$/, '') + " - Video";
                break;
        }
        seo.eVar22 = action;
        seo.prop22 = action;
        seo.events = "event9";
        seo.t(); //new page
    } catch (e) { SEOError(seo, e) }
}
function PrintFunctionSEO() {
    var seo = getSEOobj();
    try {
        seo.eVar17 = seo.pageName;
        seo.events = "event4";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function ContactSubmissionSEO() {
    var seo = getSEOobj();
    try {
        seo.events = "event8";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function SendToFriendSubmissionSEO() {
    var seo = getSEOobj();
    try {
        seo.events = "event8";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function RentNowSEO(floorplanOrID) {
    var seo = getSEOobj();
    try {
        seo.eVar23 = floorplanOrID;
        seo.events = "event10";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function RentNowNoFloorplanSEO() {
    RentNowSEO(-1);
}
//This function will eventually be no more
function FloorPlanSEO(floorplanOrID) {
    var seo = getSEOobj();
    try {
        seo.eVar23 = floorplanOrID;
        seo.events = "event11";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
/*
MapArbitrarySearchSEO
*/
function MapSearchSEO(keywords) {
    var seo = getSEOobj();
    try {
        seo.prop33 = keywords;
        seo.eVar33 = keywords;
        seo.events = "event9";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function MapArbitrarySearchSEO(keywords) {
    var seo = getSEOobj();
    try {
        seo.prop34 = keywords;
        seo.eVar34 = keywords;
        seo.events = "event9";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function ContactByEmailLeadSubmissionSEO() {
    var seo = getSEOobj();
    try {
        seo.prop21 = "contact by email";
        seo.eVar21 = "contact by email";
        seo.events = "event7";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
function MetroCommunityViewClickSEO() {
    var seo = getSEOobj();
    try {
        seo.events = "event14";
        seo.tl();
    } catch (e) { SEOError(seo, e) }
}
Event.observe(window, 'load', function() {
    ///using prototypes $$ function, find all contactLeadSEO classed elements and add an on-click event
    //This only works on page load, for ajax things get more complicated.
    $$('.contactLeadSEO').each(function(o) {
        Event.observe(o, 'click', function() {
            ContactByEmailLeadSubmissionSEO();
        });
    });
    $$('.rentnowLeadSEO').each(function(o) {
        Event.observe(o, 'click', function() {
            RentNowNoFloorplanSEO();
        });
    });
    $$('.comlistPropFooter a').each(function(o) {
        Event.observe(o, 'click', function() {
            MetroCommunityViewClickSEO();
        });
    });
});