$(window).load(function(){
	//Image Gallery
	$('.img-viewport span, .lightbox-content-stage span').html('');
	/*Variable Declaration*/
	var temp_img = new Image();
	var $ul = $('.slider-content ul');
	var $img_viewport = $('.img-viewport');
 	var len_images = $ul.find('img').length;
	var viewport_width = 466;
	var viewport_height = 311;
	var clearTime;
	var $lightbox_wrapper = $('#lightbox-wrapper');
	
	
	/*Methods*/
	/*Method to scroll the slider*/
	function scrollUL(dir){
		var curULMarginLeft = parseInt($ul.css('marginTop'));
		if(dir=='top' &&  (-curULMarginLeft) <  $ul.height() - viewport_height){
			$ul.css('marginTop', (curULMarginLeft - 3) + 'px');
		}
		if(dir=='bottom' &&  curULMarginLeft < 0 ){
			$ul.css('marginTop', (curULMarginLeft + 3) + 'px');
		}
		clearTime = window.setTimeout(function(){scrollUL(dir)},15);
	}


	/*Method to re-scaling image if its dimension exceeds the container's dimension*/
	function resizeImg(elem_img,width,height)
	{
		if($(elem_img).width() > width)
			$(elem_img).width(width);
		if($(elem_img).height() > height)
			$(elem_img).height(height);
	}


	/*Method to populate and open Lightbox*/
	function populateLightbox(obj)
	{
		$lightbox_wrapper.find('.lightbox-content-stage img').remove();
		$('<img src="'+obj.imgURL+'" alt="Image Gallery" />').prependTo($lightbox_wrapper.find('.lightbox-content-stage'));
		$lightbox_wrapper.find('p.caption').html(obj.caption);
		$('#lightbox-wrapper').show(); //This is mandatory so that JS can calculate dimensions of the img tag, bcos img tag is under a hidden container
		
		resizeImg($lightbox_wrapper.find('.lightbox-content-stage img')[0],lb_viewport_width,lb_viewport_height)
	}
	
	
	$img_viewport.find('.underlay').add('#lightbox-wrapper .overlay').css('opacity',0.7);
	$ul.height(len_images*78);
	
	$ul.find('img').click(function(){
		if(galleryType=='image')
		{
			$img_viewport.find('.stage img').remove();
			$('<img src="'+ $(this).attr('src') +'" alt="'+$(this).attr('alt')+'" width="466" />').prependTo($img_viewport.find('.stage'));
			$img_viewport.find('p').html($(this).attr('alt'));
			$ul.find('img.sel').removeClass('sel');
			$(this).addClass('sel');
	
			resizeImg($img_viewport.find('.stage img')[0],viewport_width,viewport_height);
		}
	}).eq(0).click();
	
	$('.prev').hover(function(){scrollUL('top');},function(){clearTimeout(clearTime)});
	$('.next').hover(function(){scrollUL('bottom');},function(){clearTimeout(clearTime)});
	

	
});
