var sect = document.URL.split("/");
var keywords = '';
var adTile 	= 0;
var adId	= 0;
var ord 	= Math.random() * 1000000000000000000;

switch(document.domain){
	case 'livescience.com': sitekey = "imn.ls."; 
		break;
	case 'aviation.com': sitekey = "imn.av."; 
		break;
	case 'newsarama.com': sitekey = "imn.nr."; 
		break;
	case 'space.com': sitekey = "imn.sp."; 
		break;
}

function getKeywordList(){
	var k = '';
	var m = document.getElementsByTagName('meta'); 
	for(var i in m){ 
		if(m[i].name == 'keywords' && m[i].content.length > 0){ 
			k = m[i].content.replace(/"/g,"");
			k = k.replace(/, /g,";kw=");
			k = k.replace(/,/g,";kw=");
			k = k.replace(/#/g,"");
			k = ";kw="+k.replace(/ /g,"_");
		} 
	}
	return k;
}

/// recuresive for old pages ///
function changeAd(target,pos,w,h) {
	changeDartAd(target,w,h);
}

function changeDartAd(target,w,h) {
	var ih = h + 16, iw = w + 16, key;
	var ad_obj = $(target);
	try{
		if(dartTag.split(".").length < 2){
			key = sitekey+dartTag+keywords;
		}else{ 
			key = dartTag+keywords; 
		}
				
		var str = '<iframe src="/common/inc/mod_adblock.php?dartTag='+key+'&pos='+w+'x'+h+'&ord='+ord+'&dom='+document.domain+'" width="'+iw+'" height="'+ih+'" frameborder="0" scrolling="no"></iframe>';
		ad_obj.update(str);
	}catch(e){}
}

function refreshAds(){
	for(i = 1; i <= adTile; i++){
		//adSpot[i].pos += 1;
		if(adSpot[adTile].sz.split(",").length == 1){
			var dim =  adSpot[adTile].sz.split("x");
			ifw = dim[0];
			ifh = dim[1]; 
		}else{
			ifw = 10;
			ifh = 10; 
		}
		var target = 'adcontainer_'+i;
		/*
		var str = '/common/inc/adblock.php?key='+adObj.tag+'&sz='+adObj.sz+'&ord='+ord+'&dom='+document.domain+'&tile='+i+'&pos='+adObj.pos;
		*/
		var str = '<div id="adcontainer_'+i+'" class="clearfix"><iframe id="adframe_'+i+'" src="/common/inc/adblock.php?key='+adSpot[i].tag+'&sz='+adSpot[i].sz+'&ord='+ord+'&dom='+document.domain+'&tile='+i+'&pos='+adSpot[i].pos+'" width="'+ifw+'" height="'+ifh+'" marginwidth="0" marginheight="0"  frameborder="0" scrolling="no"></iframe></div>';
		var p = $(target).ancestors()[0];
		p.innerHTML = str;
	}
}

function resizeAdBlock(tile, w, h){
	var target = $('adframe_'+tile);
	$(target).width = w;
	$(target).height = h;
}

function rebuildAd(tile,advert){
	$('adcontainer_'+tile).innerHTML = unescape(advert);
}




function DartAd(args){
	var key, ifw, ifh;
	adTile++;
	
	if(typeof adSpot == 'undefined'){
		adSpot = new Object();
		keywords = getKeywordList();
	}
	
	if(typeof dartTag == 'undefined'){
		dartTag = generateTag();
	}
	if(dartTag.split("imn.").length < 2){
		key = sitekey + dartTag + keywords;
	}else{ 
		key = dartTag + keywords; 
	}
	
	//TEST DART//
	//key = sitekey + 'adtest/home' + keywords;
	
	adSpot[adTile] = {sz:args, pos:1, tag: key};
	
	var dim =  args.split("x");
	if(args.split(",").length == 1){
		ifw = dim[0];
		ifh = dim[1]; 
		dim = "style='width: "+ifw+"px; height: "+ifh+"px; '";
	}else{
		dim = "style='width: "+dim[0]+"px; '";
		ifw = 10;
		ifh = 10; 
	}
	
	if(typeof adFrame == 'undefined'){		
		
		if ((!document.images && navigator.userAgent.indexOf("Mozilla/2.") >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
			document.write('<div id="adcontainer_'+adTile+'" class="clearfix"><a href="http://ad.doubleclick.net/jump/'+ adSpot[adTile].tag +';pos=1;tile='+ adTile +';sz='+ adSpot[adTile].sz +';ord=' + ord + '?" TARGET="_blank">');
			
			document.write('<IMG src="http://ad.doubleclick.net/ad/'+ adSpot[adTile].tag +';pos='+ 1 +';tile='+ adTile +';sz='+ adSpot[adTile].sz +';ord=' + ord + '?"></A></div>');
		}else{
			document.write('<div id="adcontainer_'+adTile+'" class="clearfix"><SCR'+'IPT LANGUAGE="JavaScript1.1" src="http://ad.doubleclick.net/adj/'+ adSpot[adTile].tag +';pos=1;tile='+ adTile +';sz='+ adSpot[adTile].sz +';ord=' + ord + '?" ><\/SCRIPT></div>');
		}
		
		
		/*
		var str = '<iframe id="adcontainer_'+adTile+'" src="/common/inc/adblock.php?key='+adSpot[adTile].tag+'&sz='+adSpot[adTile].sz+'&ord='+ord+'&dom='+document.domain+'&tile='+adTile+'&pos='+adSpot[adTile].pos+'" width="'+ifw+'" height="'+ifh+'" marginwidth="0" marginheight="0"  frameborder="0" scrolling="no"></iframe>';
		document.write(str);
		*/
		
		if(document.domain == 'space.com' && adTile == 1){
			if(dartTag.split("video").length < 2 && sitepage.split("video").length < 2 ){
			document.write('<SCR'+'IPT LANGUAGE="JavaScript1.1" src="http://ad.doubleclick.net/adj/'+ adSpot[adTile].tag +';sz=1x1;dcopt=ist;ord=' + ord + '?" ><\/SCRIPT>');
			}
		}
	}else{
		var str = '<iframe id="adcontainer_'+adTile+'" src="/common/inc/adblock.php?key='+adSpot[adTile].tag+'&sz='+adSpot[adTile].sz+'&ord='+ord+'&dom='+document.domain+'&tile='+adTile+'&pos='+adSpot[adTile].pos+'" width="'+ifw+'" height="'+ifh+'" marginwidth="0" marginheight="0"  frameborder="0" scrolling="no"></iframe>';
		$(adFrame).innerHTML = str;
	}
		
}

function generateTag(){
	var s = 'general', p = 'home', retag;
	var r = document.referrer.split("/");
	
	if(sect[3].split(".").length < 2 ){
		switch(sect[3]){
			
			case 'php': 		p = sect[4]; break; 
			case 'common': 		s = sect[4]; p = sect[5]; break;
			// Space.com
			case 'missionlaunches':		s = 'spaceflight'; break;
			case 'searchforlife': 		s = 'seti'; break;
			case 'scienceastronomy': 	s = 'science'; break;
			case 'businesstechnology': 	s = 'technology'; break;
			case 'spacewatch': 			s = 'nightsky'; break;
			case '': 					s = 'homepage'; break;
			default: if(document.domain == 'space.com'){ s = sect[3]; break; }else{ s = 'general'; p='home' }
		}
	}	
	
	if(document.domain == 'space.com'){
		if(sect.length > 4 && sect[4].split(".").length >= 2 && sect[4].split(".")[1] == 'html') p = 'news';
	}else{
		if(sect.length > 4){
			if(typeof section != 'undefined' && section != '' && sect[3] != 'common'){
				s = (section == 'home')? 'homepage' : section;
				if(sect[4] != ""){
					if(sect[4].split(".").length < 2){
						p = sect[4];
					}else if(sect[4].split(".").length >= 2 && sect[4].split(".")[1] == 'html') {
						p = 'news';
					}
				}
			}
			else if(typeof section == 'undefined'){
				s = sect[3];
			}
		} else if(sect.length <= 4){
			s = 'homepage';
		}
	}
	
	switch(p){
	// click2enlarge Page // 
		case 'multimedia' :
			p = 'galleries'; break; 
	// Topic Page //
		case 'topic' :
			p = 'home'; break; 
	}
	
	
	// ------- / Album Pages / --------- //
	if(document.URL.split("album.php").length == 2) p = 'bookvieweralbum';
	if(document.URL.split("album_view.php").length == 2) p = 'bookviewerapplication';
	
	// ------- / Blog Pages / --------- //	
	try{if(section == 'blog'){ s = 'blog'; p='home'; }}catch(e){ }
	
	// ------- / Video rewrite / --------- //
	if(document.domain == 'newsarama.com'){
		if(typeof section != 'undefined' && section == 'video'){ s = 'comics'; };
		retag = s+'/'+p;
		if(s == 'comics'){ retag += ';kw=sdcc;kw=comic-con;kw=sandiego'; }
	} else {
		retag = s+'/'+p;
	}
	
	
	if(retag.split(".").length >= 2){
		retag = retag.split(".")[0];
	}
	
	return retag;
}

// ------- / Interstitials / --------- //
function interstitial(type){
	/*
	var ck = (typeof(type) == 'undefined')? 'interstitial4' : 'interstitial4'+type;
	var cookies = document.cookie.split(";");
	var aCookie = [];
	try{
		cookies.each(function(c){ var t = c.strip().split("=");
			if(t[0]== ck){ var s = unescape(t[1]); t[1] = s.split("|"); aCookie.push(t)}});
	} catch(err) { alert(err) }
	
	if(aCookie.grep(ck)[0]){
		skipAd();
	}else{
		var kw = (typeof(type) != 'undefined')? 'prestitial=true;' : '';
		document.write('<SCR'+'IPT LANGUAGE="JavaScript1.1" src="http://ad.doubleclick.net/adj/'+ sitekey +'general/interstitial;'+kw+'sz=600x500,640x480;ord=' + ord + '?" ><\/SCRIPT>');
		Event.observe(window, 'load', function(){
			$('main').setStyle({ display: 'none' });
			$('inter-advertisement').style.display="block";
			var exdate= new Date();
				exdate.setDate(exdate.getDate()+1);
			document.cookie = ck + " = 1;path=/;expires="+exdate.toGMTString();
			var timer = setTimeout("removeAd()",20000);
		  });
		
	}*/
}


function removeAd(){
	if(ie){
		$('inter-advertisement').style.display="none";
	}else{
		Element.remove('inter-advertisement');
	}
	var main = $('main');
	if($('main') != null){
		$('main').style.display="block";
	}
}

function skipAd(){
	if(ie){
		$('inter-advertisement').style.display="none";
	}else{
		Element.remove('inter-advertisement');
	}
	var main = $('main');
	if($('main') != null){
		$('main').style.display="block";
	}
}