jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};

/**
 * All pages
 */

$(function(){
	$.expr[':'].internal = function(obj){
		return !obj.href.match(/^mailto\:/)
		&& (obj.hostname == location.hostname);
	};
});

$('a:internal').livequery(function() {
	$(this).addClass('ajaxy');
});

$(function(){
  $('label').livequery(function() {
		$(this).inlineLabel();
	});
});



// Load the About Us page when the dom is ready
$(document).ready(function() {
	var anchor = jQuery.url.attr("anchor");
	if (anchor == null) {
	  $('#about-us-link').click();
	}
});


/**
 * Home
 */

$(function() {
  $("#accordion").livequery(function() {
	  $(this).accordion({ active: "false", collapsible: "true", autoHeight: false});
	});
});

$(function() {
  $('#testimonials').livequery(function() {
		$(this).cycle( { timeout: 0, fx: 'scrollHorz', prev: '#testimonials-prev', next: '#testimonials-next', cleartypeNoBg: true });
	});
});

$(function() {
  $('#banners').livequery(function() {
		$(this).cycle( { timeout: 5000,
	    after: function() {
				var caption = $('#' + $(this).attr('id') + '_description').html();
				var link = $(this).children('a').attr('href');
				var background_target = ($(this).attr('id') + '_background');
				var all_background_ids = new Array();

			  $('#backgrounds').children().each(function() {
				  all_background_ids.push($(this).attr('id'))
				});

				background_index = jQuery.inArray(background_target, all_background_ids); // return the index

				$('#caption').html(caption);
				$('#banner-link').attr('href', link);
				$('#backgrounds').cycle(background_index).cycle('pause');
				return false;
	    }
		});
  });
});

$(function() {
  $("#caption").livequery(function() {
		$(this).hide();
	});

  $('#banner-link').livequery(function() {
		$(this).hover(function(){
	    $("#banners").cycle('pause');
	    $("#caption").stop(false, true).slideDown();
	  }, function() {
	    $("#caption").stop(false, true).slideUp();
	    $("#banners").cycle('resume');
	  });
	});
});

$("#tweets").livequery(function() {
	$(this).tweet({
		username: "aharmon",
		join_text: "auto",
		avatar_size: false,
		count: 3,
		auto_join_text_default: '',
		auto_join_text_ed: '',
		auto_join_text_ing: '',
		auto_join_text_reply: '',
		auto_join_text_url: '',
		loading_text: "loading tweets..."
	});
});


/**
 * Portfolio
 */

// Portfolio page overlay hover
$(function() {
  $('.overlay').livequery(function() {
    var transparency = "#" + $(this).attr('id') + "_transparency";
    var client = "#" + $(this).attr('id') + "_client";
    $(this).hover(function(){
      $(transparency).stop(true, false).fadeTo(250,0);
      $(client).stop(true, false).fadeTo(250,1);
    }, function() {
      $(client).stop(true, false).fadeTo(500,0);
      $(transparency).stop(true, false).fadeTo(500,1);
    });
  });
});

// Portfolio page overlay click -> transition to portfolio details
$(function() {
  $('.overlay').livequery('click', function(){
     var target = "#" + $(this).attr('id') + "_full";
     var slider = "#" + $(this).attr('id') + "_slider";

		var background_target = ($(this).attr('id') + '_background');
		var all_background_ids = new Array();

		$('#backgrounds').children().each(function() {
			all_background_ids.push($(this).attr('id'))
		});

		background_index = jQuery.inArray(background_target, all_background_ids); // return the index

		$('#backgrounds').cycle(background_index);

		$("#thumbs").hide("slide", {direction: "left"}, function() {
			$(target).show("slide", {direction: "left"});
			$(slider).cycle("resume");
		});
  });
});

$(function() {
  $('.back-to-projects').livequery('click',function(){
    $('#backgrounds').cycle(0);
    $('.slider').cycle('pause');
    var target = $(this).closest('div');
    $(target).hide("slide", {direction: "left"}, function() {
      $("#thumbs").show("slide", {direction: "left"});
    });
  });
});

$(function() {
  $('.slider').livequery(function() {
    var slider_current = $(this);
    var slider_prev = $(this).next('a.slider-left');
    var slider_next = $('a.slider-left').next();
    slider_current.cycle({
      timeout: 4000,
      fx: 'scrollHorz',
      pause: true,
      prev: slider_prev,
      next: slider_next
    }).cycle('pause');

    $(this).hover(function(){
      $(slider_prev).stop(true, false).fadeTo("slow",1);
      $(slider_next).stop(true, false).fadeTo("slow",1);
    }, function() {
      $(slider_prev).stop(true, false).fadeTo("slow",0);
      $(slider_next).stop(true, false).fadeTo("slow",0);
    });

    $(slider_next).hover(function(){
      $(slider_next).stop(true, false).fadeTo("slow",1);
      $(slider_prev).stop(true, false).fadeTo("slow",1);
    }, function() {
      $(slider_next).stop(true, false).fadeTo("slow",0);
      $(slider_prev).stop(true, false).fadeTo("slow",0);
    });

    $(slider_prev).hover(function(){
      $(slider_prev).stop(true, false).fadeTo("slow",1);
      $(slider_next).stop(true, false).fadeTo("slow",1);
    }, function() {
      $(slider_prev).stop(true, false).fadeTo("slow",0);
      $(slider_next).stop(true, false).fadeTo("slow",0);
    });
  })
});
