function build_ad(ad, curad) {
    var kw = !ad.kw ? '' : ad.kw;
    var maxHeight = !maxHeight ? 0 : maxHeight;
    var domain = !ad.domain ? '' : ad.domain;
	var ad_num = curad.num;
	
	// Build Query String here.
	// Note: kw must be last.
    var params= 'num=' + curad.num + '&dm=' + ad.domain + '&pg=' + ad.page + '&kw=' + kw;
    var adcont = document.getElementById('ad_' + curad.num);
	var ifm = null;
	
	// If we have already built the iframe use it
	// Else create a new iframe to use
    if(document.getElementById('ad_' + curad.num + '_ifm')) {
        ifm = document.getElementById('ad_' + curad.num + '_ifm');
		ifm.style.height = curad.h + 'px';
    }	
    else {
        adcont.innerHTML = '';
        ifm = document.createElement('iframe');
        ifm.id='ad_' + curad.num + '_ifm';
        ifm.style.width = curad.w + 'px';
        ifm.style.height = curad.h + 'px';
        ifm.style.border = '0';
        ifm.setAttribute('allowTransparency', "true");
		ifm.frameBorder = '0';
        ifm.scrolling = 'no';
        ifm.setAttribute('onload', 'resize_ad_frame("ad_' + curad.num + '_ifm", ' + curad.h + ', ' + maxHeight +');');
		
        if(!ifm.addEventListener) {
            ifm.attachEvent('onload',function() { 
				top.resize_ad_frame("ad_" + curad.num +"_ifm", curad.h, maxHeight); 
			});
        }
        adcont.appendChild(ifm);
    }
        ifm.src='http://www.laptopmag.com/controls/iframe_gen.html?' + params;
}

function resize_ad_frame(frameId, orig_height, maxHeight) {
    var ifm = top.document.getElementById(frameId);
    var height = ifm.contentWindow.document.documentElement.scrollHeight;
	
    if(maxHeight > 0 && maxHeight < height) { 
		height = maxHeight; 
	}
	
    if(height > orig_height) {
        ifm.style.height = height + 'px';
    }
}

function runAds() {
    if(typeof(pagebanners) !='undefined') {
        // reset the ord value so we get minty freshness
		var axel = Math.random() + "";
		var ord = axel * 1000000000000000000;
		
		var ad = {};
		ad.domain = pagebanners.domain;
		ad.page = pagebanners.page;
		ad.kw = pagebanners.kw;
			
        for(var i = 0; i < pagebanners.ads.length; i++) {
            var curad = pagebanners.ads[i];
            var cont = document.getElementById('ad_' + curad.num)
			
            if(typeof(cont) != 'undefined' && cont != null) {
				build_ad(ad, curad);
            }
        }
    }
}

function loadComscore(){
    // Omniture.
    currentUrl = window.location.href;
    COMSCORE.beacon({
		c1:2,
		c2:"6035753",
		c3:"6035753",
		c4:currentUrl.replace('index.html', ''),
		c5:"Technology - News",
		c6:"",
		c15:""
    });
}
//_gaq.push(["_trackEvent", "mini-gallery-thumbnail", "click", ""]);
function reloadScripts(){
    // Comscore
    loadComscore();
    // Google Analytics
    _gaq.push(['_trackPageview']);    
    // Reload ads
    runAds();
}
