/** ajax loading **/
function ajaxLoad(url, target, remove_in_target) {
	var loading = $('.loading');
	target.hide();
	loading.show();
	target.load(url, function(){
		loading.hide();
		target.show();
		if (remove_in_target != null) {
			$(remove_in_target, target).remove();
		}
	});
}

$(function(){
	/** EQUAL HEIGHTS **/
	$("#content .post-left").each(function(){
		var post_left = $(this);
		var post_right = $(this).next(".post-right");
		var height_left = post_left.height();
		var height_right = post_right.height();
		if (height_left > height_right) {
			post_right.height(height_left + "px");
		} else if (height_right > height_left) {
			post_left.height(height_right + "px");
		}
	});

	/** PHOTO SLIDER (http://flowplayer.org/tools/scrollable.html) **/
	$(".photo-slider .scrollable").scrollable({
		size: 1,
		speed: 1000,
		onBeforeSeek: function(ev, idx) {
			$(".photo-slider .scrollable .items li h3").animate({
				bottom: "-120px"
			});
		},
		onSeek: function(ev, idx) {
			$(".photo-slider .scrollable h3").animate({
				bottom: "5px"
			});
		}
	});
	$(".photo-slider .scrollable").circular();
	$(".photo-slider .scrollable").autoscroll(6000);

	/** TEAM SLIDER (http://flowplayer.org/tools/scrollable.html) **/
	$(".team-slider .scrollable").scrollable({
		size: 5,
		speed: 500
	});
	$(".team-slider .scrollable").circular();
	$(".team-slider .scrollable li").click(function(){
		if ($(".title:visible", this).length == 0) {
			// show link
			$("#team-show-list").show();

			// position title and show it
			var titleWidth = $(".title", this).outerWidth();
			var liWidth = $(this).outerWidth();
			var leftPos = Math.round((liWidth - titleWidth) / 2);
			// set z-indexes (important for IE 6 and 7)
			$(".team-slider li").css("z-index", "1");
			$(this).css("z-index", "2");
			$(".title", this).css("left", leftPos + "px");
			$(".team-slider .scrollable .title").hide();
			$(".title", this).show();

			// load member
			$("#team-content").removeClass("content-box");
			ajaxLoad($("a", this).attr("href") + " #page", $('#team-content'), ".content-nav");
		}
		return false;
	});
	$("#team-content li a").click(function(){
		var url = $(this).attr("href");
		$(".team-slider .scrollable li[class!=cloned] a[href=" + url + "]").parent().click();
		return false;
	});

	/** CUSTOMER SLIDER **/
	// vertical centering of logos
	var ha = $(".customer-slider a:eq(0)").height();
	var himg, top;
	$(".customer-slider a").each(function(){
		himg = $("img", this).height();
		top = Math.round((ha - himg) / 2);
		$("img", this).css("margin-top", top+"px");
	});
	// slider
	$(".customer-slider-scrollbar").slider({
    	animate: false,
    	change: handleSlider,
    	slide: handleSlider
  	});
	function handleSlider(e, ui) {
  		var maxScroll = $(".customer-slider-content").attr("scrollWidth") - $(".customer-slider-content").width();
  		$(".customer-slider-content").attr({scrollLeft: ui.value * (maxScroll / 100) });
	}
  	// load customer
	$('.customer-slider-content a').click(function() {
		ajaxLoad($(this).attr("href") + " #page", $('#customer-content'), ".content-nav");
		return false;
	});

	/** SEARCH **/
	$("#searchform-input").focus(function(){
		$(this).select();
	});

	/** EXTERNAL LINKS (open all non-internal links in a new window) **/
	var host = window.location.host.toLowerCase();
	$(".content-box a:not([href^=http://"+host+"])").attr("target", "_blank");

	/** FLICKR (flickrscript calls document.write - override function and paste to flickr content) **/
	document.write = function(evil) {
		$('#flickr-data').html(evil);
		$('#flickr-data a').attr("target","flickrimages"); // open in new window
	}
	$('#flickr-script').html('<script src="http://www.flickr.com/badge_code_v2.gne?count=9&display=random&size=s&layout=x&source=user&user=43739726@N07"></script>');

	/** POLLS **/
	// remove poll in sidebar if there is one in the content area
	if ($('#content .wp-polls').size() > 0) {
		$('#sidebar .widget_polls-widget').remove();
	}
});
