var interval;

$(document).ready(function() {
   if ($('#sidebar').length) {
	   $('#sidebar .collapse ul').hide();
	   $('#sidebar .collapse').not('#sidebar .collapse:last').addClass('boxfix');
	   $('#sidebar .collapse h3').not('#sidebar .collapse h2#exploreby').addClass('dropdownbg');
	   
	   $('#sidebar .collapse h3.dropdownbg').toggle(function() {
			$(this).addClass('activedown');						 
			$(this).parent().find('ul:first').addClass('active');
			$(this).parent().find('ul').slideDown('fast');		
		}, function() {
			$(this).parent().find('ul').slideUp('fast');
			$(this).removeClass('activedown');	
		});
   }
   
   $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
	});

	 $('#searchform input').focus(function(){
		if (this.value == 'Search posts') {
			this.value = ''
		}	
	});
	
	 $('#searchform input').blur(function(){
		if (this.value == '') {
			this.value = 'Search posts'
		}	
	});

    $('a[rel="_blank"],a.-blank').click(function(e) {
		 this.target = "_blank";
	});
	
	if ($("#divGalleria,.galleria").length && (!jQuery.browser.msie || (jQuery.browser.msie && (parseInt(jQuery.browser.version) > 6)))) {	
		showGalleria();		
	}
	
	if (location.hostname == "www.b1.com") {
		$("#posts .share a.twitter").each(function() {
			jQuery(this).getBitlyLink();
		});
	}
	
	if ($(document).height() > $(window).height()) {
		loadArrow();
	}
});

function showGalleria() {
	// Load theme
	Galleria.loadTheme('http://www.b1.com/blog/js/themes/classic/galleria.classic.js');
	// run galleria and add some options
	jQuery('#divGalleria,.galleria').galleria({
		transition: 'slide',
		preload: 3,
		image_crop: false,
		thumb_crop: true,
		thumb_fit: false
	});
}

function getFacebookLink(url,title){
	return "http://www.facebook.com/sharer.php?u="+encodeURIComponent(url)+"&t="+encodeURIComponent(title);
}

function getTwitterLink(url,title){
	return "http://twitter.com/home?status="+encodeURIComponent(title);
}

function getLinkedinLink(url,title){
	return "http://www.linkedin.com/shareArticle?mini=true&url="+encodeURIComponent(url)+"&title="+encodeURIComponent(title)+"&source="+encodeURIComponent("B1 Blog");
}

function loadArrow() {
	var arrow = $("<a href='#'>&#94;</a>");
	var div = $("<div id='scrollarrow'></div>");
	div.append(arrow).prependTo($("#wrapper"));
	arrow.click(function(e) {
		 $.scrollTo(0,{duration:400});
		 e.preventDefault();
	});
	if (!jQuery.browser.msie || (jQuery.browser.msie && (parseInt(jQuery.browser.version) > 6))) {
		div.css({'top' :$(window).height()-105+"px"});
		$(window).resize(function() {
			div.css({'top' :$(window).height()-105+"px"});
		});
	}
}

jQuery.fn.getBitlyLink = function() {
	var _this = $(this);
	var _linkedin = _this.parent().parent().find("a.linkedin");
	var _facebook = _this.parent().parent().find("a.facebook");
	
	var url = decodeURIComponent(_this.attr("href").match(/http%3A%2F%2F(www.)?b1[^\&]+/gi));

	// set up default options
	var defaults = {
		version:    '2.0.1',
		login:      'jamieshields',
		apiKey:     'R_ae743e87cf2a0d645174d246afd23dcf',
		history:    '0',
		longUrl:    url
	};

	// Build the URL to query
	var daurl = "http://api.bit.ly/shorten?"
	+"version="+defaults.version
	+"&longUrl="+defaults.longUrl
	+"&login="+defaults.login
	+"&apiKey="+defaults.apiKey
	+"&history="+defaults.history
	+"&format=json&callback=?";

	// Utilize the bit.ly API
	$.getJSON(daurl, function(data){
		shorturl = encodeURIComponent(data.results[url].shortUrl);
		_this.attr("href",_this.attr("href").replace(/http%3A%2F%2F(www.)?b1[^\&]+/gi,shorturl));	
		_linkedin.attr("href",_linkedin.attr("href").replace(/http%3A%2F%2F(www.)?b1[^\&]+/gi,shorturl));	
		_facebook.attr("href",_facebook.attr("href").replace(/http%3A%2F%2F(www.)?b1[^\&]+/gi,shorturl));	
	});
};

