primary = ['C#', 'ASP.NET', 'Python', 'Cocoa', 'Objective-C', 'SQL'];

secondary = ['HTML4', 'XHTML', 'HTML5', 'Javascript', 'Ajax', 'jQuery', 'Mac OS X', '.NET'];

tertiary = ['Drupal', 'FreeBSD', 'Debian', 'OpenBSD', 'SQL Server', 'Lucene', 'NHibernate', 'SVN',
            'Django', 'MySQL', 'PostgreSQL', 'IIS', 'Git', 'Apache', 'JSON', 'Windows Server',
            'Navision', 'Linux', 'Search Engines', 'CMS', 'Xcode', 'CSS'];
            
quaternary = ['PHP', 'XML', 'REST', 'Red Hat', 'Web Services', 'iPhone', 'MVC', 'Unit Testing',
              'ORM', 'PDF', 'VB.NET', 'Unicode', 'Photoshop', 'DNS', 'MSMQ'];
              
quinary = ['XSLT', 'XPath', 'IoC', 'Bind9', 'Ruby', 'Ruby on Rails', 'Java', 'Japanese'];




do_page_resize = function() {
    // Flickr
    pictures.reset_photos();
    
    // Tag Cloud
    var window_height = $(window).height();
    var header_height = $('#header').outerHeight(true);
    var flickr_height = $('#flickr').outerHeight(true);
    var flickr_control_height = $('#flickr-control').outerHeight(true);
    var footer_height = $('#footer').outerHeight(true);
    var cloud_height = window_height - header_height - flickr_height - flickr_control_height - footer_height - 20;
    var cloud_height = cloud_height < 200 ? 200 : cloud_height;
    $('#cloud').height(cloud_height);
    tag_cloud.resize();
}

do_background_gradient = function() {
    var canvas = document.getElementById('background');
    
    // Stupid Microsoft
    if (typeof(G_vmlCanvasManager) != 'undefined')
        canvas = G_vmlCanvasManager.initElement(canvas);
    
    var context = canvas.getContext('2d');
    var gradient = context.createLinearGradient(0, 0, 0, canvas.height * 0.7);
    gradient.addColorStop(0, '#000');
    gradient.addColorStop(1, '#555');
    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height * 0.7);
}

do_google_analytics = function() {
    var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    var src = host + 'google-analytics.com/ga.js';

    $.ajax({
        type: 'GET',
        url: src,
        success: function() {
            try {
                pageTracker = _gat._getTracker('UA-12527608-1');
                pageTracker._trackPageview();
            }
            catch(err) {}
        },
        error: $.noop(),
        dataType: 'script',
        cache: true
    });
}

$(document).ready(function() {
    // Highlight
    if (document.location.hostname.indexOf('samdoshi') == -1) {
        $('#mfd').addClass('on');
    }
    else {
        $('#sd').addClass('on');
    }
    
    // Flickr
    pictures = new Flickr('#flickr');
    $('#flickr-recent').click(function() {
        $('#flickr-interestingness').removeClass('on');
        $(this).addClass('on');
        pictures.display_recent();
        return false;
    });
    $('#flickr-interestingness').click(function() {
        $('#flickr-recent').removeClass('on');
        $(this).addClass('on');
        pictures.display_interesting();
        return false;
    });
    
    // Tag cloud
    tag_cloud = new TagCloud('#cloud', '#tags');
    tag_cloud.add_tags(primary, 1);
    tag_cloud.add_tags(secondary, 2);
    tag_cloud.add_tags(tertiary, 3);
    tag_cloud.add_tags(quaternary, 4);
    tag_cloud.add_tags(quinary, 5);
    tag_cloud.render();
    
    // Font chooser
    $('#footer a.font-chooser').click(function() {
       $('#cloud').css('font-family', $(this).attr('rel')  + ', serif');
       $('#footer a.font-chooser').removeClass('on');
       $(this).addClass('on');
       do_page_resize();
       return false; 
    });
    
    // Resize page
    do_page_resize();
    $(window).resize(do_page_resize);
    
    // Background gradient
    do_background_gradient();
    
    // Google Analytics
    do_google_analytics();
});

