$(document).ready(function() {
	$("#testimonials, #client_testimonials_landing").cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 8000
	}).hover(function() {
	  $(this).cycle('pause');
	}, function() {
	  $(this).cycle('resume');
	});
	
	items = $("#clients_in_news ul li.announcement");
	var perpage = 4;
	var first = true;
	var last = false;
	var cm;
	
	items.show();
	total = items.size();
	if (items.size() > perpage) {
		items.filter(":gt("+(perpage-1)+")").hide();
		cm = perpage;
	}
	
	$("#clients_in_news .next").click(function() {
		if (!last) {
			nm = cm + 4;
			items.hide();
			
			items.slice(cm, nm).show();
			cm = nm;
			
			if (cm >= total)	{
				last = true;
			}
			first = false;
		}
		return false;
	});
	$("#clients_in_news .prev").click(function() {
		if (!first) {
			var nm = cm - perpage;
			items.hide();
			items.slice((nm - perpage), nm).show();
			cm = nm;
			
			if (cm == perpage) {
				first = true;
			}
			last = false;
		}
		return false;
	});
	
	setUpModal();
});

function setUpModal() {
	$("a[rel=document]").click(function() {
	  $._div({"id": "download_dialog"})
	    ._h1_()
	      .html($(this).clone())
	    ._h2_()
	      .text("Articles:")
	    ._ul_({"id": "dialog_articles_list"})
	    ._h2_()
	      .text("Download Press Materials:")
	    ._ul_({"id": "dialog_press_materials_list"})
	  .div_().prependTo('body');
	  $(this).siblings('.announcement_summary').clone().insertAfter('#download_dialog h1');
	  $(this).siblings('ul').children('li.article').clone().appendTo("#dialog_articles_list");
	  $(this).siblings('ul').children('li.press_material').clone().appendTo("#dialog_press_materials_list");
	  setUpDialog();

		$("#close_dialog, #transparent_overlay").click(function() {
			$("#download_dialog").remove();
			$("#transparent_overlay").hide();
		});
		$("#download_dialog").show();
		return false;
	});
}

function setUpDialog() {
	dialog_left = Math.round(($(window).width() - 500)/2)
	position = "left:"+dialog_left+"px; top: 200px;"
	$("#download_dialog").attr({style: position})
	
	$("body").append('<div id="transparent_overlay"></div>');
	$("#transparent_overlay").attr({style: "height: 100%; width: 100%; position: fixed;"}).show();
}
