﻿/*** Hide/show video ***/

var videoHandler = function()
{
	var styles = [
		[ 'none',  'none',   'auto',  'inline', 'none',   'none'      ],
		[ 'block', 'groove', '250px', 'none',   'inline', 'table-row' ]
	]
	var swfArgMaker = {
		YouTube:	function(videoId) { return {
			urlStr: 'http://www.youtube.com/v/' + videoId + '&amp;autoplay=1&amp;rel=0', flashvars: false, params: { bgcolor:document.bgColor } } },
		Vimeo:		function(videoId) { return {
			urlStr: 'http://vimeo.com/moogaloop.swf', flashvars: { clip_id:videoId, show_byline:0, show_portrait:0, show_title:0 }, params: { allowfullscreen:'false' } } },
		Antena3:	function(videoId) { return {
			urlStr: 'http://www.antena3.com/static/swf/A3Player.swf', flashvars: {xml:'http://www.antena3.com/videoxml/' + videoId + '.xml'}, params: { bgcolor:document.bgColor } } },
		'mail.ru':	function(videoId) { return {
			urlStr: 'http://img.mail.ru/r/video2/player_v2.swf', flashvars: { orig:2, movieSrc:videoId }, params: { allowfullscreen:'false' } } },
		rutube:		function(videoId) { return {
			urlStr: 'http://video.rutube.ru/' + videoId, flashvars: false, params: { wmode:'window', allowfullscreen:'false' } } }
	}
	var swfId = 'player'
	var server,videoId,show
	var v,l,aShow,vComment,vIntro,vOutro,vFootnote

	return {
		Init: function(_server,_videoId,_videoWidth,_videoHeight,_show)
		{
			server		= _server
			videoId		= _videoId
			videoWidth	= _videoWidth
			videoHeight	= _videoHeight
			show		= 1 - _show

			v			= document.getElementById('video')
			l			= document.getElementById('lyrics')
			aShow		= document.getElementById('aShow')
			vComment	= document.getElementById('videoComment')
			vIntro		= document.getElementById('videoIntro')
			vOutro		= document.getElementById('videoOutro')
			vFootnote	= document.getElementById('videoFootnote')

			this.ToggleVideo()
		},

		AddShowOrHide: function(strShow, strHide)
		{
			var href = 'javascript:videoHandler.ToggleVideo()'

			document.write('<a href="' + href + '"><img class="button" src="Clapperboard32.gif" alt="" /></a><div id="aShow" style="display:none"><a class="button" dictid="ShowVideo" href="' + href + '">' + strShow + '</a></div><div style="display:none"><a class="button" dictid="HideVideo" href="' + href + '">' + strHide + '</a></div>')
		},

		ToggleVideo: function()
		{
			if (show = 1 - show)
			{
				var d = document.createElement('div')

				d.setAttribute('id', swfId)

				d.innerHTML =
					'<br />You either have JavaScript turned off or an old version of Adobe\'s Flash Player.<br /><br /><a href="http://www.adobe.com/go/getflashplayer/">Get the latest Flash player</a>.'

				v.appendChild(d)

				var args = swfArgMaker[server](videoId)

				swfobject.embedSWF(args.urlStr, swfId, videoWidth, videoHeight, '9.0.0', 'expressInstall.swf', args.flashvars, args.params)
			}
			else
				swfobject.removeSWF(swfId)

			var s = styles[show]

			v.style.display = s[0]

			if (vComment)
				vComment.style.display = s[0]

			if (vIntro)
				vIntro.style.display = s[5]

			if (vOutro)
				vOutro.style.display = s[5]

			if (vFootnote)
				vFootnote.style.display = s[0]

			l.style.borderStyle	= s[1]
			l.style.height		= s[2]

			aShow.style.display	= s[3]
			aShow.nextSibling.style.display = s[4]
		}
	}
}()

/*** Artist menu ***/

var menuHandler = function()
{
	var menuData = [
		{id:'AmaiaMontero',lang:'es',name:'Amaia Montero'},
		{id:'BellePerez',lang:'es',name:'Belle Perez'},
		{id:'CarlaBruni',lang:'it',name:'Carla Bruni'},
		{id:'Chambao',lang:'es',name:'Chambao'},
		{id:'Chenoa',lang:'es',name:'Chenoa'},
		{id:'ChinoYNacho',lang:'es',name:'Chino & Nacho'},
		{id:'EdithPiaf',lang:'fr',name:'Édith Piaf'},
		{id:'ElCantoDelLoco',lang:'es',name:'El Canto Del Loco'},
		{id:'GloriaEstefan',lang:'es',name:'Gloria Estefan'},
		{id:'Jimena',lang:'es',name:'Jimena'},
		{id:'Juanes',lang:'es',name:'Juanes'},
		{id:'LaOrejaDeVanGogh',lang:'es',name:'La Oreja De Van Gogh'},
		{id:'LaraFabian',lang:'fr',name:'Lara Fabian'},
		{id:'LauraPausini',lang:'it',name:'Laura Pausini'},
		{id:'MalaRodriguez',lang:'es',name:'Mala Rodríguez'},
		{id:'MariaGadu',lang:'pt',name:'Maria Gadú'},
		{id:'MariaJimena',lang:'es',name:'María Jimena'},
		{id:'MikeOldfield',lang:'en',name:'Mike Oldfield'},
		{id:'NataliaBarbu',lang:'ro',name:'Natalia Barbu'},
		{id:'NuriaFergo',lang:'es',name:'Nuria Fergó'},
		{id:'RenanLuce',lang:'fr',name:'Renan Luce'},
		{id:'RicardoArjona',lang:'es',name:'Ricardo Arjona'},
		{id:'Various',lang:'en',name:'Various'},
		{id:'ZAZ',lang:'fr',name:'ZAZ'},
		{id:'Zazie',lang:'fr',name:'Zazie'},
		{id:'AngelicaNachesova',lang:'ru',name:'Анжелика Начесова'},
		{id:'AniLorak',lang:'uk',name:'Ані Лорак'},
		{id:'Aria',lang:'ru',name:'Ария'},
		{id:'AssiyaAkhat',lang:'uk',name:'Ассія Ахат'},
		{id:'ZhenyaOtradnaya',lang:'ru',name:'Женя Отрадная'},
		{id:'Zemfira',lang:'ru',name:'Земфира'},
		{id:'MakSim',lang:'ru',name:'МакSим'},
		{id:'Melnitsa',lang:'ru',name:'Мельница'},
		{id:'PolinaGagarina',lang:'ru',name:'Полина Гагарина'},
		{id:'Ranetki',lang:'ru',name:'Ранетки'},
		{id:'SuperDetki',lang:'ru',name:'Супер Детки'},
		{id:'ShCola',lang:'ru',name:'Ш-Cola'},
		{id:'YuliaSavicheva',lang:'ru',name:'Юлия Савичева'}
	]

	var selItem

	return {
		AddMenu: function(selected)
		{
			document.write('<select id="menu" size="10" onchange="menuHandler.OnChange(this)">')

			for (var i in menuData)
			{
				var mi = menuData[i]

				document.write('<option ')

				if (selected == mi.id)
				{
					selItem = i
					document.write('selected="selected" ')
				}

				document.write('lang="' + mi.lang + '" xml:lang="' + mi.lang + '">' + mi.name + '</option>')
			}

			document.write('</select>')
		},

		SetFocus: function()
		{
			var m = document.getElementById('menu')

			if (m)
				m.focus()
		},

		OnChange: function(menu)
		{
			var index = menu.selectedIndex

			// Reinstate the selected menu item so that it will be highlighted if the back button is pressed
			menu.selectedIndex	= selItem
			document.location	= 'Index.php?a=' + menuData[index].id + lng.lParam()
		}
	}
}()

/*** Video button ***/

var visitedHandler = function()
{
	var unvisitedCol

	function GetElementColour(e)
	{
		if (e.currentStyle)
			return e.currentStyle['color']

		if (window.getComputedStyle)
			return window.getComputedStyle(e, null).getPropertyValue('color')

		return null
	}

	function IsVisited(a)
	{
		// Returns true if any translation link has been visited
		while (a)
		{
			if (a.nodeName == 'A' && GetElementColour(a) != unvisitedCol)
				return true

			a = a.nextSibling
		}
		
		return false
	}
	
	function Traverse(e)
	{
		while (e)
		{
			if (e.nodeType == 1)	// Element
				if (e.nodeName == 'IMG')
				{
					if (e.getAttribute('class') == 'video' || /*IE*/ e.getAttribute('className') == 'video')
					{
						// Get the next anchor node
						var a = e.parentNode.nextSibling.nextSibling.firstChild

						if (IsVisited(a))
							e.style.backgroundPosition = '-36px -32px'
						else
						{
							// If the link is unvisited then add an event handler to all
							// anchor nodes to change the image when clicked
							var eCopy = e
							var scrpt = function () { eCopy.style.backgroundPosition = '-36px -32px' }

							while (a)
							{
								if (a.nodeName == 'A')
									if (a.addEventListener)
										a.addEventListener('click', scrpt, false)
									else /*IE*/ if (a.attachEvent)
										a.attachEvent('onclick', scrpt)

								a = a.nextSibling
							}
						}
					}
				}
				else
					Traverse(e.firstChild)

			e = e.nextSibling
		}
	}

	return {
		SetVisitedBtns: function()
		{
			// Create a dummy anchor node to get the 'unvisited' colour.
			var a = document.createElement('a')

			a.href='x'

			// Must add the node to the document in order to give it its currentStyle in IE
			if (document.firstChild.currentStyle)
			{
				document.appendChild(a)
				document.removeChild(a)
			}

			unvisitedCol = GetElementColour(a)

			Traverse(document.firstChild)
		}
	}
}()

/*** 'Show videos' checkbox ***/

var showVideos = function()
{
	var cookieName = 'showVids'
	var show

	function ModifyHrefs()
	{
		var elems = document.getElementsByTagName('a')

		for (var i in elems)
		{
			var a = elems[i]

			if (a.getAttribute)
			{
				var href = a.getAttribute('href')

				// FireFox has a relative path, IE is absolute.
				if (href && (href.substring(0, 5) != 'http:' || href.substring(0, 22) == 'http://www.ooltra.net/') && href.indexOf('Lyrics.php') >= 0)
				{
					href = href.replace(/&v=0/, '')

					if (!show)
						href = href + '&v=0'

					a.setAttribute('href', href)
				}
			}
		}
	}
	
	return {
		AddCheckbox: function(caption)
		{
			show = cookie.Get(cookieName) == null
			document.write('<div><input type="checkbox" onchange="showVideos.OnChange(this)" ')

			if (show)
				document.write('checked="checked" ')

			document.write('/><span dictid="ShowVideo">' + caption + '</span></div>')
		},

		OnLoad: function()
		{
			ModifyHrefs()
		},

		OnChange: function(checkBox)
		{
			if (show = !show)
				cookie.Delete(cookieName)
			else
				cookie.Set(cookieName, '*', 2419200 /*28 days*/, '/')

			ModifyHrefs()
		}
	}
}()

/*** Translation language handler ***/

var xltHandler = function()
{
	var reqString, xltElem = []

	return {
		OnLoad: function(aReqString,selIndex)
		{
			reqString = aReqString

			var tags = [ 'titleXlt', 'lyricsXlt', 'introXlt', 'outroXlt', 'footnotesXlt' ]

			for (var i = 0; i < tags.length; ++i)
				xltElem[i] = document.getElementById(tags[i])

			document.getElementById('cbXlt').selectedIndex = selIndex
		},

		AddCombo: function(xlts)
		{
			document.write('<select class="combo" id="cbXlt" size="1" onchange="xltHandler.OnChange(this)">')

			for (var v in xlts)
				document.write('<option dictid="' + v + 'LangName" value="' + v + '">' + xlts[v] + '</option>')

			document.write('</select>')
		},

		OnChange: function(combo)
		{
			var reply	= xmlHttp.Request('/GetXlt.php', reqString + combo.options[combo.selectedIndex].value)
			var arr		= reply.split('|')

			for (var i = 0; i < xltElem.length; ++i)
				if (xltElem[i])
					xltElem[i].innerHTML = arr[i]
		}
	}
}()

/*** Commerce ***/

var vendor = function()
{
	var vendors = ['Amazon.com', 'Amazon.fr', 'russiandvd.com']

	var amazon = [
		{org:'1',url:'com',id:'ool-20',camp:'1789',creative:'9325'},
		{org:'8',url:'fr',id:'ool0d-21',camp:'1642',creative:'6746'}
	]

	var selIndex

	function SetTags()
	{
		// Fix the language parameter in anchor tags.

		var elems = document.getElementsByTagName('a')

		for (var i in elems)
		{
			var a = elems[i]
			
			if (a.getAttribute)
			{
				var href = a.getAttribute('href')

				// FireFox has a relative path, IE is absolute.
				if (href && (href.substring(0, 5) != 'http:' || href.substring(0, 22) == 'http://www.ooltra.net/') && href.indexOf('Index.php') >= 0)
					a.setAttribute('href', href + (href.indexOf('?') < 0 ? '?o=' : '&o=') + selIndex)
			}
		}
	}

	return {
		OnLoad: function()
		{
			selIndex = location.search

			if (!selIndex || !(selIndex = selIndex.match('(\\?|&)o=([^&]*)')) || (selIndex = Number(selIndex[2])) < 0 || vendors.length <= selIndex)
				selIndex = 0

			if (selIndex != 0)
				SetTags()

			document.getElementById('cbVendor').selectedIndex = selIndex
		},

		FixLinks: function(_selIndex)
		{
			selIndex = _selIndex

			var isAmazon = selIndex < amazon.length
			var templ, a, p = /~~~/g

			if (isAmazon)
			{
				a = amazon[_selIndex]

				templ = '<a href="http://www.amazon.' + a.url + '/gp/product/~~~?ie=UTF8&tag=' + a.id + '&linkCode=as2&camp=' + a.camp + '&creative=' + a.creative + '&creativeASIN=~~~"><img class="button" src="MP3.gif" alt="MP3" /></a><img src="http://www.assoc-amazon.' + a.url + '/e/ir?t=' + a.id + '&l=as2&o=' + a.org + '&a=~~~" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />'
			}
			else
				templ = '<a href="http://www.russiandvd.com/store/album_mp3.asp?sku=~~~"><img class="button" src="MP3.gif" alt="MP3" /></a>'

			var elems = document.getElementsByTagName('td')

			for (var i in elems)
			{
				var s = elems[i], code

				if (s.getAttribute && (code = s.getAttribute('mp3')))
					s.innerHTML = templ.replace(p, code)
			}

			templ = isAmazon ? 
				'http://www.amazon.' + a.url + '/gp/product/~~~?ie=UTF8&tag=' + a.id + '&linkCode=as2&camp=' + a.camp + '&creative=' + a.creative + '&creativeASIN=~~~"' :
				'http://www.russiandvd.com/store/product.asp?sku=~~~&aid=151492&lang=eng'

			elems = document.getElementsByTagName('a')

			for (var i in elems)
			{
				var s = elems[i], code

				if (s.getAttribute && (code = s.getAttribute('cd')))
				{
					s.href = templ.replace(p, code)
					s.setAttribute('dictid', 'Buy')
					s.innerHTML = 'Buy'
				}
			}

			if (isAmazon)
				document.write('<scr'+'ipt src="http://www.assoc-amazon.' + a.url + '/s/link-enhancer?tag=' + a.id + '&o=' + a.org + '" type="text/javascr'+'ipt"></scr'+'ipt>')
		},

		AddCombo: function(caption)
		{
			document.write('<div><span dictid="Vendor">' + caption + '</span>: <select class="combo" id="cbVendor" size="1" onchange="vendor.OnChange(this)">')

			for (var v in vendors)
				document.write('<option>' + vendors[v] + '</option>')

			document.write('</select></div>')
		},

		OnChange: function(combo)
		{
			selIndex = combo.selectedIndex

			var href = location.href
			var a    = href.match('(\\?|&)o=([^&]*)')

			location.href = a ? href.replace(a[0], a[0].substring(0, 3) + selIndex) : href + '&o=' + selIndex
		},
		
		oParam: function ()
		{
			return selIndex != 0 ? '&o=' + selIndex : ''
		}
	}
}()

/*** Share this page ***/

function ShareThisPage()
{
	var url = encodeURIComponent(document.URL), title = encodeURIComponent(document.title)

	document.write('<div><span dictid="SharePage">Share this page</span>: <a href="mailto:?subject=This%20page%20at%20OOltra.net%20may%20interest%20you&amp;body=' + title + '%3A%20'+ url +'" target="_blank" title="E-mail"><img class="share email" src="x.gif" /></a>')
	document.write('<a href="http://www.facebook.com/sharer.php?u=' + url + '" target="_blank" title="Facebook"><img class="share facebook" src="x.gif" /></a>')
	document.write('<a href="http://twitter.com/home?status=Currently looking at ' + url + '" target="_blank" title="Twitter"><img class="share twitter" src="x.gif" /></a>')
	document.write('<a href="http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+ url +'&amp;title='+ title +'" target="_blank" title="Delicious"><img class="share delicious" src="x.gif" /></a>')
	document.write('<a href="http://www.reddit.com/submit?url='+ url +'" target="_blank" title="Reddit"><img class="share reddit" src="x.gif" /></a>')
	document.write('<a href="http://digg.com/submit?url='+ url +'&amp;title='+ title +'" target="_blank" title="Digg"><img class="share digg" src="x.gif" /></a>')
	document.write('<a href="http://www.stumbleupon.com/submit?url='+ url +'&amp;title='+ title +'" target="_blank" title="Stumbleupon"><img class="share stumbleupon" style="margin-right:0" src="x.gif" /></a></div>')
}
