var DC = {
  browser: {
    IE6: (navigator.appVersion != null && navigator.appVersion.indexOf("MSIE 6") != -1)
  },
	dom_loaded: function() {
		
		// content fade reveal
		if (!DC.browser.IE6) {
  		DC.load_images();
		
  		// comments slide reveal
  		DC.comment_form = jQuery('#comment-form');
  		DC.comment_form.hide();
  		jQuery('#leave-a-reply').click(DC.comments_form_toggle);
		
      DC.parse_target();
    }
	},
	
	parse_target: function() {
	  var target = DC.target();
		if (target == "respond" || target == "leave-a-reply" || target == "comments") {
		  DC.comments_form_toggle();
		}
	},
	
	location: function () { return window.location.toString().match(/(https?:\/\/)([^\/]+)([^#]*)#?([^#]*)/); },
	
	protocol: function () { return this.location()[1]; },
	
	host: function () { return this.location()[2]; },
	
	path: function () { return this.location()[3]; },
	
	target: function() { return this.location()[4]; },
	
	
	// CONTENT FADE REVEAL
	load_images: function() {
		var images = jQuery('#primary img');

		if (images.length == 0) {
			DC.content_reveal();
		} else {
			DC.waiting = images.length;
			images.each(function(image) {
				if (this.complete) DC.image_loaded();
				else this.ready(DC.image_loaded);
			});
		}
		
	},
	
	image_loaded: function() {
		if (!--DC.waiting) {
			DC.content_reveal();
		}
	},
	
	content_reveal: function() {
		DC.fade_block = jQuery('.fade-block');
		DC.fade_block.fadeOut(300, DC.fade_block_remove);
	},
	
	fade_block_remove: function() {
		DC.fade_block.remove();
	},
	
	// COMMENTS SLIDE REVEAL
	// TODO: Scroll the window down when the comments form is 
	comments_form_toggle: function() {
		if (DC.comments_open === undefined) {
			DC.comments_open = true;
			DC.comments_form_reveal();
		}
		else { 
			DC.comments_open = undefined;
			DC.comments_form_hide();
		}
	},
	
	comments_form_reveal: function() {
		DC.comment_form.slideDown(300, DC.comments_scroll_page);
	},
	
	comments_form_hide: function() {
		DC.comment_form.slideUp(300);
	},
	
	comments_scroll_page: function() {
		jQuery('html, body').animate({
			scrollTop: jQuery("#respond").offset().top
		}, 700);
	}
		
};

jQuery(window).load(DC.dom_loaded);