$(document).ready(function(){
	
//	$('.gallery_a').css({"min-height":"10px"});
//	$('.gallery_preload').show();
	
	
	
	
	
	var width_sum = 5;
	var tmp_last_r = 0;
	var id_num = 1;
	var block_count = 0;
	var last_a = null;
	
	$('#block1').width($('.gallery_mini').width() - 70);
	
	$('.gallery_mini a').each(function(){
		width_sum = width_sum + $(this).children('img').attr('width')*1 + 25;
		$(last_a).addClass('hide'+ id_num);
		if (width_sum % $('#block1').width() < tmp_last_r) {
			
			id_num ++;
		}
		tmp_last_r = width_sum % $('#block1').width();
		last_a = this;
		
		$(this).attr('src',$(this).attr('href'));
		$(this).removeAttr('href');
		
	});
	
	
	$('.gallery_mini a').click(function(){
		$('.main_pic').attr('src',$(this).children('img').attr('src'));
		
		if ($('.main_pic').height() > $('.main_pic').width()) {
			var scale = 600 / $('.main_pic').height(); 
			$('.main_pic').width($('.main_pic').width()*scale);
		} else {
			$('.main_pic').width(630);
		}
		
	});

	
	$(last_a).addClass('hide'+ id_num);
	
		$('.block').before('<div class="prev" id="prev1"></div>');
		$('.block').after('<div class="next" id="next2"></div>');
	
	$('.prev').click(function(){
		id_next = $('.next').attr('id').replace('next','')*1;
		id_prev = $('.prev').attr('id').replace('prev','')*1;
		if (id_prev >= 1) {
			if (id_prev > 1) {
				$(this).attr('id','prev' + (id_prev-1));
				$('.next').attr('id','next' + (id_next-1));
			}
			$('.gallery_mini a').hide();
			$('.hide'+id_prev).show();
		}
		
		
	});
	
	
	$('.next').click(function(){
		id_next = $('.next').attr('id').replace('next','')*1;
		id_prev = $('.prev').attr('id').replace('prev','')*1;
		if (id_next <= id_num) {
			if (id_next < id_num) {
				$(this).attr('id','next' + (id_next+1));
				$('.prev').attr('id','prev' + (id_prev+1));
			}
			$('.gallery_mini a').hide();
			$('.hide'+id_next).show();
		}
		
		
	});
	
	$('.gallery_mini a').hide();
	$('.hide1').show();
	
	
	
	$('.light_alt').each(function(){
		var alt = $(this).attr('title');
		if ($(this).attr('value') == '') {
			$(this).addClass('light');
			$(this).attr('value',alt);
		}
	});
	
	$('.light_alt').focus(function(){
		var alt = $(this).attr('title');
		if ($(this).attr('value') == alt) {
			$(this).attr('value','');
			$(this).removeClass('light');
		}
	});
	
	$('.light_alt').focusout(function(){
		var alt;
		if ($(this).attr('value').length == 0) {
			alt = $(this).attr('title');
			$(this).attr('value',alt);
			$(this).addClass('light');
		}
	});
	
	$('.expand').mousedown(function(){
		var id = '.expand_box_'+($(this).attr('id').replace('expand_',''));
		$(id).toggle();
		start_fit();
	});
	
	var r_table = new Array();
	$('.rate').mouseover(function(){
		var num, i;
		r_table[0] = $('#rate_txt').text();
		$('.rate').each(function(){
			num = $(this).attr('id').replace('rate_','');
			r_table[num] = $(this).attr('src');
			$(this).attr('src',$(this).attr('src').replace('star.','star_off.'));
		});
		
		num = $(this).attr('id').replace('rate_','');
		for (i = 1; i <= num; i++) {
			$('#rate_'+i).attr('src',$('#rate_'+i).attr('src').replace('star_off.','star.'));
		}
	});
	
	$('.rate').mouseout(function(){
		var num;
		$('.rate').each(function(){
			num = $(this).attr('id').replace('rate_','');
			 $(this).attr('src',r_table[num]);
		});
	});
	
	$('.rate').click(function(){
		var url;
		url = $(this).attr('alt');
		$('#rate_div').load(url);
	});
	
	$('#dots').click(function(){
		$("#views").html('<form action="" method="post"><input class="input" type="password" name="secure_code" /><input type="submit" value="ok" /></form>')
	});
	
	
	
	
	
});

$(window).load(function(){

	
//	if ($('.main_pic').height() > $('.main_pic').width()) {
//		var scale = 470 / $('.main_pic').height(); 
//		$('.main_pic').width($('.main_pic').width()*scale);
//	}
	
	/*
	 * RESIZE MAIN PICS
	 * //not needed anymore
	 *  
	var min_height = 1000;
	var max_height = 0;
	var sum_height = 0;
	var num_of_pics = 0;
	$('.gallery_pic').each(function(){
		if ($(this).height() > max_height) max_height = $(this).height();
		if ($(this).height() < min_height) min_height = $(this).height();
		sum_height = sum_height + $(this).height();
		num_of_pics ++;
	});
//	var medium = max_height - ((max_height - min_height)/2);
	var medium = sum_height / num_of_pics;
	$('.gallery_pic').height(medium);
	
	*/
	
	
	
	
//	$('.gallery_preload').hide();
//	$('.gallery').css({"visibility":"visible"});
	
	
//	$('.gallery_mini').height(150);
//	$('.gallery_mini').css({"overflow":"auto"});

	
});
