/* uday code to top and bottom*/
function incrementTop() {

	new_value = parseInt($('#thumbstrip_lv2').css("top"));
	
	if (new_value < 0)
		new_value += 5;
	new_value = new_value + "px";
	$('#thumbstrip_lv2').css('top' , new_value);

}
function incrementBottom() {
			
	new_value = parseInt($('#thumbstrip_lv2').css("top"));
	if (new_value > limit)
		new_value -= 5;
	new_value = new_value + "px";
	$('#thumbstrip_lv2').css('top' , new_value);
}
function scrollTop() {
	move_interval = setInterval(incrementTop, 10);

}
function scrollBottom() {
	move_interval = setInterval(incrementBottom, 10);
}
/*end of uday code*/
function incrementRight() {
	new_value = parseInt($('#thumbstrip_lv2').css("left"));
	if (new_value < 0)
		new_value += 5;
	new_value = new_value + "px";
	$('#thumbstrip_lv2').css('left' , new_value);

}
function incrementLeft() {

	new_value = parseInt($('#thumbstrip_lv2').css("left"));
	if (new_value > limit)
		new_value -= 5;
	new_value = new_value + "px";
	$('#thumbstrip_lv2').css('left' , new_value);
}
function scrollLeft() {
	move_interval = setInterval(incrementRight, 10);

}
function scrollRight() {
	move_interval = setInterval(incrementLeft, 10);
}
function stopScroll() {
	if (move_interval !== undefined) {
		clearInterval(move_interval);
	}
}

function setPrevNextHref() {
	
	if (num_images < 1) {
		$('#thumbnails').css('visibility', 'hidden');
	} else {
		$('#thumbnails').css('visibility', 'visible');
		if (current_image != 0) {
			prev = current_image - 1;
		} else {
			prev = 0;
		}
		
		if (current_image == (num_images - 1)) {
			next = num_images - 1;
		} else {
			next = (Number(current_image)+1);
		}
		
		$('#next-prev > p > a:eq(0)').attr('href', prev);
		$('#next-prev > p > a:eq(1)').attr('href', next);
		
		$('#current_img').text(Number(current_image) + 1);
		$('#total_imgs').text(num_images);
	}
}

function stripPosition(current_image) {
	
	sum = 0;
	
	for ( var i = 0; i < num_images; i++) {
		if (i == current_image)
			curpos = -sum + 230;
		sum += $('#thumbstrip_lv2 > a:eq(' + i + ') > img').attr("width") + 4; //4px of margins
		
	}
	limit = -sum + 777;
	if (curpos > 0)
		curpos = 0;
	else if (curpos < limit)
		curpos = limit;
	
	$('#thumbstrip_lv2').css({ 'left' : curpos }); //curpos is set
}

function addScrollBehaviour() {
	move_interval = false; //just create the var so stopScroll() does not complain
	
	/*
	$('#upArrow').mousedown( function() {
		scrollLeft();
	}).mouseup( function() {
		stopScroll();
	}).mouseout( function() {
		stopScroll();
	});
	*/
	$('#prev-button').hover( 
		function() {
		scrollLeft();
		} ,
		function() {
		stopScroll();
		}
	);
	
	$('#top-button').hover( 
		function() {
		scrollTop();
		} ,
		function() {
		stopScroll();
		}
	);
	
	/*
	$('#downArrow').mousedown( function() {
		scrollRight();
	}).mouseup( function() {
		stopScroll();
	}).mouseout( function() {
		stopScroll();
	});
	*/
	$('#next-button').hover( 
		function() {
		scrollRight();
		} ,
		function() {
		stopScroll();
		}
	);
	$('#down-button').hover( 
		function() {
		scrollBottom();
		} ,
		function() {
		stopScroll();
		}
	);
}

function addThumbnailBehaviour() {
	$("#thumbstrip_lv2 > a").click(function(e) {
	//	alert('Hi');
		e.preventDefault();
		$('#dummy').css('display','block');
			var loadingImg = new Image(); 
		loadingImg.src = 'http://photographers.com/images/ajax-loader2.gif';
		//target_file = $(this).attr('href');
		var i = $("#thumbstrip_lv2 > a").index(this);
		//alert(i);
		//alert(image_data[i].large_src);
		var main_img = $('#main_photo > img:first');
					$('#main_photo > img:first').attr('src', loadingImg.src);
		main_img.fadeOut("slow", function() {
										  	
			$('#main_photo > img:first').attr('src', image_data[i].large_src);
			//DEV NOTE: add check if loaded here
			$('#main_photo > img:first').fadeIn("slow");
		});
		
		current_image = i;
		
		setPrevNextHref();
		//alert(target_file);
		
		$('#thumbstrip_lv2 .selectedThumbnail').removeClass('selectedThumbnail');
		$(this).find('img').addClass("selectedThumbnail");
	});
}
function addNextPrevBehaviour() {
	$("#next-prev > p > a").click(function(e) {
		e.preventDefault();
		$('#dummy').css('display','block');
				var loadingImg = new Image(); 
		loadingImg.src = 'http://photographers.com/images/ajax-loader2.gif';
		
		var target_file = $(this).attr('href');
		$('#main_photo > img:first').attr('src', loadingImg.src);

		//alert(target_file);
		if (target_file !== null) {
			var main_img = $('#main_photo > img:first');
			main_img.fadeOut("slow", function() {
				$('#main_photo > img:first').attr('src', image_data[target_file].large_src);
				$('#main_photo > img:first').fadeIn("slow");
			});
			current_image = target_file;
			setPrevNextHref();
			
			$('#thumbstrip_lv2 .selectedThumbnail').removeClass('selectedThumbnail');
			$('#thumbstrip_lv2 > a:eq(' + target_file + ') > img').addClass("selectedThumbnail");
		}
	});
}

function addThumbstripBehaviors() {
	
	//first the scroll behavior
	//addScrollBehaviour();
	
	//next the image thumbnail behavior
	addThumbnailBehaviour();
	
	//send requst based on existing link address with ?update=image for php to recognize and return only src locaiton
	//preload it?
	//update #main_photo > img:first 's src
	
	//update next and previous button behavior
	//addNextPrevBehaviour();
}
function preloadImages() {
	//preload the large images in the category
	preloaded_images = new Array();
	//put this on a time delay
	jQuery.each(image_data, function(i, item) {
		//alert(i);
		preloaded_images[i] = new Image();
		preloaded_images[i].src = item.large_src;
	});
	
}
function getThumbstripData() {
	
	//get user name
	username = $('#member-info').attr('member');
	//get cat id
	//warning: not all galleries are going to have an li>strong, some are single galleries in the h2 header
	
	cat_id = $('#menu .selectedCat').attr('gid');
	//send json request
	
	//$.getJSON("http://" + window.location.hostname + "/js/portfolio_ajr.php?member=" + username + "&gallery=" + cat_id, function(data) {
	$.getJSON("http://" + window.location.host + "/js/portfolio_ajr.php?member=" + username + "&gallery=" + cat_id, function(data) {
		image_data = data;
	//preloadImages();
		//
	preloaded_images = new Array();
	//put this on a time delay
		//alert( item.large_src);
		preloaded_images[0] = new Image();
		preloaded_images[0].src = image_data[0].large_src;
//
	});
	
	//load data into object
}

//check this one out Rick
function addCategoryBehaviour() {
	$("#menu > ul > li > a").click(function(e) {
		
		$('#profile_contactemail').css('display', 'none');
		$('#profile_bio').css('display', 'none');
		$('#profile_links').css('display', 'none');
		$('#profile_emailPortfolio').css('display', 'none');
		$('#profile_content1').css('display', 'none');
		$('#dummy').css('display','block');

		e.preventDefault();
		
		cat_id = $(this).attr("gid");
		//$.getJSON("http://" + window.location.hostname + "/js/portfolio_ajr.php?member=" + username + "&gallery=" + cat_id, function(data) {
		$.getJSON("http://" + window.location.host + "/js/portfolio_ajr.php?member=" + username + "&gallery=" + cat_id, function(data) {
			image_data = data;
			
			
			$('#profile_content').empty();
			var main_img = $('#main_photo > img:first');
			main_img.fadeOut("slow", function() {
				if (image_data.length > 0) {
					var imgload = new Image();
					imgload.src = image_data[0].large_src;
					//imgload.onload = function () {
						$('#main_photo > img:first').attr('src', image_data[0].large_src);
						$('#main_photo > img:first').fadeIn("slow");
					//}
					
					$('#main_photo > p').remove();
				}
				else {
					$('#main_photo').append('<p>There are no images in this gallery.</p>');
				}
			});
			current_image = 0;
			num_images = image_data.length;
			thumbs_loaded = 0;
			var img_load_arr = new Array();
			$("#thumbstrip_lv2").fadeOut('slow', function () {
				$("#thumbstrip_lv2").empty();
				$("#thumbstrip_lv2").show();
				jQuery.each(image_data, function(i, item) {
					img_load_arr[i] = new Image();
					$("#thumbstrip_lv2").append('<a href="#"><img height="72" alt="' + item.name + '" style="display: none;" src="' + item.thumb_src + '" /></a>');
					$("#thumbstrip_lv2 > a:last img").load(function() {
						$(this).fadeIn("slow");
						thumbs_loaded++;
						if (thumbs_loaded == num_images) {
							$("#thumbstrip_lv2 > a:first >img").removeClass('Thumbnail').addClass("selectedThumbnail");
							addThumbnailBehaviour();
							setPrevNextHref(); 
							stripPosition(0);
						}
					});
					
					img_load_arr[i].src = item.thumb_src;
				});
			});
			
			
			//addCategoryBehaviour();
			//reset positioning?
			
			//preloadImages();
			//
	preloaded_images = new Array();
	//put this on a time delay
		//alert( item.large_src);
		preloaded_images[0] = new Image();
		preloaded_images[0].src = image_data[0].large_src;
//
		});
		$("#menu .selectedCat").removeClass('selectedCat');
		$(this).addClass('selectedCat');
	});
}
function addLinksBehaviour() {
	$('#links > a').click(function(e) {
		e.preventDefault();
		$('#dummy').css('display','none');
		
		link = $(this).attr('id');
			
		//$.get("http://" + window.location.hostname + "/js/portfolio_ajr.php?member=" + username + "&info=" + link, function(data) {
			$.get("http://" + window.location.host + "/js/portfolio_ajr.php?member=" + username + "&info=" + link, function(data) {
		
			link_data = data;
			//alert(link_data);
			
			//clear other data
			$("#menu .selectedCat").removeClass('selectedCat');
			//$('#profile_content1').empty();profile_contactemail
			$('#profile_contactemail').empty();
			
			var main_img = $('#main_photo > img:first');
			main_img.fadeOut("slow", function() {
				if (link_data) {
				  $('#profile_contactemail').append(link_data);
					//$('#profile_content1').append(link_data);
				}
				else {
					//$('#profile_contactemail').append('<p>Not available.</p>');
				}
			});
			current_image = 0;
			num_images = 0;
			
			$("#thumbstrip_lv2").empty();
			
			setPrevNextHref();
		});
		
		
	});
}
function slowImageReveal() {
	$("#main_photo img").hide();
	$("#main_photo img").load(function() {
		$(this).fadeIn("slow");
	});
	thumbs_loaded = 0;

	$("#thumbstrip_lv2").hide();
	//$("#thumbstrip_lv2 > a > img").hide();
	
	thumb_array = $("#thumbstrip_lv2 > a > img");

	for (var i=0; i<num_images; i++)
		if (!thumb_array[i].complete) {
			$("#thumbstrip_lv2 > a:eq("+i+") > img").fadeTo(1, 0.001);
		}
	$("#thumbstrip_lv2").fadeIn("slow");
	$("#thumbstrip_lv2 > a > img").load( function () { 
		$(this).fadeTo(2000, 1);
		thumbLoaded(); });
	//for (var i=0; i<num_images; i++)
	//	if (thumb_array[i].complete) {
	//		$("#thumbstrip_lv2 > a:eq("+i+")").fadeIn("slow");
	//		alert($("#thumbstrip_lv2 > a:eq("+i+")"));
	//		thumbLoaded(); 
	//	};
}
function thumbLoaded () {
		thumbs_loaded++;
	if (thumbs_loaded == num_images) {
		stripPosition(current_image); //you want the images loaded, then this will calculate combined width, and adjust
		addThumbstripBehaviors();
		
	}
}
$(document).ready(function() {
	//Uday code for displaying the loading image
	$('#profile_contactemail').css('display', 'none');
		$('#profile_bio').css('display', 'none');
		$('#profile_links').css('display', 'none');
		$('#profile_emailPortfolio').css('display', 'none');
		//$('#profile_content1').css('display', 'none');
		$('#dummy').css('display','none');


	welcome1 = $("#current_img").html();
	//alert(welcome1);
	current_image = Number($("#current_img").html()) - 1;
	//num_images = $('#thumbstrip_lv2 > a').length;
	num_images = parseInt($('#total_imgs').text());
	if(num_images>0)
	{
		$('#profile_content1').css('display', 'none');
		$('#dummy').css('display','block');
	}
	
	slowImageReveal();
	 //adjust position based on chosen pic, so its centered-like
	//$("#thumbstrip_lv2 > a > img").fadeIn('slow');
	
	//selected image notification, with new blue dot
	$('#thumbstrip_lv2 > a:eq(' + current_image + ') > img').removeClass('Thumbnail').addClass('selectedThumbnail');

	getThumbstripData(); //preloads large images
	
	addCategoryBehaviour();
	
	addLinksBehaviour(); //contact, bio, links, email portfolio etc...
	
	getThumbstripData(); //preloads large images
	
	//addCategoryBehaviour();
	
	//addLinksBehaviour(); //contact, bio, links, email portfolio etc...
	addScrollBehaviour();
	//setPrevNextHref(); //the previous and next buttons, and the (i / n) image index indicator
	
	addNextPrevBehaviour();
	addThumbnailBehaviour();
	addThumbstripBehaviors()
	
	setPrevNextHref(); //the previous and next buttons, and the (i / n) image index indicator
	document.getElementById('large-image').style.display = "block";
	$("#large-image").removeAttr("style");
	if($.browser.mozilla && parseFloat($.browser.version) >= 1.9) {
		cat_id = $("#menu > ul > li > a").attr("gid");
		
		$.getJSON("http://" + window.location.host + "/js/portfolio_ajr.php?member=" + username + "&gallery=" + cat_id, function(data) {
			image_data = data;
			
			
			$('#profile_content').empty();
			var main_img = $('#main_photo > img:first');
			main_img.fadeOut("slow", function() {
				if (image_data.length > 0) {
					var imgload = new Image();
					imgload.src = image_data[0].large_src;
					//imgload.onload = function () {
						$('#main_photo > img:first').attr('src', image_data[0].large_src);
						$('#main_photo > img:first').fadeIn("slow");
					//}
					
					$('#main_photo > p').remove();
				}
				else {
					$('#main_photo').append('<p>There are no images in this gallery.</p>');
				}
			});
			current_image = 0;
			num_images = image_data.length;
			thumbs_loaded = 0;
			var img_load_arr = new Array();
			$("#thumbstrip_lv2").fadeOut('slow', function () {
				$("#thumbstrip_lv2").empty();
				$("#thumbstrip_lv2").show();
				jQuery.each(image_data, function(i, item) {
					img_load_arr[i] = new Image();
					$("#thumbstrip_lv2").append('<a href="#"><img height="72" alt="' + item.name + '" style="display: none;" src="' + item.thumb_src + '" /></a>');
					$("#thumbstrip_lv2 > a:last img").load(function() {
						$(this).fadeIn("slow");
						thumbs_loaded++;
						if (thumbs_loaded == num_images) {
							$("#thumbstrip_lv2 > a:first >img").removeClass('Thumbnail').addClass("selectedThumbnail");
							addThumbnailBehaviour();
							setPrevNextHref(); 
							stripPosition(0);
						}
					});
					
					img_load_arr[i].src = item.thumb_src;
				});
			});
			
			
			//addCategoryBehaviour();
			//reset positioning?
			
			//preloadImages();
			//
	preloaded_images = new Array();
	//put this on a time delay
		//alert( item.large_src);
		preloaded_images[0] = new Image();
		preloaded_images[0].src = image_data[0].large_src;
//
		});
																																	   }
	
});
