
/////////////////////////////////////////////////////////////////////////////

/// Advertisment scripts ********************//////////////////////////////
function changeAd(target,pos) {
	//pos = typeof pos == 'undefined' ? 'Postion1' : pos.toString();
	ad_obj = $(target);
	if($('adId_0') != null){
		var leader = $('adId_0');
		leader.src = leader.src;
	}
	var str = '<iframe id="ad_grab" src="/inc/mod_adblock_b.php?sitepage='+sitepage+'&pos='+pos+'" width="1" height="1" frameborder="0" scrolling="no"></iframe>';
	ad_obj.innerHTML = str;
	Event.observe('ad_grab', 'load', publishAd, false);
}
function publishAd(){
	$('ad_grab').contentWindow.document.body.style.margin = "0px";
	w=$('ad_grab').contentWindow.document.body.scrollWidth;
	h=$('ad_grab').contentWindow.document.body.scrollHeight;

	$('ad_grab').width =w;
	$('ad_grab').height =h;
	//ad_obj.innerHTML = $('ad_grab').contentWindow.document.body.innerHTML;
}
function displayBranding(clicktag,brand){
	if(clicktag == '' || brand == '') return;
	if($('adblock') == null){
		var str = '<div class="pagebranding">';
			str += '<a href="'+ clicktag + '">';
			str += '<img src="/template_images/partners/'+ brand +'">';
			str += '</a></div>';
			
		var obj = $$('div.ads');
		if(obj.length >= 1){
			
			adObj = obj[0];
			adObj.id = 'adblock';
			new Insertion.Before(adObj, str);
		}
	}
}


var adId=0;
var sect = document.URL.split("/");

function setNav(){
	nav_obj = $('navigation').getElementsByTagName('a');
	
	for(n=0; n < nav_obj.length; n++){
		if('/'+sect[3]+'/' == nav_obj[n].getAttribute('href')) nav_obj[n].className = "selected";
	}
}

Event.observe(window, 'load', setNav, false);
	
function DisplayAds(position, width, height){
	
	var eviroCheck = document.URL.split("/environment/");
	var animalCheck = document.URL.split("/animals/");
	

	/*
	if(eviroCheck.length >= 2){
		var titleCheck = document.URL.split("top10");
		if(titleCheck.length >= 2){
			displayBranding('http://ads.space.com/RealMedia/ads/click_lx.ads/www.livescience.com/sponsored/1112745096/x32/Space/LS_BP_EETechSpecial_sec_tag/clickthrough.gif/1','ls_BP_top10_emenergy_325X90.gif');
		}else{
			displayBranding('http://ads.space.com/RealMedia/ads/click_lx.ads/www.livescience.com/sponsored/1112745096/x32/Space/LS_BP_Enviroment_SectionWrap/clickthrough.gif/1','bp_section_ENV_178x55.gif');
		}
		
	}
	*/
	if(!sitepage && sitepage != '') sitepage = "www.livescience.com";
	switch(sitepage){
		case "www.livescience.com/animals": sitepage="www.livescience.com/animalworld"; break;
		case "www.livescience.com/strangenews": sitepage="www.livescience.com/news"; break;
	}
	
	if(!width || !height){
		document.write('<div id="ad_square"></div>');
		changeAd('ad_square',position);
	}else{
		str = "http://www.livescience.com/inc/advert.php?sitepage="+sitepage+"&pos="+position+"&w="+width+"&h="+height;		
		document.write('<iframe id="adId_'+adId+'" src="'+str+'" width="'+width+'" height="'+height+'" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></iframe>');
		ad_obj = $('adId_'+adId);
	}
	adId++;
}

/////////////////////////////////////////////////////////////////////////////

//Function to open a popup
function popWin(pageUrl, pageName, pageWidth, pageheight){
	newWin = open(pageUrl, pageName,"width=" + pageWidth + ",height=" + pageheight + ",status=no,scrollbars=yes,left=250,top=10,menubar=no,resizable=no,address=no")
}




/// Feature Module ********************//////////////////////////////
var fetured_module = Class.create();

fetured_module.prototype = {
	initialize: function() {
		this.obj 			= $('featured_module');
		this.r_obj			= $('featured_articles');
		this.r_dat 			= [];
		this.timer			= 0;
		this.timerDuration	= 7000; //milliseconds
		this.fadeDuration	= 1.0;
		this.play			= 1;
		this.curID			= 0;
		
		if(!this.obj) return;
		
		// Setup tab actions //
		var tabs = document.getElementById("featured_tabs").getElementsByTagName("li");
		for (var i=0; i < tabs.length; i++) {
			tabs[i].id = "tab_"+i;
			tabs[i].onclick = function () {myfeatured.openTab(this.id); return false;}
		}
		
		// Setup image rotation //
		
		if(this.r_obj){
			this.head = this.r_obj.getElementsByTagName("h1")[0];
			this.head.setAttribute('id','featured_header');
			
			var e_dat = $('featured_items').getElementsByTagName("ol")[0].getElementsByTagName("li");
			
			for (var i=0; i < e_dat.length; i++) {
				var obj	= new Object( );
				obj.img = e_dat[i].getElementsByTagName("img")[0].src;
				obj.head = e_dat[i].getElementsByTagName("a")[0].innerHTML;
				obj.href =  e_dat[i].getElementsByTagName("a")[0].href;
				this.r_dat.push(obj);
			}
			if($('r_img') == null){
				new Insertion.Top(this.r_obj,"<img src='' width='433' height='178' id='r_img' />");
			}
			this.img = $('r_img');
			
			this.overlay = document.createElement("div");
			this.overlay.setAttribute('id','overlay');
			this.overlay.style.display = "none";
			this.overlay.style.zIndex = "1";
			this.overlay.style.top = "0";
			this.overlay.style.left = "0";
			this.overlay.style.visibility = "visible";
			Element.addClassName(this.overlay,'slide');
			$('slide_0').appendChild(this.overlay);
			
			this.over_img = document.createElement("img");
			this.over_img.width = "433";
			this.over_img.height = "178";
			this.overlay.appendChild(this.over_img);
			
			this.over_head = document.createElement("h1");
			this.overlay.appendChild(this.over_head);
			
			// Set image nav //
			var n_obj = $('featured_next');
			var p_obj = $('featured_prev');
			
			Event.observe(n_obj, 'mouseover', function(e){ n_obj.style.backgroundPosition = 'bottom'; });
			Event.observe(n_obj, 'mouseout', function(e){ n_obj.style.backgroundPosition = 'top'; });
			Event.observe(n_obj, 'click', this.next);
			
			Event.observe(p_obj, 'mouseover', function(e){ p_obj.style.backgroundPosition = 'bottom'; });
			Event.observe(p_obj, 'mouseout', function(e){ p_obj.style.backgroundPosition = 'top'; });
			Event.observe(p_obj, 'click', this.prev);
			
			Event.observe(this.img, 'click', this.navigate);
			Event.observe(this.head, 'click', this.navigate);
			
			this.display(this.img, this.head);
			this.display(this.over_img, this.over_head);
			
			new Effect.Opacity(this.overlay, {duration:0.0, from:0.0, to:0.0});
			this.timer = setTimeout("myfeatured.rotate();", this.timerDuration);
		}
	},
	openTab: function(n){
		if(myfeatured.r_obj){
			if(n != 'tab_0'){
				clearTimeout(myfeatured.timer);
				$(myfeatured.overlay).style.display = "none";
			}
			else myfeatured.rotate();
		}
		else myfeatured.rotate();
		var tabs = document.getElementById("featured_tabs").getElementsByTagName("li");
		for (var i=0; i < tabs.length; i++) {
			var slide = $("slide_"+i);
			if(tabs[i].id == n){
				tabs[i].className = "selected";
				slide.className = "slide selected";
				
			} else {
				tabs[i].className = ""; 
				slide.className = "slide";
			}
		}
	},
	display: function(img,head){
		var obj				= this.r_dat[this.curID];
		$(img).src			= obj.img;
		$(head).innerHTML	= obj.head;
		
	},
	rotate: function() {
		this.curID++;
		if(this.curID > this.r_dat.size( ) - 1)
			this.curID = 0;
		
		$(this.overlay).style.display = "";
		this.display(this.over_img, this.over_head);
		new Effect.Opacity(this.overlay, {duration:this.fadeDuration, from:0.0, to:1.0, afterFinish: this.callback, IRMObj:this});
		this.timer = setTimeout("myfeatured.rotate();", this.timerDuration);
		
	},
	callback: function(obj) {
		IRMObj = obj['options']['IRMObj'];
		IRMObj.effectDone();
	},	
	effectDone: function() {
		new Effect.Opacity(this.overlay, {duration:0.0, from:0.0, to:0.0});
		$(this.overlay).style.display = "none";
		this.display(this.img, this.head);
	},
	next: function(){
		if(myfeatured.timer != 0) clearTimeout(myfeatured.timer);
		myfeatured.curID++;
		if(myfeatured.curID > myfeatured.r_dat.size() - 1)
			myfeatured.curID = 0;
		myfeatured.display(myfeatured.img, myfeatured.head);
	},	
	prev:function (){
		if(myfeatured.timer != 0) clearTimeout(myfeatured.timer);
		myfeatured.curID--;
		if(myfeatured.curID < 0)
			myfeatured.curID = myfeatured.r_dat.size() - 1;
		myfeatured.display(myfeatured.img, myfeatured.head);
	},
	navigate: function() {
		window.location = myfeatured.r_dat[myfeatured.curID].href;
	}
}

function initFeaturedMod() {
	if (!document.getElementsByTagName){ return; }
	
	myfeatured = new fetured_module();
	mycarousel = new carousel_module();
	mostrecent = new mostrecent_module();
}

/////////////////////////////////////////////////////////////////////////////


/// Common Functions for Modules ********************//////////////////////////////
updateItems = function(object){
	var str='';
	for(var i = 0; i < object.moduleLimit; i++){
		var p = (object.startID + i >= object.elements.length)? object.startID + i - object.elements.length: object.startID + i;
		str += "<li>"+object.elements[p]+"</li>";
	}
	object.content_obj.innerHTML = str;
	var id = object.objwrapper.id;
	var pre = $(object.main_obj.id+"_prewrap");
	var obj = document.createElement("div");
		obj.appendChild(object.content_obj);
		obj.style.position = 'absolute';
		obj.style.top = '0px';
		obj.style.left = '0px';
		
	Element.remove(object.objwrapper);
	obj.setAttribute('id',id);
	pre.appendChild(obj);
	object.objwrapper = obj;
}

next_item = function(object, dir){
	if(object.disabled) return;
	object.disabled = true;
	object.shift = object.shiftBy*-1;
	for(var i = 1; i <= object.moveby; i++){
		object.startID++
		object.endID++
		if(object.startID >= object.elements.length) object.startID = 0;
		if(object.endID >= object.elements.length) object.endID = 0;
		
		new Insertion.Bottom(object.content_obj, "<li>"+object.elements[object.endID]+"</li>");
	}
	
	object.action();
}

prev_item = function(object, dir){
	if(object.disabled) return;
	object.disabled = true;
	object.shift = object.shiftBy;
	
	for(var i = 1; i <= object.moveby; i++){
		object.startID--
		object.endID--
		if(object.startID < 0) object.startID = object.elements.length-1;
		if(object.endID < 0 ) object.endID = object.elements.length-1;
		
		new Insertion.Top(object.content_obj, "<li>"+object.elements[object.startID]+"</li>");
	}
	if(dir == 'top'){
		object.objwrapper.style.top = (object.shiftBy*-1)+"px";
	}else{
		object.objwrapper.style.left = (object.shiftBy*-1)+"px";
	}
	
	object.action();
}

setControl = function(obj, o, x, f){
	Event.observe(obj, 'mouseover', function(e){ obj.src = o; });
	Event.observe(obj, 'mouseout', function(e){ obj.src = x; });
	Event.observe(obj, 'click', f);
}

/// Most Recent Module ********************//////////////////////////////
var mostrecent_module = Class.create();

mostrecent_module.prototype = {
	initialize: function() {
		this.main_obj = $('most_recent_module');
		if(!this.main_obj) return;
		
		this.moduleLimit	= 8;
		this.moveby			= 1;
		this.endID			= 5;
		this.startID		= 0;
		this.duration		= .4; 
		this.shiftBy 		= 39;
		this.disabled		= false;
		
		this.content_obj 	= this.main_obj.getElementsByTagName("ul")[0];
		
		var element 	= this.content_obj.getElementsByTagName("a");
		var element_li 	= this.content_obj.getElementsByTagName("li");
		
		this.elements = [];
		for(var i=0; i < element.length; i++){
			var ph = (38 - element[i].offsetHeight)/2;
			element[i].style.padding = ph+"px 0px";
			element[i].style.display = "block";
			this.elements.push(element_li[i].innerHTML);
		}
		
		new Insertion.Before(this.content_obj,"<div id='"+this.main_obj.id+"_prewrap' class='mostrecent_outer'><div id='mostrecent_wrapper' style='position: absolute; left: 0px; top: 0px;'></div></div>");
		this.objwrapper = $('mostrecent_wrapper');
			
		this.objwrapper.appendChild(this.content_obj);
		new Effect.MoveBy(this.objwrapper, 0, 0, {duration: 0.0, transition: Effect.Transitions.sinoidal});
		
		setControl($('mra_up'), '/images/btn_arrow_up_active.gif', '/images/btn_arrow_up_inactive.gif', function(e){ prev_item(mostrecent, 'top') });
		setControl($('mra_down'), '/images/btn_arrow_down_active.gif', '/images/btn_arrow_down_inactive.gif', function(e){ next_item(mostrecent, 'top') });
		
		updateItems(this);
	},
	action: function() {
		new Effect.MoveBy(this.objwrapper, this.shift, 0, {duration: this.duration, transition: Effect.Transitions.sinoidal, afterFinish: mostrecent.end })
	},
	end: function() {
		updateItems(mostrecent);
		mostrecent.disabled = false;
	}
}
						


/// Carousel (homepage) ********************//////////////////////////////
var carousel_module = Class.create();

carousel_module.prototype = {
	initialize: function() {
		this.main_obj	 	= $('carousel');
		if(!this.main_obj) return;
		
		this.moduleLimit	= 8;
		this.moveby			= 7;
		this.endID			= 1;
		this.startID		= 0;
		this.duration		= 1; // seconds
		this.disabled		= false;
		
		var element		= this.main_obj.getElementsByTagName("li");
		var objwidth	= element[0].clientWidth;
		
		if(element.length <= this.moduleLimit){
			$('carousel_next').style.display = "none";
			$('carousel_prev').style.display = "none";
			return;
		}
		if(element.length <= 11){
			this.moveby = element.length - this.moduleLimit;
			this.duration = (element.length - 3)*.1;
		}
		
		this.shiftBy = this.moveby * objwidth;
		
		this.content_obj = this.main_obj.getElementsByTagName("ul")[0];
		this.content_obj.style.width = (this.moduleLimit+this.moveby)*objwidth+"px";
		
		this.elements= [];
		for(var i=0; i < element.length; i++){
			this.elements.push(element[i].innerHTML);
		}
		
		new Insertion.Before(this.content_obj,"<div id='"+this.main_obj.id+"_prewrap' class='prewrap'><div id='carouselwrapper' style='position: absolute; left: 0px; top: 0px;'></div></div>");
		this.objwrapper = $('carouselwrapper');
		this.objwrapper.appendChild(this.content_obj);
		
		
		new Effect.MoveBy(this.objwrapper, 0, 0, {duration: 0.0, transition: Effect.Transitions.sinoidal});
		
		setControl($('carousel_next'), '/images/btn_nextsm.gif', '/images/btn_nextsm_inactive.gif', function(e){ next_item(mycarousel, 'left') });
		setControl($('carousel_prev'), '/images/btn_prevsm.gif', '/images/btn_prevsm_inactive.gif', function(e){ prev_item(mycarousel, 'left') });
		
		updateItems(this);
	},
	action: function() {
		new Effect.MoveBy(this.objwrapper, 0, this.shift, {duration: this.duration, transition: Effect.Transitions.sinoidal, afterFinish: mycarousel.end })
	},
	end: function() {
		updateItems(mycarousel);
		mycarousel.disabled = false;
	}
}
/// Image Gallery ********************//////////////////////////////
var imageGallery = Class.create();

imageGallery.prototype = {
	initialize: function(type) {
		this.type = type;
		this.curImg = 0;
		this.slide = [];
		this.cont_a = $('i_container');
		this.cont_b = $('t_container');
		
		var elements = $('items').getElementsByTagName("li");
		
		Event.observe('next', 'click', this.nextImage);
		Event.observe('prev', 'click', this.prevImage);
		
		if(type == 1){
			this.p_on = "/images/btn_previmage_active.gif";
			this.p_off = "/images/btn_previmage_inactive.gif";
			this.n_on = "/images/btn_nextimage_active.gif";
			this.n_off = "/images/btn_nextimage_inactive.gif";
			this.bar = $("bar").getElementsByTagName("img")[0];
			
			for(var i=0; i < elements.length; i++){
				var o	= new Object( );
				o.src = elements[i].getElementsByTagName("img")[0].src;
				o.credit = elements[i].getElementsByTagName("h4")[0].innerHTML;
				o.title = elements[i].getElementsByTagName("h3")[0].innerHTML;
				o.cap = elements[i].getElementsByTagName("p")[0].innerHTML;
				o.href  = elements[i].getElementsByTagName("a")[0].href;
				this.slide.push(o);
			}
			this.img = document.createElement("img");
			this.img.width = "276";
			this.img.height = "221";
			this.cont_a.appendChild(this.img);
			
			Event.observe(this.img, 'click', this.navigate);
			Event.observe('enlarge', 'click', this.navigate);
			
		}else if(type == 2){
			this.p_on = "/images/btn_previous_active.gif";
			this.p_off = "/images/btn_previous_inactive.gif";
			this.n_on = "/images/btn_next_active.gif";
			this.n_off = "/images/btn_next_inactive.gif";
			
			var count = $('countdown').getElementsByTagName("li");
			
			for(var i=0; i < elements.length; i++){
				var o	= new Object( );
				o.src = elements[i].getElementsByTagName("img")[0].src;
				o.credit = elements[i].getElementsByTagName("p")[0].innerHTML;
				o.title = elements[i].getElementsByTagName("h1")[0].innerHTML;
				o.desc = elements[i].getElementsByTagName("h4")[0].innerHTML;
				this.slide.push(o);
				count[i].setAttribute('value', i);
				Event.observe(count[i], 'click', this.update.bindAsEventListener(count[i]));
			}
			
				
			this.img = document.createElement("img");
			this.img.width = "250";
			this.img.height = "169";
			
			this.credit = document.createElement("div");
			
			this.cont_a.appendChild(this.img);
			this.cont_a.appendChild(this.credit);
		}
		
		this.updateImages();
	},
	update: function(obj){
		myGallery.curImg = this.getAttribute('value');
		myGallery.updateImages();
	},
	updateImages: function() {
		if(this.curImg >= this.slide.length-1){
			$('next').getElementsByTagName("img")[0].src = this.n_off;
			this.curImg = this.slide.length-1;
		}else{
			$('next').getElementsByTagName("img")[0].src = this.n_on;
		}
		if(this.curImg <= 0){
			$('prev').getElementsByTagName("img")[0].src = this.p_off;
			this.curImg = 0;
		}else{
			$('prev').getElementsByTagName("img")[0].src = this.p_on;
		}
		
		this.img.src = this.slide[this.curImg].src;
		
		if(this.type == 1){
			this.cont_b.getElementsByTagName("h4")[0].innerHTML = this.slide[this.curImg].credit;
			this.cont_b.getElementsByTagName("h3")[0].innerHTML = this.slide[this.curImg].title;
			this.cont_b.getElementsByTagName("p")[0].innerHTML = this.slide[this.curImg].cap;
			Element.update('curImage', "Image "+(this.curImg+1)+" of "+this.slide.length);
			this.bar.setAttribute('width',((this.curImg+1)/this.slide.length)*142);
		}else if(this.type == 2){
			var count = $('countdown').getElementsByTagName("li");
			var w = 0;
			for(var i=0; i < 10; i++){
				count[i].className = (this.curImg == i)? "selected":"";
				w += count[i].clientWidth + 10;
			}
			$('countdown').style.width = (w >=360)? "360px":w+"px";
			this.cont_b.getElementsByTagName("h1")[0].innerHTML = this.slide[this.curImg].title;
			this.credit.innerHTML = this.slide[this.curImg].credit;
			$('text').innerHTML = this.slide[this.curImg].desc;
			
		}
		
		changeAd('ad_square','Position1');
		
	},
	navigate:function(){
		window.location = myGallery.slide[myGallery.curImg].href;
	},
	nextImage: function() {
		myGallery.curImg++;
		myGallery.updateImages();
		_hbPageView(page,section);
	},
	prevImage: function() {
		myGallery.curImg--;
		myGallery.updateImages();
		_hbPageView(page,section);
	}
}
function initGalleries() {
	if (!document.getElementsByTagName){ return; }
	myGallery = new imageGallery(1);
}
function initTopten() {
	if (!document.getElementsByTagName){ return; }
	myGallery = new imageGallery(2);
}
/////////////////////////////////////////////////////////////////////////////

/// Related Image Module [article pages] ********************//////////////////////////////
var related_module = Class.create();
related_module.prototype = {
	initialize: function() {
		this.cid = 0;
		this.obj_dat = [];
		
		var elements = $('related_images_module').getElementsByTagName("ul")[0].getElementsByTagName("li");
		
		for(var i=0; i < elements.length; i++){
			var o	= new Object( );
			o.img = elements[i].getElementsByTagName("img")[0].src;
			o.head = elements[i].getElementsByTagName("h4")[0].innerHTML;
			o.href  = elements[i].getElementsByTagName("a")[0].href;
			this.obj_dat.push(o);
		}
		if(elements.length > 1){
			setControl($('rm_next'), '/images/btn_nextsm.gif', '/images/btn_nextsm_inactive.gif', function(e){ myrelated.next_img() });
			setControl($('rm_prev'), '/images/btn_prevsm.gif', '/images/btn_prevsm_inactive.gif', function(e){ myrelated.prev_img() });
		}else{
			$('related_images_module').getElementsByTagName("ol")[0].style.display = "none";
		}
		this.img = document.createElement("img");
		this.img.width = "163";
		this.img.height = "110";
		$('imgHolder').appendChild(this.img);
		
		Event.observe(this.img, 'click', this.navigate);
		Event.observe('enlarge_img', 'click', this.navigate);
		
		this.updateImage();
	},
	updateImage:function(){
		var o					= this.obj_dat[this.cid];
		this.img.src			= o.img;
		$('caption').innerHTML	= o.head;
	},
	next_img:function(){
		this.cid = (this.cid >= this.obj_dat.length-1)? 0 : this.cid+1;
		this.updateImage();
	},
	prev_img:function(){
		this.cid = (this.cid <= 0)? this.obj_dat.length-1 : this.cid-1;
		this.updateImage();
	},
	navigate:function(){
		window.location = myrelated.obj_dat[myrelated.cid].href;
	}
}
function initRelatedImages() {
	if (!document.getElementsByTagName){ return; }
	myrelated = new related_module();
}
