var overlay = function () {
	
	var config = {
		  thumb : '.gallery_item_thumb'
		, background : '#overlay_bg'
		, overlay : '#overlay'
	},
	
    videoLoaded = false,
    
	priv = {
		
		setOverlay : function () {
			
			$(config.thumb).click( function () {
				
				$(config.thumb).removeClass('selected_item');
				$(this).addClass('selected_item');
				
			});
		},
		
		setCloseEventOnBg : function () {
			$(config.background).click( function () {
				overlay.close();
			});
		}

	};
	
	return {
		
		
		close : function () {
			$('#sample_img_carousel').hide();
			$('#flash_movie').hide();
			$(config.overlay).fadeOut( 400, function () {
				 $(config.background).hide();
			});
		},
		
		showSampleImages : function (){
			$('#overlay').css({width:'560px'});
			$(config.background).show();
			$(config.overlay).fadeIn(400);
			$('#sample_img_carousel').show();
		},
		
		showVideo : function (swfUrl,flvUrl){
			$('#overlay').css({width:'700px'});
			$(config.background).show();
			$(config.overlay).fadeIn(400);
			
			$('#flash_movie').show();
			
			if (videoLoaded == false) { 
			
				var flashvars = {};
				/*flashvars.video_server = '/';				
				flashvars.fullscreen_infobutton = "copy for info";*/
				flashvars.videofile = flvUrl;
				
				var params = {};
				params.play = "true";
				params.loop = "true";
				params.menu = "true";
				params.quality = "best";
				params.scale = "showall";
				params.bgcolor = "#000000";
				params.wmode = "opaque";
				params.devicefont = "true";
				params.allowfullscreen = "true";
				params.allowscriptaccess = "all";
				
				var attributes = {};
				attributes.id = "flash_movie";
				attributes.align = "middle";
				
				swfobject.embedSWF(
					swfUrl,
					"flash_movie",
					700,
					385,
					"9.0.115",
					"",
					flashvars,
					params,
					attributes
				);
				
				videoLoaded = true;
			
			}
		},
		
		initiate : function () {
			priv.setOverlay();
			priv.setCloseEventOnBg();
		}
	};
}();



var simpleCarousel = function(){
	
	var totalItems = 0;
	var current = 0;
	var imageWidth = 520;
	var wrapperId = "#sample_images_carousel"; 
	var containerId = "#sample_image_container";
	var items = [];
	
	return {
		create : function (imageWidth_){
			if (imageWidth_){
				imageWidth = imageWidth_;
			}
			//totalItems = total_;
			
			$(containerId).css({width:(imageWidth*totalItems)+'px'});
			$(wrapperId).css({width:imageWidth+'px'});
			
			for(var i = 0; i<items.length; i++){
				$(containerId).append('<div class="sample_image"><img  width="560" height="400" src="'+items[i]+'" /></div>');	
			}
		},
		
		next : function(){
			
			if (current+1 < totalItems) {
				current++;
				var src = document.getElementById('carouselBack').src.replace('inact.jpg', 'normal.jpg');
				jQuery('#carouselBack').attr('src', src);
			}
			
			if (current+1 == totalItems) {
				var src = document.getElementById('carouselForw').src.replace('hover.jpg', 'inact.jpg');
				jQuery('#carouselForw').attr('src', src);
			}
			
			
			
			$(wrapperId).animate({scrollLeft:current*imageWidth});
		},
		
		previous : function(){
			if (current-1 >= 0) {
				current--;
				var src = document.getElementById('carouselForw').src.replace('inact.jpg', 'normal.jpg');
				jQuery('#carouselForw').attr('src', src);
			}
			
			if (current == 0) {
				var src = document.getElementById('carouselBack').src.replace('hover.jpg', 'inact.jpg');
				jQuery('#carouselBack').attr('src', src);
			}
			
			
			$(wrapperId).animate({scrollLeft:current*imageWidth});
		},
		
		addImage : function(url){
			items.push(url);
			
			totalItems++;
		}
	}
}();

$(function () { 
	overlay.initiate(); 
	simpleCarousel.create(560);
});
