

var h = {
    blog: {
        initialize: function() {
//			$('.blog-archives-show').click(h.blog.showarchives);
	    	$.anchorHandler
		        .add(/\#blog\-archives\-show/, 	 h.blog.showarchives, true)
		        .add(/\#hypem\-widget\-show/, 	 h.blog.showhypem, true)
        },
        showarchives: function() {
            if ($('#blog-archives').css('display') == 'none') {
                $('.blog-archives-show').text('LATEST');
                $('#blog-latest, #hypem-widget').hide('fast');
                $('#blog-archives').fadeIn('slow');
            }
            else {
                $('.blog-archives-show').text('ARCHIVES');
                $('#blog-archives, #hypem-widget').hide('fast');
                $('#blog-latest').fadeIn('slow');
            }
        },
        showhypem: function() {
            $('#blog-latest, #blog-archives').hide('fast');
            if (!$('#hypem-script').get(0)) {
                $('#hypem-widget').after('<script id="hypem-script" type="text/javascript" src="http://hypem.com/widget/v2/loved/h3/10/hype.js?bcol=111111&tcol=cccccc&lcol=226699"></script>');
            }
            $('#hypem-widget').fadeIn('slow');
        }
    },
	comment: {
        size: 0,
		initialize: function() {
			// event bindings
			$('.comment-compose').click(h.comment.compose);
//			$('#comment-compose form').submit(h.comment.preview).hide();
    		$('.comment>h6>a:contains(permalink)').click(h.comment.permalinkClicked);
    		$('a[href*=#comment-]').click(h.comment.focus);
			$('#id_person_name').focus(function(){$(this).val('').unbind('focus') });
			$('#comment-grow').click(h.comment.grow);
    		
			$.anchorHandler
				.add(/\#ch\-cheatsheet/, 	 h.comment.showCheatsheet)
				.add(/\#comment\-compose/,   h.comment.showCompose)
				.add(/\#comment\-\d+/,       h.comment.focus);
		},
		grow: function(){
		    if ($('#id_comment').height() > 800) { 
                $('#comment-grow').text('Having fun ? :D'); return false;
            }
            var height = $('#id_comment').height() + 50;
    		$('#id_comment').animate({height: height+'px'});
            h.comment.size = height; 
        },
		focus: function() {
			var node = document.location.hash.match(/\#comment\-(\d+)/);
			if (node) $(node[0]).addClass('selected');
		},
		
		showCheatsheet: function(){ 
			$('#comment-compose form, #ch-cheatsheet').show();
			//window.scrollTo(0, window.document.height);
		},
		
		showCompose: function(){ 
			$('#comment-compose form').show().find('#id_comment').focus();
		},
		
		compose: function(){
			$('#comment-compose form').show().find('#id_comment').focus();
		},
		
		preview: function(e) {
            e.preventDefault();
			var container = $(this).parent();
			$.ajax({
				type: 	'POST',
				url: 	$(this).attr('action'),
				cache:  false,
				data: 	$(this).serialize() + ((e.originalEvent.explicitOriginalTarget.name=='preview')? '&preview=true': '&post=true'), // ewh ..
				success: function(html){
					$('#id_comment').focus();
    		        setTimeout(function(){
                        $('#comment-grow').click(h.comment.grow)
                        if (h.comment.size != 0) {
		    			    $('#id_comment').height(h.comment.size);
                        }
                    }, 200);
					container.html(html).children('form').submit(h.comment.preview); }});
				
			return false; // prevent form submit
		},
		
		permalinkClicked: function() {
    		$('.comment>h6>a:contains(permalink)').parents('.comment').removeClass('selected');
    		$(this).parents('.comment').addClass('selected');			
		}
	},
	portfolio: {
		initialize: function() {
			if (window.location.href.match(/\/\#tb\:/)) {
				var tb = window.location.href.match(/\/\#tb\:(.*)/)[1].split(',')
				setTimeout(function(){tb_show(unescape(tb[1]), tb[2], unescape(tb[0]));}, 200);
			}
		}
	}
}
$(function(){
    $('a[rel^=lightbox-]').slimbox()
 	h.comment.initialize();
	h.blog.initialize();
	h.portfolio.initialize();
    $('#technologies a').fadeTo('fast', 0.5)
        .hover(function(){ $(this).fadeTo('fast', 1.0); }, function(){ $(this).fadeTo('slow', 0.5); });
    $('#bd a')
	    .filter('[href^=http]:not(:has(img))')
            .each(function(){
                if ($(this).parent().get(0).nodeName != "TD") {
                    $(this).addClass('extlink');
                }
            })
            
            .filter('[href~=youtube.com/watch?v=]')
                .removeClass('extlink') // erh
                .append($('<span>&nbsp;<img src="{{ "img/youtube-w.png"|media }}" alt="Youtube movie" border="0" /></span>'));
});

function tb_callback(caption, url, imageGroup) {
  if (typeof(pt) != 'undefined') {
    var virtualURL = [
    	window.location.href.replace(window.location.href.match(/#.*$/),''),
    	'#tb:', escape(imageGroup), ',', escape(caption), ',', url].join();
    
    pt._setAllowAnchor(true);
    pt._trackPageview(virtualURL);
  }
}

