$(document).ready(function(){ 

	// ############### Get image height ################
	var currentSlideImageHeight = 400;
	var slideArrowsPosition = 26;
	function resizeImage() {
		currentSlideImageHeight = $('#photo_gallery_slide_content img:visible').height()
		$('#photo_gallery_slide_content').animate({
			height: currentSlideImageHeight
		  }, 50, 'linear', function() {
			//alert(currentSlideImageHeight);
			slideArrowsPosition = currentSlideImageHeight / 2;
			slideArrowsPosition = slideArrowsPosition - 65;
			slideArrowsPosition = slideArrowsPosition.toFixed(0);

			$('#content #photo_gallery_slide #photo_gallery_slide_buttons #gallery_slide_prev').css({'margin-top': slideArrowsPosition+"px"});
			$('#content #photo_gallery_slide #photo_gallery_slide_buttons #gallery_slide_next').css({'margin-top': slideArrowsPosition+"px"});
		  });
		
	};

	// #################################################
	
	// ################### Get URL #####################
	//var target_slide_id = $.query.get('img');
	var starting_slide = 0;
	try {
      if (target_slide_id) {
		  target_slide_id = $("#gallery_list ." + target_slide_id).index();
		  starting_slide = target_slide_id;
		  }
	} catch (err) {
		
	}
	// #################################################
	
	// ################### Main gallery ################
	$('#photo_gallery_slide_content').cycle({
		after: resizeImage,
		fx:     'fade',
		timeout: 0,
		pager:  '#gallery_list_single',
		startingSlide: 0,
		prev:   '#gallery_slide_prev',
	    next:   '#gallery_slide_next',
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '#nav .single_gallery_item:eq(' + idx + ') a'; 
		}
	});
	
	$.fn.cycle.pagerAnchorBuilder = function(pager, currSlideIndex) { 
		$("#gallery_list_single").find('.single_gallery_item').removeClass('active') 
			.filter('.single_gallery_item:eq('+currSlideIndex+')').addClass('active');
	};
	
	$('#gallery_list_single .single_gallery_item').click(function(){
		var current_single_image = $(this).parent().children(".single_gallery_item").index(this);
		$('#photo_gallery_slide_content').cycle(current_single_image);
	});

	$('#gallery_slide_next').click(function(){		
		var current_parent_id = $('#photo_gallery_slide_content img:visible').parent().next('a').attr("class");
		$("#gallery_list").find('.gallery_item').removeClass('active') 
		$('#' + current_parent_id).addClass('active');
	});
	
	$('#gallery_slide_prev').click(function(){
		var current_parent_id = $('#photo_gallery_slide_content img:visible').parent().prev('a').attr("class");
		$("#gallery_list").find('.gallery_item').removeClass('active') 
   	 	$('#' + current_parent_id).addClass('active');
	});
	
	$("#gallery_list .gallery_item:first").addClass('active');
	$('#gallery_list .gallery_item').click(function(){
		var current_image = $(this).attr("id");
		target_image_slide_id = $("#photo_gallery_slide_content ." + current_image + ":first").index();
		$("#gallery_list").find('.gallery_item').removeClass('active') 
		$(this).addClass('active');
		$('#photo_gallery_slide_content').cycle(target_image_slide_id);
	});
	// #################################################
	
	// ############### Main gallery arrows #############
	$('#photo_gallery_slide').mouseover(function() {
		$('#gallery_slide_prev').show(0);
		$('#gallery_slide_next').show(0);
	}).mouseout(function(){
		$("#gallery_slide_prev").hide(10);
		$("#gallery_slide_next").hide(10);
	});
	// #################################################

});

