var a = new Array();

// Skin the slider control
a['i'] = new Array();
a['i']['leftArrow'] = '/images/slider/leftArrow.jpg';
a['i']['rightArrow'] = '/images/slider/rightArrow.jpg';
a['i']['selectedDot'] = '/images/slider/selectedDot.gif';
a['i']['unselectedDot'] = '/images/slider/unselectedDot.gif';
a['i']['bgToolbar'] = '/images/slider/bgToolbar.gif';
a['i']['bgSelectedTab'] = '/images/slider/bgSelectedTab.gif';
a['i']['bgContent'] = '/images/slider/bgContent.gif';

// All of the dimensions aside from the Blocks
a['d'] = new Array();
a['d']['sliderControlWidth'] = 782; // SliderWindow width, plus the width of the two arrows
a['d']['sliderControlHeight'] = 160; // Sliderwindow height plus the height of the toolbar
a['d']['leftArrowWidth'] = 23; // Height of the arrows is the same as the sliderControlHeight
a['d']['rightArrowWidth'] = 23;
a['d']['toolbarHeight'] = 27;
a['d']['sliderWindowWidth'] = 736;
a['d']['sliderWindowHeight'] = 133;

// Styles
a['styles'] = new Array();
a['styles']['toolbarText'] = 'font-family:Arial Narrow; font:14px; color:black;';

function CreateSlider() {

var i
var s=''

s+='<table cellpadding=0 cellspacing=0 border=0 width='+a['d']['sliderControlWidth']+' height='+a['d']['sliderControlHeight']+'>'
s+='<tr>'

// left arrow
s+='<td rowspan=2 width='+a['d']['leftArrowWidth']+' height='+a['d']['sliderControlHeight']+'>'
s+='<a href="javascript:handleSliderLeftClick()"><img width='+a['d']['leftArrowWidth']+' height='+a['d']['sliderControlHeight']+' border=0 src="'+a['i']['leftArrow']+'"></a></td>'

// toolbar
s+='<td width='+a['d']['sliderWindowWidth']+' height='+a['d']['toolbarHeight']+'>'
s+='<table cellpadding=0 cellspacing=0 border=0 width=100% height=100% style="background:url('+a['i']['bgToolbar']+')">'
s+='<tr>'
s+='<td valign=top>'
s+='<table cellpadding=0 cellspacing=0 border=0 height=100%>'
s+='<tr>'

// make tabs
for (i=0; i<a['T'].length; i++) {
s+='<td id=tab'+i+' align=center style="padding-left:10px; padding-right:10px; background:url('+a['i']['bgToolbar']+')">'
s+='<a id=tabText'+i+' href="javascript:SelectTab('+i+')" style="'+a['styles']['toolbarText']+' text-decoration:underline">'+a['T'][i]['name']+'</a>'
s+='</td>'
s+='<td width=1 bgcolor="#999999"><spacer></td>'
}

s+='</tr>'
s+='</table>'
s+='</td>'
s+='<td align=right>'
s+='<table>'
s+='<tr>'
s+='<td>'
s+='<a href="javascript:handleSliderLeftClick()" style="'+a['styles']['toolbarText']+'">&lt; Prev</a>&nbsp;'
s+='</td>'
s+='<td id=dots>'
s+='</td>'
s+='<td>'
s+='&nbsp;<a href="javascript:handleSliderRightClick()" style="'+a['styles']['toolbarText']+'">Next &gt;</a> &nbsp;  &nbsp;'
s+='<a href="javascript:handleViewAll()" style="'+a['styles']['toolbarText']+'">View All</a>&nbsp;&nbsp;'
s+='</td>'
s+='</tr>'
s+='</table>'
s+='</td>'
s+='</tr>'
s+='</table>'
s+='</td>'

// right arrow
s+='<td rowspan=2 width='+a['d']['rightArrowWidth']+' height='+a['d']['sliderControlHeight']+'>'
s+='<a href="javascript:handleSliderRightClick()"><img width='+a['d']['rightArrowWidth']+' height='+a['d']['sliderControlHeight']+' border=0 src="'+a['i']['rightArrow']+'"></a></td>'

s+='</tr>'
s+='<tr>'
s+='<td width='+a['d']['sliderWindowWidth']+' height='+a['d']['sliderWindowHeight']+'>'
s+='<div style="position:relative; width:'+a['d']['sliderWindowWidth']+'; height:'+a['d']['sliderWindowHeight']+'; background:url('+a['i']['bgContent']+'); overflow:hidden;">'


// Make a slider for each tab
for (i=0; i<a['T'].length; i++) {

// figure out the number of slides
var nBlocksPerSlideForTab=Math.floor(a['d']['sliderWindowWidth'] / a['T'][i]['blockWidth'])
var nBlocksForTab=a['T'][i]['B'].length
var nSlidesForTab=Math.ceil(nBlocksForTab / nBlocksPerSlideForTab)

s+='<div id="tab'+i+'Content" style="position:absolute; top:0px; left:0px; height:'+a['d']['sliderWindowHeight']+'px; visibility:hidden">'
s+='<table cellpadding=0 cellspacing=0 border=0 width='+(nSlidesForTab * a['d']['sliderWindowWidth'])+' height=100%>'
s+='<tr>'

for (var j=0; j<nSlidesForTab*nBlocksPerSlideForTab; j++) {

if (j % nBlocksPerSlideForTab == 0) {
// start a new slide
s+='<td width='+a['d']['sliderWindowWidth']+'>'
s+='<table cellpadding=0 cellspacing=10 border=0 width=100% height=100%>'
s+='<tr>'
}

// Make a block
s+='<td align=center valign=top width='+Math.floor(100 / nBlocksPerSlideForTab)+'%>'
if (j < nBlocksForTab) { // Make a content block
s+=eval(a['T'][i]['blockHTMLFunc']+'(i,j)')
}
else if (j == nBlocksForTab) { // Make a block after all of the DB defined blocks that has "Return to Start" and "View All"
s+='<br><a href="javascript:handleDotClick(0)" style="'+a['styles']['toolbarText']+'">< Return To Start</a><br>'
s+='<br>'
s+='<a href="javascript:handleViewAll()" style="'+a['styles']['toolbarText']+'">View All<br>'+a['T'][i]['name']+'</a><br>'
}
else { // Make a blank block (to fill out the end of the last slide)
s+='&nbsp;'
}
s+='</td>'
// Finished making block

if (j % nBlocksPerSlideForTab == nBlocksPerSlideForTab - 1) {
// end cur slide
s+='</tr>'
s+='</table>'
s+='</td>'
}

}

s+='</tr>'
s+='</table>'
s+='</div>'
}

s+='</div>'
s+='</td>'
s+='</tr>'
s+='</table>'

document.write(s)
}

var ns6=document.getElementById&&!document.all?1:0
var ie=document.all?1:0

var nSlides=-1
var curSlide=-1
var nBlocks=-1
var curBlock=-1
var blocksPerSlide=-1
var curTab=-1

var sliderContentWidth=0
var clipleft=0
var clipright=0
var cliptop=0
var clipbottom=0

var nScrollFs=71
var curScrollF=1

function handleSliderRightClick()
{
curScrollF=1
var timer=setTimeout("Scroll('right')", 20)
}

function handleSliderLeftClick()
{
curScrollF=1
var timer=setTimeout("Scroll('left')", 20)
}

function handleDotClick(dotIdx) {
curScrollF=1
var direction
var nSlidesToScroll

if (dotIdx == curSlide) return
else if (dotIdx < curSlide) {
direction='left'
nSlidesToScroll=curSlide - dotIdx
}
else {
direction='right'
nSlidesToScroll=dotIdx - curSlide
}

var timer=setTimeout("Scroll('"+direction+"', "+nSlidesToScroll+")", 20)
}

function handleViewAll() {
	document.location = a['T'][curTab]['viewAll'];
}

function Scroll(direction, nSlidesToScroll) {
var sliderContent=document.getElementById('tab'+curTab+'Content')
var leftPos=0
if (ie) leftPos=sliderContent.style.posLeft
if (ns6) leftPos=parseInt(sliderContent.style.left)
var FWidth=Math.floor(a['d']['sliderWindowWidth'] / nScrollFs)

if (direction == 'right') {
if (curSlide < nSlides - 1) {
if (ie) sliderContent.style.posLeft -= FWidth
if (ns6) sliderContent.style.left=leftPos - FWidth
clipright += FWidth
clipleft += FWidth
}
else {
if (ie) sliderContent.style.posLeft=0
if (ns6) sliderContent.style.left=0
clipright=a['d']['sliderWindowWidth']
clipleft=0
curScrollF=nScrollFs
}
}
if (direction == 'left') {
if (curSlide > 0) {
if (ie) sliderContent.style.posLeft += FWidth
if (ns6) sliderContent.style.left=leftPos+FWidth
clipright -= FWidth
clipleft -= FWidth
}
else {
if (ie) sliderContent.style.posLeft=-(a['d']['sliderWindowWidth'] * (nSlides - 1))
if (ns6) sliderContent.style.left=-(a['d']['sliderWindowWidth'] * (nSlides - 1))
clipright=a['d']['sliderWindowWidth'] * nSlides
clipleft=a['d']['sliderWindowWidth'] * (nSlides - 1)
curScrollF=nScrollFs
}
}

sliderContent.style.clip="rect("+cliptop+" "+clipright+" "+clipbottom+" "+clipleft+")"

if (curScrollF < nScrollFs - 4 ) {
delay=15
}
else if (curScrollF < nScrollFs - 2) {
if (nSlidesToScroll > 1) delay=15
else delay=15
}
else if (curScrollF < nScrollFs) {
if (nSlidesToScroll > 1) delay=15
else delay=15
}
else {

if (direction == 'right') curSlide++
else if (direction == 'left') curSlide--

if (curSlide == nSlides) curSlide=0
if (curSlide < 0) curSlide=nSlides - 1

// Set final position for this Slide since we may have division issues that leave the 
// Slide slightly out of place.
if (ie) sliderContent.style.posLeft=-(a['d']['sliderWindowWidth'] * curSlide)
if (ns6) sliderContent.style.left=-(a['d']['sliderWindowWidth'] * curSlide)
clipright=a['d']['sliderWindowWidth'] * (curSlide+1)
clipleft=a['d']['sliderWindowWidth'] * curSlide

sliderContent.style.clip="rect("+cliptop+" "+clipright+" "+clipbottom+" "+clipleft+")"

if (nSlidesToScroll > 1) {
nSlidesToScroll--
curScrollF=1
var timer=setTimeout("Scroll('"+direction+"', "+nSlidesToScroll+")", 20)
return
}
else {
SetSelectedDot()
return
}
}
curScrollF++
var timer=setTimeout("Scroll('"+direction+"', "+nSlidesToScroll+")",delay)

// TODO: need to handle other browser types in some simple way.
}

function SelectTab(selectedTab) {
var curTabDiv
if (curTab >= 0) curTabDiv=document.getElementById('tab'+curTab+'Content')

if (curTab != selectedTab) {
if (curTab >= 0) curTabDiv.style.visibility="hidden"

curTab=selectedTab
curTabDiv=document.getElementById('tab'+curTab+'Content')
blocksPerSlide=Math.floor(a['d']['sliderWindowWidth'] / a['T'][curTab]['blockWidth'])
nBlocks=a['T'][curTab]['B'].length
nSlides=Math.ceil(nBlocks / blocksPerSlide)

SetTabState()
}

clipleft=0
clipright=a['d']['sliderWindowWidth']
cliptop=0
clipbottom=a['d']['sliderWindowHeight']
sliderContentWidth=curTabDiv.offsetWidth

if (ie) {
//sliderContent.style.posTop=scrollertop
curTabDiv.style.posLeft=0
}
if (ns6) {
//sliderContent.style.top=scrollertop
curTabDiv.style.left=0
}

curTabDiv.style.clip="rect("+cliptop+" "+clipright+" "+clipbottom+" "+clipleft+")"
curTabDiv.style.visibility="visible"

curBlock=0
curSlide=0

MakeDots()
SetSelectedDot()
}

function MakeDots() {
var dots=document.getElementById('dots')

dots.innerHTML='&nbsp;'
for (var i=0; i<nSlides; i++) {
dots.innerHTML += '<a href="javascript:handleDotClick('+i+')"><img id=dot'+i+' height=8 width=8 border=0 src="images/slider/unselectedDot.gif"></a>&nbsp;'
}
}

function SetSelectedDot() {
for(var i=0; i<nSlides; i++) {
dot=document.getElementById('dot'+i)
if (i == curSlide) dot.src=a['i']['selectedDot']
else dot.src=a['i']['unselectedDot']
}
}

function SetTabState() {
var tab
var tabText
for (var i=0; i<a['T'].length; i++) {
tab=document.getElementById('tab'+i)
tabText=document.getElementById('tabText'+i)
if (curTab == i) {
tab.style.background="url("+a['i']['bgSelectedTab']+")"
tabText.style.textDecoration="none"
}
else {
tab.style.background="url("+a['i']['bgToolbar']+")"
tabText.style.textDecoration="underline"
}
}
}

// Templates for building Blocks

// TemplateBook a parameters include:
// 0: img url
// 1: title
// 2: author
// 3: destination url
function TemplateBook(t, j) {
var b=a['T'][t]['B'][j]
var s=''

s+='<a title="'+b[1]+'\n'+b[2]+'" href="'+b[3]+'" style="font-family:Arial; font:12px; color:black;"><img border=0 height=70 src="'+b[0]+'"><br>'
s+='<div style="padding-top:6px;">'
s+='<b>'+ShortenText(b[1],15)+'</b><br>'
s+=ShortenText(b[2],17)
s+='</div>'

return s
}

function TemplateMusic(t, j) {
var b=a['T'][t]['B'][j]
var s=''

s+='<a title="'+b[1]+'\n'+b[2]+'" href="'+b[3]+'" style="font-family:Arial; font:12px; color:black;"><img border=0 height=70 src="'+b[0]+'"><br>'
s+='<div style="padding-top:6px;">'
s+='<b>'+ShortenText(b[1],15)+'</b><br>'
s+=ShortenText(b[2],17)
s+='</div>'

return s
}

function TemplateSpecial(t, j) {
var b=a['T'][t]['B'][j]
var s=''

s+='<table cellpadding=0 cellspacing=0 border=0><tr><td>'+b[0]+'</td></tr></table>'

return s
}

function ShortenText(str, maxlen) {
	if (str.length > maxlen+1) {
		str = str.substr(0, maxlen);
		str += "...";
	}
	return str;
}