/* b-linked custom JS -- */

$(document).ready(function () {

	/* when searching for a friend, add the search name to the three extra hidden fields */
	$('#friend_find').submit(function() {
		var thisName = $('#friend_find input[@name=public_name]').attr("value");
		// look for separate names
		Names = thisName.split(" ");
		if (Names.length > 1) {
			$('#friend_find input.friend_hide').attr("value", Names[0]);
			$('#friend_find input[@name=first_name]').attr("value", Names[0]);
			$('#friend_find input[@name=last_name]').attr("value", Names[(Names.length-1)]);
			$('#friend_find input[@name=last_name_current]').attr("value", Names[(Names.length-1)]);
		} else {
			$('#friend_find input.friend_hide').attr("value", Names[0]);
		}
				
		return;
	});
	
	/* membership form */
	$('#parent_2_expand').click(function() {
		$('#parent_2_expand').fadeOut("normal", function() {
			$('#parent_2').slideDown("normal");
		});
		return false;
	});

	/* membership pay form, total sum */
	$('#contribute input.checkradio').click(function() {
		var radioVal = $("#contribute input.checkradio:checked").val();
		radioVal = radioVal.replace('$', '');
		
		addVal = $('#id_additional_donation').val();
		
		if (addVal && radioVal) {
			fullVal = parseInt(addVal) + parseInt(radioVal);
		} else if (addVal && !radioVal) {
			fullVal = addVal;
		} else {
			fullVal = radioVal;
		}
		
		$('#total_line').html('$'+fullVal);
	});
	
	$('#id_additional_donation').change(function() {
		var radioVal = $("#contribute input.checkradio:checked").val();
		if (radioVal) {
			radioVal = radioVal.replace('$', '');
		}
		
		addVal = $('#id_additional_donation').val();
		
		if (addVal && radioVal) {
			fullVal = parseInt(addVal) + parseInt(radioVal);
		} else if (addVal && !radioVal) {
			fullVal = addVal;
		} else {
			fullVal = radioVal;
		}
		
		$('#total_line').html('$'+fullVal);
	});	
	/* photo gallery codes */
	$('.profile_choose').click(function() {
		$('.profile_chosen').removeClass("profile_chosen");
		var imgVal = $(this).children("img").attr("src");
		$(this).parent("div.profile_pic_op").addClass("profile_chosen");
		$('#user_img_value').attr("value", imgVal);
		return false;
	});
	
	$('form').submit(function(){
		labels_uninit();
	});
	
	$('#main_picture').hover(
		function() {
			$(this).children(".edit_link").slideDown("normal");
		},
		function() {
			$(this).children(".edit_link").slideUp("normal");
		}
	);
	
	
	$('li.gallery_img').click( function() {
		var file = $(this).children("#file").html();
		var caption = $(this).children("#caption_txt").html();
		var nick = $(this).children("#nick").html();
		var id = $(this).children("#id").html();
		var edit = $(this).children("#edit_link").html();

		$('#main_picture').fadeOut("normal", function() {
			$('#throb').fadeIn("normal", function() {
				$('#main_picture').append(
					'<div id="imagetest" style="display: block; position: absolute; left: -10000px;"><img src="'+file+'" style="display: block;"/></div>'
				);
	
				$("#imagetest > img").load(function() {
					$('#imagetest').remove();
			
					$('#main_picture img#thisone').attr("src").empty;
					$('#main_picture p.caption_txt').empty;
				
					$('#main_picture #pic_link').attr('href', file);
					$('#main_picture img#thisone').attr("src", file);
					$('#main_picture img#thisone').attr("alt", caption);
					$('#main_picture p.caption_txt').html(caption);
					$('#main_picture .edit_link').remove();
					$('#main_picture').prepend(edit);
					
					$('#throb').fadeOut("normal", function() {
						$('#main_picture').fadeIn("normal");
					});
				});
			});
		});
		return false;
	});
	
	
	/* program media upload handler */
	
	var mediaform_options = { 
        url: '/program_media_upload', 
    	beforeSubmit: function() {
			var fileName = $('#program_upload').attr('value');
			
			if (!fileName) {
				$('#upload_form').fadeOut('normal', function() {
					$('#upload_wait').html('Error: Please select a file to upload.');
					$('#upload_wait').fadeIn('normal');
				});
				setTimeout("restoreForm()", 4000);
				return false;
			}
			
			fileName = fileName.replace(/^.*[\\\/](.*?)$/, "$1");
			$('#upload_form').fadeOut('normal', function(){
				$('#upload_wait').html('Uploading '+fileName+'... <img id="spinner" src="/images/admin/indicator.white.gif">');
				$('#upload_wait').fadeIn('normal');
			});
		},
		success:    function(responseText) { 
			// What kind of file?
			if (responseText.match(/program_image/)) {
	  			if (!$('#program_media').html().match(/Pictures/)) {
	  				$('#program_media').append('<h3>Pictures:</h3>');
	  			}
	  			$('#program_media').append(responseText);
	  			// get the new value and put it in the hidden textarea
	  			var Files = new Array;
	  			if($('#attached_images').text().length > 0) {
		  			Files.push($('#attached_images').text());
		  		}
	  			Files.push($('#new_item').attr("value"));
	  			$('#attached_images').empty();
	  			$('#attached_images').text(Files.join(','));
	  			$('#new_item').remove();
			} else {
				if (!$('#program_edit_documents').html().match(/Documents/)) {
	  				$('#program_edit_documents').append('<h3>Documents:</h3>');
	  			}
				$('#program_edit_documents').append(responseText);
	  			// get the new value and put it in the hidden textarea
	  			var Files = new Array;
	  			if($('#attached_documents').text().length > 0) {
	  				Files.push($('#attached_documents').text());
	  			}
	  			Files.push($('#new_item').attr("value"));
	  			$('#attached_documents').empty();
	  			$('#attached_documents').text(Files.join(','));
	  			$('#new_item').remove();
			}
			
			$('#upload_wait').fadeOut('normal', function() {
				$('#program_upload').attr('value', '');
				$('#upload_success').fadeIn('normal', function() {
					setTimeout(function() {
						$('#upload_success').fadeOut('normal', function() {
							$('#up_new').fadeIn();
					})}, 4000);
				});

			});
			setImageBehaviors();

   		}   
    }; 
	
	$('#program_media_form').ajaxForm(mediaform_options);
	
	/* Set the location of the upoad div */
	$('#up_new').click(function(eventObj){
		$('#program_media_form').css('top', (eventObj.pageY - 10));
		$('#up_new').fadeOut('normal', function() {
			$('#upload_form').show();
			$('#program_media_form').fadeIn();
		});
		
		return false;
	});
	
	/* cancel an upload */
	$('#program_upload_cancel').click(function() {
		$('#program_media_form').fadeOut();
		$('#program_upload').attr('value', '');
		$('#up_new').fadeIn();
	});
	
	setImageBehaviors();

	// Set the first enlarged image height	
	
	var skipfirst = 0;
	$('a.program_image_enlarge').each(function() {
		//preload all images
		if (skipfirst == 1) {
			skipfirst = 0;
		} else {		
			jQuery("<img>").attr("src", $(this).attr("href"));
		}
	});
		
	$('a.program_image_enlarge').click(function() {
		var newImage = $(this).attr('href');
		
		$("#enlarge_image").append(
			'<div id="imagetest" style="display: block; position: absolute; left: -10000px;"><img src="'+newImage+'" class="image_enlarged" style="display: block;"/></div>'
		);
		
		$("#imagetest > img").load(function() {
			var newHeight = $("#imagetest > img").height();
		
			$('#imagetest').remove();
			$('#enlarge_image').fadeOut('normal', function(){
				$('#enlarge_image').empty();
				$('#enlarge_image').animate({
				 	height: newHeight
				}, "normal", 'linear', function() {
					$('#enlarge_image').html(	
						'<img src="'+newImage+'" class="image_enlarged" style="display: none;"/>'
					);
				
					$('.image_enlarged').fadeIn('slow');
				});
			});
		
		});
		
		$('img.active_thumb').removeClass("active_thumb");
		$(this).children('img').addClass("active_thumb");	
				
		return false;	
	});
	
	$('a.program_image_enlarge:first').click();
	
	/* Show WLFW if results were found */
	if ($('.friendinfo_display').length) {
		$('#wlfw').show();
	}

}); /* end document.ready */


function setImageBehaviors() {
	$('a.moveup').unbind('click');
	$('a.movedown').unbind('click');
	$('a.remove').unbind('click');
	
	$('a.remove').click(function(event) {
		var val_to_remove = $(this).parents("p").children("a").attr("href");
		var what = ($(this).parents("p").hasClass("program_document")) ? 'doc' : 'img';
		Files = new Array();
		if (what == 'doc') {
			Files = $('#attached_documents').text().split(',');
		} else {
			Files = $('#attached_images').text().split(',');
		}
		
		newFiles = new Array();
		while (thisFile = Files.shift()) {
			if (thisFile != val_to_remove) {
				newFiles.push(thisFile);
			}
		}
		
		if (what == 'doc') {
			$('#attached_documents').text(newFiles.join(','));
		} else {
			$('#attached_images').text(newFiles.join(','));
		}
		
		$(this).parents("p").remove();
		return false;
	});
	
	$('a.moveup').click(function() {
		var previousItem = $(this).parents("p").prev();
		var what = ($(this).parents("p").hasClass("program_document")) ? 'doc' : 'img';
		
		if (!previousItem || (previousItem.attr("class") != 'program_image' && previousItem.attr("class") != 'program_document')) {
			return false;
		}
		
		var currentItem = $(this).parents("p");
		
		previousItem.fadeOut('normal', function() {
			var myClone = previousItem.clone(true);
			previousItem.remove();
			myClone.insertAfter(currentItem).fadeIn('normal', function() {
				Files = new Array;
				if (what == 'doc') {
					$("#program_edit_documents").children("p.program_document").each(function() {
							Files.push($(this).children("a").attr('href'));
					});
					$('#attached_documents').text(Files.join(','));
				} else {
					$("#program_media").children("p.program_image").each(function() {
							Files.push($(this).children("a").attr('href'));
					});
					$('#attached_images').text(Files.join(','));
				}
			});
		});
				
		return false;
	});
	
	$('a.movedown').click(function() {
		var nextItem = $(this).parents("p").next();
		var what = ($(this).parents("p").hasClass("program_document")) ? 'doc' : 'img';
		
		if (!nextItem || (nextItem.attr("class") != 'program_image' && nextItem.attr("class") != 'program_document')) {
			return false;
		}
		
		var currentItem = $(this).parents("p");
		

		nextItem.fadeOut('normal', function() {
			var myClone = nextItem.clone(true)
			nextItem.remove();
			myClone.insertBefore(currentItem).fadeIn('normal', function() {
				Files = new Array;
				if (what == 'doc') {
					$("#program_edit_documents").children("p.program_document").each(function() {
							Files.push($(this).children("a").attr('href'));
					});
					$('#attached_documents').text(Files.join(','));
				} else {
					$("#program_media").children("p.program_image").each(function() {
							Files.push($(this).children("a").attr('href'));
					});
					$('#attached_images').text(Files.join(','));
				}
			});
		});

		
		return false;
	});
}	

function restoreForm() {
	$('#upload_wait').fadeOut('normal', function() {
		$('#upload_form').fadeIn('normal');
	});
}
