/*

####INDEX.JS###########################################

author: Robert Ahlin Toftegaard
date: May 6th 2005
contact: waisgai@hotmail.com

© Copyright Robert Ahlin Toftegaard 2005

#######################################################

Konstruerad för Umeå Studentradio, innehåller metoder 
för att ladda in dagens program i den blå rutan.

#######################################################

*/

var currenPosition = 0
var main = null
var small = null
var image = null
var previousButton = null
var nextButton = null
var initialised = false		
var playlist = null
var all = null
var path = 'http://www.umeastudentradio.se/'   //Ändra till http://www.umeastudentradio.se/
var selected = false		
var initialised = false	


function preload()
{
		var ImageString = new Array('programmen','spellistor','tablan','gastbok','kontakt','reklam','vanner','logga_in')
		var path = "images/structure/menu/"
		var ImageArray = new Array()
		for(var i=0; i < ImageString.length; i++)
		{
			ImageArray[i] = new Image()
			ImageArray[i].src = path+ImageString[i]+"_out.gif"
			ImageArray[i+1] = new Image()
			ImageArray[i+1].src = path+ImageString[i]+"_over.gif"
		}
}

function getObj(name)
{		
	if (document.getElementById)
	{			
		this.obj = document.getElementById(name)			
		this.style = document.getElementById(name).style
	}
	else if (document.all)
	{
		this.obj = document.all[name]
		this.style = document.all[name].style
	}

	else if (document.layers)
	{
		this.obj = getObjNN4(document,name)
		this.style = this.obj	
	}
}			

function initialise()
{
	small = new getObj("small")	
	image = new getObj("image")	
	previousButton = new getObj('previous')
	nextButton = new getObj('next')	
	main = parent.mainFrame				
	initialised = true;
	setImage()		
}



function resizeImage(img,maxHeight,maxWidth)
{	
	//Kvoten mellan höjden och bredden
	var ratio = img.height/img.width
	
	if(img.height>maxHeight)
	{   		
		img.height = maxHeight
		img.width = Math.round(img.height/ratio)		
		resizeImage(img,maxHeight,maxWidth)
	}
	else if(img.width>maxWidth)
	{   						
		img.width = maxWidth 
		img.height = Math.round(img.width*ratio)
		resizeImage(img,maxHeight,maxWidth)
	}

	return [img.width,img.height]
}

function largeImage(img,w,h)
{
	if(img!=null)
	{			
		var largeImg = new Image()		
		largeImg.src=img+'.gif'			
		largeImg.width=w
		largeImg.height=h

		var size = resizeImage(largeImg,80,310)
		
		var padding = (80-size[1])/2												

		return 'src=\"'+largeImg.src+'\" width=\"'+size[0]+'\" height=\"'+size[1]+'\" style=\"margin-top:'+padding+'px;\"'
	}		
}


function smallImage(img,w,h)
{
	if(img!=null)
	{			
		var smallImg = new Image()		
		smallImg.src=img+'.gif'
		smallImg.width=w
		smallImg.height=h
		
		var size = resizeImage(smallImg,60,210)
		
		var padding = (60-size[1])/2												

		return 'src=\"'+smallImg.src+'\" width=\"'+size[0]+'\" height=\"'+size[1]+'\" style=\"margin-top:'+padding+'px;\"'
	}		
}

function selectProgram()
{		
	previousButton.style.display='none'
	nextButton.style.display='none'
	small.style.display='none'	
	image.style.display='block'	
	image.obj.onclick = deselectProgram
	
	image.obj.innerHTML='<img '+largeImage(playlist[currentPosition][3],playlist[currentPosition][4],playlist[currentPosition][5])+' alt=\"'+playlist[currentPosition][1]+'\" title=\"Återgå\" />'
	main.location.href='program_info.php?id='+playlist[currentPosition][0]
	selected = true		
}

function deselectProgram()
{
	image.style.display='none'
	small.style.display='block'			
	selected = false	
	refresh()
}

function next()
{	
	if(currentPosition!=(playlist.length-1))
	{					
		small.obj.innerHTML='<img '+smallImage(playlist[currentPosition+1][3],playlist[currentPosition+1][4],playlist[currentPosition+1][5])+' alt=\"'+playlist[currentPosition+1][1]+'\" title=\"'+playlist[currentPosition+1][1]+'\" />'							
		currentPosition++
	}
	refresh();
	
}

function previous()
{	
	if(currentPosition!=0)
	{			
		small.obj.innerHTML='<img '+smallImage(playlist[currentPosition-1][3],playlist[currentPosition-1][4],playlist[currentPosition-1][5])+' alt=\"'+playlist[currentPosition-1][1]+'\" title=\"'+playlist[currentPosition-1][1]+'\" />'				
		currentPosition--
	}
	refresh()
}

function refresh()
{		
	if(currentPosition==0)
	{
		previousButton.style.display='none'
		nextButton.style.display='block'
	}
	else if(currentPosition==(playlist.length-1))
	{
		previousButton.style.display='block'
		nextButton.style.display='none'
	}
	else
	{
		previousButton.style.display='block'
		nextButton.style.display='block'
	}
}

function checkSource()
{			
	if(initialised)
	{			
		url = main.location.href;			
		if( (url.indexOf('program_') !=-1))
		{				
			if(!selected)
			{
				for(i=0;i<all.length;i++)
				{
					if(all[i][0]==(url.substr(url.indexOf('id=')+3)))
					{
						previousButton.style.display='none'
						nextButton.style.display='none'
						small.style.display='none'						
						image.style.display='block'
				
						image.obj.innerHTML='<img src=\"'+all[i][3]+'.gif\" />'	
						selected = true
					}
				}
			}
		}
		else if(selected)			
			deselectProgram()			
	}
}