//var img_num;
//var th_size; //includes padding and margins
//var th_lrg_size; //includes padding and margins


function gallery(th_total,name)
{
	
	//alert(th_total);
	this.name = name;
	
	this.th_total = th_total;
	this.th_width = this.th_total * 90 + 30;
	if(th_total > 1)
		this.active = 2;
	else
		this.active = 1;
	this.offset_val = 85;
	this.offset = 0;
	$("#" +this.name+"_th_ul").css({width:this.th_width + "px"});
	//$("#first_th_ul").css({width:"800px"});
	this.move_th_left = function( el ) //next
	{
		
		var num = this.th_total;
		if(this.active < num)
		{
			var img = $("#" + this.name + "_img_" + this.active);
			img.animate( { width:60, height:60 }, 600 );
			
			this.offset -= this.offset_val;
			this.active += 1;
			//if( ( this.active + 1 ) == this.th_total)
			{
				//$(el).animate( { left:this.offset }, 600 );
			}
		}
		if(this.active < ( num ) )
		{	
			$(el).animate( { left:this.offset }, 600 );
		}	
	}
	
	this.move_th_right = function( el ) //previous
	{
		//alert("test1");
		//alert(this.active);
		if(this.active > 1)
		{
			var img = $("#" + this.name + "_img_" + this.active);
			//alert(img.attr("id"));
			img.animate( { width:60, height:60 },600 );
			this.offset += this.offset_val;
			this.active -= 1;
			//if( ( this.active + 1) != this.th_total)			
		}
		if(this.active >= 2 )
		{
			$(el).animate( { left:this.offset}, 600 );
		}
	}
	
	
	this.fill_large_img = function( el ) { 	
		$(el).fadeOut();
		
		var img = $("#" + this.name + "_img_" + this.active);
		
		img.animate( { width:80, height:80 },600 );
		
		setTimeout("change_img('"+this.name+"','"+this.active+"')",400);
		//el.innerHTML = "<img src="+src+" >";
		$(el).fadeIn();
	}
	
	
	this.set_events = function(gallery) {
		
		var el_ul = document.getElementById(name + "_th_ul");
		var el_large = document.getElementById(name + "_large_area");
		if(th_total > 3 )
		{
			document.getElementById( this.name + "_btn_prev" ).onclick = function(  )
			{ 	
				gallery.move_th_left( el_ul );
				gallery.fill_large_img( el_large );
				
			}
			
			
			document.getElementById( this.name + "_btn_next" ).onclick = function( )
			{
				gallery.move_th_right( el_ul );
				gallery.fill_large_img( el_large );
				
			}
		}
		$("#" + name + "_th_ul li img").click(function(){
			
			var img = $("#" + gallery.name + "_img_" + gallery.active);
			img.animate( { width:60, height:60 },600 );
			var id = this.id.split( "_" );			
			id = parseInt(id[2]);
			
			if(id > gallery.active)
			{
				gallery.move_th_left(el_ul)
			}
			else if(id < gallery.active)
			{
				gallery.move_th_right(el_ul)
			}
			
			
			
			//gallery.active = parseInt( id[2] );
			gallery.fill_large_img( el_large );
			
		});
	}
	document.getElementById("gallery_main").style.display = "block";	
	$("#gallery_temp").remove();	
}

function change_img(name, active)
{
	
	var img = $("#" + name + "_img_" + active);
	var src = img.attr( "src" );
	document.getElementById( name + "_large_area" ).innerHTML = "<a class='dev_enlarge' rel='#enlarge_img' title='Gallery Image' href='#enlarge_img'><img src="+src+" style='border:1px solid #BBBBBB; padding:3px;'></a>";
	src = src.replace(/d=200/g, "d=-1");
	document.getElementById( "enlarge_img" ).innerHTML = "<img id='enlarge_imgage' src="+src+" style='border:1px solid #BBBBBB; padding:3px;'>";
	
	
}



