function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

var artistTo = null;
var commTo = null;

var hideCommunity = false;
var hideArtist = false;

jQuery(document).ready(function(){
	$("#navArtists a").mouseover(function(){
		$("#artistListBlock").stop();
		$("#artistListBlock").queue([]);
		if($(this).parent().hasClass('t')){
			$("#artistListBlock").css("top","-220px");
		} else {
			$("#artistListBlock").css("top","220px");
		}
		
		$("#artistListBlock").parent().css("left","0px");
		$("#artistListBlock").css("left","0px");
		
		$("#artistListBlock").animate({top:"0px"},300,null,function(){});
		hideArtist = true;
	});
	
	$("#navArtists a").delay({
		delay:10,
		event:'mouseout',
		fn:hideArtistBlock
	});
	
	$("#artistListBlock").mouseover(function(){
		hideArtist = false;
	});
	
	$("#artistListBlock").mousemove(function(){
		clearTimeout(artistTo);
		artistTo = setTimeout(function(){hideArtistBlock2("#navArtists a")},1200);

	});
	
	$("#navCommunity a").mouseover(function(){
		$("#commListBlock").stop();
		$("#commListBlock").queue([]);
		if($(this).parent().hasClass('t')){
			$("#commListBlock").css("top","-220px");
		} else {
			$("#commListBlock").css("top","220px");
		}
		$("#commListBlock").parent().css("left","0px");
		$("#commListBlock").css("left","0px");
		$("#commListBlock").css("left","0px");
		$("#commListBlock").animate({top:"0px"},300,null,function(){});
		hideCommunity = true;
	});
	
	$("#navCommunity a").delay({
		delay:10,
		event:'mouseout',
		fn:hideCommunityBlock
	});
	
	$("#commListBlock").mouseover(function(){
		hideCommunity = false;
	});
	
	$("#commListBlock").mousemove(function(){
		clearTimeout(commTo);
		commTo = setTimeout(function(){hideCommunityBlock2("#navCommunity a")},1200);
	});
});

function hideArtistBlock(){
	if(hideArtist === true){
		if(Math.abs(parseInt($("#artistListBlock").css("top"))) == 0){
			var end = $(this).parent().hasClass('b') ? "220px" : "-220px";
			$("#artistListBlock").animate({top:end},300,null,function(){
				$("#artistListBlock").parent().css("left","-10000px");
			});
		}
	}
}

function hideArtistBlock2(selector){
	var end = $(selector).parent().hasClass('b') ? "220px" : "-220px";
	$("#artistListBlock").animate({top:end},300,null,function(){
		$("#artistListBlock").parent().css("left","-10000px");
	});
}

function hideCommunityBlock(){
	if(hideCommunity === true){
		var end = $(this).parent().hasClass('b') ? "220px" : "-220px";
		$("#commListBlock").animate({top:end},300,null,function(){
			$("#commListBlock").parent().css("left","-10000px");
		});
	}
}

function hideCommunityBlock2(selector){
	if(Math.abs(parseInt($("#commListBlock").css("top"))) == 0){
	var end = $(selector).parent().hasClass('b') ? "220px" : "-220px";
		$("#commListBlock").animate({top:end},300,null,function(){
			$("#commListBlock").parent().css("left","-10000px");
		});
	}
}

var __timer__artist_list_ = null;
var __timer__hide_artist_list = null;
var __mem__anchor = null;

var addBlur = function(el){
	el.onfocus = function(){this.blur();}
}

var anim = function(element,style,start,end,steps,interval,callback){
	var diff = Math.abs(start-end);
	var step = diff/steps;
	if(start>end) step = step * -1;
	
	__timer__artist_list_ = setInterval(function(){move(element,style,step,start,end,callback)},interval);
}

function move(element,prop,step,start,end,callback){
	var s = parseInt(element.style[prop]);
	if(callback === undefined) callback = function(){}
	if(isNaN(s)) s = start;
	if(start>end){
		if(s+step <= end){
			element.style[prop] = end+"px";
			clearTimeout(__timer__artist_list_);
			callback();
		} else {
			element.style[prop] = s+step+"px";
		}
	} else {
		if(s+step >= end){
			element.style[prop] = end+"px";
			clearTimeout(__timer__artist_list_);
			callback();
		} else {
			element.style[prop] = s+step+"px";
		}
	}
}

var showArtist = function(el,anchor){
	hideArtist('commListBlock',anchor);
	hideArtist('artistListBlock',anchor);
	
	el = document.getElementById(el);
	el.style.left = "0px";
	el.parentNode.style.left = "0px";

	switch(anchor){
		case 't':
			var s = parseInt(el.style.top);
			if(isNaN(s)) s = -220;
			var e = 0;
			var style = 'top';
			break;
		case 'b':
			var s = parseInt(el.style.top);
			if(isNaN(s)) s = 220;
			var e = 0;
			var style = 'top';
			break;
	}
	
	anim(el,style,s,e,15,5);
	__mem__anchor = anchor;
}

var timeHideArtist = function(el,anchor,timeout){
	anchor = anchor || __mem__anchor;
	timeout = timeout || 1000;
	clearTimeout(__timer__hide_artist_list);
	__timer__hide_artist_list = setTimeout(function(){hideArtist(el,anchor)},timeout);
}

var hideArtist = function(el,anchor){
	el = document.getElementById(el);
	switch(anchor){
		case 'b':
			var s = parseInt(el.style.top);
			var e = 220;
			var style = 'top';
			break;
		case 't':
			var s = parseInt(el.style.top);
			var e = -220;
			var style = 'top';
			break;
	}
	
	if(s != e){
		clearTimeout(__timer__hide_artist_list);
		anim(el,style,s,e,15,5,function(){/*el.style.left="-10000px";el.parentNode.style.left="-10000px";*/});
	}
}

var slideShow = function(images,id){
	var images = images;
	var index = 0;
	var imgHolder = document.createElement("img");

	this.container = document.getElementById(id);
	var cont = this.container;
	var ih = imgHolder;
	
	var width = parseInt(cont.style.width) || 300;

	for(var i = 0, l=images.length; i<l; i++){
		images[i].Image = new Image();
		images[i].Image.src = images[i].src;
		if(i==0){
			images[i].Image.onload = function(){
				ih.src = this.src;
				ih.height = (width / this.width) * this.height;
				ih.width = width; //this.width;
				//ih.style.height = this.height+"px";
				//ih.style.width = this.width+"px";
				cont.appendChild(ih);	
			}
		}
	}

	var imgHolderNext = null;
	var imgHolderPrev = null;
	
	
	
	var prevButton = document.getElementById("picsControlPrev");
	var nextButton = document.getElementById("picsControlNext");
	
	function makeNextImg(){
		nextButton.style.cursor = "";
		
		if(images.length > index + 1){
			imgHolderNext = document.createElement("img");
			imgHolderNext.src = images[index+1].Image.src;
			imgHolderNext.height = (width / images[index+1].Image.width) * images[index+1].Image.height;
			imgHolderNext.width = width; //images[index+1].Image.width;
			//imgHolderNext.style.height = images[index+1].Image.height+"px";
			//imgHolderNext.style.width = images[index+1].Image.width+"px";
			
			nextButton.style.cursor = "pointer";
		}
	}
	
	function makePrevImg(){
		prevButton.style.cursor = "";
		
		if(index != 0){
			imgHolderPrev = document.createElement("img");
			imgHolderPrev.src = images[index-1].Image.src;
			imgHolderPrev.height = (width / images[index-1].Image.width) *  images[index-1].Image.height;
			imgHolderPrev.width = width; //images[index-1].Image.width;
			//imgHolderNext.style.height = images[index-1].Image.height+"px";
			//imgHolderNext.style.width = images[index-1].Image.width+"px";
			
			prevButton.style.cursor = "pointer";
		}
	}
	
	this.next = function(){
		if(index < images.length - 1){
			index++;
			this.container.removeChild(imgHolder);
			this.container.appendChild(imgHolderNext);
			
			imgHolder = imgHolderNext;
			
			makeNextImg();
			makePrevImg();
		}
	}
	
	this.prev = function(){
		if(index != 0){
			index--;
			this.container.removeChild(imgHolder);
			this.container.appendChild(imgHolderPrev);
			
			imgHolder = imgHolderPrev;
			
			makeNextImg();
			makePrevImg();
		}
	}
	
	makeNextImg();
}


addLoadEvent(function(){
	var a = document.getElementsByTagName('a');
	var c = a.length;
	for(i=0;i<c;i++){
		addBlur(a[i]);
	}	
});


function playTrack(idx){

	if(navigator.appName.indexOf('Microsoft') != -1){
		var gp=window['audioPlayerEmbed'];
	}else{
		var gp = document['audioPlayerEmbed'];
	}
	if(gp && gp.SetVariable){
		gp.SetVariable('jsTrackCom',idx);
	} else {
		setTimeout(function(){playTrack(idx)},10);
		return; 
	}
}
