$(document).ready(function() {

// --------- GENERAL

	// add popup to external links (using class instead of rel because CSS formatting also applied)
	$(".external a, a.external").click(function () {
		window.open($(this).attr('href'));
		return false;
	});
	
	// border color animation on release and video thumbnails
	$('.entry a, .latest_release img, .buy .entry a img').hover(
      function () {
        $(this).css({borderColor: '#c7cdd1'}).animate({borderColor: '#b3382d'}, 150);
      }, 
      function () {
        $(this).animate({borderColor: '#c7cdd1'}, 500);
      }
    );

// --------- NEWS "LATEST NEWS" ANIMATION

	// set defaults
	$('.news_content').hide();
	$('.news_item_bg div:first').show();
	$('#latest_news a:first').addClass('selected');
	$('.news_item_bg div a').addClass('links_to_fade');
	
	// set news links behavior
	$('#latest_news a').click(function () {
		// if animated object doesn't exists (aka. if nothin movin) and if link not already selected
		if (!$('.news_content:animated').length  && $(this).attr('class') != 'selected') {
			$('#latest_news a').removeClass('selected');
			$(this).addClass('selected');
			var target = $(this).attr('href');
			// slide current content up + fade out + slide target content down + fade in
			$(".links_to_fade").animate({color: '#fff'}, 300);
			// slide current content up + fade out + slide target content down + fade in
			$(".news_content").animate({color: '#fff'}, 300).slideUp(400, function() {
				$(target).slideDown({
					duration: 500,
					easing: 'easeInSine',
					complete: function() {
						$('.news_content').animate({color: '#333'}, 300);
						$(".links_to_fade").animate({color: '#b3382d'}, 300);
					}
				});
			});
		}
		return false;
	});

// --------- RELEASES MUSIC PLAYER

	$('#playlist a, #mp3_player p a').click(function() {
		if (!$(this).hasClass('playing')) { // disable functionality on currently playing track
			var mp3_link = $(this).attr('href');
			// get the class from the playlist li to get corresponding image
			var li01_class = $('#playlist li:first').attr('class');
			var li_class = $(this).parent().attr('class');
			// flash player stuff
			var player_url = '/_img/mediaplayer.swf';
			var flash_vars = 'frontcolor=0xb3382d&lightcolor=0x63686b&showdigits=false&autostart=true&volume=100&&width=210&height=20';
			var flash_obj = '<object id="ie6_crash_detector" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="210" height="20" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"><param name="movie" value="' + player_url +'" /><param name="wmode" value="transparent" /><param name="flashvars" value="' + flash_vars + '&file=' + mp3_link + '" /><embed width="210" height="20" wmode="transparent" flashvars="' + flash_vars + '&file=' + mp3_link + '" src="' + player_url +'" type="application/x-shockwave-flash" /></object>';
		
		
			// we have to make sure there's already a mp3 object on the page to use html()
			// or else IE6 crashes... don't ask me why... Ask Microsoft...
			if ($('#ie6_crash_detector').length) { 
				$('#mp3_player').html(flash_obj);
			} else {
				$('#mp3_player p').hide();
				$('#mp3_player p').after(flash_obj);
			}


			$('#playlist li, #playlist a').removeClass('playing'); // old link highlight
			$('#player img:visible').hide(); // old image
		
			if ($(this).parent().get(0).tagName == 'LI') { // this test if the link is in an <li> track list or <p> "preview tracks"
				if (!$(this).parent().is('playing')) {
					$(this).parent().addClass('playing'); // highlight current track
					$(this).addClass('playing'); // highlight current track

					if ($('#player img.' + li_class).length) { // check to make sure an image for current track exist
						$('#player img.' + li_class).css("display","block"); // because show() uses display: inline on img
					} else {
						$('#player img.album').show(); // or show album cover if no image attached to track in CMS
					}
				}
			} else if ($('#player img.' + li01_class).length) { // if link is P but there's an LI image (CMS has only one track so LI mp3 doesn't show up but li image does if image attached to track)
				$('#playlist li:first, #playlist li:first a').addClass('playing'); // highlight first track
				$('#player img.' + li01_class).css("display","block"); // show first track artwork
			} else { // if there's only one track and no track image
				$('#player img.album').show(); // or show album cover if no image attached to track in CMS
			}
		}
		return false;
	});


// --------- CONTACT FORM VALIDATION
	// add minimum word count method
	jQuery.validator.addMethod("minWords", function(value, element, params) { 
		return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
	}, "Please enter at least {0} words.");
	// custom validation for anti-spam question
	jQuery.validator.addMethod("spam_bot", function(value, element) {
		return this.optional(element) || /^green$/.test(value);
	}, "Wrong color");
	// validate signup form on keyup and submit
	$("#c_form").validate({
		rules: {
			name: { required: true, minlength: 2 },
			form_email: { required: true, email: true },
			message: { required: true, minWords: 5 },
			bot: { required: true, spam_bot: true }
		},
		messages: {
			name: {
				required: "Required Field",
				minlength: "2 letters minimum"
			},
			form_email: "Valid email required",
			message: {
				required: "Message required",
				minWords: "5 words minimum"
			},
			bot: {
				required: "Answer Required"
			}
		}
	});	

// --------- MAILING LIST VALIDATION

	// validate signup form on keyup and submit
	$("#mailinglist_form").validate({
		rules: {
			ml_name: { required: true, minlength: 2 },
			email: { required: true, email: true }
		},
		messages: {
			ml_name: {
				required: ""
			},
			email: ""
		}
	});
});