var Site = this.Site = {};

(function($){

$.extend(Site,{

	init: function(){

		var works = $('#works');
		var projects = $('#works li');
		var selections = $('.selections a');

		$('#wrap').click(function(){
			if (works.is(":visible")){
				works.slideUp();
				// projects.hide();
				// $('#prev-work').hide();
				// $('#next-work').show();
			}
		 });

		// works.click(function(event){
		// 	event.stopPropagation();
		//  });

		$('#networks a#projects').live('click', function(){
			var el = $(this);
			works.slideToggle();

			if (!projects.find(':visible').length){
				projects.eq(0).fadeIn('slow').addClass('selected');
			}

			return false;
		});

		$('#next-work').live('click', function(){
			var selected = works.find('li:visible');
			var index = projects.index(selected);

			selected.fadeOut(function(){
				if (++index < projects.length){
					projects.eq(index).fadeIn();
					index++;

					if (index == projects.length){
						$('#next-work').fadeOut();
					}
					if (index > 0){
						$('#prev-work').fadeIn();
					}
				}
			});

			return false;
		});

		$('#prev-work').live('click', function(){
			var selected = works.find('li:visible');
			var index = projects.index(selected);

			selected.fadeOut(function(){
				if (--index >= 0){
					projects.eq(index).fadeIn();

					if (index < projects.length){
						$('#next-work').fadeIn();
					}
					if (index == 0){
						$('#prev-work').fadeOut();
					}
				}
			});

			return false;
		});

		selections.live('click', function(){
			var el = $(this);
			var backgroundImage = el.css('background-image');
			var selected = works.find('li:visible');

			selected.find('.preview a').css('background-image', backgroundImage);

			return false;
		});
	}
});

$(Site.init);

})(jQuery);

if (!window.console){
	var names = 'log debug info warn error assert dir dirxml group groupEnd time timeEnd count trace profile profileEnd'.split(' ');
	window.console = {};
	for (var i=names.length; i--; i) window.console[names[i]] = function(){};
}

window.onload = function(){
	if (navigator.userAgent.match(/iPhone/i)){
		var a = document.getElementsByTagName ("a");

		for (var i = 0; i < a.length; i++){
			if (a[i].getAttribute ('href').search (/callto:/i) === 0){
				a[i].setAttribute ('href', a[i].getAttribute ('href').replace (/^callto:/, "tel:"));
			}
		}
	}
};

