// JavaScript Document

$(document).ready(function(){ 
	
	//submenu NEW
	$("ul.submenu").hide();
	 
	$("ul.menu #artists").hover(function() {
		
		$("ul.submenu .list").hide();
		
        $(this).next("ul.submenu").fadeIn(200);
  
        $(this).parent().hover(function() {  
        }, function(){  
            //$(this).parent().find("ul.submenu").fadeOut(200); //When the mouse hovers out of the subnav, move it back up  
			$(this).parent().find("ul.submenu").fadeOut(200);
        });  
  
	});
	
	$("ul.menu #artists").siblings("ul.submenu").children("li").children("a").hover(function() {

		$(this).addClass("selected");
		$(this).next(".list").fadeIn(200);
  
        $(this).parent().hover(function() {  
        }, function(){  
            $(this).parent().find(".list").fadeOut(200);
			$("ul.menu #artists").siblings("ul.submenu").children("li").children("a").removeClass("selected");
        });  
  
	});
	
	//z-index of tabs	
	var itemSum = $(".tabs li").size();
	
	for(i=0; i<itemSum; i++) {
		//alert($(".tabs li").eq(i).css("z-index"));
		$(".tabs li").eq(i).css("z-index",10-i);
	}
	
	//tabs
	
	$(".artists .pictures .group").hide();
	$(".artists .pictures .group:first").show();
	
	var effectCount = 0;
	var effectLimit = 0;
	
	$(".artists .tabs li a").click(function() {											

		$(".artists .tabs li a").removeClass("selected");
		$(this).addClass("selected");
		
		$(".artists .pictures .group").hide();
		$(".artists .pictures .group").animate({"left": "0px"}, 50);
		var elem = $(this).attr("id");
		$('.'+elem).fadeIn(600);
		effectCount = 0;
		effectLimit = 0;
		category = '.'+elem;
		slider(elem);
		
	});
	
	function slider(elem) {
		//Disable left arrow	
		//$("#left").animate({"opacity": 0.1}, 200);
		//$("#right").animate({"opacity": 1.0}, 200);			
		
		$("#left").fadeOut(300);
		
		//Get size of the segments, how many segments there are, then determin the size of the area to avoid line breaks.
		var maskWidth = $('.'+elem).width();
		var segmentSum = $(".pictures").children('.'+elem).children(".item").size();
		var groupWidth = (maskWidth+25) * segmentSum;
		//alert(segmentSum);
			 
		//Counter and number of limit of movements
		effectCount = 0;
		effectLimit = segmentSum - 6;
		
		//alert(segmentSum);
		
		if(segmentSum<=6) {
			//$("#right").animate({"opacity": 0.1}, 200);
			$("#right").fadeOut(300);
			effectLimit = 0;
		}
		else {
			$("#right").fadeIn(300);
		}
		
		//alert(effectLimit);
		
		//Adjust the area to its new size
		$('.'+elem).css({'width' : groupWidth});
		
		if(category=="none") {
			category = '.'+$(".artists .tabs .selected").attr("id");
		}
		
		
		//slider functions		
		$("#left").click(function(event){
		//$('#left').unbind('click', function() {
			//$(this).unbind('click');
			//$("#left").die("click", this)
			event.stopImmediatePropagation();
			if(effectCount>0) {
				//$("#right").animate({"opacity": 1}, 200);
				$("#right").fadeIn(500);
				$(category).animate({"left": "+=135px"}, 500);
				effectCount-=1;
				if(effectCount<=0) {
					//$("#left").animate({"opacity": 0.2}, 500);
					$("#left").fadeOut(500);
				}
			}
		$(this).bind('click');
		});
		
		$("#right").click(function(event){			
		//$('#right').unbind('click', function() {
			//$("#right").die("click", this)
			event.stopImmediatePropagation();
			//$('.'+elem).clearQueue();			
			if(effectCount<effectLimit) {
				//$("#left").animate({"opacity": 1}, 200);
				$("#left").fadeIn(500);
				$(category).animate({"left": "-=135px"}, 500);
				effectCount+=1;
				if(effectCount>=effectLimit) {
					//$("#right").animate({"opacity": 0.2}, 500);
					$("#right").fadeOut(500);
				}
			}
		});		
	
	return false;
	}
	
	//Load artists
	
	var categoriesSum = $(".artists .pictures .group").size();
	//alert(categoriesSum);
	
	var category = "none";
	
	for(i=0; i<categoriesSum; i++) {
		categoryName = $(".artists .pictures .group").eq(i).attr("class").replace("group ","");
		$("."+categoryName).load("/artists/index.html #"+categoryName+" .artists .item", function() {
			
			slider($(".artists .tabs .selected").attr("id"));
			
		});
	}
		
	
	
	//Artists Page
		
	function getParameterByName(name)
	{
	  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	  var regexS = "[\\?&]" + name + "=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(window.location.href);
	  if(results == null)
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}
	
	var str = window.location.href;
	var pos = str.search(/type=/);
	if(pos>-1) {
		var artistType = getParameterByName("type");
		$(".pages").children("div").hide();
		$("#"+artistType).show();
	}
	
	$(".reviews").hide();
	$(".discography").hide();
	$(".photos").hide();
	$(".pages .tabs li a").click(function() {
		
		$(".pages .tabs li a").removeClass("selected");
		$(this).addClass("selected");
		
		$(".group").slideUp(600);
		var elem = $(this).attr("id");
		$('.'+elem).slideDown(600);
		
	});
	
	/*if($(".biography p").html().length==0) {
		$("#biography").hide();
	}
	if($(".reviews p").html().length==0) {
		$("#reviews").hide();
	}
	if($(".photos p").html().length==0) {
		$("#photos").hide();
	}*/
		
});
