﻿
function Advert() {

    this.showSky = function (elementId) {
    };
    this.showMpu = function (elementId) {
    };
    this.showBanner = function (elementId) {
    };
    this.showBottomAds = function (elementId) {
    };
    this.showButtonA = function (elementId) {
    };
    this.showButtonB = function (elementId) {
    };
    this.showSignOut = function (elementId) {
    };
}

function FieldMap(adServerField, FRDField, isDefault) {
    this.adServerField = adServerField;
    this.FRDField = FRDField;
    this.isDefault = isDefault == undefined ? false : true;
}

function IsNumeric(input) {
    return ((input - 0) == input && (input.length > 0));
}

function Lookup(id, mappings) {
    var length = mappings.length;
    var field;
    var adServerField = "x"; //coupled
    var i = 0;

    if (id != undefined) {
        id = id.toLowerCase();
    }

    for (i = 0; i < length; i++) {
        field = mappings[i];

        if (field.isDefault != undefined) {
            adServerField = field.isDefault == true ? field.FRDField : adServerField;
        }

        if (id != undefined) {
            if (field.FRDField.indexOf(id) > -1) {
                adServerField = field.adServerField.toString();
                break;
            }
        }
        else {
            break;
        }

    }
    return adServerField;
}

// UK inherits from Advert
function UK(fields) {

    //Advert.call(fields);

    this.writeIFrame = function (elementId, width, height) {
        document.write
        (
            "<iframe id=\"" + elementId + "\" src = \"\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"" + width + "\" height=\"" + height + "\"></iframe>"
        );
    };

    this.updateIFrameSrc = function (elementId, url) {
        jQuery("#" + elementId).attr("src", url);
    };

    // mappings:
    var ageMapping = function (age) {
        var ageRange = "x";
        if (age >= 18 && age <= 29) {
            ageRange = "18";
        }
        else if (age >= 30 && age <= 39) {
            ageRange = "30";
        }
        else if (age >= 40 && age <= 49) {
            ageRange = "40";
        }
        else if (age >= 50 && age <= 59) {
            ageRange = "50";
        }
        else if (age > 60) {
            ageRange = "60";
        }

        return ageRange;
    };

    var iDMappings =
    [
        new FieldMap(fields["username"] != "" ? "home.loggedin" : "root", "", true),
        new FieldMap("home.loggedin", "memberhome,newfriends,join"),
        new FieldMap(fields["username"] != "" ? "profile" : "registration", "viewprofile,regedit_basic,regedit_aboutyou,regedit_yrprofile,regedit_match,regedit_photo,regedit_extended,myaccount,membersettings"),
        new FieldMap("search", "searchresults,sitenamesearch,whosonline,photogallery,audioprofilesearch"),
        new FieldMap("viewinghistory", "memberview,favourites,memberblock"),
        new FieldMap("whosonline", "whosonline"),
        new FieldMap("instantchat", "friendsmessenger"),
        new FieldMap("horoscopes", "horoscopes"),
        new FieldMap("datersdiary", "datersdiary"),
        new FieldMap("mail", "inbox"),
        new FieldMap("signout", "signout")
    ];

    var genderMappings = [new FieldMap("x", ""), new FieldMap("m", "m"), new FieldMap("f", "f")];
    var maritalStatusMappings = [new FieldMap("x", ""), new FieldMap("y", "y"), new FieldMap("n", "n")];
    var childrenMappings = [new FieldMap("x", ""), new FieldMap("n", "n"), new FieldMap("y", "y")];
    var smokingMappings = [new FieldMap("x", "-1"), new FieldMap("n", "1"), new FieldMap("y", "2"), new FieldMap("y", "4"), new FieldMap("y", "8")];

    // get the zone settings
    var zone = Lookup("", iDMappings); ; // set default
    // find if this is an mvc page:
    if (window.location.href.indexOf(".asp") > -1) {
        // non mvc
        zone = Lookup(fields["siteArea"], iDMappings);
    }
    else {
        // find controller?
        var pathSections = location.pathname.split("/");
        if (pathSections.length > 1) {
            if (pathSections[1] != "") {
                zone = pathSections[1];
            }
        }
    }

    //set "zone" values.
    this.Zone = zone;

    // get the key values 
    this.KeyValues = "gender=" + Lookup(fields["gender"], genderMappings) + ";age=" + ageMapping(fields["age"]) + ";mar=" + Lookup(fields["married"], maritalStatusMappings) + ";kid=" + Lookup(fields["children"], childrenMappings) + ";reg=" + fields["region"] + ";smo=" + Lookup(fields["smoking"], smokingMappings) + ";pc=" + fields["postcode"];

}

// inheritence
UK.prototype = new Advert();
UK.prototype.constructor = UK;
UK.prototype.showBanner = function (elementId) {
    this.writeIFrame(elementId, 728, 90);
};
UK.prototype.showSky = function (elementId) {
    this.writeIFrame(elementId, 160, 600);
};
UK.prototype.showMpu = function (elementId) {
    this.writeIFrame(elementId, 300, 250);
};
UK.prototype.showBottomAds = function (elementId) {
    this.writeIFrame(elementId, 728, 90);
};
UK.prototype.showButtonA = function (elementId) {
};
UK.prototype.showBottonB = function (elementId) {
};
UK.prototype.showSignOut = function (elementId) {
    this.writeIFrame(elementId, 640, 480);
};

// australia
function Australia(fields) {


    // set australia mappings  
    var genderMappings = [new FieldMap("male", "m"), new FieldMap("female", "f")];
    var maritalStatusMappings = [new FieldMap("single", "n"), new FieldMap("married", "y")];
    var childrenMappings = [new FieldMap("nochildren", "n"), new FieldMap("children", "y")];
    var smokingMappings = [new FieldMap("nonsmoker", "1"), new FieldMap("smoker", "2"), new FieldMap("smoker", "4"), new FieldMap("smoker", "8")];

    var args = {};

    args.area = "DATING";

    if (IsNumeric(fields["age"])) {
        args.age = fields["age"] < 1 ? "" : fields["age"];
    }

    var gender = Lookup(fields["gender"], genderMappings);
    if (gender != "x" || gender != "") {
        args.gender = gender;
    }

    var marritalStatus = Lookup(fields["married"], maritalStatusMappings);
    if (marritalStatus != "x" || marritalStatus != "") {
        args.marritalStatus = marritalStatus;
    }

    var children = Lookup(fields["children"], childrenMappings);
    if (children != "x" || children != "") {
        args.childrenStatus = children;
    }

    var smoking = Lookup(fields["smoking"], smokingMappings);
    if (smoking != "x" || smoking != "") {
        args.smokingStatus = smoking;
    }

    if (fields["postcode"] != "") {
        args.postCode = fields["postcode"];
    }

    MediaSmart.Utilities.initialise(args);
    jQuery(document).ready(function () {
        MediaSmart.insertAllAds();
    });

}

// inheritence
Australia.prototype = new Advert();
Australia.prototype.constructor = Australia;
Australia.prototype.showBanner = function () {

    MediaSmart.newAd({
        id: "728X90_1",
        width: 728,
        height: 90,
        position: 1
    });

};
Australia.prototype.showSky = function () {

    MediaSmart.newAd({
        id: "160X600_1",
        width: 160,
        height: 600,
        position: 1
    });

};
Australia.prototype.showMpu = function () {

    MediaSmart.newAd({
        id: "300X250_1",
        width: 300,
        height: 250,
        position: 1
    });

};
Australia.prototype.showBottomAds = function () {

    MediaSmart.newAd({
        id: "728X90_1",
        width: 728,
        height: 90,
        position: 1
    });

};
Australia.prototype.showButtonA = function () {

    MediaSmart.newAd({
        id: "1X1_1",
        width: 1,
        height: 1,
        position: 1,
        iframeWidth: 0,
        iframeHeight: 0
    });
};
Australia.prototype.showBottonB = function () {
};

Australia.prototype.mediaSmartSkin = function () {
    MediaSmart.newAd({
        id: "4X1_1",
        width: 4,
        height: 1,
        position: 1,
        iframeWidth: 0,
        iframeHeight: 0
    });
};


// add to namespace
FRD.Adverts = {};
FRD.Adverts.UK = UK;
FRD.Adverts.Australia = Australia;



