var Clusterdev = {
  initialize: function() {
    Clusterdev.UI.windowLayout();

    Clusterdev.Controllers.loadNavigation();
    Clusterdev.Controllers.loadPortfolio();
    Clusterdev.Controllers.loadAbout();
    Clusterdev.Controllers.loadOpensource();

    Clusterdev.Observers.mainCommand();

    Clusterdev.UI.resizeWindow();
  }
};

Clusterdev.UI = {
  windowLayout: function() {
    var dimensions = document.viewport.getDimensions();
    var pageDimensions = $('pageContainer').getDimensions();
    var contentDimensions = $('contentWrapper').getDimensions();
    
    $('hireme').setStyle('top: ' + ((dimensions.height / 2) - 75) + 'px');
    
    $('tooltip').setStyle('top: ' + (pageDimensions.height - 375) + 'px');
    $('tooltip').setStyle('right: ' + ((dimensions.width / 2 ) - 425) + 'px');

    $('previous').setStyle('margin-top: ' + ((contentDimensions.height / 2) - 16) + 'px')
    $('next').setStyle('margin-top: ' + ((contentDimensions.height / 2) - 16) + 'px')    
  },
  resizeWindow: function() {
    window.onresize = function() {
      Clusterdev.Main.windowLayout();
    };
  }
};

Clusterdev.Controllers = {
  loadNavigation: function() {
    var container = 'menu';
    Clusterdev.Utilities.navigation(container);
  },
  loadPortfolio: function() {
    var container = 'portfolio';
    var data = 'portfolio';
    Clusterdev.Utilities.slideshow(container, data);
  },
  loadAbout: function() {
    var url = 'about';
    Clusterdev.Utilities.xhrData(url, 'get', 'about');
  },
  loadOpensource: function() {
    var url = 'opensource';
    Clusterdev.Utilities.xhrData(url, 'get', 'opensource');
  }
}

Clusterdev.Observers = {
  mainCommand: function() {
    $('hireme').observe('click', function(event) {
      var url = 'mailto:tricipta@clusterdev.com?subject=Hire Me';
      Clusterdev.Utilities.mailTo(url);
    });
    $('sinatra').observe('click', function(event) {
      var url = 'http://www.sinatrarb.com';
      Clusterdev.Utilities.redirectLink(url);
    });
    $('mongodb').observe('click', function(event) {
      var url = 'http://www.mongodb.com';
      Clusterdev.Utilities.redirectLink(url);
    });
    $('prototype').observe('click', function(event) {
      var url = 'http://www.prototypejs.org';
      Clusterdev.Utilities.redirectLink(url);
    });
    $('twitter').observe('click', function(event) {
      Clusterdev.Utilities.xhrSocial('twitter', 'get', 'streamContent');
    });
    $('facebook').observe('click', function(event) {
      Clusterdev.Utilities.xhrSocial('facebook', 'get', 'streamContent');
    });
    $('linkedin').observe('click', function(event) {
      Clusterdev.Utilities.xhrSocial('linkedin', 'get', 'streamContent');
    });
    $('lastfm').observe('click', function(event) {
      Clusterdev.Utilities.xhrSocial('lastfm', 'get', 'streamContent');
    });
    $('github').observe('click', function(event) {
      Clusterdev.Utilities.xhrSocial('github', 'get', 'streamContent');
    });    
  }
}

Clusterdev.Utilities = {
  slideshow: function(container, data) {
    var slider = new Ferdinand.Slider(container, {
      url: data, 
      containerClass: container,
      timeout: 7200,
      loader: 'img/loader-black.gif'
    });
    $('previous').observe('click', function(event) {
      slider.Prev();
    });
    $('next').observe('click', function(event) {
      slider.Next();
    })
  },
  navigation: function(container) {
    var navigation = new Control.Tabs(container);
  },
  redirectLink: function(url) {
    window.open(url);
  },
  mailTo: function(url) {
    document.location = url;
  },
  xhrData: function(url, method, element) {
    xhr = new Ajax.Request(url, {
      method: method,
      onCreate: function() {
        
      },
      onComplete: function(response) {
        var strResponse = response.responseText;
        $(element).innerHTML = strResponse;
      }
     })
  },
  xhrSocial: function(url, method, element) {
    $('tooltip').show();
    xhr = new Ajax.Request(url, {
      method: method,
      onCreate: function() {
        $('spinner').show();
        $(element).hide();
      },
      onComplete: function(response) {
        $('spinner').hide();
        $(element).show();
        var strResponse = response.responseText;
        $(element).innerHTML = strResponse;
      }
     })
  }
};

document.observe('dom:loaded', Clusterdev.initialize.bind(Clusterdev.initialize));
