


var loadImageToStage = function (obj){
	var src = $(obj).attr('rel') ;
	var id = $(obj).attr('id');
	
	if (src=='') {
		$(".stage").html("Loading...") ;
		
		$.ajax({
			url:document.URL + "?vid="+$(obj).attr('vid'),
			success: function(data){
				$(".stage").html(data) ;
			},
			error: function(){
				alert("Internal Error") ;
			}
		
		}) ;
	} else { 
	var img_src = $(".stage").children('img').attr('src') ;
	
		$(".stage").html("<img src='"+src+"' rel= '"+id+"' onclick='nextImage(this)' alt='' title='' />") ;
		$(".stage").css('background','url('+img_src+') no-repeat');
		$(".stage img").load(function(){
			//$(".stage").css('background','');
			$(this).animate({opacity: 1},400);
		})
	}
}
var scr_api ;
$(document).ready(function(){
	$('.project-gallery-img-container').css('background','url(/images/ajax-loader-grey.gif) no-repeat center');
	imagesPreloader();
	
});






function initScroller ( _anime , img_container ){
	
	img_container = img_container - 2;
	
	$(".project-gallery").scrollable({  circular: false,
	
										keyboard:_anime,
										touch: _anime,
										// assign left/right keys to the actively viewed scrollable
										onSeek: function(event, i) {
											SetPointsVisible(i , img_container, event.type);
										}
	
	});
	
	
	
	
	
	
	scr_api = $(".project-gallery").data('scrollable') ;
	
	
	$(".scr_prev").hide() ;
	$(".scr_page").css('text-decoration','none') ;
	$(".scr_page_0").css('text-decoration','underline') ;
	if(scr_api.getSize() < 4)
	{
		$(".scr_next").hide() ;
	}
	
	
	
}


/*
*
* SetPointsVisible
*
* this function called from both places
*
*  first jquery.tools.min.js
*  seccond scripts.js
*
*/

function SetPointsVisible(i , img_container, _type){
	
	var _all = img_container-1;
	
	if(i == img_container)
	{
		galleryScrollPrev(1);
	}
	else if(i == _all)
	{
		$(".scr_next").hide();
	}
	else if( i < _all && _type == 'onSeek')
	{
		$(".scr_next").show();
	}
	
	
	if(i == 1 &&  img_container >= 2 )
	{
		$(".scr_prev").show() ;
	}
	else if ( i == 0)
	{
		$(".scr_prev").hide() ;
	}
	
}

function galleryScrollNext (){

	if(scr_api.getIndex() != scr_api.getSize()-1){
		scr_api.next() ;
		if (scr_api.getIndex() == scr_api.getSize()-3)
			$(".scr_next").hide() ;
		$(".scr_prev").show() ;
		
		
	}
	
}

function galleryScrollPrev (_dont_show){
	if(scr_api.getIndex() != 0){
		scr_api.prev() ;
		if(scr_api.getIndex() == 0)
		$(".scr_prev").hide() ;
		if(_dont_show == '')
			$(".scr_next").show() ;
	}
	
}
function moveScrTo(offset){
	
	scr_api.seekTo(offset) ;

	if(scr_api.getIndex() == 0){
		$(".scr_prev").hide() ;
		$(".scr_next").show() ;
	}
	if (scr_api.getIndex() == scr_api.getSize()-2){
		$(".scr_next").hide() ;
		$(".scr_prev").show() ;
	}
	$(".scr_page").css('text-decoration','none') ;
	$(".scr_page_"+scr_api.getIndex()).css('text-decoration','underline') ;
	
}
var _image = new Array();
var _storeg = new Object();
var imagesPreloader = function()
{
	
     var _image = new Array();
	 if (document.images)
	 {
		 for(var i = 0 ; i < document.images.length ; i++)
		 {
			
			_image[i] = new Image();
			_storeg[document.images[i].src] = i;
			_image[i].onerror = function()
			{
			  
			  var _el = document.images[_storeg[this.src]];

			  _el.src= '/images/logo_logo.jpg';
			  $(_el).fadeIn(1000);
			}
			_image[i].onload = function()
			{
			
			
			        var self = this;
					_el = document.images[_storeg[this.src]];
					//$(_el).fadeOut(500,function(){ _el.src= self.src;
					//$(_el).fadeIn(1000);});
					//_el.src= self.src;

					$(_el).animate({opacity: 1},1000);
					//console.log(_el)
					
			};
			_image[i].src = document.images[i].src;
			//document.images[i].src = '/images/loading.gif';
			
		 }
	 }
}
var first_image_id = 0;
var _current = 0;

var nextImage = function(_obj){

	
	var _curId = $(_obj).attr('rel');
	
	if(_current ==0)
	{
		first_image_id = _curId;
	}
	_current++;
	
	
	
	
	var _curElem = $('.items').find('#' + _curId);

	var _next = _curElem.parent().next().children('a');
	
	if(_obj.loops && _obj.loops == 1)
		_next = _curElem;
		
	if(_next[0])
		loadImageToStage(_next);
	else
	{
		var _next = _curElem.parent().parent().next().children().children('a');
		if(_next[0])
			loadImageToStage(_next);
		else
		{
			var _obj   = new Object();
			_obj.rel   = first_image_id;
			_obj.loops = 1;
			
			nextImage(_obj);
			
		}
		
	}
	//console.log(_next);
}

setTimeout('_opacity()',3500);

function _opacity(){

	$('img').each(function(e,i){
	
		$(this).animate({opacity: 1},100);
		
	});
	

}

