


$(document).ready(function() {

	// ----------------
	// OUR TEAM PAGES
	// ----------------	
	
		$("#sidenav ul li.closed a, #sidenav ul li.open a").click(function() {
			
			if($(this).parent().children("ul").is(":visible")) {
				$(this).parent().children("ul").slideUp();
			} else {
				$(this).parent().children("ul").slideDown();
			}
			
			if($(this).parent().attr("class") == "closed") $(this).parent().attr("class", "open");
			else $(this).parent().attr("class", "closed");
						
		});
		
	
		$("#our_team div.partner div.desc a.more").click(function() {
			
			$(this).parent().toggleClass("open");
		
			if($(this).parent().children("div.desc_more").is(":visible")) {
				$(this).parent().children("div.desc_more").slideUp();
			} else {
				$(this).parent().children("div.desc_more").slideDown();
			}
			
			return false;
		
		});
		
	
		$("#our_team div.associate div.desc a.more").click(function() {
			
			$(this).parent().toggleClass("open");
		
			if($(this).parent().children("div.desc_more").is(":visible")) {
				$(this).parent().parent().children("div.buttons").slideUp();
				$(this).parent().children("div.desc_more").slideUp();
			} else {
				$(this).parent().children("div.desc_more").slideDown(function() {
					$(this).parent().parent().children("div.buttons").slideDown();
				});
			}
			
			return false;
		
		});
	
	
});


// ----------------
//   SLIDE SHOWS
// ----------------

function slideshowlauncher(gallery_id) {
	
	$("#image_gallery_background").css({"opacity" : "0.7"}).fadeIn("slow");
	$("#image_gallery").css({
		top:'50%',
		left:'50%',
		margin:'-'+($('#image_gallery').height() / 2)+'px 0 0 -'+($('#image_gallery').width() / 2)+'px'
	});
	
	$("#image_gallery").fadeIn("slow");
	
	// var gallery_id = $(this).attr("href").substring(25);
	// console.log(gallery_id);
	
	// AJAX bit
	$.ajax({
		url: 'includes/image_gallery_ajax.php',
		type: 'POST',
		data: 'gallery_id=' + gallery_id,
		success: function(result) {
			$("#image_gallery").html(result);
			
			// fade out all thumb images
			$("#image_gallery div.nav div.reel ul li img").css({"opacity" : "0.4"});
			
			// on hover fade in thumb image
			$("#image_gallery div.nav div.reel ul li img").hover(
				function() {
					$(this).css({"opacity" : "1"});
				}, 
				function() {
					if(!$(this).hasClass("active")) $(this).css({"opacity" : "0.4"});
				}
			);
			
			// show first image
			$("#image_gallery div.top div.photos img.pic1").fadeIn("fast");
			$("#image_gallery div.nav div.reel ul li a[rel=pic1] img").css({"opacity" : "1"});
			$("#image_gallery div.nav div.reel ul li a[rel=pic1] img").addClass("active");
			$("#image_gallery div.top div.caption div.pic1").fadeIn("fast");
			
			// show arrows
			if($("#image_gallery div.nav div.reel ul li").length > 2) $("#image_gallery div.inner div.top a.next").fadeIn("fast");
			if($("#image_gallery div.nav div.reel ul li").length > 7) $("#image_gallery div.nav a.next").fadeIn("fast");
		}, 
		error: function(result) {
			$("#image_gallery").html("<p>failed</p>");
		}
	});
				
	return false;
	
}


function close_image_gallery() {
	$("#image_gallery").fadeOut("slow");
	$("#image_gallery_background").fadeOut("slow");
	return false;
}


function change_image(pic_id) {
	
	var visible_pic = $("#image_gallery div.nav div.reel ul li a img.active").parent().attr("rel");
	var next_pic = "pic" + (parseInt(visible_pic.substr(3)) + 1);
	var previous_pic = "pic" + (parseInt(visible_pic.substr(3)) - 1);
	var total_pics = $("#image_gallery div.nav div.reel ul li").length;

	if(pic_id == "next") pic_id = next_pic;
	if(pic_id == "previous") pic_id = previous_pic;
		
	if(pic_id != visible_pic && $("#image_gallery div.top div.photos img."+pic_id).length != 0) {
	
		$("#image_gallery div.inner div.top span.photo_number").html(pic_id.substr(3) + " of " + total_pics);
	
		$("#image_gallery div.nav div.reel ul li a[rel=" + visible_pic + "] img").attr("class", "");			// remove class from visible pic
		$("#image_gallery div.nav div.reel ul li a[rel=" + pic_id + "] img").attr("class", "active");			// add 'active' class to new pic

		$("#image_gallery div.top div.photos img."+visible_pic).fadeOut(800);									// fade out visible pic
		$("#image_gallery div.top div.caption div."+visible_pic).fadeOut(800);									// fade out visible caption
		$("#image_gallery div.nav div.reel ul li a[rel=" + visible_pic + "] img").css({"opacity" : "0.5"});		// fade out thumb of visible pic
		$("#image_gallery div.top div.photos img."+pic_id).fadeIn(800);											// fade in new pic
		$("#image_gallery div.top div.caption div."+pic_id).fadeIn(800);										// fade in new caption
		$("#image_gallery div.nav div.reel ul li a[rel=" + pic_id + "] img").css({"opacity" : "1"});			// fade in new thumb
		
		// THUMBS IN REEL
		var str_len = $("#image_gallery div.nav div.reel ul").css("left").length;
		var reel_pos = $("#image_gallery div.nav div.reel ul").css("left").substr(0, str_len - 2);
		if(reel_pos == 0) var first_visible_thumb = "1";
		else var first_visible_thumb = (reel_pos.substring(1, reel_pos.length) / 82) + 1;
		var last_visible_thumb = parseInt(first_visible_thumb) + 6;
		if(last_visible_thumb > total_pics) last_visible_thumb = total_pics;
		
			if(last_visible_thumb < pic_id.substr(3)) move_reel("forwards", 1);
			if(first_visible_thumb > pic_id.substr(3)) move_reel("backwards", 1);
		
		// TOP ARROW - next
		if($("#image_gallery div.inner div.top a.next").is(":visible")) {
			if(pic_id.substr(3) == total_pics) $("#image_gallery div.inner div.top a.next").fadeOut("fast");
		} else {
			if(pic_id.substr(3) < total_pics) $("#image_gallery div.inner div.top a.next").fadeIn("fast");
		}
		
		// TOP ARROW - previous
		if($("#image_gallery div.inner div.top a.previous").is(":visible")) {
			if(pic_id.substr(3) == 1) $("#image_gallery div.inner div.top a.previous").fadeOut("fast");
		} else {
			if(pic_id.substr(3) > 1) $("#image_gallery div.inner div.top a.previous").fadeIn("fast");
		}
	
	}

	return false;
}


function move_reel(direction, no_of_slides) {
	
	var str_len = $("#image_gallery div.nav div.reel ul").css("left").length;
	var reel_pos = $("#image_gallery div.nav div.reel ul").css("left").substr(0, str_len - 2);		// current reel position
	var total_pics = $("#image_gallery div.nav div.reel ul li").length;
	var reel_end_pos = "-" + 82 * (total_pics - 7);
	
	if(no_of_slides == 1) reel_move_px = 82;
	else var reel_move_px = 82 * 7;

	if(direction == "forwards") {
		if(reel_pos - reel_move_px <= reel_end_pos) {
			$("#image_gallery div.nav div.reel ul").animate({ left: reel_end_pos + "px" });
			if($("#image_gallery div.nav a.next").is(":visible")) $("#image_gallery div.nav a.next").fadeOut("fast");
			if(total_pics > 7) $("#image_gallery div.nav a.previous").fadeIn("fast");
		} else {
			$("#image_gallery div.nav div.reel ul").animate({ left: "-=" + reel_move_px + "px" });
			if(!$("#image_gallery div.nav a.previous").is(":visible")) $("#image_gallery div.nav a.previous").fadeIn("fast");
		}
	}
	
	if(direction == "backwards") {
		if(parseInt(reel_pos) + parseInt(reel_move_px) >= 0) {
			$("#image_gallery div.nav div.reel ul").animate({ left: "0px" });
			if($("#image_gallery div.nav a.previous").is(":visible")) $("#image_gallery div.nav a.previous").fadeOut("fast");
			if(total_pics > 7) $("#image_gallery div.nav a.next").fadeIn("fast");
		} else {
			$("#image_gallery div.nav div.reel ul").animate({ left: "+=" + reel_move_px + "px" });
			if(!$("#image_gallery div.nav a.next").is(":visible")) $("#image_gallery div.nav a.next").fadeIn("fast");
		}
	}
	
	return false;
}



// ----------------
// HOME PAGE PHOTOS
// ----------------	
function rotate_home_photos(total_imgs, speed) {
	
	var reel = $("#home_top div.big_photo div.reel");
	
	if(!$(reel).is(":visible")) $(reel).fadeIn("fast");
	
	var j = 2;
		
	setInterval(slide_img, speed);
	
	function slide_img() {
		if(j == 1) {
			$(reel).fadeOut(2100, function() {
				$(this).animate({ left: "-0px" }, function() {
					$(this).fadeIn("fast");
					j++;
				});
			});
		} else {
			$(reel).animate({ left: "-=422px" }, 550);
			if(j == total_imgs) j = 1;
			else j++;
		}
	}
	
	return false;
}


// ---------------------
// SECTION HEADER PHOTOS
// ---------------------
function fading_photos(container_id, total_images, img_path) {
	var img_extension = ".jpg";
	var i = 1;
	
	// insert first image
	$('<img src="' + img_path + i + img_extension + '" class="pic' + i + '" />').appendTo(container_id).show();
	i++;
	
	// every 5 seconds - run the change_photo function
	setInterval(change_photo, 7000);

	function change_photo() {
		
		if(i > total_images) {
			i = 1;
			current_img = 1;
		}
		else current_img = i;
		
		if(current_img == 1) last_img = total_images;
		else last_img = (current_img - 1);
		
		// fade in the new image
		$('<img src="' + img_path + current_img + img_extension + '" class="pic' + current_img + '" />').appendTo(container_id).fadeIn(1500);
		// fade out + remove the existing image
		$(container_id + ' img.pic' + last_img).fadeOut(2100, function() {
			$(container_id + ' img.pic' + last_img).remove();
		});
		
		i++;
	
	}
	
	return false;
}


// --------------------------
// PRESENTATIONS SLIDE VIEWER
// --------------------------
function presentations_slide_viewer(total_images, img_path, img_extension) {
	var i = 1;

	// insert initial caption
	$('#pres_player span.caption').empty().append('slide 1 of ' + total_images);
	// insert + fade in first image
	$('<img src="' + img_path + i + img_extension + '" class="pic1" />').appendTo('#photo_container').fadeIn("fast");
	
	$('ul.player_nav li.next a').click(function() {
		if(i < total_images) {
			var delete_img = i;
			$('#photo_container img.pic' + delete_img).fadeOut(1000, function() {
				$('#photo_container img.pic' + delete_img).remove();
			});
			i++;
		}
	});
	
	$('ul.player_nav li.back a').click(function() {
		if(i > 1) {
			var delete_img = i;
			$('#photo_container img.pic' + delete_img).fadeOut(1000, function() {
				$('#photo_container img.pic' + delete_img).remove();
			});
			i--;
		}
	});
	
	$('ul.player_nav li.first a').click(function() {
		if(i > 1) {
			var delete_img = i;
			$('#photo_container img.pic' + delete_img).fadeOut(1000, function() {
				$('#photo_container img.pic' + delete_img).remove();
			});
			i = 1;
		}
	});
	
	$('ul.player_nav li.last a').click(function() {
		if(i < total_images) {
			var delete_img = i;
			$('#photo_container img.pic' + delete_img).fadeOut(1000, function() {
				$('#photo_container img.pic' + delete_img).remove();
			});
			i = total_images;
		}
	});
	
	$('ul.player_nav li a').click(function() {
		$('<img src="' + img_path + i + img_extension + '" class="pic' + i + '" />').appendTo('#photo_container').fadeIn(1000);
		$('#pres_player span.caption').empty().append('slide ' + i + ' of ' + total_images);
	});
	
	return false;
}
