//number of images in the home sharing gallery
var sharing_images = 0;
var cur_share = 0;

jQuery(document).ready(function(){
	//setup galleries
	setupGalleries();
});

jQuery(window).load(function(){
	//setup sharing gallery
	setupSharingGallery();
});

function setupSharingGallery(){
	//set the number of images
	sharing_images = jQuery(".home_sharing_image").find("a").length;
	
	//set the lefts of the images
	jQuery(".home_sharing_image a").each(function(){
		if(jQuery(".home_sharing_image").width() > jQuery(this).width()){
			var left = (jQuery(".home_sharing_image").width() - jQuery(this).width()) / 2;
			if(left > 0){
				jQuery(this).css({ 'left':left });
			}
		}
		
		jQuery(this).css({ display:'none', visibility:'visible' });
	});
	
	jQuery(".home_sharing_image a").eq(0).show();
}

function goImage(dir){
	//if its already animate
	if(jQuery(".home_sharing_image").find("a").eq(cur_share).is(":animated")) return;
	
	var new_image = 0;
	
	//get the next image
	if(dir+cur_share >= sharing_images){
		new_image = 0;
	} else if(dir+cur_share < 1){
		new_image = sharing_images - 1;
	} else {
		new_image = cur_share + dir;
	}
	
	//animate the images in/out
	jQuery(".home_sharing_image").find("a").eq(cur_share).fadeOut(250);
	jQuery(".home_sharing_image").find("a").eq(new_image).fadeIn(250);
	
	//set the current image
	cur_share = new_image;
}

function setupGalleries(){
	jQuery('.thumbnail_image').click(function(){
		//set the fade element
		var fadeEle = jQuery(this).parents('.home_gal').find('.main_image img');
		
		//set the clicked element
		var clicked = jQuery(this);
		
		//return if its the same image
		if(fadeEle.attr("src") == clicked.find('img').attr('large')) return;
		
		//if its already animated stop it
		if(!fadeEle.is(":animated")){
			//set the new source
			fadeEle.fadeOut(250,function(){
				jQuery(this).attr('src',clicked.find('img').attr('large')).load(function(){
					jQuery(this).fadeTo(250,1);
				});
			});
		} else {
			//stop the animation and set the elment to not visible
			fadeEle.stop(true).fadeOut((parseFloat(fadeEle.css("opacity")) * 250),function(){
				//set the new source and fade in
				jQuery(this).attr('src',clicked.find('img').attr('large')).load(function(){
					jQuery(this).fadeTo(250,1);
				});
			});
		}
		
		//remove the selected and add the new selected
		jQuery(this).parents('.home_gal').find('.thumb_selected').removeClass('thumb_selected');
		
		//set the new image as active
		jQuery(this).addClass('thumb_selected');
	});
}

function changeImage(dir,ele){
	//get the element
	var ele = jQuery(ele).parents('.home_gal');
	
	//get the thumbs
	var thumbs = ele.find('.thumbnail_image');
	
	//get the selected images index
	var index = thumbs.index(ele.find('.thumb_selected'));
	
	//get the elements length
	var length = thumbs.length;
	
	//new index
	var new_index = index;
	
	//get the new image
	if(index + dir >= length){
		new_index = 0;
	} else if(index + dir < 0){
		new_index = length - 1;
	} else {
		new_index += dir;
	}

	//triger the click
	thumbs.eq(new_index).trigger('click');
}
