
/////////////////////////////////////////////////////////////////////////////

/// Advertisment scripts ********************//////////////////////////////
/*
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);
		}
	}
}
*/




	

/////////////////////////////////////////////////////////////////////////////

//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	= 6;
		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'), '/template_images/buttons/btn_arrow_up_active.gif', '/template_images/buttons/btn_arrow_up_inactive.gif', function(e){ prev_item(mostrecent, 'top') });
		setControl($('mra_down'), '/template_images/buttons/btn_arrow_down_active.gif', '/template_images/buttons/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	= 6;
		this.moveby			= 5;
		this.endID			= 5;
		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'), '/template_images/buttons/btn_nextsm.gif', '/template_images/buttons/btn_nextsm_inactive.gif', function(e){ next_item(mycarousel, 'left') });
		setControl($('carousel_prev'), '/template_images/buttons/btn_prevsm.gif', '/template_images/buttons/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({
	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 = "/template_images/buttons/btn_previmage_active.gif";
			this.p_off = "/template_images/buttons/btn_previmage_inactive.gif";
			this.n_on = "/template_images/buttons/btn_nextimage_active.gif";
			this.n_off = "/template_images/buttons/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 = "/template_images/buttons/btn_previous_active.gif";
			this.p_off = "/template_images/buttons/btn_previous_inactive.gif";
			this.n_on = "/template_images/buttons/btn_next_active.gif";
			this.n_off = "/template_images/buttons/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("h3")[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){
		_hbPageView(page,section);
		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);
			changeAd('ad_square','Position1',160,600);
		}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("h3")[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','Middle', 300, 250);
		}
		
		
	},
	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; }
	var myGallery = new imageGallery(2);
}
/////////////////////////////////////////////////////////////////////////////




function showTab(id) {
	var slideobj = $("slides");
	if (!slideobj) return;

	var divs = slideobj.getElementsByTagName("div");
	var slides = new Array();
	
	// go through all divs, and if they are a slide, store them in the slides array
	for (var i=0; i < divs.length; i++) {
		if (divs[i].className == "slide" || divs[i].className == "slide firstslide"){
			slides.push(divs[i]);
			divs[i].style.display = "none";
		}
	}	
	
	// then just display the current slide
	
	slides[id].style.display = "block"; 		
	var prevButton = $("previous");
	if (prevButton){
		
		//show and hide previous and next buttons in top ten module		
		if (id!=0){		
			prevButton.style.display = "block";
			$("previous_inactive").style.display = "none";
		} else {
			prevButton.style.display = "none";
			$("previous_inactive").style.display = "block";
		}
	}
	var nextButton = $("next");
	if (nextButton){
	
		if (id!=slides.length-1){		
			nextButton.style.display = "block";
			$("next_inactive").style.display = "none";
		} else {
			nextButton.style.display = "none";
			$("next_inactive").style.display = "block";
		}
	}
	
	var thumbobj = $("thumbs");
	if (thumbobj){
		var divs = thumbobj.getElementsByTagName("div");
		var thumbs = new Array();
		
		// go through all divs, and if they are a thumb, store them in the thumbs array
		for (var i=0; i < divs.length; i++) {
			if (divs[i].className == "thumb" || divs[i].className == "thumb selected"){
				thumbs.push(divs[i]);
				divs[i].className = "thumb";
			}
		}
		// then just select the current thumb
		thumbs[id].className = "thumb selected";
	}
}
