var KobSettings = {
	storeCatalog: 0,
	downloadCatalog: 0
}

var HashCookie = new Hash.Cookie('KOB');

if(document.ELEMENT_NODE == null) {
   document.ELEMENT_NODE = 1;
   document.TEXT_NODE = 3;
}

function centerDiv(div) {
	var coords = div.getCoordinates();
	div.setStyles({
		left: ((window.getWidth()-coords.width)/2)+"px",
		top: ((window.getHeight()-coords.height)/2)+"px"
	});
}

function getTextValue(el) {
   var s = "";
   var node = el.firstChild;
   while(node) {
      if(node.nodeType == document.TEXT_NODE) {
         s += node.nodeValue;
      }
      else {
         if(node.nodeType == document.ELEMENT_NODE) {
            if(node.tagName == "BR") {
               s += " ";
            }
            else if(node.tagName == "INPUT") {
               s += node.value;
            }
	    else {
	    	s += getTextValue(node);
	    }
         }
         else {
            s += getTextValue(node);
         }
      }
      node = node.nextSibling;
   }
   return s;
}

var logoAppear = function() {
	var logo = $('logo');
	if(!logo)
		return;
	logo.setStyles({
		display:'block',
		opacity: 0
	});

	new Fx.Style(logo, 'opacity', {duration: 1000} ).start(1);
};

function findArtist(name) {
	for(a=0; a<artistAlbums.length; a++) {
		if(artistAlbums[a].name==name) {
			return artistAlbums[a];
		}
	}
	return null;
}

function browseArtist(artistName) {
	var div = $('browser').setStyles({
		display:'block',
		opacity: 0
	});
	centerDiv(div);
	h1 = div.getElementsByTagName('h1')[0];
	h1.innerHTML = artistName;
	var settings = { tl: { radius: 8 }, tr: { radius: 8 }, bl: { radius: 8 }, br: { radius: 8 }, antiAlias: true, autoPad: true, validTags: ["div"]};
        var corners = new curvyCorners(settings, div);
        corners.applyCornersToAll();
        $('albums_content').innerHTML = '';
	new Fx.Style(div, 'opacity', {duration: 1000} ).start(1);
	var artist = findArtist(artistName);
	if(artist!=null) {
		for(i=0; i< artist.albums.length; i++) {
			var img = document.createElement('IMG');
			var album = artist.albums[i];
			img.src = 'img/'+album+'.jpg';
			img.className = 'album_cover';
			img.album = album;
			img.onclick = function() {
				$('album_view').src = this.album+".html";
			};
			$('albums_content').appendChild(img);
		}
	}
}

var artistsAppear = function() {
	var artistDivs = $ES('div[class="artist"]');
	var cx = window.getWidth()/2;
	var cy = window.getHeight()/2;
	var rx = cx-100;
	var ry = cy-100;
	
	for(i=0; i<artistDivs.length; i++) {
		var div = artistDivs[i].setStyles({
			display: 'block',
			opacity: 0
		});
		var angle = i*(Math.PI*2)/artistDivs.length;
		var px = Math.sin(angle)*rx+cx;
		var py = Math.cos(angle)*ry+cy;
		var coords = div.getCoordinates();
		div.setStyles({
			left: (px-coords.width/2)+"px",
			top: py+"px",
			zIndex: i+1
		});
		new Fx.Style(div, 'opacity', {duration: 2000} ).start(1);
	}
	
	artistDivs.each(function(artist) {
		var fx = new Fx.Styles(artist, {duration:200, wait:false});
		artist.addEvent('mouseenter', function(){	
			fx.start({
				color: '#fff'
			});
		});
 
		artist.addEvent('mouseleave', function(){
			fx.start({
				'color': '#000'
			});
		});
		
		artist.addEvent('mousedown', function(){
			browseArtist(getTextValue(artist));
		});
	});
}

var Menu = new Class({
	initialize: function(selector, options) {
		this.options = Object.extend({
			subElement: false,
			subElementSelector: 'a',
			slideIn: false,
			slideInDelayed: false,
			slideInDelay: 150,
			slideInProperty: 'margin-top',
			slideInRange: ['-30', '0']
		}, options || {})
		this.selector = selector;
		this.currTimer = 500;
		if (this.options.slideIn) {
			$A($E(selector).childNodes).each(function(el){
				if ($type(el) == 'element')
				{
					el = $(el);
					if (this.options.subElement)
						el = el.getElement(this.options.subElementSelector);
					this.slideIn(el);
					this.addSpecialEffects(el);
				}
			}, this);
		}
		if (this.options.slideOut) {
			$ES('a').each(function (element) {
				if (!(element.hasClass('QE_Link') | element.hasClass('download')))
				{
					element.onclick = function() {
						loadLink = function ()
						{
							document.location = this.href;
						}
						mainContentEffect = new Fx.Style($E('.navigation ul'), 'opacity', {
						duration: 300,
						onComplete: loadLink.bind(this)
						});
						mainContentEffect.custom(1, 0);
						return false;
					};
				}
			});
		}
		$ES(selector + ' li ul').each(function(el) {
			el.setStyles({
				'display': 'block',
				'opacity': 0
			});
			elParent = $(el.parentNode);
			
			currentMenu = new Fx.Style(el, 'opacity');
			elParent.addEvents({
				'mouseover': function(submenu, myParent) {
					myParent.addClass('hover');
					submenu.clearTimer();
					submenu.custom(1);
				}.pass([currentMenu, elParent]),
				'mouseout': function(submenu, myParent) {
					myParent.removeClass('hover');
					submenu.clearTimer();
					submenu.custom(0);
				}.pass([currentMenu, elParent])
			})
			
		}.bind(this));
	},
	slideIn: function(el) {
		$(el).setStyle(this.options.slideInProperty, this.options.slideInRange[0] + "px");
		//el.setStyle('zIndex', 1);
		var effect = el.effect(this.options.slideInProperty, {transition: Fx.Transitions.elasticOut, duration: 1000});
		if (this.options.slideInDelayed)
		{
			effect.custom.delay(this.currTimer, effect, this.options.slideInRange);
			this.currTimer += this.options.slideInDelay;
		} else
		effect.custom.delay(this.options.slideInDelay, effect, this.options.slideInRange);
	},
	slideOut: function(el) {
		var effect = el.effect(this.options.slideOutProperty, {transition: Fx.Transitions.elasticOut, duration: 1000});
		window.addEvent('beforeunload', function (effect) {
			effect.custom.pass(this.options.slideInRange, effect);
			}.pass(effect, this)
		);
	},
	addSpecialEffects: function(el)	{
		if (!$(el.parentNode).hasClass('active'))
		{
			el.setStyles({
				'borderTopWidth': '0px',
				'borderTopColor': '#000',
				'borderTopStyle': 'solid'
			});
			currentItem = new Fx.Style(el, 'borderTopWidth', {transition: Fx.Transitions.backOut, duration: 800});
			$(el.parentNode).addEvents({
				'mouseover': function(itemBorder) {
					itemBorder.clearTimer();
					itemBorder.custom(5);
				}.pass(currentItem),
				'mouseout': function(itemBorder) {
					itemBorder.clearTimer();
					itemBorder.custom(0);
				}.pass(currentItem)
			})
		}
	}
});

function menuSetup() {
    //var menus = new Menu("#navigation ul", {slideIn:false, slideInDelayed:false, slideOut:false, subElement:true});
    $('logo').addEvent("click", function() {
		document.location="index.html";
	});
}

function albumsSetup() {
	var check = $('albums_container');
	if(!check)
		return;
	var carousel = new Carousel( 'albums_container', 'albums', 'albums_left', 'albums_right', 'albums_title');
	return;
}

function artistsSetup() {
	var check = $('artistlist');
	if(!check)
		return;
	var artistTabs = new mootabs('artistlist', {
		changeTransition: 'none',
		activateOnLoad:		'first',
		duration: 1000,
		mouseOverClass: 'over',
		width: '100%'
	});
}

function albumSetup() {
	var check = $('album');
	if(!check)
		return;
	var albumTabs = new mootabs('album', {
		changeTransition: 'none',
		activateOnLoad:		'first',
		duration: 1000,
		mouseOverClass: 'over'
	});
	$ES('#albums_related a').each(function(el) {
		el.setOpacity( 0.5 );
		el.addEvents({
			'mouseover': function(item) {
				item.setOpacity( 1 );
				$("relatedinfo").innerHTML=el.rel.replace(/-/,"<br/>");
			}.pass([el]),
			'mouseout': function(item) {
				item.setOpacity( 0.5 );
				$("relatedinfo").innerHTML="";
			}.pass([el])
		})
		
	}.bind(this));
}

function mapSetup() {
	var map = $('map');
	if(!map)
		return;
	var mapScroll = new Scroller('map', {area: 50, velocity: 1});
	map.addEvent('mouseover', mapScroll.start.bind(mapScroll));
	map.addEvent('mouseout', mapScroll.stop.bind(mapScroll));
	map.scrollTo(400,50);
}

function doSearch() {
	var slide = new Fx.Slide('search_results');
	slide.slideOut();
}

window.addEvent("domready", function() {
	menuSetup();
	albumsSetup();
	artistsSetup();
	albumSetup();
	mapSetup();
});

