var active = document.activeElement;

var placeholder = $('<div class="placeholder">SEARCH</div>').css({
       	position: 'absolute',
       	top: 5,
       	left: 8,
       	fontSize: 12,
        cursor: 'text'
       });
var search = $('.search .input');

search.val('').after(placeholder)

placeholder.click( function(){
    search.focus();
});

$('.search .input').focus(function () {
        placeholder.hide();

}).blur(function () {
    placeholder.show();
    
    Cufon.refresh('.placeholder');
});

search.blur();
$(active).focus();

Raphael.fn.container = function (width, height, amountOfWaves, top, left) {
    var fillHeight, curve = 62;
    var fillHeight = height - curve; ;

    if (fillHeight < 0) {
        fillHeight = 0;
    }

    if (height < 20) {
        amountOfWaves = 0;
    }

    var wave = "";
    if (amountOfWaves > 0) {
        var waveLength = width / amountOfWaves;

        for (var i = 0; i < amountOfWaves; i++) {
            wave += "c" + [5, 20, 20, 0, waveLength, 0];
        }
    }
    else {
        wave = "l" + width + ",0";
    }

    var path = "M" + left + " " + top +
	wave +
	"l0," + fillHeight +
	"c0,0 0," + curve + " -" + curve + "," + curve +
	"l-" + (width - (curve * 2)) + ",0" +
	"c0,0 -" + curve + ",0 -" + curve + ",-" + curve +
	"Z";
    return this.path(path);
}


function createWater(options)
{
	if( $('#dewater').length > 0 )
	{
		var settings = { 
			width: $('#dewater').width(),
			height: $('#dewater').height(),
			filled: $('.amountDewatered').text(), // Percentage filled,
			amountOfWaves: 7,
			containerId: 'dewater',
			fgColour: "#45b6d1"
		}; 
	
		var settings = $.extend({}, settings, options);
		var paper = Raphael(settings.containerId, settings.width, settings.height);
		var filledHeight = settings.height * (settings.filled/100);
		paper.container(127, settings.height, 0, 0, 17).attr({ stroke: "#dbdbdb", fill: '#f1f1f1' });
		paper.container(127, filledHeight, settings.amountOfWaves, settings.height - filledHeight, 17).attr({
			stroke: "none", 
			fill: settings.fgColour
        });

        paper.path("m16,196c0,0 0,63 70,62l-70,0l0,-62z").attr({stroke:"none", fill:"#ffffff"});

        paper.path("m80,192c0,0 0,63 70,62l-70,0l0,-62z").attr({
            stroke: "none",
            fill: "#ffffff"
        }).rotate(-90);

	}
}

function createMilestones() {
    if ($('.milestones .graph').length > 0) {
        var startDate = new Date(2011, 2, 1);
        var endDate = new Date(2012, 1, 28);
        var currentDate = new Date();
        if (currentDate > endDate) {
            currentDate = startDate;
        }

        var max = endDate.getTime() - startDate.getTime();
        var current = currentDate.getTime() - startDate.getTime();
        var progressWidth = $("#home .milestones .graph .bar").width() * (current / max);
        animateBar(progressWidth);
    }

    var overlay = $("#home .milestones .graph .overlay");
    $("#home .milestones .graph .milestone").click(function () {
        $(".info", overlay).empty().append($(".info", this).html());
        overlay.stop(true).fadeIn(function () {
            
        });

		Cufon.refresh('.title');
		
        $(".close", overlay).unbind().click(function () {
            overlay.stop(true).fadeOut();
        });
    });
}

var progress = $("#home .milestones .graph .bar .progress");

function animateBar(maxWidth) {
    setTimeout(function () {
        var width = progress.width();
        width++;
        if (width <= maxWidth) {
            progress.css({
                width: width
            });

            animateBar(maxWidth);
        }
    }, 50);

}

function setupTabs()
{
    $(window).hashchange(function () {
        var hashSplit = getHashSplit();
        var hash = hashSplit[0];

        // Call back for Tabs
        var obj = $(".project nav a[name$=" + hash + "]");
        if (obj.length == 0) {
            obj = $(".project nav a:first-child");
        }

        if (obj.length > 0 && !obj.parent().hasClass('selected')) {
            $(".project nav a").removeClass("selected");
            obj.addClass("selected");
            obj.append("<div class='loader'></div>");
            $('.hiddenUpdateField input').val(obj.html());
            __doPostBack($(".hiddenUpdateButton").attr("id").replace(/_/g, "$"), '');
        }
    });

    $(window).hashchange();

    $(".project nav a").click(function () {
        var obj = $(this);
        _gaq.push(['_trackPageview', obj.attr("href")]);
        window.location.hash = obj.parent().attr("name");
        return false;
    });

    function rtrim(str) {
        return str.replace(/\/+$/, '');
    }
}


$(document).ready(function () {

    createWater();
    createMilestones();

    if ($(".infoboxes .board").length > 0) {
        $(".infoboxes .board").scrollable({ circular: true
        });
	  }

	var gallery = $('.gallery');

	if (!gallery.hasClass('videogal')) {

		$(".gallery a").fancybox({
			'titlePosition': 'outside',
			'overlayColor': '#E1E1E1',
			'overlayOpacity': 0.7,
			'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	}

    $(".countdown:not(.disabled)").tickerClock();
	
	$(".flipbook").fancybox({ 
		'width'				: '75%',
		'height'			: '75%',
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
});
/*

setInterval(function () {
    $(".explorationCounter:not(.disabled)").tickerClock('increment');

}, 140000); // ~ 2.5 meters / hour

setInterval(function () {
    $(".drillingCounter:not(.disabled)").tickerClock('increment');

}, 115000); // ~ 3 meters / hour*/


(function( $ ){

  var methods = {
        init : function( options ) {
            return this.each(function () {
              
                var elementString = '';
                var string = $(this).text();
                
                for( var i = 0; i < string.length; i++ )
                {
                    var char = string.substr(i,1);
                    if( !isNaN(parseFloat(char)) )
                    {
                        elementString += '<div><span>' + char + '</span></div>';
                    }
                }

                $(this).html(elementString);

                $(this).children().last().addClass('last');
                Cufon.refresh(".drilling .countdown");

            });
        },
        increment : function() {
             return this.each(function () {

                incrementArray($.makeArray($(this).children()));

            });
        }
  };

  function incrementArray(spanList)
  {
        var element = spanList.pop();
        var number = parseFloat($(element).children().first().text());
        number++;
        if( number > 9 )
        {
            number = 0;
            incrementArray(spanList);
        }


        var currentScroll = $(element).scrollTop();
        $(element).prepend('<span>' + number + '</span>');
        Cufon.refresh(".drilling .countdown");

        $(element).scrollTop(currentScroll + $(element).children().first().outerHeight());

        $(element).animate({
            scrollTop: 0
        }, 900, function()
        {
            $(element).children(":not(:first-child)").remove();
        });

  };


  $.fn.tickerClock = function( method ) {
    
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tickerClock' );
    }    
  
  };

})( jQuery );


		

 
            
