﻿function writeDFPAd(dimensions) {

    var indDelim = dimensions.indexOf('x');
    var len = dimensions.length;
    var width = dimensions.substring(0, indDelim);
    var height = dimensions.substring(indDelim + 1, len);

    var thisUrl = location.href;
    thisUrl = thisUrl.toLowerCase();
           
    var videoUrl = /videos.cineplex.com/;
    var videofrUrl = /videosfr.cineplex.com/;
    var tixUrl = /onlineticketing.cineplex.com/;
    var movieDetailsURLEN = /movies\/moviedetails/;
    var theatreDetailsURLEN = /theatredetails/;
    var movieDetailsURLFR = /films\/detailsfilms/;
    var theatreDetailsURLFR = /detailscinemas/;
    var movieListingsURLEN = /movies.aspx/;
    var movieListingsURLFR = /films.aspx/;
    var theatreListingsURLEN = /theatres/;
    var theatreListingsURLFR = /cinemas/;
    
    var blogEN = /news/;
    var blogFR = /nouvelles/;
    var contest = /contests/;
    var corpSales = /corporatesales/;
    var event = /events/;
    var scene = /promos\/scene.aspx/;

    var zoneName = "Homepage";
       
    if (thisUrl.search(videoUrl) != -1) {
        zoneName = "Video";
    }
    else if (thisUrl.search(videofrUrl) != -1) {
        zoneName = "Video";
    }    
    else if (thisUrl.search(tixUrl) != -1) {
        zoneName = "Ticketing";
    }
    else if (thisUrl.search(movieDetailsURLEN) != -1 || thisUrl.search(movieDetailsURLFR) != -1) {
        zoneName = "MovieDetails";
    }
    else if (thisUrl.search(movieListingsURLEN) != -1 || thisUrl.search(movieListingsURLFR) != -1) {
        zoneName = "MovieListings";
    }
    else if (thisUrl.search(theatreDetailsURLEN) != -1 || thisUrl.search(theatreDetailsURLFR) != -1) {
        zoneName = "TheatreDetails";
    }
    else if (thisUrl.search(theatreListingsURLEN) != -1 || thisUrl.search(theatreListingsURLFR) != -1) {
        zoneName = "TheatreListings";
    }
    else if (thisUrl.search(blogEN) != -1 || thisUrl.search(blogFR) != -1) {
        zoneName = "Blog";
    }
    else if (thisUrl.search(contest) != -1) {
        zoneName = "Contest";
    }
    else if (thisUrl.search(corpSales) != -1) {
        zoneName = "CorpSales";
    }
    else if (thisUrl.search(event) != -1) {
        zoneName = "Events";
    }
    else if (thisUrl.search(scene) != -1) {
        zoneName = "Scene";
    }
	
	if (document.location.hostname != "undefined" && document.location.hostname != "") //&& document.location.hostname != "localhost"
    {        
        if (document.location.hostname.toLowerCase().indexOf("ticket") != -1 || document.location.hostname.toLowerCase().indexOf("spiderman") != -1 || location.href.toLowerCase().indexOf("iframeheader.aspx?tm=1") != -1) {
            zoneName = "Ticketing";
        }
    }

    var pageName = window.location.pathname.toLowerCase();
    
    //begin code for local environment testing
    //pageName = pageName.replace('/cineplex/', '/');
    //end code for local environment testing

    pageName = encodeURIComponent(pageName);

    var protocol = window.location.protocol;
    var ord = window.ord || Math.floor(Math.random() * 1e16);
    var lang;
    var adLang = readHbxLanguageCookie('Cineplex_Language');
    if (adLang == "en-us") {
        lang = "eng"
    }
    if (adLang == "fr-ca") {
        lang="fre"
    }
    var strScript;
    strScript = "<a style=\"border:none;\" href=\""+protocol+"//ad.doubleclick.net/N7226/jump/cplx.com_"+zoneName+"_"+lang+"/" + width + "x" + height + ";sz="+width+ "x" + height + ";ord=" + ord + ";page="+ pageName +"?\">";
    strScript += "<img style=\"border:none;\" src=\""+ protocol+"//ad.doubleclick.net/N7226/ad/cplx.com_"+zoneName+"_"+lang+"/" + width + "x" + height + ";sz=" +width+ "x" +height+ ";ord=" + ord + ";page="+ pageName +"?\" width=\"" + width + "\" height=\"" + height + "\" />";
    strScript += "</a>";
    document.write(strScript);
 }
