var linkclass=".light_box"
var lightboxImages=[];
var counter=0;
var opened=false;
var defaultSize = {width:300,height:300};
$(document).ready(function(){
						  
						   $(linkclass).each(function(n){
													  
														  lightboxImages[n] = $(this).attr('href');
														  $(this).attr('id','light-box_'+n);
														  $(this).bind('click',openLightBox);
													     
													    });
						   addLightBoxHTML();
						   
						   $('#MainLightBox #lightbox_image').bind('load',onLoadComplete);
						   $('#Close').find('a').eq(0).bind('click',closeLightBox);
						   
						   $('#prevLink').eq(0).bind('click',next_prev);
						   $('#nextLink').eq(0).bind('click',next_prev);
						   resizing();
						   
						   });

function openLightBox(e)
{
	e.preventDefault();
	counter=$(this).attr('id').split("_")[1];
	openImage();
	opened=true;	
}


function openImage()
{
	$('#MainLightBox #lightbox_image').hide();
	$('#MainLightBox #lightbox_image').css('opacity',0);
	
	
	$('#MainLightBox #lightbox_image').eq(0).attr("src",lightboxImages[counter]);	
	$('#MainLightBox').fadeIn();
	$('#img_text').html("LOADING IMAGE....");
}

function onLoadComplete()
{
  $('#img_text').html(Number(counter+1)+" of "+lightboxImages.length)
  W = $(window).width();	
  H = $(window).height();	
  $('#MainLightBox #lightbox_image').show();
  var div_resize_time=850;
  var hx = $('#MainLightBox #lightbox_image').height()
  var wx = $('#MainLightBox #lightbox_image').width()
  var left_amount = (W - wx)/2;
  
  h = $(document).height();
																			 
 if(hx<H){$('#MainLightBox').find('#bg-shadow').eq(0).css('height',H);}
 else
 {$('#MainLightBox').find('#bg-shadow').eq(0).css('height',Number(hx+50)); }
  
  
  $('#MainLightBox .img-cotroller').eq(0).animate({width:wx},div_resize_time);
  $('#MainLightBox #img-container').eq(0).animate({width:wx,left:left_amount},div_resize_time)
  
  
  
  $('#MainLightBox #imagebox').animate({height:hx,width:wx},div_resize_time,function(){
																											   
																			$('#MainLightBox #lightbox_image').animate({opacity:1},1000);  
																											   
																											   });
  
}

function addLightBoxHTML(){
var html_content="";

html_content = html_content+'<div id="MainLightBox" style="display:none; z-index:2000;">';
html_content = html_content+'<div id="bg-shadow"></div>';
html_content = html_content+'<div id="img-container">';
html_content = html_content+'<div class="img-cotroller">';
html_content = html_content+'<a href="#"><img src="images/prevlabel.gif" name="prevLink" border="0" id="prevLink" /></a>';
html_content = html_content+'<a href="#"><img src="images/nextlabel.gif" name="nextLink" border="0" id="nextLink" /></a> </div>';
html_content = html_content+'<div id="imagebox"><img id="lightbox_image"  /></div>';
html_content = html_content+'<div><span id="img_text">img1 of 2</span>';
html_content = html_content+'<span id="Close"><a href="#"><img src="images/closelabel.gif" width="66" height="22" border="0" /></a></span>';
html_content = html_content+'<br class="clearfix"/>';
html_content = html_content+'</div>';
html_content = html_content+'</div>';
html_content = html_content+'</div>';
$('body').append(html_content);
}

function resizing()
{
	div_resize_time = 550;
	W = $(window).width();
	H = $(window).height();
	$('#MainLightBox').find('#bg-shadow').eq(0).css('width',W);
	$('#MainLightBox').find('#bg-shadow').eq(0).css('height',H);
	$('#MainLightBox').find('#bg-shadow').eq(0).css('opacity',0.6);
	
	
	var wx = $('#MainLightBox #imagebox').width()															 
	var left_amount = (W - wx)/2;
	
	$('#MainLightBox #img-container').eq(0).animate({left:left_amount},div_resize_time)
	
	
	
}

function closeLightBox(e)
{
  
  e.preventDefault();	
  $('#MainLightBox').hide();
  opened=false;	
}


function next_prev()
{
  	switch($(this).attr('id'))
	{
	  
	  case "prevLink":
	  
	  if(counter>0)
	  {
	  counter--;
	  openNextImage();
	  }
	  break;
		
	  
	  case "nextLink":
	   if(counter<lightboxImages.length-1)
	  {
	  counter++;
	  openNextImage();
	  }
	  break;
	}
}


function openNextImage()
{
	$('#MainLightBox #lightbox_image').fadeOut(650,function(){
															     
    $('#MainLightBox #lightbox_image').css('opacity',0); 
	$('#MainLightBox #lightbox_image').css('z-index',20000); 
	$('#MainLightBox #lightbox_image').attr('src',lightboxImages[counter]);
	$('#img_text').html("LOADING IMAGE....");
	
	
	
															     });
}
