//<![CDATA[
//REQUIRES jQuery
$(function(){
	var fadeRate = 333;
	var favOpacity = .33;
	$previewPhoto = $('#preview_photo');
	$previewPhoto.bind('load', function(){
		$(this).fadeIn(fadeRate);
	});
	$('#addfav').hide()//hide addfav initially
		.click(function(){//add thumbs to favs
			var thisel = this;
			var imgId = getImgId($previewPhoto);
			var oldText = $(this).text();
			$(this).addClass('disabled').text('Please Wait...');
			$.ajax({//perform httprequest to update session variables
				data: 'photoid='+imgId,
				type: 'GET',
				url: '/php/fav_add.php',
				dataType: 'text',
				cache: false,
				error: function(){alert('An AJAX error occurred.');},
				success: function(text){
					if(text == 'limit'){
						alert('You have reached your limit of favorites. You must remove one to add this photo.');
					}else if(text != 'error'){//add thumbnail to favorites
						var favid = text;
						//var srcstring = '/images/vote/'+text+'/fav.jpg';
						var srcstring = $previewPhoto.attr('src').replace(/\/[0-9]{1,3}\//, '/'+text+'/');
						srcstring = srcstring.replace(/preview/, 'thumb');
						$('<div id="fav_'+favid+'"><img src="'+srcstring+'" title="" width="105" height="70"/></div>')
							.append('<a href="#remove" class="remove" title="Click to remove this from favorites">x</a>')
							.appendTo('#favorites').hide().fadeIn(fadeRate);
						$('#photo'+favid).addClass('fav').css('opacity', favOpacity);//grey out thumbnail
					}
				},
				complete: function(){
					$('#addfav').removeClass('disabled').text(oldText).hide();					
				}
			});
			return false;
		});
	
	$('.fav').css('opacity', favOpacity);//set opacity for php onload favorites
	
	function getImgId($o){//$o is jQuery object
		var previewSrc = $o.attr('src').split('/');
		return previewSrc[previewSrc.length-2];
	};

	function showPreview($o){//$o is jQuery object
		var thisId = getImgId($o);
		var oldSrc = $previewPhoto.attr('src');
		if(!oldSrc.match('/'+thisId+'/')){
			$('img').removeClass('selected');
			$o.addClass('selected');
			var newSrc = oldSrc.replace(/\/[0-9]{1,3}\//, '/'+thisId+'/' );
			$previewPhoto.fadeOut(fadeRate, function(){
				$previewPhoto.attr({src: newSrc})
			});
		}
	}
	
	$('#preview').hover(function(){//mouseover
		var imgId = getImgId($previewPhoto);
		if(imgId != 0 && !$('#fav_'+imgId).length){//check if the showing picture is the default
			$('#addfav').fadeIn(fadeRate);
		}//else do nothing
	}, function(){//mouseout
		$('#addfav').fadeOut(fadeRate);
	});

	$('#thumbnails img').click(function(){//change preview on thumb select
		if($(this).attr('className') != 'fav'){
			showPreview($(this));
		}
		return false;
	});
	
	$('#favorites').click(function(event){
		var $tgt = $(event.target);
		if($tgt.is('a:not(#submit)')){//remove thumbs from fav
			$tgt.hide();
			var $parentel = $($tgt).parent();
			$parentel.fadeTo(fadeRate,favOpacity);
			var $photoid = getImgId($($tgt).prev('img'));//get photo number
			$.ajax({//perform ajax function to remove from $_SESSION
				data: 'photoid='+$photoid,
				type: 'GET',
				url: '/php/fav_remove.php',
				dataType: 'text',
				cache: false,
				success: function(text){
					if(text != 'error'){
						$('div').remove('#fav_'+text);
						$('#photo'+text).removeClass('fav').css('opacity', 1);
					}else{
						$parentel.fadeTo(fadeRate, 1, function(){$tgt.show();});
					}
				}
			});
			return false;
		}
		if($tgt.is('img')){//display thumbnail in preview window
			showPreview($tgt);
			return false;
		}
	});
});
//]]>