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

var videoHandler = function()
{
	var styles = [
		[ 'none', 'none', 'auto', 'inline', 'none' ],
		[ 'block', 'groove', '250px', 'none', 'inline' ]
	];
	var youTubeId,show;
	var v,l,aShow,vIntro,vFoot;

	return {
		Init: function(_youTubeId,_videoWidth,_videoHeight,_show)
		{
			youTubeId	= _youTubeId;
			videoWidth	= _videoWidth;
			videoHeight	= _videoHeight;
			show		= 1 - _show;

			v		= document.getElementById('video');
			l		= document.getElementById('lyrics');
			aShow	= document.getElementById('aShow');
			vIntro	= document.getElementById('videoIntro');
			vFoot	= 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', 'content');

				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);
					
				swfobject.embedSWF('http://www.youtube.com/v/' + youTubeId + '&autoplay=1&rel=0', 'content', videoWidth, videoHeight, '9.0.0', 'expressInstall.swf', false, { bgcolor:document.bgColor });
			}
			else
				swfobject.removeSWF('content');

			var s = styles[show];

			v.style.display = s[0];

			if (vIntro)
				vIntro.style.display = s[0];

			if (vFoot)
				vFoot.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:'Chambao',lang:'es',name:'Chambao'},
		{id:'Chenoa',lang:'es',name:'Chenoa'},
		{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:'MariaJimena',lang:'es',name:'María Jimena'},
		{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:'AngelicaNachesova',lang:'ru',name:'Анжелика Начесова'},
		{id:'AniLorak',lang:'uk',name:'Ані Лорак'},
		{id:'ZhenyaOtradnaya',lang:'ru',name:'Женя Отрадная'},
		{id:'MakSim',lang:'ru',name:'МакSим'},
		{id:'ShCola',lang:'ru',name:'Ш-Cola'},
		{id:'YuliaSavicheva',lang:'ru',name:'Юлия Савичева'}
	];

	var selItem;

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

			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, isAlbum)
		{
			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	= (isAlbum ? 'AlbumIndex.php?a=' : 'Index.php?a=') + menuData[index].id + lng.lParam() + vendor.oParam();
		}
	};
}();

/*** Video button ***/

var visitedHandler = function()
{
	var unvistedCol;

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

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

		return null;
	}

	function Traverse(e)
	{
		while (e)
		{
			if (e.nodeType == 1)	// Element
				if (e.nodeName == 'IMG')
				{
					if (e.src.indexOf('Video.gif') >= 0)
						if (GetElementColour(e) == unvisitedCol)
							// If the link is unvisited then add an event handler to change the image when clicked
							e.setAttribute('onclick', 'javascript:src="VisitedVideo.gif"');
						else
							// Otherwise change the image now and the onclick event is not required
							e.src = 'VisitedVideo.gif';
				}
				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);
		}
	};
}();

/*** Translation language handler ***/

var xltHandler = function()
{
	var reqString, xltHdr, xltData;

	return {
		OnLoad: function(aReqString,selIndex)
		{
			reqString	= aReqString;
			xltHdr		= document.getElementById('xltHdr');
			xltData		= document.getElementById('xltData');

			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 i		= reply.indexOf('|');

			xltHdr.innerHTML	= reply.substring(0, i);
			xltData.innerHTML	= reply.substring(i + 1);
		}
	};
}();

/*** Commerce ***/

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

	var amazon = [
		{org:'1',url:'com',id:'ool-20',camp:'1789',creative:'9325'},
		{org:'2',url:'co.uk',id:'ool-21',camp:'1634',creative:'6738'},
		{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('<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>');
		},

		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 : '';
		}
	};
}();