/*
 * Lightbox and paging for images on blizzard.com
 *    sV = screenViewer  
 */
var replacebr 

var sV = {
	//initialize the screen viewer
	initialized: false,
	frame: null,
	frameContent: null,	
	currentIndex: 0,
	imageCollection: null,	
	loader: null,
	t:null,
	init: function()
	{	
		//store references
		sV.frame   = $j("#screenViewer");
		sV.content = $j("#viewerContent");		
		sV.loader  = $j("#loadingAnimation");		
				
		//set top position
		$(sV.frame).css("top",$j(window).scrollTop() + "px")
		
		//show blackout and image
		$j(sV.frame).show();
		
		if(Browser.ie6 || Browser.ie7){ $j("#blackout").show(); $j("#blackout").height($j(document).height())}
		else  $j("#blackout").fadeIn(1000);	
		
		//only image collections have paging
		if(sV.imageCollection)
			sV.enablePaging();						
		else
			sV.disablePaging();					
		
		sV.initialized = true;
	},
	//go to next image
	nextImage: function()
	{	if(sV.t) return;
		//get proper index
		sV.currentIndex = sV.imageIndex("next");

		//set image
		sV.setImage(sV.imageCollection[sV.currentIndex]);
		
		//preload next image
	//	var preloadNextImage = new Image();
	//	preloadNextImage.src = sV.imageCollection[sV.imageIndex("next")];
	},
	//go to previous image
	prevImage: function()
	{
		//get proper index
		sV.currentIndex = sV.imageIndex("prev");

		//set image
		sV.setImage(sV.imageCollection[sV.currentIndex]);
	},
	//determine proper index of image collection
	imageIndex: function(direction)
	{
		if(direction == "next")
			return (sV.currentIndex+1 >= sV.imageCollection.length) ? 0 : sV.currentIndex+1;
		else if(direction == "prev")
			return (sV.currentIndex-1 < 0) ? sV.imageCollection.length-1 : sV.currentIndex-1;
	},
	setVideo : function (title, width, height)
	{
		//set title
		$j("#viewerTitle").html(title);
		
		if(!sV.initialized){
			sV.init();
			
			//get video list and append it
			var videoList = $j("#video_list").clone();
			videoList.show();
			
			$j(sV.content).append(videoList);
		}
		
		//set dimensions (add 180 pixels for bottom thumbnails)
		sV.setContainerDimensions(width,(height*1 + 200));	
	},
	showImage: function(src)
	{
		if(!sV.initialized)
			sV.init();
		sV.setImage(src);
	},	
	setImageCollection: function(imageCollection,index,captionList)
	{
		//set image collection
		sV.imageCollection = imageCollection;
		if(captionList) sV.captionList = captionList;
		
		if(!sV.initialized)
			sV.init();
		
		//set index
		sV.currentIndex = (index) ? index : 0;
			
		//set the image
		sV.setImage(sV.imageCollection[sV.currentIndex]);
	},
	setImage: function(src)
	{
		//create new image so we can tell when its done loading
		var image = new Image();
		image.src = src;
		
		//show loader only if image isnt already loaded
		if(!image.complete)
			$j(sV.loader).show();

		sV.loadImage(image);	
	},
	loadVideo: function(name, width, height, clickedLink, title)
	{
		//strip other thumbails of selected style
		if(clickedLink){
			$j(".videoThumbnail").removeClass("selected");
			$j(clickedLink).addClass("selected");
		}
		
		var gameType = gt;
		
		var videoArray = gameType + "-" + name + "-" + locale + ":" + gameType + "-" + name + ":" + "games/" + gameType + "/" + name; 

		var flashvars = {
			"vidArr":    videoArray, 
			"skincolor": skincolor||"00080d:30a6f1:142E47",
			"rating":    "",
			"autoplay":  "false"
		};

		var params = {
			menu:              "false", 
			base:              ctx + "movies/player/", 
			allowFullScreen:   "true", 
			allowScriptAccess: "always", 
			bgcolor:           "000000"
		};
		
		var attributes = {
			id:   "videoContainer", 
			name: "videoContainer"
		};
		
		
		sV.setVideo(title, width, height);

		swfobject.embedSWF(ctx + "movies/player/video_loader2.swf", "videoContainer", width, Number(height) + 30, "8","expressInstall.swf", flashvars, params, attributes);
             
	},
	//load the image and set the dimensions
	loadImage: function(image)
	{
		if(image.complete)
		{	
			//once loaded, set dimensions and holder html
			sV.setContainerDimensions(image.width, image.height);			
			$j(sV.content).css("background-image","url("+image.src+")")
			$j(sV.loader).fadeOut(200);
			if(sV.captionList && sV.captionList[sV.currentIndex]) 
			{  if(replacebr) var captext = sV.captionList[sV.currentIndex].replace(/\<br\/\>|\<br\>/g,"<span class='lbreak'>"+replacebr+"</span>")
				$j("#viewerTitle").html(captext);  
				if(fanarthash && !Browser.mac) location.href = "#"+(sV.imageCollection.length-sV.currentIndex-1)
			}
			else $j("#viewerTitle").html('');

			clearTimeout(sV.t); sV.t = null
		}
		else
			sV.t=window.setTimeout(function() { sV.loadImage(image) } ,50);		
	},
	//set height and width of container
	setContainerDimensions: function(width,height)
	{
		$j(sV.frame).css("height",height + "px").css("width",width + "px");
		$j(sV.content).css("height",height + "px").css("width",width + "px");
	},
	//enable paging functionality and styles
	enablePaging: function()
	{
		//image collections have paging
		$j(sV.content).click(function(){
			sV.nextImage();
		});
		
		$(sV.frame).addClass("viewerPaging");
	},
	//disable paging functionality and styles
	disablePaging: function()
	{		
		//unbind click
		$j(sV.content).unbind("click");
		
		$(sV.frame).removeClass("viewerPaging");		
	},	
	//close the preview frame
	close: function()
	{
		$j(sV.frame).hide();
		$j("#blackout").hide();
		
		//clear background image and remove children
		$j(sV.content).css("background-image","none").empty();
		clearTimeout(sV.t); sV.t = null
		
		//create video container div to re-append
		var videoContainer = document.createElement("div");
		videoContainer.id = "videoContainer";
		$j(sV.content).html(videoContainer);
		
		//set initializers to false
		init = false;
		sV.initialized = false;
		sV.imageCollection = null;
	}	
};