$(document).ready(function() {

var tumblr ="http://hurricanebells.tumblr.com";
var twtruser ="hurricanebells";
var bandcampid ="1428584667";
var fbphotos = "10150315786382970";
var fbheaders = "137183452969";

$.getJSON(tumblr + "/api/read/json?&callback=?", function(data) {
 $('.contactbox div').html(data.tumblelog.description);
});




$.getJSON(tumblr + "/api/read/json?type=link&tagged=menu&callback=?", function(data) {
$('#masthead').html('<h3>' + data.tumblelog.title + '</h3> ');
$.each(data.posts, function() {
$('<span></span>')
.append('<a href="' + this["link-url"] + '" target="_blank">' + this["link-text"] + '</a>')
.appendTo('#masthead'); 
});
});




//BANDCAMP

$.getJSON("http://api.bandcamp.com/api/band/3/discography?key=blomgadrbradlatrlikendrforn&band_id=" + bandcampid + "&callback=?", function(data) {
(data.discography).sort(function(a,b) { return parseFloat(b.release_date) - parseFloat(a.release_date) } );
$.each(data.discography, function() {
(data.discography).sort(function(a,b) { return parseFloat(b.release_date) - parseFloat(a.release_date) } );
var albumart = (this.large_art_url);
var title = (this.title);
var preorder = (this.downloadable);
 if(preorder != null) {
 var preorder = 'buy';
 } else {
 var preorder = 'pre-order';
}

var albumlink = (this.url).match('[a-z\-:\/\/.0-9A-Z]*');
var artist = (this.artist).match('^Hurricane Bells');

 
$('<li class="release"></li>')
  .append('<span class="thumb '+artist+'" style="background: url(' + albumart + ')center no-repeat;"><a class="play '+preorder+'" href="javascript:void(0);"  onclick="openWindow(\'http://bandcamp.com/EmbeddedPlayer/v=2/album=' + this.album_id + '/size=grande3/bgcol=eeeeee/linkcol=4285BB/%22%20allowtransparency=%22true%22%20frameborder=%220%20\', \'follow\',400,450);"></a><span class="info"><p class="title">' + title + '</p><p class="share"><a class="purchase" href="' + albumlink + '?action=buy" target="_new">'+preorder+'</a>&nbsp;<a class="tweet" href="javascript:void(0);"  onclick="openWindow(\'http://twitter.com/share?url=' + albumlink + '\', \'follow\',530,340);"></a>&nbsp;<a class="fb" href="javascript:void(0);"  onclick="openWindow(\'http://www.facebook.com/sharer.php?u=' + albumlink + '\', \'follow\',530,340);"></a></p></span></span>')
  .appendTo('ul.discography');
});

 $('.discography').jcarousel({
 	wrap: 'both',
 	scroll: 2
 });

});//END BANDCAMP



//PHOTO GALLERY

$.getJSON('https://graph.facebook.com/' + fbphotos + '/photos&callback=?',function(json){
    $.each(json.data,function(){
	$('<li></li>')	
    .append('<span class="thumb" style="background: url(' + this.images[1].source + ')center no-repeat;"><a href="' + this.images[0].source + '"></a></span>')
  .appendTo('#photos');
  });


 $('.photogallery').jcarousel({
 	wrap: 'both',
 	scroll: 2
 });


 $("#photos .thumb a").fancybox({
	'speedIn'		:	100, 
	'speedOut'		:	100,
	'overlay'		: 	false
 });

});//END PHOTOS



//VIDEOS

$.getJSON(tumblr + "/api/read/json?type=video&num=10&tagged=gallery&callback=?", function(data) {
	$.each(data.posts, function() {
	var vidurl = (this["video-source"]);
	var vidcaption = (this["video-caption"]);
	var vidid = (this["video-source"].match(new RegExp('v=([a-zA-Z0-9_]*)')));
 	$('<li></li>')
	.append('<span class="thumb" style="background: url(http://img.youtube.com/vi/' + vidid[1] + '/0.jpg) center no-repeat;"><a class="play" href="' + vidurl + '&autoplay=1"></a><span class="info"><p class="title">' + vidcaption + '</p><p class="share"><a class="tweet" href="javascript:void(0);"  onclick="openWindow(\'http://twitter.com/share?url=' + vidurl + '\', \'follow\',530,340);"></a>&nbsp;<a class="fb" href="javascript:void(0);"  onclick="openWindow(\'http://www.facebook.com/sharer.php?u=' + vidurl + '\', \'follow\',530,340);"></a></p></span></span>')
	.appendTo('#videos');
});


 $('.videogallery').jcarousel({
 	wrap: 'both',
 	scroll: 2
 });

$('#videos .thumb a.play').click(function() {
	$.fancybox({
 	'padding'             : 0,
	'autoScale'   		: false,
	'transitionIn'        : 'none',
	'transitionOut'       : 'none',
	'title'               : this.title,
	'width'               : 640,
	'height'              : 385,
	'href'				: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
	'type'                : 'swf', 
	'swf'                 : {'allowfullscreen':'true'}
	});
	return false;
	}); 


});//end VIDEOS


//CONTACT

$('.contactbox div').hide();

$('a.contact').toggle(function() {
 $('.contactbox div').fadeIn(100,'linear')
 $('nav a.contact').css({'background-position':'54px 10px', 'background-color':'rgba(255,255,255,.2)'});
}, function() {
 $('.contactbox div').fadeOut(100,'linear')
 $('nav a.contact').css({'background-position':'54px -34px', 'background-color':'transparent'});
});



/*--HEADER

$.getJSON(tumblr + "/api/read/json?type=photo&num=4&tagged=header&callback=?", function(data) {
 $.each(data.posts, function() {
 var photolink = (this["photo-link-url"]);
 if(typeof photolink != 'undefined') {
 var photolink = photolink;
 } else {
 var photolink = '#';
 }
 $('<a href="' + photolink + '"></a>')
  .append('<img src="' + this["photo-url-500"] + '" width="500" title="' + this["photo-caption"] + '">')	
  .appendTo('#featured');
});
});

$(window).load(function() {
  $('#featured').nivoSlider({
	effect: 'fade',
	animSpeed: 500, // Slide transition speed
    pauseTime: 4000,
	captionOpacity: 1
  });
});--*/



//HEADER

$.getJSON('https://graph.facebook.com/' + fbheaders + '/photos&limit=4&callback=?',function(json){
    $.each(json.data,function(){
 $('<a href="' + this.link + '" target="_blank"></a>')
  .append('<li style="background: url(' + this.images[0].source + ');"></li>')	
  .appendTo('#featured');
});
});

$(window).load(function() {
  $('#featured').nivoSlider({
	effect: 'fade',
	animSpeed: 00, // Slide transition speed
    pauseTime: 4000,
	width: 500,
	captionOpacity: 1
  });
});


//BLOG and NEWS

$.getJSON(tumblr + "/api/read/json?type=text&num=3&tagged=web&callback=?", function(data) {
 $.each(data.posts, function() {
 $('<li></li>')
  .append('<a href="' + this.url + '"><h3>' + this["regular-title"] + '</h3></a><p class="post">' + this["regular-body"] + '<span class="more">...<a href="' + this.url + '">read more</a></span></p><span class="share"><a href="javascript:void(0);"  onclick="openWindow(\'http://twitter.com/share?url=' + this.url + '&amp;via=' + twtruser + '&amp;text=' + this["regular-title"] + ' \', \'follow\',530,340);">tweet</a>&nbsp;<a href="javascript:void(0);"  onclick="openWindow(\'http://facebook.com/sharer.php?u=' + this.url + '\', \'follow\',530,340);">fb</a></span>')
  .appendTo('.news ul');
});

var maxNum = function($limit, num) {
    var $this;
    $limit.each(function() {
        $this = $(this); 
        $this.text( $this.text().split(/\s+/).slice(0,num).join(" ") );
    });
}

$(function() {
    maxNum($('.news p'),25);
});

});//END NEWS






//E4M

$('input.clear').each(function() {
	$(this)
		.data('default', $(this).val())
		.addClass('inactive')
		.focus(function() {
			$(this).removeClass('inactive');
			if ($(this).val() == $(this).data('default') || '') {
				$(this).val('');
			}
		})
		.blur(function() {
			var default_val = $(this).data('default');
			if ($(this).val() == '') {
				$(this).addClass('inactive');
				$(this).val($(this).data('default'));
			}
		});
	});



$('#signup').submit(function() { 
        $(this).ajaxSubmit();
        $('#email').val('thanks!');
		return false; 
	});





});//end
