$(document).ready(function() {

    if (document.getElementById('newsFeed')) {
        var nf = new newsFeed();
        nf.init();
    }
    if (document.getElementById('primaryMenu')) {
        createPrimaryHover();
    }
    if (document.getElementById('homeBanner')) {
        var hb = new homeBanner();
        hb.init();
    }
    if (document.getElementById('helpArea')) {
        var ha = new helpArea();
        ha.init();
    }
    if (document.getElementById('supportMethods')) {
        var sm = new supportMethods();
        sm.init();
    }
    if (document.getElementById('albumTable')) {
        createAlbumIndexHover();
    }
    if (document.getElementById('albumSlide')) {
        createAlbumSlide();
    }
    if (document.getElementById('albumPhoto')) {
        centerAlbumPhoto();
    }
//    if (document.getElementById('highlights')) {
//        createHighlights();
//    }

    $('#primaryMenu').children("ul").children("span.cufon").remove();
});

function newsFeed() {
	var self = this;
	this.frame = document.getElementById('newsFeed');
	this.width = 0;
	this.timer = null;
	this.left = 0;
	this.increment = 1;
	this.init = function() {
		// var lis = this.frame.getElementsByTagName('li');
		// for(var x=0;x<lis.length;x++) {
			// this.width += lis[x].offsetWidth;
		// }
		
		$(this.frame).wrapInner(document.createElement("div"));
		var wrap = $(this.frame).find('div');
		wrap.width(5000);
		this.width = wrap.find('ul').width();
		
		var copy = wrap.html();
		$(wrap).append(copy);
		
		wrap.width(this.width * 2 + 770);
		
		this.frame.onmouseover = function() {
			clearInterval(self.timer);
			self.timer = null;
		}
		this.frame.onmouseout = function() {
			self.timer = setInterval(function(){self.slide();},40);
		}
		
		this.timer = setInterval(function(){self.slide();},40);
	}
	this.slide = function() {
		this.left = this.left + this.increment;
		if(this.left > this.width) {
			this.left = 0;
		}
		this.frame.scrollLeft = this.left;
		this.left = this.left;
	}
}

function createPrimaryHover() {
	$('#primaryMenu li').hover(function() {
		if(this.className.indexOf('selected') < 0) {
			this.className = this.className + "Hover";
		}
	},
	function() {
		this.className = this.className.rtrim("Hover");
	});
}

function homeBanner() {
	var self = this;
	this.current = 0;
	this.uls = document.getElementById('bannerThumbs').getElementsByTagName('li');
	this.init = function() {
		for(var x=0;x<this.uls.length;x++) {
			self.addHover(x);
		}
	};
	this.addHover = function(num) {
		this.uls[num].onmouseover = function() {
			self.select(num);
		}
	}
	this.select = function(num) {
		this.uls[this.current].className = "";
		this.uls[num].className = "active";
		
		document.getElementById('banner_'+(this.current+1)).className = "hide";
		document.getElementById('banner_'+(num+1)).className = "";
		
		this.current = num;
	};
}


function getAssistance(arg) {
	if(arg == "open") {
	document.getElementById('getAssistance_org').style.display = "none";
	document.getElementById('getAssistance_dd').style.display = "block";
		
	}
	else if(arg == "close") {
		document.getElementById('getAssistance_dd').style.display = "none";
		document.getElementById('getAssistance_org').style.display = "block";
	}
	return false;
}

function helpArea() {
	var self = this;
	this.current = "Default";
	this.init = function() {
		// $('#helpArea li a').click(function() {
			// if(this.className.indexOf('active') < 0) {
				// $('#ha_'+self.current).removeClass('active');
				// $('#sa_'+self.current).css('display','none');
				// self.current = this.id.ltrim('ha_');
				// $(this).addClass('active');
				// $('#sa_'+self.current).css('display','block');
			// }
			// else {
				// $('#ha_'+self.current).removeClass('active');
				// $('#sa_'+self.current).css('display','none');
				// self.current = "Default";
				// $('#sa_'+self.current).css('display','block');
			// }
			// return false;
		// });
		$('#helpArea li a').hover(
			function() {
				$(this).addClass('active');
				$('#sa_Default').css('display','none');
				self.current = this.id.ltrim('ha_');
				$('#sa_'+self.current).css('display','block');
			},
			function() {
				$(this).removeClass('active');
				$('#sa_Default').css('display','block');
				$('#sa_'+self.current).css('display','none');
			}
		);
	}
}

function supportMethods() {
	var self = this;
	this.current = "Default";
	this.init = function() {
		$('#supportMethods li a').click(function() {
			var parent = this.parentNode;
			if(parent.className.indexOf('active') < 0) {
				$('#sh_'+self.current).removeClass('active');
				$('#sm_'+self.current).css('display','none');
				self.current = parent.id.ltrim('sh_');
				$(parent).addClass('active');
				$('#sm_'+self.current).css('display','block');
			}
			else {
				$('#sh_'+self.current).removeClass('active');
				$('#sm_'+self.current).css('display','none');
				self.current = "Default";
				$('#sm_'+self.current).css('display','block');
			}
			return false;
		});
	}
}

function createAlbumIndexHover() {
	$('#albumTable td').hover(function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	});
}

function createAlbumSlide() {
	var as = new filmstrip();
	as.frame = document.getElementById('albumSlide');
	as.next = document.getElementById('albumSlideDown');
	as.previous = document.getElementById('albumSlideUp');
	as.settings.totalVisible = 4;
	as.settings.direction = 1;
	as.settings.scrollWheel = true;
	as.init();
}

function centerAlbumPhoto() {
	var ap = document.getElementById('albumPhoto');
	var img = ap.getElementsByTagName('img')[0];
	if(img.offsetHeight > 700) {
		img.style.height = "700px";
	}
	var arrowTop = 134 + (img.offsetHeight / 2);
	$('#nextPhoto, #previousPhoto').css('top',arrowTop + 'px');
	if(img.offsetWidth > 510) {
		img.style.width = "510px";
		ap.style.width = "552px";
	}
	else {
		ap.style.width = img.offsetWidth + 42 + "px";
	}
}

function createHighlights(panel) {
	var wrapper = document.getElementById('highlights');
	var cbs = $(wrapper).find('ul.content li');
	var scrollBoxes = new Array();
	for(var x=0;x<cbs.length;x++) {
		scrollBoxes[x] = new scrollBox();
		scrollBoxes[x].elems.frame = $(cbs[x]).find('.frame')[0];
		scrollBoxes[x].elems.up = $(cbs[x]).find('.frameUp')[0];
		scrollBoxes[x].elems.down = $(cbs[x]).find('.frameDown')[0];
		scrollBoxes[x].init();
	}
	var dds = wrapper.getElementsByTagName('dd');
	var filmstrips = new Array();
	for(var x=0;x<dds.length;x++) {
		filmstrips[x] = new filmstrip();
		filmstrips[x].frame = $(dds[x]).find('.slider')[0];
		filmstrips[x].previous = $(dds[x]).find('.previous')[0];
		filmstrips[x].next = $(dds[x]).find('.next')[0];
		filmstrips[x].settings.totalVisible = 7;
		filmstrips[x].settings.prefix = "slider"+x;
		filmstrips[x].settings.pageSlide = true;
		filmstrips[x].events.onAfterInit = function() {
			if(filmstrips[x].count <= filmstrips[x].settings.totalVisible) {
				filmstrips[x].previous.style.display = "none";
				filmstrips[x].next.style.display = "none";
			}
		}
		filmstrips[x].init();
	}
	//var panel = 0;
	var hl = new highlights(panel);
	hl.frame = wrapper;
	hl.dds = dds;
	hl.init();
	
	$('#highlights .slider a').click(function() {
		if(this.className != "active") {
			$(this).parents('ul').find('a').removeClass('active');
			$(this).addClass('active');
			var index = $(this).parent('li').prevAll().length;
			var lis = $(this).parents('dd').find('ul.content li');
			lis.removeClass('active');
			if(lis[index]) {
				lis[index].className = 'active';
			}
			window.location = this.attr("href");
		}
		return false;
	});
}

function highlights(panel) {
	var self = this;
	this.frame = null;
	this.dts = null;
	this.dds = null;
	this.heights = new Array();
	this.anim = { time:0, duration:60, speed:10, timer:null, ease:"sineInOut" };
	this.current = panel;
	this.init = function() {
		if(!this.dts) {
			this.dts = this.frame.getElementsByTagName('dt');
		}
		if(!this.dds) {
			this.dds = this.frame.getElementsByTagName('dd');
		}
		for(var x=0;x<this.dds.length;x++) {
			this.heights[x] = this.dds[x].offsetHeight;
			if(x != this.current) {
				this.dds[x].style.display = "none";
			}
			this.addClick(x);
		}
	}
	this.addClick = function(num) {
		this.dts[num].onclick = function() {
			if(self.anim.time == 0) {
				self.switchTo(num);
			}
			return false;
		}
	}
	this.switchTo = function(num) {
		if(this.current >= 0) {
			this.heights[this.current] = this.dds[this.current].offsetHeight;
			this.dds[this.current].style.height = this.heights[num] + "px";
		}
		if(num != this.current) {
			this.dds[num].style.height = "0px";
			this.dds[num].style.display = "block";
			if(document.all)
				$(this.dds[num]).addClass('hideH2s');
		}
		this.anim.timer = setInterval(function(){self.slide(num); }, this.anim.speed);
	}
	this.slide = function(num) {
		if (this.anim.time > this.anim.duration) {
			clearInterval(this.anim.timer);
			this.anim.timer = null;
			if(this.current >= 0) {
				this.dds[this.current].style.display = "none";
				if(document.all)
					$(this.dds[this.current]).removeClass('hideH2s');
			}
			if(num != this.current) {
				this.dds[num].style.height = "auto";
				this.current = num;
			}
			else {
				this.current = -1;
			}
			this.anim.time = 0;
		}
		else {
			var height = Math.ceil(c2.ease[this.anim.ease](this.anim.time, 0, this.heights[num], this.anim.duration));
			if(num != this.current) {
				this.dds[num].style.height = height + "px";
			}
			if(this.current >= 0) {
				this.dds[this.current].style.height = this.heights[num] - height + "px";
			}
			if(document.all) {
				if(this.anim.time == 45 && this.current >= 0)
					$(this.dds[this.current]).addClass('hideH2s');
				if(this.anim.time == 15 && num != this.current)
					$(this.dds[num]).removeClass('hideH2s');
			}
			this.anim.time++;
		}
	} 
}


