// jQuery menu cruncher
// for nicepond.com
// andi farr (andi at semibad dot com)

function menu_crunch() {

	// start off by hiding menu items
	
	$("ul.section_list").hide();
	
	// here's my action functions
	
	function extend() {
		$("ul.section_list").slideDown("normal", extended);
	}
	
	function collapse() {
		$("ul.section_list").slideUp("slow", collapsed);
	}
	
	// here's my callbacks
	
	function extended() {
		is_out = true;
		if (!is_over) {
			collapse();
		}
		
	}
	
	function collapsed() {
		is_out = false;
	}
	
	// and here's the beef!
	
	var is_out;
	var is_over;
	
	$("div#menu-cont").hover(
      function () {
      	is_over = true;
	    if (!is_out) {
      		extend();
      	}
      }, 
      function () {
	    is_over = false;
	    if (is_out) {
	    	collapse()
	    }
	  }
	);
}

function slider_init() {

	var oldvalue = 0

	// change any attributes
	$('#imagelist').attr('id', 'slideshow');
	$('#not-here').attr('id', 'slider-container');
	
	// position the images
	var images = $('#images li');
	var pos = 0;
	margin = 10;
	var max_height = 0
	for (i=1; i<=images.length; i++) {
		$('#img-'+i).css('left', pos);
		pos += $('#img-'+i).width() + margin;
		if ($('#img-'+i).height() > max_height) {
			max_height = $('#img-'+i).height();
		}
	}
	$('#slideshow').css('height', max_height);
	pos -= margin;
	
	// set the image thing up
	ss_width = pos;
	
	// fr_width = $('#slideshow').width();
	fr_width = $('#slideshow').width() - 160;
	if (ss_width <= fr_width) {
		$('#slider').attr('id', 'not-here');
	} else {
		ss_range = (0 - ss_width) + fr_width;
		ss_step = ss_range/100;
		$('#slider').slider({
			animate: true,
			min: 0,
			max: 100,
			step: 0.2,
			slide: function(e,ui) {
				if (!oldvalue) { oldvalue = 0; }
				var ss_pos = ss_step * ui.value;
				var dist = Math.abs(oldvalue - ui.value);
				if (dist > 10) {
					$('#images').stop().animate({marginLeft: ss_pos}, 200);
				} else {
					$('#images').stop().css('margin-left', ss_pos);
				}
				oldvalue = ui.value;
			}
		});
	}
}

function superslider_init() {
	// change any attributes needed for graceful failure
	$('.people .description').css('display', 'none');
	$('p.see_full_profile').css('display', 'block');
	
	// resize the list and build the flash array!
	var images = $('#images li');
	var max_height = 0;
	var slide_width = 0;
	flash_obj = new Object();
	flash_obj['margin'] = margin;
	flash_obj['count'] = images.length;
	for (i=1; i<=images.length; i++) {
		if ($('#img-'+i).height() > max_height) {
			max_height = $('#img-'+i).height();
			flash_obj['item_height'] = $('#img-'+i+' img').height();
		}
		flash_obj['width_'+i] = $('#img-'+i).width();
		slide_width += flash_obj['width_'+i] + margin;
		flash_obj['pass_'+i] = $('#img-'+i+' input').attr('value');
	}
	$('#slideshow').css('height', max_height);
	
	if ($.hasFlashPlayer) {
		// display the h3 - FOR TESTING ONLY!
		$('#section_header').css('display', 'block');
		$('#section_header').css('color', '#f00');
		$('#section_header').text('FOR TEST ONLY');
		// sort the flash thingie
		var flash_width = slide_width - margin;
		var flash_height = 400; // REPLACE WITH SOMETHING BETTER
		var list_html = $('#images').html();
		// finish off the flash_obj
		// create the flash overlay
		$('#images').html(list_html +"<li id='overlay'></li>");
		$('li#overlay').flash({
			swf: '/ndxz-studio/site/marker/team_overlay.swf',
			height: flash_height,
			width: flash_width,
			params: { wmode: 'transparent' },
			flashvars: flash_obj
		});
	}	
	
	// set up link functionality
	$('p.see_full_profile a').bind('click', function(e) {
		// get the relevant id (pID)
		var pID = $(this).parent().parent().parent().attr("id");
		e.preventDefault();
		// build details
		var name = $('#'+pID+' .name').text();
		var job = $('#'+pID+' .job').text();
		var desc = $('#'+pID+' .description').text();
		var replace = "<h3 id='person_name'>"+name+"</h3>";
		replace += "<h4 id='person_job'>"+job+"</h4>";
		replace += "<hr />";
		replace += "<p id='person_desc'>"+desc+"</p>";
		$('#team_info').html(replace);
	});
}

function video_replace() {
	var filename = $('#video img').attr('src');
	$('#video').flash({
		swf: '/ndxz-studio/site/marker/video_player.swf',
		height: '100%',
		width: '100%',
		params: { wmode: 'opaque' },
		flashvars: { filename: filename }
	});
}

function video_resize(vidW, vidH) {
	$('#video').css({ width:vidW, height:vidH });
}

function togglerInit() {
	// hide all direct descendants of .toggler that aren't .no-toggle
	$('.toggler:not(.show-me) > *:not(.no-toggle)').hide();
	// make all .toggler elements clickable, and bind the toggle behaviour to them
	$('.toggler').bind('click', function(e) {
		// add a class of .show-me to only the one that has just been clicked
		$('.show-me').removeClass('show-me');
		$(this).addClass('show-me');
		// now show the .show-me element, remove all the others.
		$(this).children('*:not(.no-toggle)').slideDown('fast');
		$('.toggler:not(.show-me) > *:not(.no-toggle)').slideUp('fast');
	});
}