var l_color1 = '#715142';
var l_color2 = '#33251E';
var h_color = '#D2C1D7';
var query = false;

window.onload = function init()
{
	document.getElementById('radio').innerHTML = '<div id="radio_dropdown"></div><div id="radio_obj" style="position: absolute;top: 0px;right: 0px;"><object type="application/x-shockwave-flash" width="400" height="15" data="src/xspf_player.swf?playlist_url=src/xspf.php&autoplay=true"><param name="movie" value="src/xspf_player.swf?playlist_url=src/xspf.php&autoplay=true" /></object></div>';	
	document.getElementById('cart').innerHTML = '<img class="icon" src="images/common/cart.png" alt="" /> shopping cart<br /><b id="item_count">0</b> item(s)<br /><span class="myLink" onclick="fixURL(\'#cart\')">view</span> | <span class="myLink" onclick="fixURL(\'#orderinfo\')">info</span>';
	document.getElementById('menu').innerHTML = '<div id="location" class="location"></div><div class="menu"><img class="icon" src="images/common/heart.png" alt="" /> <span class="myLink" onclick="fixURL(\'#about\')">about</span> <img class="icon" src="images/common/download.png" alt="" /> <span class="myLink" onclick="fixURL(\'#downloads\')">downloads</span> <img class="icon" src="images/common/rss.png" alt="" /> <a href="src/rss.php">rss</a></div>';
	document.getElementById('w3c').innerHTML = '<img class="icon" src="images/common/check.png" alt="" /> <a href="http://validator.w3.org/check?uri=referer">xhtml 1.0</a> <img class="icon" src="images/common/check.png" alt="" /> <a href="http://jigsaw.w3.org/css-validator/check/referer">css</a> | <span class="myLink" onclick="fixURL(\'#sitemap\')">sitemap</span>';
	document.getElementById('p_top_1').innerHTML = '. <span class="myLink" onclick="fixURL(\'#index\')">close</span>'
	document.getElementById('p_top_2').innerHTML = '. <span class="myLink" onclick="fixURL(\'#index\')">close</span>'
	
	var oldcart = readCookie('cartcookie');
	
	if (oldcart)
	{
		oldcart = oldcart.split(',')

		for(var i = 0;i < oldcart.length;i++)
		{
			cart[i / 2] = new Array(oldcart[i]*1,oldcart[i+1]*1);
			i++;
		}

		document.getElementById('item_count').innerHTML = cart.length;
	}

	var oldlocation = readCookie('locationcookie');

	if (oldlocation)
		c_loc_current = oldlocation*1;

	var oldkinds = readCookie('kindscookie');

	if (oldkinds)
		r_kinds_current = oldkinds*1;

	var oldartist = readCookie('artistcookie');

	if (oldartist)
		c_r_artist = oldartist;

	r_load();
	r_fill();
	fill_radio_dropdown();

	var loc = this.location.hash;
	query = this.location.search;

	if (query)
		getSite(query.replace(/\?/,'#'));
	else if (loc)
		fixURL(loc);
	else
		fixURL('#index');
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else
		var expires = "";

	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i = 0;i < ca.length;i++)
	{
		var c = ca[i];

		while (c.charAt(0) == ' ')
			c = c.substring(1,c.length);

		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}

	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function cv(lyr)
{
	if (document.getElementById(lyr).style.display == '')
	{
		document.getElementById(lyr).style.display = 'none';
	}
	else
	{
		document.getElementById(lyr).style.display = '';
	}
}

function show(lyr)
{
	document.getElementById(lyr).style.display = '';
}

function hide(lyr)
{
	document.getElementById(lyr).style.display = 'none';	
}

function include(loading,lyr,url)
{
	if (loading)
	{
		document.getElementById(lyr).innerHTML = '<div class="box" style="top: 50%;left: 50%;margin-left: -30px;width: 60px"><br /><center><img src="images/common/loading.gif" alt="" /></center><br /></div>';
	}
	
	var xmlhttp;

	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
		if (window.ActiveXObject)
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

	xmlhttp.open('GET',url,true);
	xmlhttp.setRequestHeader('If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT');
	xmlhttp.setRequestHeader('Cache-Control', 'no-cache');
	xmlhttp.setRequestHeader('X-Requested-With','XMLHttpRequest');
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			document.getElementById(lyr).innerHTML = xmlhttp.responseText;
			fixlinks();
		}
	}

	xmlhttp.send(null);
}

function count()
{
	self.counter.location.replace('src/counter.php?' + current.substring(1));
}

function fill_radio_dropdown()
{
	include(false,'radio_dropdown','src/radio_dropdown.php');
}

function radio(id, title)
{
	if (title.length > 30)
	{
		title = title.substring(0, 27) + '...';
	}
	
	document.getElementById('radio_selected').innerHTML = title;
	document.getElementById('radio_obj').innerHTML = '<object type="application/x-shockwave-flash" width="400" height="15" data="src/xspf_player.swf?playlist_url=src/xspf.php?' + id + '&autoload=true&autoplay=true"><param name="movie" value="src/xspf_player.swf?playlist_url=src/xspf.php?' + id + '&autoload=true&autoplay=true" /></object>';
}

function email(a, b)
{
	return("mail" + "to:" + b + "@" + a.replace(/%23/g,"."));
}

function o_info()
{	
	p_clean();
	include(true,'p_content','src/o_info.php');
	document.getElementById('location').innerHTML = '<span class="myLink" onclick="fixURL(\'#index\')">INDEX</span> | ORDERINFO';
	document.title = 'redstarcommunity.com | order information';
}

function s_open()
{
	p_clean();
	include(true,'p_content','src/sitemap.php')
	document.getElementById('location').innerHTML = '<span class="myLink" onclick="fixURL(\'#index\')">INDEX</span> | SITEMAP';
	document.title = 'redstarcommunity.com | sitemap';
}

function a_open()
{
	document.getElementById('a_popup').style.display = '';	
}

function a_close()
{
	document.getElementById('a_p_content').innerHTML = '';
	document.getElementById('a_popup').style.display = 'none';
}

function a_error()
{
	document.getElementById('a_p_content').innerHTML = '<div class="box" style="padding: 10px;text-align: center">error: section not found</div>';
	a_open();
	document.getElementById('location').innerHTML = '<span class="myLink" onclick="fixURL(\'#index\')">INDEX</span> | ERROR';
	document.title = 'redstarcommunity.com | error';
}

function b_close()
{
	document.getElementById('b_popup').innerHTML = '';
	document.getElementById('b_popup').style.display = 'none';
}

function p_open()
{	
	document.getElementById('p_clean').style.display = 'none';
	document.getElementById('cart').style.zIndex = '1';
	document.getElementById('cart').style.borderStyle = 'dashed';
	document.getElementById('cart').style.borderWidth = '1px';
	document.getElementById('cart').style.borderColor = '#666666';
	document.getElementById('cart').style.left = '-1px';
	document.getElementById('cart').style.top = '-1px';
	document.getElementById('p_main').style.display = '';
}

function p_clean()
{
	document.getElementById('p_main').style.display = 'none';
	document.getElementById('p_clean').style.display = '';
}

function p_close()
{
	if (document.body.style.overflow)
		document.body.style.overflow = 'auto';
	else
	{
		document.getElementsByTagName('html')[0].style.overflowY = 'auto';
		document.getElementsByTagName('html')[0].style.overflowX = 'hidden';
	}
	
	document.getElementById('cart').style.left = '0px';
	document.getElementById('cart').style.top = '0px';
	document.getElementById('cart').style.zIndex = '0';
	document.getElementById('cart').style.borderStyle = '';
	document.getElementById('cart').style.borderWidth = '';
	document.getElementById('cart').style.borderColor = '';
	document.getElementById('p_main').style.display = 'none';
	document.getElementById('p_clean').style.display = 'none';
	r_kinds_current = -1;
	createCookie('kindscookie',-1,0.08);
	r_fill();
	document.getElementById('location').innerHTML = 'INDEX';
	document.title = 'redstarcommunity.com | index';	
}

function p_error()
{
	document.getElementById('p_content').innerHTML = '<div class="box" style="padding: 10px;text-align: center">error: section not found</div>';
	p_open();
	document.getElementById('location').innerHTML = '<span class="myLink" onclick="fixURL(\'#index\')">INDEX</span> | ERROR';
	document.title = 'redstarcommunity.com | error';
}

function fixlinks()
{
	var links = document.getElementsByTagName('span');

	for (var i = 0;i < links.length;i++)
	{
		if (links[i].className == 'myLink')
		{
			links[i].onmouseover = function () {this.style.color = l_color1;}
			links[i].onmouseout = function () {this.style.color = l_color2;}
		}
	}
}


var explorer = BrowserDetect.browser == 'Explorer';
var safari  = BrowserDetect.browser == 'Safari';
var current = '';
var f;

function getSite(loc)
{
	var dfix = false;
	var old = current.split('&');
	current = loc;
	loc = loc.split('&');

	if ((old[0] == loc[0] && old[1] == loc[1]) || (old[0] == loc[0] && (loc[0] != '#releases' && loc[0] != '#artists')))
		dfix = true;

	d_close();
	b_close();

	switch (loc[0])
	{
		case '#about':			
			if (!dfix)
					h_about();
				else
					dfix = false;

				if (loc[1])
					switch (loc[1])
					{
						case 'artists':
							h_artists();
							break;
						case 'releases':
							h_releases();
							break;
						case 'demos':
							h_demos();
							break;
						case 'distribution':
							h_distribution();
							break;
						case 'faq':
							h_faq();
							break;
						case 'contact':
							h_contact();
							break;
						default:
							a_error();
					}
				else
					h_end();
			break;
		case '#artists':
			if (loc[1])
			{
				if (!dfix)
					a_view(loc[1]);
				else
					dfix = false;

				if (loc[2])
					switch (loc[2])
					{
						case 'releases':
							a_releases();
							break;
						case 'articles':
							a_articles();
							break;
						case 'files':
							a_files();
							break;
						case 'images':
							a_images();
							break;
						case 'videos':
							a_videos();
							break;
						default:
							a_error();
					}
				else
					a_end();
			}
			break;
		case '#releases':
			if (loc[1])
			{
				if (!dfix)
					r_view(loc[1]);
				else
					dfix = false;

				if (loc[2])
					switch (loc[2])
					{
						case 'listen':
							r_listen();
							break;
						case 'artwork':
							r_artwork();
							break;
						case 'files':
							r_files();
							break;
						case 'lyrics':
							r_lyrics();
							break;
						case 'reviews':
							r_reviews();
							break;
						default:
							a_error();
					}
				else
					r_end();
			}
			break;
		case '#orderinfo':
			p_close();
			o_info();
			break;
		case '#cart':
			p_close();
			c_view();

			if (loc[1])
				switch (loc[1])
				{
					case 'success':
						c_success();
						break;
					case 'cancel':
						c_cancel();
						break;
					default:
						p_error();
				}
			break;
		case '#downloads':
			a_close();
			p_close();
			d_open();
			break;
		case '#sitemap':
			p_close();
			s_open();
			break;
		case '#index':
			a_close();
			p_close();
			break;
		default:
			p_error();
	}

	fixlinks();

	if (!(explorer || safari))
		count();
}

function fixURL(myhash)
{
	if (query)
		this.location = 'http://www.redstarcommunity.com/' + myhash;

	clearTimeout(f);

	if (explorer || safari)
	{
		self.iehistory.location.href = 'src/helper.php?' + myhash.substring(1);
		IEcheckURL();
	}
	else
	{
		this.location.href = myhash;
		checkURL();
	}
}

function checkURL()
{
	var hash = this.location.hash;
		
	if(current != hash)
		getSite(hash);

	f = setTimeout('checkURL()', 100);
}

var helper = '';

function IEcheckURL()
{
	if (helper != current)
	{
		if (!safari)
			this.location.href = helper;

		getSite(helper);
	}

	f = setTimeout('IEcheckURL()', 100);
}
