"use strict"

function get_page_ad_source(pos) {
    
	var source, testRE;

    if (document.location == null) {
        return 's1homes-global';
    }
    
    if (document.location.hostname.indexOf('s1homes') != -1) {
	    source = 's1homes-';
	    testRE = new RegExp ("(/[^/]*/?)", "i");

	    switch (document.location.pathname.match(testRE)[0]) {
			case "/" :  source += 'homepage'; return source;
			case "/myaccount/" : true;
			case "/property-for-sale/" : source += 'buying-'; break;
		    case "/newhomes/" : source += 'newhomes-'; break;
			case "/selling/" : source += 'selling-'; break;
			default: return 's1homes-buying-global';
		}
	} else if (document.location.hostname.indexOf('s1rental') != -1) {
	    source = 's1rental-';
	} else {
        return 's1homes-buying-global';
    }

    testRE = new RegExp("^/([^/]+/)?(index.s?html)?$");

    if (document.location.pathname.match(testRE)) {
	    source += 'homepage';
	} else if (document.location.search.match(/^.*location=([^&;]+).*$/)) {
	    source += 'location-' + document.location.search.replace(/^.*location=([^&;]+).*$/, "$1");
	} else {
	    source += 'global';
	}

    return source;
}

function OAS_AD(pos) {
    var advertPos = pos;
	
	var OAS_url = 'http://oas.newsquestdigital.co.uk/RealMedia/ads/',
        OAS_sitepage,
        OAS_sitepage = 'www.s1homes.com/ads/' + get_page_ad_source(pos),
        OAS_listpos = 'Top,Right,x01,x02,x10,x11,x12,x13,x14,x15,x16,x17',
        OAS_version = 11,
        OAS_rns = (Math.random() + "").substring(2, 11),
        ad_id = 'mpu_target',
        parent_ad_id = 'mpu';

    if (pos == 'Top') {
        ad_id = 'leaderboard_target';
        parent_ad_id = 'leaderboard';
    } else if (pos == 'Right') {
        ad_id = 'skyscraper_target';
        parent_ad_id = 'skyscraper';
    } else if (pos == 'x10') {
        ad_id = 'additionalBanner1_target';
        parent_ad_id = 'additionalBanner1';
    } else if (pos == 'x11') {
        ad_id = 'additionalBanner2_target';
        parent_ad_id = 'additionalBanner2';
    } else if (pos == 'x17') {
        ad_id = 'premierBanner1_target';
        parent_ad_id = 'premierBanner1';
		advertPos = 'x17';
    }
    
    // hide the parent element
	//document.getElementById(parent_ad_id).style.display = 'none';
	
	// fill the parent element with the iframed advert
	// add an onload call to check if the parent element contains the empty.gif image that indicates no advert is present
	var advertContainer = document.getElementById(parent_ad_id);
	if (advertContainer){
		
		
		if(parent_ad_id == 'mpu'){
			//document.write('<SCR' + 'IPT language=JavaScript1.1 SRC="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + advertPos + '"><\/SCR' + 'IPT>');
			//advertContainer.innerHTML = '<SCR' + 'IPT language=JavaScript1.1 SRC="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + advertPos + '"><\/SCR' + 'IPT>';
			//OAS_RICH(pos);
			
			
		} else {
			advertContainer.innerHTML = '<iframe class="ad_target targets" scrolling=no hspace=0 ' + 
                                    ' vspace=0 frameborder=0 marginwidth=0 marginheight=0 ' + 
                                    ' id="' + ad_id + '" src="' + OAS_url + 'adstream_sx.ads/' + 
                                    OAS_sitepage + '/1' + OAS_rns + '@'+ advertPos + '"><\/iframe>';
		}
		
							
	}
}

function isOASAdEmpty(ad_id){
	// onload call from advert iframeto check if the parent element contains the empty.gif image that indicates no advert is present
	// If no advert is present keep the parent element hidden, otherwise show it
}

