// JavaScript Document
function popitupCONTACT(url){
	newwindow=window.open(url,'contact','toolbar=no,resizable=yes,scrollbars=yes,height=420,width=460');
	if (window.focus) {newwindow.focus()}
}
		
function popitup(url){
	newwindow=window.open(url,'name','toolbar=no,resizable=yes,scrollbars=yes,height=732,width=599');
	if (window.focus) {newwindow.focus()}
}

function updateMessage(message, stay){
	$("#messages").html(message);
	$("#messageBox").slideDown("slow");
	$("#messageBox").fadeIn("slow");
	if(!stay){
		setTimeout(function(){
			$("#messageBox").slideUp("slow");
			$("#messageBox").fadeOut("slow", function () {
				$("#messageBox").hide();
			 });
		}, 8000);
	}
}

function showMessage(message, stay){
	if($("#messageBox").is(":visible")){
		$("#messageBox").stop().show().effect("pulsate", { times:3 }, 250, function(){
			$("#messageBox").slideUp("slow", function () {
				updateMessage(message, stay);
			});
		});
		
	}else updateMessage(message, stay);
}

//Profile viewer functions
function addImageClicks(){
	$('.openLink').each(function(){
		$(this).click(function(event){
			event.preventDefault();
			var image_link = this.href;
			$('.active').removeClass('active');

			if($(this).hasClass('firstPhoto')){
				clearPhoto('#talentPhoto');
				$(this).addClass('active');
				displaySecondImage(image_link);
				$('#leftArrow').fadeOut('fast');
			}
			else{
				if($(this).hasClass('oneOfTwo')){
					$(this).addClass('active');
					displayImage(image_link);
					var second_image_link = getSecondImageLink(this);
					if($(second_image_link).attr('href')){
						$(second_image_link).addClass('active');
						displaySecondImage($(second_image_link).attr('href'));
					}else
						clearPhoto('#talentPhoto2');
				}else if($(this).hasClass('twoOfTwo')){
					$(this).addClass('active');
					displaySecondImage(image_link);
					var second_image_link = getSecondImageLink(this);
					if($(second_image_link).attr('href')){
						$(second_image_link).addClass('active');
						displayImage($(second_image_link).attr('href'));
					}
					else
						clearPhoto('#talentPhoto');
				}
			}
			if($('.active:last').attr('href') == $('.openLink:last').attr('href'))
				$('#rightArrow').fadeOut('fast');
			else
				$('#rightArrow').fadeTo(0, 0.5);
				
			if($('.active:first').attr('href') == $('.openLink:first').attr('href'))
				$('#lefttArrow').fadeOut('fast');
			else
				$('#leftArrow').fadeTo(0, 0.5);
		});
	});
}

function getSecondImageLink(clicked_link){
	//determine if link has class oneOfTwo or twoOfTwo
	if($(clicked_link).hasClass('oneOfTwo')){
		//get next image
		return $(clicked_link).parent('li').next().find('.twoOfTwo');
		
	}else if($(clicked_link).hasClass('twoOfTwo')){
		//get previous image
		return $(clicked_link).parent('li').prev().find('.oneOfTwo');
	}else
		return '';
}

function clearPhoto(image_id){
	$(image_id).fadeOut('fast');
}

function centerImage(image_id){
	var img = $(image_id);
	var img_height = $(img).height() + 10;
	var img_width = $(img).width() + 10;
	var viewer_height = 400;
	var viewer_width = 410;
	//$(img).css('top', Math.round((viewer_height - img_height)/2)+'px');
	//$(img).css('left', Math.round((viewer_width - img_width)/2) +'px');
	$(image_id).fadeIn('fast');
}


function displaySecondImage(image_link){
	//var href = $(image_link).attr('href');
	//$(image_link).addClass('active');
	$('#talentPhoto2').fadeOut('fast', function(){
		$('#talentPhoto2').attr('src', image_link);
		$('#imageScreen2').waitForImages(function(){
			var t=setTimeout("centerImage('#talentPhoto2')",10);				
		});
	});
}

function displayImage(image_link){
	$('#talentPhoto').fadeOut('fast', function(){
		$('#talentPhoto').attr('src', image_link);
		$('#imageScreen').waitForImages(function(){
			var t=setTimeout("centerImage('#talentPhoto')",10);				
		});
	});
}

function displayDefaultImage(){
	var default_img = $('.openLink:first');
	$(default_img).addClass("active");
	displaySecondImage(default_img.attr('href'));
	$("#rightArrow").fadeTo(0, 0.5);
	$('#leftArrow').fadeOut('fast');
	if(!($('.openLink').length > 1))
		$('#rightArrow').fadeOut('fast');
}

function activateNavButtons(){
	$("#leftArrow").fadeOut('fast');
	$("#rightArrow").fadeTo(0, 0.5).show();
	$("#thumb_prev").fadeTo(0, 0.5);
	$("#thumb_next").fadeTo(0, 0.5);
	
	$("#rightArrow").click(function(){
		//locate next next images to display;
		var last_active_lnk = $('.active:last');
		var next_first_img = $(last_active_lnk).parent('li').next().find(".oneOfTwo");
		var next_second_img = $(next_first_img).parent('li').next().find(".twoOfTwo");
		
		if(typeof $(next_first_img).attr('href') != 'undefined'){
			$('.active').removeClass('active');
			$(next_first_img).addClass('active');
			displayImage($(next_first_img).attr('href'));
		}
		if(typeof $(next_second_img).attr('href') != 'undefined'){
			$(next_second_img).addClass('active');
			displaySecondImage($(next_second_img).attr('href'));
		}else
			clearPhoto('#talentPhoto2');
		
		$("#leftArrow").fadeTo(0, 0.5);
		
		if($('.active:last').attr('href') == $('.openLink:last').attr('href'))
			$('#rightArrow').fadeOut('fast');
	});
	
	$("#rightArrow").hover(
		function(){
			if($(this).is(":visible"))
				$("#rightArrow").fadeTo(0, 1);
		},
		function(){
			if($(this).is(":visible"))
				$("#rightArrow").fadeTo(0, 0.5);
		}
	);
	
	$("#leftArrow").click(function(){
		//locate next next images to display;
		var last_active_lnk = $('.active:first');
		var prev_second_img = $(last_active_lnk).parent('li').prev().find(".twoOfTwo");
		var prev_first_img = $(prev_second_img).parent('li').prev().find(".oneOfTwo");
		
		if(typeof $(prev_second_img).attr('href') != 'undefined'){
			$('.active').removeClass('active');
			$(prev_second_img).addClass('active');
			displaySecondImage($(prev_second_img).attr('href'));
		}
		else{
			clearPhoto('#talentPhoto');
			$('.active').removeClass('active');
			displayDefaultImage();
			$('#leftArrow').fadeOut('fast');
		}
			
		if(typeof $(prev_first_img).attr('href') != 'undefined'){
			$(prev_first_img).addClass('active');
			displayImage($(prev_first_img).attr('href'));
		}
		
		$("#rightArrow").fadeTo(0, 0.5);
	});
	
	$("#leftArrow").hover(
		function(){
			if($(this).is(":visible"))
				$("#leftArrow").fadeTo(0, 1);
		},
		function(){
			if($(this).is(":visible"))
				$("#leftArrow").fadeTo(0, 0.5);
		}
	);
	
	// Gallery
	if(jQuery("#thumbs").length){
		// Declare variables
		var totalImages = jQuery("#thumbs > li").length,
			totalRows = Math.round((totalImages / 6) + .5),
			imageWidth = 67.5,
			totalWidth = imageWidth * totalImages,
			visibleImages = Math.round(jQuery("#imageThumbnails").width() / imageWidth),
			visibleWidth = visibleImages * imageWidth,
			stopPosition = (visibleWidth - totalWidth);
			
		jQuery("#gallery-prev").click(function(event){
			event.preventDefault();
			
			if(jQuery("#thumbSlider").position().left < 0 && !jQuery("#thumbSlider").is(":animated")){
				jQuery("#thumbSlider").animate({left : "+=" + (imageWidth * 6) + "px"}, function(){
				jQuery("#thumb_next").fadeTo(0, 1);
				
				if(jQuery("#thumbSlider").position().left < 0){
					jQuery("#thumb_prev").fadeTo(0, 1)}
				else{
					jQuery("#thumb_prev").fadeTo(0, 0.5)}});
			}

			return false;
		});

		jQuery("#gallery-next").click(function(event){
			event.preventDefault();

			if(jQuery("#thumbSlider").position().left > stopPosition && !jQuery("#thumbSlider").is(":animated")){
				jQuery("#thumbSlider").animate({left : "-=" + (imageWidth * 6) + "px"}, function(){
					jQuery("#thumb_prev").fadeTo(0, 1);

					if(jQuery("#thumbSlider").position().left > stopPosition){
						jQuery("#thumb_next").fadeTo(0, 1)}
					else{
						jQuery("#thumb_next").fadeTo(0, 0.5)}});
			}

			return false;
		});
	}
}
