/**
 * Copyright 2009 SamynReid 
 */

//hovers
$(document).ready(function(){
	$(".hover").imghover({prefix: '/hover'});
	$(".twitterStatus").twitterLoad();
	$("a.feed").feedLoad();
	
	//preload navigation hovers
	$.preloadImages(
	     "/i/hover/n_approach.gif"
	    ,"/i/hover/n_blogs.gif"
	    ,"/i/hover/n_clients.gif"
	    ,"/i/hover/n_contact.gif"
	    ,"/i/hover/n_home.gif"
	    ,"/i/hover/n_news.gif"
	    ,"/i/hover/n_people.gif"
	    ,"/i/hover/n_services.gif"
	    ,"/i/hover/cn_business_coms.gif"
	    ,"/i/hover/cn_case_studies.gif"
	    ,"/i/hover/cn_consumer.gif"
	    ,"/i/hover/cn_corporate.gif"
	    ,"/i/hover/cn_locations.gif"
	    ,"/i/hover/cn_management.gif"
	    ,"/i/hover/cn_speed.gif"
	    ,"/i/hover/cn_technology.gif"
	    );
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function LocationPoint(name, gLatLng, html)
{
	this.Name = name;
	this.GLatLng = gLatLng;
	this.Html = html;
}

function CreateMarker(map, number) {
  var loc = locations[number];
  var pin = new GIcon();
  pin.image = '/i/google_pin.gif';
  pin.iconSize = new GSize(20, 34);
  pin.shadow = "/i/google_pin_shadow.png";
  pin.shadowSize = new GSize(31, 35);
  pin.iconAnchor = new GPoint(5, 34);
  pin.infoWindowAnchor = new GPoint(5, 2);
  pin.transparent = "/i/google_pin_trans.png";
  pin.imageMap=[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];
  
  var options;
  if (loc.Html) {
  	options = {title: loc.Name, icon: pin};
  } else {
  	options = {title: loc.Name, icon: pin, clickable: false};
  }
  var marker = new GMarker(loc.GLatLng, options);
  marker.value = number;
  if (loc.Html) {
  	GEvent.addListener(marker, "click", function(){
  		jQuery('#addressContent').html(loc.Html);
  	});
  }
  return marker;
}
function GoogleMapInit(mapId, locations, mapCenter, mapZoom)
{
	//initialise map
	jQuery(mapId).jmap(
		'init', {
			 'mapType':'map'
			,'mapCenter': mapCenter
			,'mapZoom': mapZoom
			,'mapEnableType': true
			,'mapEnableOverview': false
			,'mapEnableScaleControl': true
			,'mapShowjMapsIcon': false
			,'mapControl': 'large'
			},
		function (map, element, options){
			for(i in locations) {
				map.addOverlay(CreateMarker(map, i));
			}
		});
}

/* twitter feed */
/* NOTE:
 * Potential issue with twitter: http://groups.google.com/group/twitter-development-talk/browse_thread/thread/beb4451b1dd53d66
 */
(function($){
	$.fn.extend({
		twitterLoad: function(opt){
			return this.each(function() {
        		opt = $.extend({
				             url: '/people/twitter.ashx?screen_name='
							,dateSeperator: '<br />'
				          }, opt || {});
				
				var node = $(this);
				var screenName = node.attr('id');
				//@TODO: error handling
				
				//make call
				$.getJSON(opt.url
					+ screenName 
					+ "&life=61&count=1&callback=?",
					function(data){
						var item = data[0];
						if(item)
						{
						    node.html('<strong>' + screenName + '</strong> ' + item.text + opt.dateSeperator  + '<em>' + humane_date(item.created_at) + '</em>');
						}
						else
						{
						    node.html('<em>Error loading twitter feed</em>');
						}
					}
				);
			});
		}
	});
})(jQuery);

(function($){
	$.fn.extend({
		feedLoad: function(opt){
			return this.each(function() {			    
                var elem = $(this);
                jQuery.getFeed({
                    url: this.href, 
                    success: function(feed) {
                        var html = ''; 
                    	for(var i = 0; i < feed.items.length && i < 5; i++) {
							var item = feed.items[i];
							html += '<p class="post">' + 
							    '<em>' + item.updated.substring(0,16) + ' by</em> ' + 
							    '<a href="#">' + item.creator + '</a><br />' +
							    '<span class="title">' + item.title + '</span>' +
							    item.description + 
							    '&nbsp;<a href="' + item.link + '">Read more</a>' + 
							    '</p><hr />'
						}
                        $(elem).replaceWith(html); 
                    }
                }); 			    			    
			});
		}
	});
})(jQuery);

var elSel;
function ShowPerson(number, largeImg){
    var marginValue = (number*196);
	elSel = null;
	if(arguments.length == 3) {
	    elSel = arguments[2];
	}
	//alert(elSel);
    $('.mugShot .title').fadeOut(400, function() {
			//callback
			$('.mugShot .title').attr('src', largeImg); //swap image src
			$("#peopleContent").animate( { marginLeft:"-" + marginValue + "px"}, 750 );
			//wait til image is loaded
			$('.mugShot .title').load(function() {
			    $('.mugShot .title').fadeIn(400);
			
			    $(".peopleProfile").show();
            		
                if(elSel) {
                    //alert(elSel);
                    //$("#profileContent").show();
		            $('#profileContent').hide(400, function() {
			            $(".profileText").hide();
			            $(elSel).show();
			            $("#profileContent").show(400);
		            });
                }
			});
		});
}