jQuery(document).ready(function($){
	$('.jshare-this').click(function(){
		var theInfo = $(this).children().attr('title').split(' | ');
		var theTitle = theInfo[0];
		var theUrl = theInfo[1];
		var thisId = $.data(this);
		thisId = 'jshare'+thisId;
		if ( $('#jshare-window').length===1 ) $('#jshare-window').remove(); 
		$(this).attr('id', thisId).addClass('jshare-this-loading').after('<div id="jshare-window"></div>');		
		var inVideoFrame=false;
		inVideoFrame = ( $('#'+thisId).parents('.media-details').length === 1 && $('#'+thisId).parents('.media-details').attr('id').match('video')) ? true: false;
		$.get('index.php', { jshare_this: 'do', title: theTitle, url: theUrl }, function(data){
			if(data){				
				$('#jshare-window').append(data);
				$('#'+thisId).removeClass('jshare-this-loading');
				var jsWinHeight = $('#jshare-window').outerHeight(true);
				var jsWinWidth = $('#jshare-window').outerWidth(true);
				$('#jshare-window').css('bottom', -jsWinHeight);
				if ( inVideoFrame ){
					$('#jshare-window').append('<iframe id="jshare-iframe" frameborder="0" />');
					$('#jshare-iframe').css({
						height: jsWinHeight,
						width: jsWinWidth
					});
				}
				$('#jshare-close').click(function(){
					$('#jshare-window').remove();
				});
				$('.jshare-panel-select', '#jshare-window').click(function(){
					if ( $(this).hasClass('active') ) {
						return false;
					} else {
						$('#jshare-window .active').removeClass('active').addClass('inactive');					
						$(this).addClass('active');
						var activatePanel = $(this).attr('href').split('#')[1];
						$('#'+activatePanel).removeClass('inactive').addClass('active');
						var jsWinWidth = $('#jshare-window').outerWidth(true);
						var jsWinHeight = $('#jshare-window').outerHeight(true);
						$('#jshare-window').css('bottom', -jsWinHeight);
						if ( inVideoFrame ){
							if ( $.browser.msie ){
								$('#jshare-iframe', '#jshare-window').css({
								height: '270px',
								width: jsWinWidth
							});
							} else {
								$('#jshare-iframe', '#jshare-window').css({
									height: jsWinHeight,
									width: jsWinWidth
								});
							}
						}
					}										
					return false;
				});
				$('#jshare-send-form').submit(function(){
					$(this).addClass('jshare-this-loading');
					var sendAction = $('#jshare-send-form').attr('action');
					var serializeInput = $(this).serialize();
					$('.jshare-message').fadeOut('normal', function(){ $(this).remove(); });
					$.post(sendAction, serializeInput, function(response){
						$('#jshare-send').append(response);
						$('#jshare-send-form').removeClass('jshare-this-loading');
						var jsWinHeight = $('#jshare-window').outerHeight(true);
						$('#jshare-window').css('bottom', -jsWinHeight);
						if ( inVideoFrame ){
							$('#jshare-iframe').css('height', jsWinHeight);
						}
					})
					return false;
				});
			}
		});		
		return false;
	});	
});