function ts_ajaxError (request, status, error) {
	alert('Error: ' + error);
	alert('There was an error processing your request. Please refresh the page and try again.');
}

function ts_ajaxSuccessError (response) {
	if (response.error) {
		alert('There was an error processing your request: '+response.error);
		return true;
	}
	return false;
}

function ts_tooltip (tooltip, options, content) {
	
	if (!tooltip) {
		var tooltip = $('<div></div>')
			.addClass('tooltip')
			.append('<div class="tooltip-tl"><div class="tooltip-tr"><div class="tooltip-tick"></div></div></div>')
			.append('<div class="tooltip-ml"><div class="tooltip-mr"><div class="tooltip-frame"></div></div></div>')
			.append('<div class="tooltip-bl"><div class="tooltip-br"></div></div>');
		
	}
	
	var frame = tooltip.find('.tooltip-frame');
	var close_btn = tooltip.find('.tooltip-ml > .tooltip-mr > .close_tooltip');
	
	if (!close_btn.length) {
		close_btn = $('<img src="/images/tooltip_close.gif" class="close_tooltip" alt="Close" title="Close" />')
		.click(function() {
			tooltip.remove();
		}).appendTo(tooltip.find('.tooltip-ml > .tooltip-mr'));
	}
	
	// Determine if the tooltip needs to be shifted
	var window_width = $(window).width();
	if (options.left + options.width + 10 > window_width) {
		options.left = options.left - options.width + 25;
		tooltip.find('.tooltip-tick').addClass('right');
	} else {
		if (options.left) {
			options.left -= 25;
		}
		tooltip.find('.tooltip-tick').removeClass('right');
	}
	
	if (options.top) {
		tooltip.css('top',options.top + 'px');
	}
	
	if (options.left) {
		tooltip.css('left',options.left + 'px');
	}
	
	if (options.width) {
		tooltip.css('width',options.width + 10 + 'px');
	}
	
	if (options.height) {
		frame.css('height',options.height + 'px');
	}
	
	frame.html(content);
		
	return tooltip;
}

// Tooltips
$(document).ready(function() {
	$('.tooltip > .tooltip-ml > .tooltip-mr > .close_tooltip').click(function() {
		$(this).parents('.tooltip').remove();
	});
});

jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};

// Live Events
(function($) {
	// Toggle entry favorite
	$('a.favorite_toggle').live('click',function(event) {
		var link_obj = $(this);
		var item_id = link_obj.attr('id');
		
		// Change on the server via AJAX
		$.ajax({
			type: 'POST',
			cache: false,
			url: '/services.php?run=toggle_favorite',
			data: {id: item_id},
			dataType: 'json',
			cache: false,
			error: ts_ajaxError,
			success: function (response) {
				if (ts_ajaxSuccessError(response)) {
					return false;
				}
				// Favorite status: 1 = off, 2 = on
				if (response.favorite_status == 2) {
					link_obj.html('<img src="/images/favorite_10o.png" class="icon" border="0" />Remove Favorite');
				} else if (response.favorite_status == 1) {
					link_obj.html('<img src="/images/favorite_10.png" class="icon" border="0" />Add Favorite');
				}
			}
		});
	});
	
	// Toggle friend
	$('a.friend_toggle').live('click',function(event) {
		var link_obj = $(this);
		var username = link_obj.attr('id');
		
		// Change on the server via AJAX
		$.ajax({
			type: 'POST',
			cache: false,
			url: '/services.php?run=toggle_friend',
			data: {'username': username},
			dataType: 'json',
			cache: false,
			error: ts_ajaxError,
			success: function (response) {
				if (ts_ajaxSuccessError(response)) {
					return false;
				}
				// Favorite status: 1 = off, 2 = on
				if (response.friend_status == 2) {
					link_obj.html('<img src="/images/friend_10o.png" class="icon" border="0" />Remove from Friends');
				} else if (response.friend_status == 1) {
					link_obj.html('<img src="/images/friend_10.png" class="icon" border="0" />Add to Friends');
				}
			}
		});
	});
	
	// Quick-Help
	$('a[rel=quickhelp]').live('click',function() {
		var href = $(this).attr('href');
		if (!href) {
			return false;
		}
		
		switch (href) {
			case '#thotivity':
				window.open('/quickhelp/thotivity', 'ts_quickhelp', 'width=530, height=335, resizable=no');
				break;
			default:
				window.open(href, 'ts_quickhelp', 'width=400, height=420, resizable=no');
		}
		
		return false;
	});
})(jQuery);

// Ready Events
$(document).ready(function() {
	
	// Generic hover
	$('.hoverable').hover(function(event) {
		$(this).addClass('hover');
	},function(event) {
		$(this).removeClass('hover');
	});
	
	// Hover for tab panels
	$('.ts_tabpanel > .tab_bar > ul > li a.tab').hover(function(event) {
		if (!$(event.target).parent('li').hasClass('selected')) {
			$(event.target).parent('li').addClass('hover');
		}
	},function(event) {
		if (!$(event.target).parent('li').hasClass('selected')) {
			$(event.target).parent('li').removeClass('hover');
		}
	});
	
	// Hover for search button
	$('#heading_navbar form.search input#search_btn').hover(function() {
		$(this).attr('src','/images/header_search_btnh.png');
	},function() {
		$(this).attr('src','/images/header_search_btn.png');
	});
	
	// Navigation bar category menus
	$('#heading_navbar ul#root_cats li').hover(function() {
		$(this).addClass('hover');
		$(this).find('.cat_menu').show();
	},function() {
		$(this).removeClass('hover');
		$(this).find('.cat_menu').hide();
	});
	
	// Form Buttons
	$('form.ts_form button')
		.mousedown(function() {
			if (!$(this).attr('disabled')) {
				$(this).addClass('active');
			}
		})
		.mouseup(function() {
			if (!$(this).attr('disabled')) {
				$(this).removeClass('active');
			}
		})
		.mouseout(function() {
			if (!$(this).attr('disabled')) {
				$(this).removeClass('active');
			}
		});
	
	// Form link submit buttons
	$('form .submit_link[name]').click(function() {
		$(this).after('<input type="hidden" name="' + $(this).attr('name') + '" value="' + $(this).html() + '" />');
		
		$(this).parents('form:first')
			.get(0).submit();
		
		return false;
	});
	
	// Disable submit buttons for forms after submit
	$('form').submit(function(e) {
		var form = this;
		// Add a delay such that the button still gets sent with the form for Firefox/Chrome
		setTimeout(function() {
			$(form).find(':submit').attr('disabled',true).addClass('disabled');
		},50);
		
		return true;
	});
	
	// CAPTCHA Form Fields
	$('.captcha_block').each(function() {
		var cur_captcha = this;
		
		$(this).find('.refresh_captcha').click(function() {
			var captcha_image = $(cur_captcha).find('img');
			var image_src = captcha_image.attr('src');
			
			var rand_no = Math.random();
			rand_no = rand_no * 100;
			rand_no = Math.ceil(rand_no);
			
			captcha_image.attr('src',image_src+'&rand=' + rand_no);
			
			return false;
		});
	});
	
});

// Toggle visibility for blocks
$(document).ready(function() {
	$('.toggle_visible').click(function() {
		var t = $(this).attr('href');
		
		var show = "Show", hide = "Hide";
		
		if (!t.length) {
			return;
		}
		
		$(t).toggle();
		
		// Add specific changes here
		switch (t) {
		}
		
		if ($(t).is(':visible')) {
			$(this).html(hide);
			return false;
		}
		
		$(this).html(show);
		
		return false;
	});
});

//Content Slider
$(document).ready(function() {
	var slide_delay = 10000;
	
	function nextSlide (dom) {
		if ($(dom).hasClass('slider_disabled')) {
			return;
		}
		
		// Find and hide the current visible slide
		var cur_slide = $(dom).find('.content:visible');
		var cur_index = $(dom).find('.content').index(cur_slide[0]);
		
		var next_slide = cur_slide.next('.content');
		var next_index = cur_index + 1;
		
		if (!next_slide.length) {
			next_slide = $(dom).find('.content:first');
			next_index = 0;
		}
		
		$(dom).find('.slider_controls .step').removeClass('active_step').attr('src','/images/slider_step.png');
		$(dom).find('.slider_controls .step:eq('+next_index+')').addClass('active_step').attr('src','/images/slider_stepo.png');
		
		cur_slide.fadeOut(500,function() {
			next_slide.fadeIn(500);
		});
		
		
		setTimeout(function() { nextSlide(dom); },slide_delay);
	}
	
	function showSlide (dom, index, button) {
		
		var cur_slide = $(dom).find('.content:visible');
		var cur_index = $(dom).find('.content').index(cur_slide[0]);
		
		var next_slide = $(dom).find('.content:eq('+index+')');
		
		if (!next_slide.length) {
			return;
		}
		
		// Change the step button
		$(dom).find('.slider_controls .step').removeClass('active_step').attr('src','/images/slider_step.png');
		$(button).addClass('active_step').attr('src','/images/slider_stepo.png');
		
		
		cur_slide.fadeOut(500,function() {
			next_slide.fadeIn(500);
		});
		
	}
	
	function pauseSlide (dom, button, hover) {
		$(dom).addClass('slider_disabled');
			
		$(button).attr({
				alt: 'Play',
				title: 'Play'
			});
			
		if (hover) {
			$(button).attr('src','/images/slider_playh.png');
		} else {
			$(button).attr('src','/images/slider_play.png');
		}
	}
	
	function playSlide (dom, button, hover) {
		$(dom).removeClass('slider_disabled');
			
		$(button).attr({
				alt: 'Pause',
				title: 'Pause'
			});
		
		if (hover) {
			$(button).attr('src','/images/slider_pauseh.png');
		} else {
			$(button).attr('src','/images/slider_pause.png');
		}
		
		setTimeout(function() { nextSlide(dom); },slide_delay);
	}
	
	$('.content_slider').each(function() {
		var cur_dom = this;
		setTimeout(function() { nextSlide(cur_dom); },slide_delay);
		
		$(this).find('.slider_controls').hover(function() {
			$(this).stop().animate({opacity: 1}, 500);
		},function() {
			$(this).stop().animate({opacity: 0.5}, 500);
		});
		
		$(this).find('.slider_controls .pause').toggle(function() {
			pauseSlide(cur_dom,this,true);
		},function() {
			playSlide(cur_dom,this,true);
		}).hover(function() {
			if ($(cur_dom).hasClass('slider_disabled')) {
				// Play button
				$(this).attr('src','/images/slider_playh.png');
			} else {
				// Pause button
				$(this).attr('src','/images/slider_pauseh.png');
			}
		},function() {
			if ($(cur_dom).hasClass('slider_disabled')) {
				// Play button
				$(this).attr('src','/images/slider_play.png');
			} else {
				// Pause button
				$(this).attr('src','/images/slider_pause.png');
			}
		});
		
		$(this).find('.slider_controls .step').click(function() {
			// Find the index
			var index = $(cur_dom).find('.slider_controls .step').index(this);
			var active_index = $(cur_dom).find('.slider_controls .step').index($(cur_dom).find('.slider_controls .step.active_step'));
			
			if (index != active_index) {
				showSlide(cur_dom,index,this);
			}
			
			pauseSlide(cur_dom,$(cur_dom).find('.slider_controls .pause').get(0));
		}).hover(function() {
			$(this).attr('src','/images/slider_steph.png');
		},function() {
			if ($(this).hasClass('active_step')) {
				$(this).attr('src','/images/slider_stepo.png');
			} else {
				$(this).attr('src','/images/slider_step.png');
			}
		})

		$(this).find('.slider_controls .step.active_step').attr('src','/images/slider_stepo.png');
		
	});
});

// Manual scroller
$(document).ready(function() {
	$('.manual_scroll').each(function() {
		var window_height = $(this).find('.scroller').height();
		var content = $(this).find('.scroll_content');
		var content_height = content.height();
		var max_scroll = parseInt(content_height / window_height);
		var cur_scroll = 0;
		var cur_top;
		
		var scroll_top = $(this).find('.scroll_controls .top');
		var scroll_up = $(this).find('.scroll_controls .up');
		var scroll_down = $(this).find('.scroll_controls .down');
		var scroll_bottom = $(this).find('.scroll_controls .bottom');
		
		// Set up scroll
		if (cur_scroll < max_scroll) {
			scroll_down.css({opacity: 1});
			scroll_bottom.css({opacity: 1});
		}
		
		$(this).find('.scroll_controls .down').click(function() {
			if (cur_scroll >= max_scroll) {
				return false;
			}
			
			cur_top = content.position()['top'];
			content.animate({top: cur_top - window_height + 'px'},"normal");
			cur_scroll++;
			
			if (cur_scroll >= max_scroll) {
				$(this).css({opacity: 0.5});
				scroll_bottom.css({opacity: 0.5});
			}
			
			scroll_up.css({opacity: 1.0});
			scroll_top.css({opacity: 1.0});
			
			return false;
		});
		
		$(this).find('.scroll_controls .up').click(function() {
			if (cur_scroll <= 0) {
				return false;
			}
			cur_top = content.position()['top'];
			content.animate({top: cur_top + window_height + 'px'},"normal");
			cur_scroll--;
			
			if (cur_scroll <= 0) {
				$(this).css({opacity: 0.5});
				scroll_top.css({opacity: 0.5});
			}
			
			scroll_down.css({opacity: 1.0});
			scroll_bottom.css({opacity: 1.0});
			
			return false;
		});
		
		$(this).find('.scroll_controls .top').click(function() {
			if (cur_scroll <= 0) {
				return false;
			}
			
			content.animate({top: '0px'},"fast");
			cur_scroll = 0;
			
			$(this).css({opacity: 0.5});
			scroll_up.css({opacity: 0.5});
			
			scroll_down.css({opacity: 1.0});
			scroll_bottom.css({opacity: 1.0});
			
			return false;
		});
		
		$(this).find('.scroll_controls .bottom').click(function() {
			if (cur_scroll >= max_scroll) {
				return false;
			}
			
			content.animate({top: window_height * max_scroll * -1 + 'px'},"fast");
			cur_scroll = max_scroll;
			
			$(this).css({opacity: 0.5});
			scroll_down.css({opacity: 0.5});
			
			scroll_up.css({opacity: 1.0});
			scroll_top.css({opacity: 1.0});
			
			return false;
		});
	});
});

// Composer
$(document).ready(function() {
	// Publish or unpublish buttons
	$('#publishNotice .publish_confirm').one('click',function() {
		var f = $('form.entry_composer');
		
		if (!f.length) {
			return false;
		}
		
		f.append('<input type="hidden" name="publish" value="Publish" />');
		
		f[0].submit();
		
		return false;
	});
	
	$('#unpublishNotice .unpublish_confirm').one('click',function() {
		var f = $('form.entry_composer');
		
		if (!f.length) {
			return false;
		}
		
		f.append('<input type="hidden" name="unpublish" value="Unpublish" />');
		
		f[0].submit();
		
		return false;
	});
	
	$('.entry_composer :submit[name=publish_start]').click(function() {
		if (!$('#publishNotice').length) {
			return false;
		}
		
		// Open a Thickbox item for publish notice
		tb_show(null,'#TB_inline?height=210&width=450&inlineId=publishNotice');
		
		return false;
	});
	
	$('.entry_composer #unpublish_link').click(function() {
		if (!$('#unpublishNotice').length) {
			return false;
		}
		
		// Open a Thickbox item for unpublish notice
		tb_show(null,'#TB_inline?height=260&width=480&inlineId=unpublishNotice');
		
		return false;
	});
	
	// Product rating selector
	function ts_composer_hoverStar (star_num) {
		$('.rating_selector .star').each(function(){
			if ($(this).attr('id') <= star_num) {
				$(this).attr('src','/images/star24a.gif');
			} else {
				$(this).attr('src','/images/star24b.gif');
			}
		});
		// Select the description
		$('.rating_selector .rating_description').hide();
		//alert('.rating_selector #'+star_num+'.rating_description');
		$('.rating_selector #star'+star_num+'.rating_description').show();
	}
	
	$('.rating_selector .star').mouseover(function(event) {
		var star_num = $(this).attr('id');
		ts_composer_hoverStar(star_num);
	}).click(function(event) {
		var star_num = $(this).attr('id');
		$('.rating_selector input[name=product_rating]').val(star_num);
	});
	
	$('.rating_selector .rating_stars').mouseout(function(event) {
		// Reset the stars to their original positions
		var cur_rating = $('.rating_selector input[name=product_rating]').fieldValue()[0];
		if (!cur_rating) {
			cur_rating = 0;
		}
		ts_composer_hoverStar(cur_rating);
	}).trigger('mouseout');
	
	// Verdict field
	$('.review_verdict textarea[name=verdict]').keyup(function(event) {
		var cur_text = $(this).fieldValue()[0];
		if (cur_text.length > 255) {
			$(this).val(cur_text.substr(0,255));
			$('.review_verdict .verdict_chars').html('Character limit reached!');
		} else {
			$('.review_verdict .verdict_chars').html((255 - cur_text.length) + ' characters left.');
		}
	}).change(function() {
		$(this).keyup();
	}).keyup();
		
});

// Entry ratings
$(document).ready(function() {
	
	$('.rate_block').each(function() {
		var entry_id = $(this).attr('id');
		var cur_rate_block = $(this);
		
		var up_img;
		var down_img;
		
		function entryVote(event) {
			if ($(this).hasClass('up')) {
				var rating = 1;
			} else if ($(this).hasClass('down')) {
				var rating = -1;
			}
			
			// Quit if neither ID or rating is present
			if (!entry_id || !rating) {
				return false;
			}
			
			$.ajax({
				type: 'POST',
				url: '/services.php?run=rate_entry',
				data: {'id': entry_id, 'rating': rating},
				dataType: 'json',
				success: function (response) {
					if (response.error) {
						alert(response.error);
						return false;
					}
					
					$('.entry_rating').html(response.rate_str);
					
					// Change the images
					//var up_img = cur_rate_block.find('img.entry_rate.up');
					//var down_img = cur_rate_block.find('img.entry_rate.down');
					if (rating == 1) {
						up_img.attr('src','/images/thumbsup_16.gif');
						down_img.attr('src','/images/thumbsdown_16g.gif');
					} else {
						up_img.attr('src','/images/thumbsup_16g.gif');
						down_img.attr('src','/images/thumbsdown_16.gif');
					}
					
				},
				error: ts_ajaxError
			});
			return false;
		}
	
		
		$(this).find('img.entry_rate').each(function() {
			if ($(this).hasClass('up')) {
				up_img = $(this);
			} else if ($(this).hasClass('down')) {
				down_img = $(this);
			}
		}).click(entryVote);
		
	});
	
});

// Abuse Form
$(document).ready(function() {
	var cur_form = null;
	
	var abuse_options = {
		dataType: 'json',
		beforeSubmit: abuseBefore,
		success: function() {
			$("#TB_load").remove();
			var cur_panel = cur_form.parents('.abuse_form_panel');
			
			cur_panel.empty();
			
			cur_panel.append('<h2>Thank You!</h2>');
			cur_panel.append('Your abuse report has been sent and logged!');
			cur_panel.append("<p>Thotswap strives to foster a rich environment where quality content can flourish. We rely on people like you to warn us of any content that is below the standards of the community.</p>");
			cur_panel.append('<p>Thank you for your help!</p>');
			cur_panel.append('&raquo; <a href="#" onclick="return tb_remove()">Close this window</a>');
			
			$('a.report_abuse').html('Abuse Reported').attr('href','javascript:void(0)').unbind('click');
		},
		error: function() {
			cur_form.find(':button, :submit').removeAttr('disabled');
			$("#TB_load").remove();
			
			ts_ajaxError();
		}
	};
		
	$('.abuse_form').ajaxForm(abuse_options);
	
	function abuseBefore (fields, form) {
		cur_form = form;
		cur_form.find(':button, :submit').attr('disabled','true');
			
		$('.abuse_form_panel').append("<div id='TB_load'><img src='"+tb_pathToImage+"' /></div>");
		$("#TB_load").show();
	}
});

// User information box
$(document).ready(function() {
	var active_overlay;
	var cur_user;
	
	var is_active;
	
	function open (link_dom, content, event) {
		//var offset = $(link_dom).offset();
		
		active_overlay = ts_tooltip(active_overlay,{
				width: 350,
				//height: 170,
				top: event.pageY,
				left: event.pageX
			},content);
		
		active_overlay.appendTo('body')
			.unbind('hover')
			.hover(function() {
				is_active = true;
			},function() {
				is_active = false;
				
				setTimeout(close_overlay,1000);
			});
		
		//active_overlay.find('a.friend_toggle').click(ts_toggle_friend);
	}
	
	function open_overlay (username, link_dom, event) {
		
		if (!is_active) {
			return;
		}
		
		if (username == cur_user && active_overlay) {
			// Use the existing information
			open(link_dom,active_overlay.find('.frame').html(),event);
		} else {
			// Get user information
			var content;
			
			$.ajax({
				type: 'POST',
				url: '/services.php?run=user_overlay',
				data: {name: username},
				dataType: 'html',
				success: function (response) {
					if (response == 'Error') {
						return false;
					}
					
					open(link_dom,response,event);
					
				},
				error: ts_ajaxError
			});
			
			cur_user = username;
		}
		
		
	}
	
	function close_overlay() {
		if (is_active) {
			return;
		}
		
		if (active_overlay) {
			active_overlay.remove();
			active_overlay = null;
		}
		
		cur_user = null;
	}
	
	
	
	$('.member_link').hover(function(event) {
		var username = $(this).attr('name');
		if (!username) {
			return;
		}
		var link_dom = this;
		
		is_active = true;
		
		setTimeout(function() {
			open_overlay(username,link_dom,event);
		},1000);
		
	},function() {
		is_active = false;
		
		setTimeout(close_overlay,1000);
	});
	
});

// Thotwave
$(document).ready(function() {
	
	var thotwave_block = $('#thotwave');
	var thotwave_form = thotwave_block.find('form');
	var thotwave_current = thotwave_block.find('.current_thotwave');
	
	function setThotwave() {
		var text = thotwave_form.find('input:text').val();
		
		$.ajax({
			type: 'POST',
			url: '/services.php?run=set_thotwave',
			data: {'text': text},
			dataType: 'json',
			success: function (response) {
				if (response.thotwave_text.length) {
					thotwave_current.find('span').html(response.thotwave_text);
					$('.thotwave#thotwave_for_'+response.username+' .thotwave_text').html(response.thotwave_text);
					$('.thotwave#thotwave_for_'+response.username).fadeIn('slow');
				} else {
					thotwave_current.find('span').html("Your <b>Thotwave</b> is a snapshot of what's on your mind.");
					$('.thotwave#thotwave_for_'+response.username).fadeOut('slow');
				}
				
				thotwave_form.find('input:text').val('');
				thotwave_form.hide();
				thotwave_current.show();
			},
			error: ts_ajaxError
		});
		
		return false;
	}
	
	$('#thotwave_link').click(function() {
		if (thotwave_block.is(':visible')) {
			
			thotwave_form.find('input:text').val('');
			thotwave_form.hide();
			thotwave_current.show();
			
			thotwave_block.fadeOut('fast');
		} else {
			thotwave_block.fadeIn('fast');
		}
		return false;
	});
	
	thotwave_form.find('.cancel').click(function() {
		thotwave_form.find('input:text').val('');
		thotwave_form.hide();
		thotwave_current.show();
		
		return false;
	});
	
	thotwave_block.find('.change').click(function() {
		thotwave_current.hide();
		thotwave_form.show();
		thotwave_form.find('input:text').focus();
		
		return false;
	});
	
	thotwave_block.find('.set').click(setThotwave);
	thotwave_form.submit(setThotwave);
	
	$('#thotwave > .close').click(function() {
		thotwave_form.find('input:text').val('');
		thotwave_form.hide();
		thotwave_current.show();
		thotwave_block.fadeOut('fast');
		
		return false;
	});
});

// RSS Button
$(document).ready(function() {
	
	var container_code = '<div class="bdlg_container">';
	container_code += '<div class="bdlg_button"><span class="button_inside"></span></div>';
	container_code += '<div class="bdlg_middle">';
	container_code += '<div class="bdlg_main">';
	container_code += '<div class="bdlg_cap"></div>';
	container_code += '</div>';
	container_code += '</div>';
	container_code += '<div class="bdlg_bottom"><span></span></div>';
	container_code += '</div>';
	
	function closeDialog (container) {
		var button_block = $(container).parents('.bdlg:first');
		
		var content_block = $(container).find('.bdlg_content');
		
		button_block.append(content_block);
		
		$(container).remove();
	}
	
	$('.bdlg').click(function(e) {
		var button = $(this);
		var menu_content = button.find('.bdlg_content');
		
		if (!menu_content.length) {
			return;
		}
		
		if ($(e.target).is('.bdlg_container') || $(e.target).parents('.bdlg_container').length) {
			return;
		}
		
		$('.bdlg_container:visible').each(function() {
			closeDialog(this);
		});
		
		var button_content = button.contents().not(menu_content[0]);
		var position = button_content.position();
		
		var menu_width = menu_content.width();
		
		var button_width = button_content.width() + 12;
		var button_height = button_content.height() + 12;
		
		var container = $(container_code)
			.width(menu_content.width() + 3)
			.appendTo(button)
			.css({'left': position.left - menu_width + button_width - 6 + 'px',
				'top': position.top - 6 + 'px'});
		
		container.find('.bdlg_main').append(menu_content).width(menu_width);
		
		container.find('.bdlg_button').click(function() {
			closeDialog($(this).parents('.bdlg_container')[0]);
		});
		
		var button_block = container.find('.bdlg_button')
			.css({width: button_width + 3 + 'px',
				height: button_height + 'px'});
		
		var button_clone = button_content.clone()
			.css({'position' : 'relative',
				top: '6px'
			})
			.appendTo(button_block.find('.button_inside'));
		
		container.show();
		
		
		return false;
	});
	
	$(document).click(function(e) {
		
		if ($(e.target).is('.bdlg_main') || $(e.target).parents('.bdlg_main').length) {
			return;
		}
		
		$('.bdlg_container:visible').each(function() {
			closeDialog(this);
		});
		
	});
	
});

// Social Bookmarking Bar
$(document).ready(function() {
	$('.share_bar').each(function() {
		var bar = this;
		var buttons = $(bar).find('.share_buttons');
		var basic_w = buttons.width();
		var max_w = buttons.find('.share_block').length * 24;
		
		buttons.find('.share_in').css('width',max_w + 'px');
		
		$(bar).find('.more-less').click(function() {
			if (buttons.width() == basic_w) {
				// Expand
				$(buttons).find('.more_block').fadeIn(500);
				$(buttons).animate({'width': max_w + 'px'},500,'swing');
				$(this).html('&laquo; Less');
			} else {
				// Reduce
				$(buttons).find('.more_block').fadeOut(500);
				$(buttons).animate({'width': basic_w + 'px'},500,'swing');
				$(this).html('More &raquo;');
			}
			
			return false;
		});
	});
});