base_url = 'http://'+ window.location.hostname + '/';

this.screenshotPreview = function(){	
	xOffset = 10; // these 2 variable determine popup's distance from the cursor
	yOffset = 30; // you might want to adjust to get the right result

	$("a.screenshot").live('mouseover', function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    });
    
	$("a.screenshot").live('mouseout', function(e){
		this.title = this.t;	
		$("#screenshot").remove();
    });
	$("a.screenshot").live('mousemove', function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

this.tooltip = function(){	
	xOffset = 10;
	yOffset = 20;
	
	$("a.tooltip").live('mouseover', function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    });
	$("a.tooltip").live('mouseout', function(e){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").live('mousemove', function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function add_sifr() {	
	// Sifr
	if(typeof sIFR == "function"){
		$('.sifr').each(function() {
			sIFR.replaceElement(named({
				sSelector: $(this).find('.selector').attr('title'), 
				sFlashSrc: $(this).find('.sFlashSrc').attr('title'), 
				sWmode:"transparent",
				sColor:  $(this).find('.sColor').attr('title'),
				sLinkColor: $(this).find('.sLinkColor').attr('title'),
				sBgColor: $(this).find('.sBgColor').attr('title'),
				sHoverColor: $(this).find('.sHoverColor').attr('title'),
				nPaddingTop:0,
				nPaddingBottom:0,
				sFlashVars:"textalign=left&offsetTop=0"
			}));
		});
	};

}
function resize_img() {
	$('#entries_ajax img').each(function(){
		var max_width = $('#entries_ajax').width() / 10 * 9; 	//Sets the max width, in pixels, for every image

		var width = $(this).width();
		var height = $(this).height();

		if (width > max_width) {
			//Set variables	for manipulation
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
 
			//Shrink the image and add link to full-sized image
			$(this).height(new_height).width(new_width);
			$(this).after('<a href="'+$(this).attr('src')+'" style="display: block; font-style: italic; font-size: 10px;">This image has been resized, click here to view a full size version.</a>');
		} //ends if statement
	});
}

$(document).ready(function(){

	// WIP
	$('#entries_ajax div:first').after('<div class="advertise"></div>');
	$('.advertise').load(base_url + 'utilities/show_ads_in_blog/');
	
 	$(window).load(function() {
		resize_img();
	});

	// Archive
	var open_nav = '';
	
	$('.archive_month a').click(function() {

		var nav = $(this).attr('href');

		if(nav != open_nav) {
			
			$('.archive_ajax').hide();
			$('.archive_ajax').remove();

			$(this).parent().append('<div class="archive_ajax"></div>')
			
			var year = $(this).attr('class');
			var month = $(this).attr('href').substr(1); 
			
			$('.archive_ajax').load(base_url + 'archive_ajax/' + year+ '/' + month);
			open_nav = $(this).attr('href');
			
		} else {
			$('.archive_ajax').hide();
			$('.archive_ajax').remove();
			open_nav = '';
		}
		
		return false;
	});
	// Archive
	
	// Feed select
	$('#banner_dingblog select.feeds').change(function() {
		window.location = 'http://' + $(this + 'option:selected').html() + '.dingblog.com';
	});
	// Feed select
	
	// Pages select
	$('#banner_dingblog select.pages_nav').change(function() {
		window.location = 'http://www.dingblog.com/' + $('#banner_dingblog select.pages_nav option:selected').attr('value');
	});
	// Pages select

	// Blog entry pagination
	var page = 0;
	$('#pagination .left').hide();
	if($('#entries_ajax span:first').attr('class') < 2) {
		$('#pagination .right').hide();	
	}

	$('#pagination a').click(function () {
		
		var cur = $('#entries_ajax span:first').text();

		var direction = $(this).attr('href');

		if(direction == '#next') {
			page++;
			$('#pagination .left').show();
		}
		
		if(direction == '#previous') {
			page--;
			$('#pagination .right').show();
		}
		
			$('html, body').animate({scrollTop: $("body").offset().top}, 'fast', function() {
				$("#entries_ajax").load(base_url + 'page/' + page + '/' + cur + ' #entries_ajax > *', function() {
					add_sifr();
					resize_img();
				});
				$('#categories ul li').removeClass('current');
			});

		if(page > $('#entries_ajax span:first').attr('class')-2) {
			$('#pagination .right').hide();	
		}
		
		if(page < 1) {
			$('#pagination .left').hide();
		}
		
		return false;		
	});
	// Blog entry pagination
	
	// Categories
	$('#categories ul li a').live('click', function () {

		if($('#categories ul li').hasClass('return_index')) {
		
		} else {
			$(this).parent().parent().append('<li class="return_index"><a href="#0">Return to Index</a></li>');
		}

		if($(this).parent().hasClass('current')) {
			
		} else {
			
			page = 0;
			type = $(this).attr('href').substr(1);

			$('html, body').animate({scrollTop: $("body").offset().top}, 'fast', function() {
				$("#entries_ajax").load(base_url + 'page/' + page + '/' + type + ' #entries_ajax > *', function() {

					add_sifr();
					resize_img();
		
					$('#pagination .left').hide();
				
					if($('#entries_ajax span:first').attr('class') > 1) {
					//	alert('show');
						$('#pagination .right').show();
					} else {
					//	alert('hide');
						$('#pagination .right').hide();
					}

				});
			});

		
			$('#categories ul li').removeClass('current');
			$(this).parent().addClass('current');

			if(type == '0') {
				$(this).parent().remove();
			}

		}
		
		return false;
	});
	// Categories
	
	
	// Add avatar on bloglinks
	screenshotPreview();
	tooltip();
	// Add avatar on bloglinks
	
	// Clear autocomplete
	$('form').attr('autocomplete', 'off');
	
	// Quote redirects
	$('a#c_quote').live("click", function () {

		var already_written = $('textarea').val();
		var quote = $(this).attr('href').substr(7);
		
		$('html, body').animate({scrollTop: $(".leave_comment").offset().top}, 'fast', function() {	

			$('textarea').val(already_written + '[quote='+quote+']');
			
		});
			
		return false;
	
	});
	// Quote redirect
	
	// Ajax commenting
	var timer = 'false';
	
	$("form").submit(function() {

		comment_text = $('#c_comment').val().length;
		if(timer == 'false' && comment_text > 2) {
		
			timer = 'true';
			setTimeout(function() {
				timer = 'false';
			},5000);
	
			$.post(base_url + 'add_comment', { c_id: $('#c_id').val(), c_blog: $('#c_blog').val(), c_name: $('#c_name').val(), c_email: $('#c_email').val(), c_website: $('#c_website').val(), c_comment: $('#c_comment').val() }, function (commentid) {

			var commentcount = $('.entry_comment_count').text();
			$('.entry_comment_count').text(parseInt(commentcount) + 1);
			
			var counter = $('.counter:last').text();
			if(!counter) {
				counter = 0;
			}

			$('input#c_name, input#c_website, input#c_email, textarea').val('');
			$('#comment_ajax').append('<div class="new_comment"></div>');

			$('.new_comment:last').load(base_url + 'comment_ajax/' + commentid + '/' + counter + ' #comment_ajax');
			$('html, body').animate({scrollTop: $('html, body')[0].scrollHeight});

	
			});
		
		}

		return false;
	});
	// Ajax commenting

	// Sifr
	add_sifr();
});