function activeTab(cls){
	$('.'+cls).click(function() {
		var id = $(this).attr('id');	
 		$('#tab'+id).show();
 		$('#'+id).addClass('active');
 		$('.tabcontent').not('#tab'+id).hide();
 		$('.tab').not('#'+id).removeClass('active');
	});	
}

function readMore(cls){
	$('.'+cls).click(function() {
		var id = $(this).parent('p').attr('id');
		if($('#'+id+' .moretext').is(":visible") == false){
			$('#'+id+' .moretext').show();
			$('#'+id+' .'+cls).html('Minimize');
		} else {
			$('#'+id+' .moretext').hide();
			$('#'+id+' .'+cls).html('&hellip Read More');
		}
	});	
}

function unitOvers(cls,h,spd){
	$(cls).hover(function() {
 		$(this).children('div').animate({ marginTop: -h }, spd);
	}, function() {
		$(this).children('div').animate({ marginTop: -0 }, spd);
	});	
}

function initListHolder(){
	if($("#arch_container").hasClass('list_holder')){
		centerTourItem();
	}
}

function centerTourItem(){
	
	var total_shows = $("#newsarchive > li").siblings().length;
	var current_location = window.location.toString();
	var item_index; //will hold the location in the list of the current item
	
	$("#newsarchive > li > a").each(function(i) {	
		var this_loc = $(this).attr('href');
		//grab the matching substring from the current location
		var index = current_location.length - this_loc.length;
		var curr_loc = current_location.substring(index);

		if(this_loc == curr_loc){ $(this).parent().addClass('current');$(this).removeAttr('href');item_index = i;}
		
	});
	
	//we will center the container if we can, but we cant do that for the first 6 items
	if(item_index > 5 && item_index < (total_shows-6)){
		var offset = -1 * ((item_index-5) *31) + 4;
		$("#newsarchive").css('top',offset+'px');
		
	}else if(item_index >= (total_shows-6)){
		if($("#newsarchive").outerHeight() - (18 * 31) < 0){
			var offset = 0;
		}else{	
			var offset = -1 * ($("#newsarchive").outerHeight() - (18 * 31));
		}
		$("#newsarchive").css('top',offset+'px');
	}
	
	//make the nav active, in need-be
	if(item_index > 5){
		$("#scroll_last_up").removeClass('off');
		$("#scroll_last_up").addClass('on');
	}
	if(item_index >= (total_shows-6)){
		$("#scroll_last_down").removeClass('on');
		$("#scroll_last_down").addClass('off');
	}
	
}

function start_scrollLast(dir){
	scrolling = setInterval("scrollLast('"+dir+"')",25);
}
   		
function stop_scrollLast(){
	clearInterval(scrolling);
}

function scrollLast(dir){
	var curr_top = sansPX($("#newsarchive").css('top'));
	var min_top = -1 *($("#newsarchive").outerHeight());
	var offset = sansPX($("#arch_container").css('height'));
	var max_top = 0;
	
	if(dir == 'down'){
		if(curr_top > (parseInt(min_top) + parseInt(offset))){
			var new_top = parseInt(curr_top) - 10;
		}
	}else{
		if(curr_top < max_top){
			var new_top = parseInt(sansPX(curr_top)) + 10;
		}
	}
	
	if(curr_top <= (parseInt(min_top) + parseInt(offset))){
		$("#scroll_last_down").removeClass('on');
		$("#scroll_last_down").addClass('off');
	}else{
		$("#scroll_last_down").removeClass('off');
		$("#scroll_last_down").addClass('on');
	}
	
	if(curr_top >= max_top){
		$("#scroll_last_up").removeClass('on');
		$("#scroll_last_up").addClass('off');
	}else{
		$("#scroll_last_up").removeClass('off');
		$("#scroll_last_up").addClass('on');
	}
	
	
	$("#newsarchive").css('top',new_top);
}

function sansPX(str){
	var tempMeasure = str;
	if(tempMeasure == ''){return 0;}
	else {
		if(tempMeasure.substring(tempMeasure.length-2, tempMeasure.length) == 'px'){
			var asInt = tempMeasure.substring(0, tempMeasure.length-2);
			return asInt;
		}else{
			return tempMeasure;
		}
	}
	
}

function scrollToLink(id){
	$('html, body').animate({
	scrollTop: $(id).offset().top
	}, 500);
}

function fadeOvers(id){
	$('#'+id+' li a img').hover(function(){ $(this).fadeTo('fast', 0.7); },function(){ $(this).fadeTo('fast', 1.0); });
}

function mycarousel_initCallback(carousel)
{
   
   carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function show_subnav(id,subid){
	$('#'+id).hover(
		function(){$('.flyout').fadeOut('fast'); elemCoords(id,subid); $('#nav li').removeClass('showsub'); fadeIn(id,subid);}, 
		function (){fadeOut(id,subid);}
	);
}

timer = null;

function fadeIn(id,subid){
	if(timer) clearTimeout(timer);
	$('#'+subid).fadeIn('fast');
	$('#'+id).addClass('showsub');
}

function fadeOut(id,subid){
	timer = setTimeout("doFade('"+id+"','"+subid+"')",100);
}

function doFade(id,subid){
	$('#'+subid).fadeOut('fast');
	$('#'+id).removeClass('showsub');
}

function subOvers(id,subid){
	$('#'+subid).hover(function() {
 		fadeIn(id,subid);
	}, function() {
		fadeOut(id,subid);
	});	
}

function changeVideo(vid){
	var obj = '<object width="455" height="280"><param name="movie" value="http://www.vevo.com/VideoPlayer/Embedded?videoId='+vid+'&playlist=false&autoplay=1&playerId=62FF0A5C-0D9E-4AC1-AF04-1D9E97EE3961&playerType=embedded"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.vevo.com/VideoPlayer/Embedded?videoId='+vid+'&playlist=false&autoplay=1&playerId=62FF0A5C-0D9E-4AC1-AF04-1D9E97EE3961&playerType=embedded" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="455" height="280" bgcolor="#000000"></embed></object>';
	$("#vevo").html(obj);
}

function selectMenu(){
	$('#videoselect').hover(
		function(){$(this).addClass('over');}, 
		function(){$(this).removeClass('over');}
	);
	$('#videoselect').click(
		function(){$('#videolist').slideToggle('fast');}
	);
	$('#videolist li').click(function(){
		var vid = $(this).attr('title');
		changeVideo(vid);
		$('#videolist').slideUp('fast');
	});
}

function addFields(cls){
	$('.'+cls).click(function() {
		var id = $(this).attr('id');	
 		$('#fields-'+id).show();
	});	
}

function elemCoords(id,subid){
	var pos = $('#'+id).position();
	var posLeft = pos.left;
	$('#'+subid).css('left', posLeft);
}

$(document).ready(function(){
	show_subnav('nav_artists','subnav');
	show_subnav('nav_records','recsubnav');
	subOvers('nav_artists','subnav');
	subOvers('nav_records','recsubnav');
	activeTab('tab');
	readMore('readmore');
	unitOvers('.covers li','100',100);
	$('#artistcarousel').jcarousel({ auto:3, scroll:1, wrap:'circular', initCallback: mycarousel_initCallback });
	$('a.lightbox').lightBox();
	initListHolder();
	$('a.commentjump').click(function(){ scrollToLink('#comment'); });
	fadeOvers('artistcarousel');
	fadeOvers('artistblocks');
	selectMenu();
	addFields('plus');
	$('#heroslides').cycle({ fx:'fade', timeout:8000 });
});