$(function(){	
	// all popups
	$('a.popup').fancybox({
		hideOnContentClick: false,
		padding:15,
		titleShow: false,
		overlayOpacity: 0.6,
		overlayColor: '#000',
		width: 570,

		onStart: function(node, foo, fancy){
			var height = $(node).attr('class').match(/height-([0-9]+)/);

			if (typeof height[1] !== 'undefined'){
				fancy.height = parseInt(height[1]);
			}
		}
	});
	
	// subculture playlist
	$('#music-player area[class]').click(function(){
		launch('cat', parseInt($(this).attr('class').substring(4)));
		return false;
	});

	$('#music-player-open').click(function(){
		launch('track', '');
		return false;
	});

		// Search input text handling on focus
	var $searchq = 'Search…';

	$('div#search input[type="text"]').focus(function(){
		if ( $(this).val() === $searchq) {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() === '') {
			$(this).val($searchq);
		}
	}).val($searchq);

	// Show the hidden card
	$('a.show-hidden').click(function(){
	   $('div.hidden-card').stop(true, true).fadeIn('slow');
	   return false;
	})
	// Remove on mouseleave
	.parent().parent().mouseleave(function(){
	    $('div.hidden-card').stop(true, true).fadeOut('slow');
	});

	var scroller = $('#scroller'),
		total = $('li', scroller).length,
		next  = $('.next', scroller),
		prev  = $('.prev', scroller);

	function getActiveIndex(){
		var i = $('li.active', scroller).prevAll().length;
		return i;
	}

	function enable(index){
		$('li.active', scroller).stop(true,true).fadeOut(300, function(){
			$('li', scroller).removeClass('active').eq(index).addClass('active').fadeIn();
		})
	}

	prev.click(function(){
		var i = getActiveIndex();
		i < 1 ? enable(total-1) : enable(i-1);
		return false;
	});

	next.click(function(){
		var i = getActiveIndex();
		i > (total -2) ? enable(0) : enable(i+1);
		return false;
	});

	// Make divs clickable
    $('.clickable:has(a[href])').live('click', function()
    {
        var location = $('a[href]', this).eq(0).attr('href');

        if (location !== undefined)
        {
        	window.location = location;
                window.status = location;
        }

    }).bind('mouseenter mouseleave', function()
    {
        $(this).toggleClass('rollover');
    });

	$('form.validate').validate({
		submitHandler:function(form){
			form.submit();
		},
		errorElement:'small'
	});

	$('#music_genre').change(function(){
		window.location = baseurl + 'stories?music_genre=' + $('option:selected', this).val();
	});
	
	$('.shoutout').fancybox({
		overlayColor: '#c3c2b6',
		overlayOpacity: '0.6',
		showCloseButton: true,
		padding: -30,
		content: $('#shout-container'),
		onStart: function() {
			$('#shout-container').css({'display':'block'});
		}
	});

	
});
