
/******************************************************************************
 *
 *	PROJECT: reefLESS Classifieds Software
 *	VERSION: 3.1
 *	LISENSE: RL97E4LDFI9Y - http://www.reefless.com/license-agreement.html
 *	DOMAIN: www.nautisme.pro
 *	FILE: LIB.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with reefLESS Owners Team and be agree to reefLESS License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of reefLESS respective owners.
 *
 *	Copyrights reefLESS Classifieds Software | 2009
 *	http://www.reefless.com/
 *
 ******************************************************************************/

if ( thumbnail_syn )
{
	var thumb_size_style = ' width: '+thumbnail_width+'px;height: '+thumbnail_height+'px;';
}		

$(document).ready(function(){
	/* build main photo */
	var obj_main_photo = '<img style="width: '+main_photo_width+'px" src="'+files_url+photos[0][1]+'" title="'+photos[0][2]+'" alt="'+photos[0][2]+'" />';
	$('#main_photo').html(obj_main_photo);
	$('#main_photo').attr({accessKey: files_url+photos[0][1], title: photos[0][2]});
	
	$('#main_photo img').load(function(){
		if (mask == 0)
		{
			var main_photo_height = $.browser.msie ? $('#main_photo img').attr('height') + 'px' : $('#main_photo img').css('height');
			var obj_load_mask = '<div id="pg_load_mask" style="width: '+main_photo_width+'px; height: '+main_photo_height+';margin-top: -'+main_photo_height+';"></div>';
			$('#main_photo').append(obj_load_mask);	
			$('#pg_load_mask').css('opacity', 0);
			mask = 1;
		}	
	});
	
	/* build photo list */
	var obj_photos = '';
	for(var pgi = 0; pgi < photos.length; pgi++)
	{
		if ( photos_per_page > pgi )
		{
			obj_photos += '<div style="float: left;"><img onclick="setPhoto('+pgi+');" class="pg_photo_item" id="photo_'+pgi+'" style="cursor: pointer;'+thumb_size_style+'" src="'+files_url+photos[pgi][0]+'" alt="'+photos[pgi][2]+'" title="'+photos[pgi][2]+'" /></div>';
			if ( (pgi+1)%photo_columns == 0 )
			{
				obj_photos += '<div class="clear"></div>';
			}
		}
	}
	$('#photos_area').html(obj_photos);
	$('#photo_0').addClass('pg_active_photo');
	$('#lager_0').show();
	
	/* build pages */
	if ( pages > 1 )
	{
		var nav_page = cur_page+1;
		var paging = '<ul class="paging" style="margin-top: 0;"><li id="pg_nav" class="navigator" style="padding: 1px 8px 0pt; cursor: default;">'+phrase_page+' '+nav_page+' '+phrase_of+' '+pages+'</li>';
		for(var j=0; j < pages; j++ )
		{
			var page_disp = j+1;
			var pg_paging_class = j == 0 ? 'class="active"' : '';
			paging += '<li '+pg_paging_class+' id="pg_page_'+j+'"><a href="javascript:void(0)">'+page_disp+'</a></li>';
		}
		paging += '</ul>';
		$('#pg_paging_area').html(paging);
	}
	
	/* paging actions */
	$('#pg_paging_area li').click(function(){
		cur_page = parseInt($(this).attr('id').split('_')[2]);
		
		var nav_page = cur_page+1;
		$('#pg_nav').html(phrase_page+' '+nav_page+' '+phrase_of+' '+pages);
		
		$('ul.paging li').removeClass('active');
		$('#pg_page_'+cur_page).addClass('active');
		
		var obj_photos = '';
		var start = cur_page * photos_per_page;
		
		for(var pgi = start; pgi < photos.length; pgi++)
		{
			if ( (photos_per_page * cur_page)+photos_per_page > pgi )
			{
				obj_photos += '<div style="float: left;"><img onclick="setPhoto('+pgi+');" class="pg_photo_item" id="photo_'+pgi+'" style="cursor: pointer;'+thumb_size_style+'" src="'+files_url+photos[pgi][0]+'" alt="'+photos[pgi][2]+'" title="'+photos[pgi][2]+'" /></div>';
				if ( (pgi+1)%photo_columns == 0 )
				{
					obj_photos += '<div class="clear"></div>';
				}
			}
		}

		$('#photos_area').html(obj_photos);
		$('#photo_'+start).addClass('pg_active_photo');
		$('#lager_'+current_id).hide();
		$('#lager_'+start).show();
		
		if ( !$.browser.msie )
		{
			$('#pg_load_mask').animate({opacity: 0.5});
		}
		
		$('#main_photo img').attr({src: files_url+photos[start][1], alt: photos[start][2], title: photos[start][2]});
		$('#main_photo').attr({accessKey: files_url+photos[start][1], title: photos[start][2]});
		$('#main_photo img').load(function(){
			$('#pg_load_mask').stop();
			
			var main_photo_height = $.browser.msie ? $('#main_photo img').attr('height') + 'px' : $('#main_photo img').css('height');
			var main_photo_margin = '-'+main_photo_height;

			$('#pg_load_mask').css({height: main_photo_height}).css('margin-top', main_photo_margin);
			$('#pg_load_mask').animate({opacity: 0});
		});
		
		current_id = start;
	});

	$('#gallery a.large_item').lightBox({fixedNavigation: true});
});

var setPhoto = function(current_photo_id){
	$('#photo_'+current_id).removeClass('pg_active_photo');
	$('#lager_'+current_id).hide();
	$('#photo_'+current_photo_id).addClass('pg_active_photo');
	$('#lager_'+current_photo_id).show();

	if (current_id != current_photo_id)
	{
		if ( $.browser.msie || $.browser.opera)
		{
			$('#main_photo img').attr({src: files_url+photos[current_photo_id][1], alt: photos[current_photo_id][2], title: photos[current_photo_id][2]});
			$('#main_photo').attr({accessKey: files_url+photos[current_photo_id][1], title: photos[current_photo_id][2]});
		}
		else
		{
			$('#pg_load_mask').animate({opacity: 0.5});
	
			var obj_main_photo = '<img style="width: '+main_photo_width+'px" src="'+files_url+photos[current_photo_id][1]+'" title="'+photos[current_photo_id][2]+'" alt="'+photos[current_photo_id][2]+'" />';
			$('#tmp_area').html(obj_main_photo);
			$('#tmp_area img').load(function(){
				$('#pg_load_mask').stop();
	
				$('#main_photo img').attr({src: files_url+photos[current_photo_id][1], alt: photos[current_photo_id][2], title: photos[current_photo_id][2]});
				$('#main_photo').attr({accessKey: files_url+photos[current_photo_id][1], title: photos[current_photo_id][2]});
				
				var main_photo_height = $.browser.msie ? $('#main_photo img').attr('height') + 'px' : $('#main_photo img').css('height');
				var main_photo_margin = '-'+main_photo_height;

				$('#pg_load_mask').css({height: main_photo_height}).css('margin-top', main_photo_margin);
				$('#pg_load_mask').animate({opacity: 0});
			});
		}
		current_id = current_photo_id;
	}
}