﻿
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){
    } 
}

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 i = mappings.length
    var field
    var adServerField = "x"; //coupled
    
    if (id != undefined) {
        id = id.toLowerCase();
    }

    while (i--){
          field = mappings[i];
          adServerField = field.isDefault == true ? field.FRDField : adServerField;
          if (field.FRDField.indexOf(id) > -1){
            adServerField =  field.adServerField.toString();
            break;
          } 
    }
    return adServerField;
} 


// UK inherits from Advert
function UK(domain, folder, page, fields){

    //Advert.call(this, domain, folder, page, fields);

    this.adServer = domain;

    // 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";
        }
        else{
        }         
        return ageRange;
    }     

    var iDMappings = 
    [
        new FieldMap("home","", 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")
    ];
   
    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 page settings
    page = page + Lookup(fields["siteArea"], iDMappings) + ";";
    
    // get paramters           
    var parameters = "gen=" + 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"];
    
    // construnct the url 
    var url = domain + folder + page + parameters + ";{dimensions};ord={ord}";

    this.advertURL = function(){
        return (url);
    }  
    // temp
    this.advertURLNoParameters = function(){
        return (domain + folder + page +  ";{dimensions};ord={ord}");
    }    
}

// inheritence
UK.prototype = new Advert();
UK.prototype.constructor = UK;
UK.prototype.showBanner = function(elementId){
   if(jQuery("#" + elementId).length == 0){
       document.write(
            "<iframe id=\"" + elementId + "\" src = \"http://" + window.location.host + "/WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "tile=1;pos=1;dcopt=ist;sz=1x1,468x60,728x90").replace("{ord}", Math.random() * 100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"728\" height=\"90\"></iframe>"
       );
   }
}
UK.prototype.showSky = function(elementId){
   if(jQuery("#" + elementId).length == 0){
      	document.write(
      	    "<iframe id=\"" + elementId + "\" src = \"WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "tile=4;pos=4;sz=120x600,160x600").replace("{ord}", Math.random()*100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"160\" height=\"600\"></iframe>"
      	);
   }
}
UK.prototype.showMpu = function(elementId){
    if(jQuery("#" + elementId).length == 0){
        document.write(
            "<iframe id=\"" + elementId + "\" src = \"WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "tile=2;pos=2;sz=300x600,300x250").replace("{ord}", Math.random()*100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"300\" height=\"250\"></iframe>"
        );
   }
}
UK.prototype.showBottomAds = function(elementId){
    if(jQuery("#" + elementId).length == 0){
       document.write(
            "<iframe id=\"" + elementId + "\" src = \"http://" + window.location.host +"/WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "tile=3;pos=3;sz=468x60,728x90").replace("{ord}", Math.random()*100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"728\" height=\"90\"></iframe>"
        );
   }
}
UK.prototype.showButtonA = function(elementId){
    if(jQuery("#" + elementId).length == 0){
        document.write(
            "<iframe id=\"" + elementId + "\" src = \"WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "120x60").replace("{ord}", Math.random()*100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"120\" height=\"60\"></iframe>"
        );
   }
}
UK.prototype.showBottonB = function(elementId){
    if(jQuery("#" + elementId).length == 0){
        document.write(
            "<iframe id=\"" + elementId + "\" src = \"WCContent/adverts/advert.htm?url=" + this.advertURL().replace("{dimensions}", "120x60").replace("{ord}", Math.random()*100000000000000000) + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"120\" height=\"60\"></iframe>"
        );
   }
}
   
// 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(){
}

// add to namespace
FRD.Adverts = {};
FRD.Adverts.UK = UK;
FRD.Adverts.Australia = Australia;


