
$(document).ready(function(){
	
	$('input.search-text').click(function(){ $(this).val(''); });

	// artists tab in horiz. nav
	$('#top-menu-item3').click(function(){ 
		
		$(this).find('a').toggleClass('open');
		$('#artist-list, #side-tabs').toggle(); 
		$(this).find('a').blur();
		return false; 
	});

	$('#side-tabs li#side-tab1').click(function(){ // looking at images
		
		$(this).css('background', 'transparent url(/images/btn_images_a.png) repeat scroll 0 0');
		$('#side-tabs li#side-tab2').css('background', 'transparent url(/images/btn_listing_b.png) repeat scroll 0 0');
		$('#artist-list-cont').hide();
		$('#artist-list').addClass('open');
		$('#artist-images-cont').show();
		$(this).find('a').blur();
		$('#side-tabs').css('top', '180px');
		$('#artist-images-slide').jScrollHorizontalPane({scrollbarHeight:5});
		$('#artist-filter').hide();
	});

	$('#side-tabs li#side-tab2').click(function(){ // looking at listing
		
		$(this).css('background', 'transparent url(/images/btn_listing_a.png) repeat scroll 0 0');
		$('#side-tabs li#side-tab1').css('background', 'transparent url(/images/btn_images_b.png) repeat scroll 0 0');
		$('#artist-list-cont').show();
		$('#artist-list').removeClass('open');
		$('#artist-images-cont').hide();
		$(this).find('a').blur();
		$('#side-tabs').css('top', '278px');
		$('#artist-filter').show();
	});
	// end artists tab

	$('table.artist-images td').mouseover(function(){
		
		$(this).addClass('over');
	});

	$('table.artist-images td').mouseout(function(){
		
		$(this).removeClass('over');
	});

	$('#artist-images-arrows a').click(function(){
		
		inc			= 838;
		curr_offset	= $('#artist-images-slide').position();
		curr_pos	= curr_offset.left;
		
		if ($(this).parent().hasClass('next')){

			if (curr_pos < 3000) {

				$('#artist-images-slide')[0].scrollTo((curr_pos - inc) * -1);
			
			} else {
				
				$('#artist-images-slide')[0].scrollTo(3000);
			}
		
		} else {
			
			if (curr_pos < (inc * -1)) {
				
				$('#artist-images-slide')[0].scrollTo((curr_pos + inc) * -1);
			
			} else {
				
				$('#artist-images-slide')[0].scrollTo(0);
			}
		}
		
		$(this).blur();
		return false;
	});
	
	// artist roster filter

	$('#artist-filter a').click(function(){
		
		rel = $(this).attr('rel');
		
		if ($(this).hasClass('on')){ // turn off
			
			$('#artist-filter a').removeClass('on');
			$('#artist-list-cont li').removeClass('off');
			$(this).removeClass('on');
			//$('#artist-list-cont li.' + rel).addClass('off');
		
		} else { // turn on
			
			$('#artist-filter a').removeClass('on');
			$('#artist-list-cont li').addClass('off');
			$(this).addClass('on');
			$('#artist-list-cont li.' + rel).removeClass('off');

			if (rel == 'r1' || rel == 'r2' || rel == 'r3'){

				$('#artist-list-cont li.r4').removeClass('off');
			}
		}

		if ($(this).attr('rel') == 'symph' && $(this).hasClass('on')){

			$('#artist-list-cont li.symph').show();
		
		} else {
			
			$('#artist-list-cont li.symph').hide();
		}

		if ($(this).attr('rel') == 'holiday' && $(this).hasClass('on')){

			$('#artist-list-cont li.holiday').show();
		
		} else {
			
			$('#artist-list-cont li.holiday').hide();
		}
		
		$(this).blur();
		return false;
	});

	$('#header h1').click(function(){ window.location = '/'; });

	$('input#eletter-subscribe, input#send-to-friend, input#send-friend-txt').click(function(){
		
		$(this).val('');
		$(this).css('color', '#000000');
	});

	$('#eletter-submit').click(function(){
		
		$.post('/emails/submit/', { email: $('input#eletter-subscribe').val() }, function(data){
		
			$('input#eletter-subscribe').val(data.message);
			$('input#eletter-subscribe').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");

		return false;
	});

	$('#send-to-friend-submit').click(function(){
		
		$.post('/emails/send_to_friend/', { email: $('input#send-to-friend').val(), link: window.location.href }, function(data){
		
			$('input#send-to-friend').val(data.message);
			$('input#send-to-friend').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");

		return false;
	});

	$('#send-friend-btn').click(function(){
		
		$.post('/emails/send_to_friend/', { email: $('input#send-friend-txt').val(), link: window.location.href }, function(data){
		
			$('input#send-friend-txt').val(data.message);
			$('input#send-friend-txt').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");

		return false;
	});

	$('select.jump').change(function(){
		
		window.location = $(this).val();
	});

	$('td.listen').click(function(){
		
		$.getJSON('/media/data/' + $(this).parent().attr('id') + '/', null, function(data){
			
			swfobject.embedSWF("/flash/audio-player.swf", "music-player-box", "340", "72", "8.0.0", false, data);
		});
		
		curr_clip = $(this).parent().attr('id');
		$('td.playing').removeClass('playing');
		$(this).parent().find('td').addClass('playing');
		share_media($(this).parent().attr('id'));
	});

	$('a.listen').click(function(){
		
		$.getJSON('/media/data/a-' + $(this).attr('rel') + '/', null, function(data){
			
			swfobject.embedSWF("/flash/audio-player.swf", "music-player-box", "340", "72", "8.0.0", false, data);
		});
		
		curr_clip = $(this).attr('rel');
		$('td.playing').removeClass('playing');
		$('#a-' + curr_clip + ' td').addClass('playing');
		share_media($(this).closest('tr').attr('id'));
		return false;
	});

	$('td.watch').click(function(){
		
		$.getJSON('/media/data/' + $(this).parent().attr('id') + '/', null, function(data){
			
			swfobject.embedSWF("/flash/video-player.swf", "video-player-box", "340", "322", "9.0.0", false, data);
		});
		
		curr_clip = $(this).parent().attr('id');
		curr_video = $(this).parent().attr('id');
		$('td.playing').removeClass('playing');
		$(this).parent().find('td').addClass('playing');
		share_media($(this).parent().attr('id'));
	});

	$('tr.curr-clip').each(function(){
		
		$.getJSON('/media/data/' + $(this).attr('id') + '/', null, function(data){
			
			swfobject.embedSWF("/flash/audio-player.swf", "music-player-box", "340", "72", "8.0.0", false, data);
		});
		
		curr_clip = $(this).attr('id');
		$('td.playing').removeClass('playing');
		$(this).find('td').addClass('playing');
	});

	var curr_clip = 'all'; 

	$('a.audio-popper').click(function(){
		
		swfobject.embedSWF("/flash/audio-player.swf", "music-player-box", "340", "72", "8.0.0", false, false);

		window.open('/media/stand_alone/' + curr_clip + '/', 'stand_alone', 'width=340, height=550, resizable=no, scrollbars=no, toolbar=no');

		$(this).blur();
		return false;
	});

	var curr_video = 'all'; 

	$('a.video-popper').click(function(){
		
		swfobject.embedSWF("/flash/video-holder.swf", "video-player-box", "340", "191", "8.0.0", false, false);

		window.open('/media/stand_alone_video/' + curr_video + '/', 'stand_alone', 'width=340, height=750, resizable=no, scrollbars=no, toolbar=no');

		$(this).blur();
		return false;
	});
	
	if ($('#video-player-box').length > 0){

		swfobject.embedSWF("/flash/video-holder.swf", "video-player-box", "340", "191", "8.0.0", false, false, { wmode : 'opaque' });
	}

	$('#video-starter').click(function(){
		
		$('#video-player-tab td.watch:first').trigger('click');
	});

	
	// Audio and Video Scrollers
	$('.player-list-cont').each(function(){
		
		var n_clips_visible		= 7;
		var n_clips				= $(this).find('tr').length;

		if (n_clips > n_clips_visible){
			
			$(this).data('pos', 0);
			$(this).prev().find('img.arrow').click(function(){
				
				var container	= $(this).parent().parent();
				var parent		= container.next();
				var pos			= parent.data('pos');
				
				if ($(this).is('.arrow-down')){
					
					parent.find('tr:visible:first').hide();
					parent.find('tr:visible:last').next().show();
					var new_pos = pos + 1;
					
				} else {
					
					parent.find('tr:visible:first').prev().show();
					parent.find('tr:visible:last').hide();
					var new_pos = pos - 1;
				}

				parent.data('pos', new_pos);

				if (new_pos == 0){

					container.find('.arrow-up').hide();
				
				} else {
					
					container.find('.arrow-up').show();
				}

				if (new_pos == n_clips - n_clips_visible - 1){
					
					container.find('.arrow-down').hide();
				
				} else {
					
					container.find('.arrow-down').show();
				}
			});
		}
	});

	function share_media(id){
		
		var url		= document.location.toString();
		var split	= url.split('#')[0];
		var html	= '<div class="media-sharer">Share this: <input value="' + split + '#play-' + id + '" /></div>';
		
		$('.media-sharer').remove();

		if (id.indexOf('a') > -1){
			
			$('#music-player-tab').before(html);
		
		} else {
			
			$('#video-player-tab').before(html);
		}

		$('.media-sharer input').click(function(){
			
			$(this).select();
		});
	}

	var parent_url		= document.location.toString();

	if (parent_url.match('#play-')){

		var target_click = parent_url.split('#play-')[1];
		$('#' + target_click + ' td:first').trigger('click');
	}
});