
resizer = function() {
	
	if($.browser.msie==true)
	{
		var height = document.documentElement.clientHeight;
		var width = document.documentElement.clientWidth;
	}
	else
	{
		var height = window.innerHeight;
		var width = window.innerWidth;
	}

	$('#zoomer_thumbs').height(height-60);
	$('#zoomer_thumbs .zoomer_list').height(height-180);
	
	$('#zoomer_container, .ui-platinum-zoomoverlay-buttons').width(width-220);
	$('#zoomer_container').height(height-60);

	if($('#zoomer_container .preview_img').length > 0)
	resizePhoto(img_width, img_height)
}


$.fn.slideCenter = function(xwidth, xheight) {
	$(this).css({'position': 'absolute', 'top': $(document).scrollTop()+window.innerHeight/2, 'left': '50%', 'marginLeft': 0, 'marginTop': 0, 'width': 0, 'height': 0});
	$(this).animate({'width': xwidth, 'height': xheight, 'marginLeft': xwidth/-2, 'marginTop': xheight/-2}, '800');
}

$.fn.zoomerLoader = function(options) {
	$(this).prepend('<div class="ui-platinum-zoomoverlay-loading"><img src="loading.gif" /></div>');
	$('.ui-platinum-zoomoverlay-loading', this).delay(500).fadeOut('normal');
}


function resizePhoto(photo_width, photo_height)
{

	c_width = $('#zoomer_container').width();
	c_height = $('#zoomer_container').height();
	img_width = photo_width;
	img_height = photo_height;

	container_scale = c_width / c_height;

	img_scale = photo_width / photo_height;

	$('#btn_zoomer_resize').show();

	if (photo_width <= c_width && photo_height <= c_height)
	{
		$('#btn_zoomer_resize').hide();
		hj = photo_height;
		wj = photo_width;
	}
	else if(container_scale < img_scale)
	{
		hj = Math.round(photo_height*(c_width/photo_width));
		wj = c_width;

	}
	else
	{
		hj = c_height;
		wj = Math.round(photo_width*(c_height / photo_height));
	}

	if(!$('#zoomer_container .preview_img').hasClass('mover'))
	$('#zoomer_container .preview_img').css({width: wj, height: hj, marginTop: c_height/2-(hj/2), marginLeft: c_width/2-(wj/2)});
}


gallery = function($overlay) {

	myimg = new Image;


	$overlay.html('<div class="ui-platinum-zoomoverlay-buttons overlay_element"><div class="btn btn_close overlay_close"></div><div id="btn_zoomer_resize" class="btn btn_extend"></div></div><div id="zoomer_thumbs" class="overlay_element"><div class="btn btn_thumb_up"></div><div class="btn btn_thumb_down"></div><div class="zoomer_list"><div class="zoomer_thumbs_list"></div></div></div><div id="zoomer_container" class="overlay_element"><div class="ui-platinum-zoomoverlay-loading"><img src="/media/core/loading.gif" /></div></div>');
	
	html ='';
	$('.gallery_thumb img').each(function() {
		html += '<div class="ui-zoomer-thumb"><img src="'+$(this).attr('src')+'"></div>';
	});


	$('.ui-platinum-zoomoverlay-buttons .btn').mouseover(function() {
		$('.tooltip', this).show();
	});

	$('.ui-platinum-zoomoverlay-buttons .btn').mouseout(function() {
		$('.tooltip', this).hide();
	});

	$('.zoomer_thumbs_list').html(html);

	resizer();

	$('.ui-zoomer-thumb').click(function() {


		if($(this).hasClass('active'))
		return false;

		$('#zoomer_container .ui-platinum-zoomoverlay-loading').fadeIn();


		name = $('img', this).attr('src').replace(/_sm/, '');
		$('.ui-zoomer-thumb').fadeTo('normal', 1).removeClass('active');
		$(this).addClass('active').stop().fadeTo('normal', 0.7);
		
		if($('#zoomer_container .preview_img').length > 0)
		$('#zoomer_container .preview_img').stop(true, true).fadeOut('normal', function() {$(myimg).attr('src', name);});
		else
		$(myimg).attr('src', name);

	})

	$(myimg).addClass('preview_img').load(function() {

		$('#zoomer_container .preview_img').remove();

		$('#btn_zoomer_resize').removeClass('btn_fit');
		$(this).hide();
		$('#zoomer_container').append($(this).clone());
		

		resizePhoto(this.width, this.height)

		$('#zoomer_container .ui-platinum-zoomoverlay-loading').stop(true, true).fadeOut(500, function() {
			$('#zoomer_container .preview_img').stop(true, true).fadeTo('100', 0.01).fadeTo(400, 1);
		});

		$('#btn_zoomer_resize').click(function() {
			
		
			if($('#zoomer_container .preview_img').hasClass('mover'))
			{	
				$(this).removeClass('btn_fit');
				$('#zoomer_container .preview_img').fadeOut('normal', function() {
					$(this).css({height: hj, width: wj, marginTop: 0, marginLeft: c_width/2-(wj/2), marginTop: c_height/2-(hj/2)}).removeClass('mover');
					
					$(this).fadeIn();
				});
			
			}
			else
			{
				$(this).addClass('btn_fit');
					
				$('#zoomer_container .preview_img').fadeOut('normal', function() {
					$(this).css({width: img_width, height: img_height, marginTop: 0, marginLeft: 0}).addClass('mover');
					$(this).fadeIn();
				});
			}
		});	
			

	});

	$('.ui-zoomer-thumb').eq(active_index).trigger('click');

	$('#zoomer_container').mousemove(function(e) {
		offset = $(this).offset();
		var x = e.pageX - offset.left;
		var y = e.pageY - offset.top;


		goTop = (-y/$(this).height() * ($('img.mover').height()-$(this).height()));
		goLeft = (-x/$(this).width() * ($('img.mover').width()-$(this).width()));


		$('img.mover', this).css({'marginTop': goTop, 'marginLeft': goLeft});

	});

	$('.btn_thumb_up').mouseover(function() {
		slide = setInterval(function() {
			if(parseInt($('.zoomer_list .zoomer_thumbs_list').css('marginTop')) < 0)
			$('.zoomer_list .zoomer_thumbs_list').animate({'marginTop': '+=3px'}, 1); 
		}, 20);
	});

	$('.btn_thumb_down').mouseover(function() {
		slide = setInterval(function() {
			if($('.zoomer_list .zoomer_thumbs_list').height()-$('.zoomer_list').height() > parseInt($('.zoomer_list .zoomer_thumbs_list').css('marginTop'))*-1)
			$('.zoomer_list .zoomer_thumbs_list').animate({'marginTop': '-=3px'}, 1); 
		}, 20);
	});

	$('.btn_thumb_up').click(function() {
		clearInterval(slide);
		if(parseInt($('.zoomer_list .zoomer_thumbs_list').css('marginTop')) > -100)
		zoomer = 0;
		else
		zoomer = '+=100px';

		$('.zoomer_list .zoomer_thumbs_list').animate({'marginTop': zoomer}, 100); 
	});

	$('.btn_thumb_down').click(function() {
		clearInterval(slide);
		if($('.zoomer_list .zoomer_thumbs_list').height()-$('.zoomer_list').height()-100 < parseInt($('.zoomer_list .zoomer_thumbs_list').css('marginTop'))*-1)
		zoomer = ($('.zoomer_list .zoomer_thumbs_list').height()-$('.zoomer_list').height())*-1;
		else
		zoomer = '-=100px';

		$('.zoomer_list .zoomer_thumbs_list').animate({'marginTop': zoomer}, 100); 
	});

	$('.btn_thumb_down, .btn_thumb_up').mouseout(function() {
		if(typeof(slide)!='undefined')
		clearInterval(slide);
	});
}


$(function() {
	$('.enlarge').click(function() 
	{	
		$(window).resize(resizer);
		active_index = $(this).index('.enlarge');
		if(active_index < 0) active_index = 0;
		overlay(gallery);

		return false;
	});
});
