apnpageNum = Math.round(Math.random() * 100000000000);
SA = Math.round(Math.random() * 9);


var adServer	= "http://data.apn.co.nz/apnnz";
var h_Server	= '/hserver/';
var random	= Math.round(Math.random() * 10000000000);
var frame;
var RenderedAdContinuesText = false;

if (!pageNum) var pageNum = Math.round(Math.random() * 10000000000);
var adCache = '/random=' + random + '/viewid=' + pageNum;

function RenderAd(adWidth,adHeight,adSizeName,adSpace,srandpos,callback){
	var adSize = '/size=' + adSizeName + '/SA=' + SA;
	var NadCache = adCache;
	var NadSize = adSize;
	var NadSpace = adSpace;
	if (srandpos.length) {NadSize = NadSize + srandpos;}
	if (keyword.length) {NadCache = NadCache + '/KEYWORD=' + keyword;}
	$('<iframe/>')
	.attr({
		'id': NadSpace ,
		'height':adHeight ,
		'width': adWidth ,
		'scrolling':'no' ,
		'allowtransparency':'true' ,
		'marginWidth':'0' ,
		'marginHeight':'0' ,
		'vspace':'0' ,
		'hspace':'0' ,
		'noresize':'true' ,
		'frameBorder':'0' ,
		'align':'middle' ,
		'style':'border:0px none; padding: 0px; margin: 0px;' ,
		'src': adServer + h_Server + siteTarget + NadSize + NadCache
	})
	.appendTo('div#Div' + adSpace);
	
	if (typeof callback === "function") {
        callback();
    }
}

function expandDiv(ifmid,width,height){
    if (ifmid == 'SuperRect') {
        if (smrec_pos) {
            ifmid = smrec_pos;
        }
        $('#Div' + ifmid + ',iframe#' + ifmid).css({"width":width + "px","height":height + "px"});
        SuperMrecCleanup(ifmid);
    }
}

function SetCss(CssData){
	if(CssData.length){
		var sRaw = unescape(CssData);
		var a=0;
		var aTemp1 = sRaw.split(',');
		var aCssSettings = Array(aTemp1.length);
		
		for(a in aTemp1){
			aCssSettings[a] = Array(2);
			aCssSettings[a] = aTemp1[a].split('|');
		}
		if(aCssSettings.length){
			for(a=0; a < aCssSettings.length; a++){
				if(aCssSettings[a].length == 2){
					aCssSettings[a][1] = aCssSettings[a][1].replace(/!/,"!important");
					$(aCssSettings[a][0]).css('cssText', aCssSettings[a][1]);
				}
			}
		}
	}
}

function SuperMrecCleanup(iframe) {
    var div = $('#Div'+iframe);
    // check if the ad is a super mrec
    if (div.width() >= 460) {
        var parentBlock = div.parent();
        // check if the mrec is on the homepage or section page
        if (parentBlock.hasClass('recurring-inline-container')) {
            parentBlock = parentBlock.parent('.span-8');
            if (parentBlock.length > 0) {
                parentBlock.removeClass('span-8').addClass('span-12');
                var prev = parentBlock.prev('.span-16');
                if (prev.length > 0) {
                    div.addClass('superMrec');
                    prev.removeClass('span-16').addClass('span-12 smrec-sib');
                    prev.children('.last').removeClass('last').addClass('fullwidth');
                    prev.children('.c_ps-quad:nth-child(3)').addClass('last');
                }
            }
        // check if the mrec is on the gallery page
        } else if (parentBlock.hasClass('c_ps-gallery-items')) {
            div.addClass('superMrec');
            div.next('.c_ps-gallery-item').addClass('last');
        // check if the mrec is on the entry detail page, this js is purely for ie6 & ie7
        } else if (parentBlock.hasClass('entryAdspace')) {
            parentBlock.css({ width: 460+'px' });
        }
    }
}


/* FIXME: THE CLEANUP FUNCTION IS BUGGY BECAUSE THERE'S A RACE CONDITION BETWEEN LOADING ADS AND CHECKING THEM! Cleanup function is only going to work if we load the ads before dom ready*/
/*FIXME: or is it actually because it can't check the pattern inside an iframe?*/

// Once the ads have been rendered, we need to clean up the page,
// as not all ad spaces will have ads targeted to them, and we
// still have a few 'holes' in the page. It will hide any empty
// ad spaces, hide ad groups that have no visible spaces.
// We can pass in a couple of options; one being a regex we can 
// use to find placeholder content in empty ad spaces; Options
// exist to change the `id` attributes on ad spaces
// giving us more freedom. All options have defaults. If a function
// is passed as a second parameter, if will be called upon completion.
/*
function CleanupAd(adSpaceID, options, callback) {
    var settings, rgx, pattern, adSpaceObject, theAdSpaceID, doWeHideParent;
    
    theAdSpaceID = adSpaceID || '';
    settings = options || {};
    doWeHideParent = settings.hideParent || true;
    rgx = settings.rgx || '/default\\.gif/mig';
    pattern = eval(rgx);
    if (theAdSpaceID != '') { adSpaceObject = $('#' + theAdSpaceID); };
    
    if (adSpaceObject.html().search(pattern) > 0) { console.log(adSpaceID+" matches regex"); };
    if (!adSpaceObject.html().length > 0) { console.log(adSpaceID+" is empty"); };
    
    if (adSpaceObject.length > 0) {
        if ( (adSpaceObject.html().search(pattern) > 0) || (!adSpaceObject.html().length > 0) ) {
            if (doWeHideParent) {adSpaceObject.parent().hide();} else { adSpaceObject.hide(); };
        }

        if (typeof callback === "function") {
            callback();
        }
    }
}*/


