






var cssLoadingClass='imLoading';

jQuery(document).ready(function(){


	preparePage();


	jQuery('#cmdPre').click(function(){

		if (current_image > 0){
			current_image--;
			loadKWDoggettImg(current_image);
		}

	});

	jQuery('#cmdNext').click(function(){
		if (current_image < total_images){
			current_image++;
			loadKWDoggettImg(current_image);
		}
	});





});

(function($){


	preparePage = function()
	{
		if (total_images > 1)
		{
			var images_html = '';
			for (i=0; i<=total_images; i++)
			{
				var img = (i == 0)?'full':'empty';
				images_html += '<a href="javascript:void(0)" onclick="javascript:loadKWDoggettImg(\''+i+'\')"><img src="/components/com_rsgallery2/views/default/tmpl/images/c'+img+'-featured.png" id="iconimg'+i+'" /></a>';
			}

			$('#featuredprojectnumbers').empty();
			$('#featuredprojectnumbers').html(images_html);
		}
		else
		{
			$('#featuredprojectnumbers').empty();
			$('#cmdNext').hide();
			$('#cmdPre').hide();
			$('#prev_next_divider').hide();
		}



		loadthisimage = oriPrefix+ featured_gallery_images[0];
		$('#imageholder > img').stop();
		//$('#imageholder > img').remove();

		var myImg=new Image();
		var imgPlaceHolder=$('#imageholder');
		imgPlaceHolder.addClass(cssLoadingClass);

			$(myImg).load(function(){

				//alert('imgload');
				$(this).css('display','none');
				imgPlaceHolder.append(this);


				var maxWidth=imgPlaceHolder.width();
				var maxHeight=imgPlaceHolder.height();

				var w=$(myImg).width();
				var h=$(myImg).height();

				var d=w/h;

				//lets move the image using negative margins.
				if (maxWidth < w)
				{
					move_left = parseInt((w - maxWidth) / 2);
					$(myImg).css('margin-left', '-'+move_left+'px');
				} else {
					move_left = parseInt((maxWidth - w) / 2);
					$(myImg).css('margin-left', move_left+'px');
				}

				if (maxHeight < h)
				{
					move_up = parseInt((h - maxHeight) / 2);
					$(myImg).css('margin-top', '-'+move_up+'px');
				} else {
					move_up = parseInt(( maxHeight - h) / 2);
					$(myImg).css('margin-top', move_up+'px');
				}






				$(this).fadeIn('slow',function(){
					imgPlaceHolder.removeClass(cssLoadingClass)
				})
			}).error(function(){
				imgPlaceHolder.empty()


			}).attr('src',loadthisimage).attr('alt','');

	}



	loadKWDoggettImg=function(selectedindex){

		current_image = selectedindex;

		loadthisimage = oriPrefix + featured_gallery_images[selectedindex];

		$('#imageholder > img').stop();

		var myImg=new Image();
		var imgPlaceHolder=$('#imageholder');
		imgPlaceHolder.addClass(cssLoadingClass);

		$('#imageholder > img').fadeOut('medium',function(){

			$(myImg).load(function(){

				//alert('imgload');
				$(this).css('display','none');
				imgPlaceHolder.append(this);


				var maxWidth=imgPlaceHolder.width();
				var maxHeight=imgPlaceHolder.height();

				var w=$(myImg).width();
				var h=$(myImg).height();

				var d=w/h;

				//lets move the image using negative margins.
				if (maxWidth < w)
				{
					move_left = parseInt((w - maxWidth) / 2);
					$(myImg).css('margin-left', '-'+move_left+'px');
				} else {
					move_left = parseInt((maxWidth - w) / 2);
					$(myImg).css('margin-left', move_left+'px');
				}

				if (maxHeight < h)
				{
					move_up = parseInt((h - maxHeight) / 2);
					$(myImg).css('margin-top', '-'+move_up+'px');
				} else {
					move_up = parseInt(( maxHeight - h) / 2);
					$(myImg).css('margin-top', move_up+'px');
				}




				//if (total_images > 1)
				//{
					for (redo_images_index=0; redo_images_index<=total_images; redo_images_index++)
					{
						redo_images_src = (redo_images_index == selectedindex)?'full':'empty';
						$('#iconimg'+redo_images_index).attr('src', '/components/com_rsgallery2/views/default/tmpl/images/c'+redo_images_src+'-featured.png');
					}
				//}

				$(this).fadeIn('slow',function(){
					imgPlaceHolder.removeClass(cssLoadingClass)
				})
			}).error(function(){
				imgPlaceHolder.empty()


			}).attr('src',loadthisimage).attr('alt','');

		})
	}



















		})(jQuery);