var sImages = {};
var requiredImg = 2;

function preloadImages(images) {
	for (var i in images) { 
		var img = new Image();
		img.src = "/data/photo/pic/" + images[i]["file"];	
		sImages[i] = img.src;
	}
}

var popup = {
	opened: false,
	wClass: "w-pop",
	lClass: "w-loader",
	img_int: false,
	h_int: false,
	hide: function() {
		$("."+this.wClass).css("display","none");
		$("."+this.wClass).addClass(this.lClass);
		$("."+this.wClass).html("");
		$("."+this.wClass).attr("style","");
		clearInterval(this.h_int);
		clearInterval(this.h_int);
	},
	
	show: function(img,name) {
		if (typeof name == 'undefined') name = '';
		if (this.opened) this.hide();
		this.opened = true;
		var i = new Image();
		i.src= img;
		$("."+this.wClass).css("display","block");
		
		getHTML = function() { 
			var w = i.width;
			var h = i.height;
//			if(h > 400) { w = Math.round(400/h * w); h = 400 }
//			else if(w > 600) { h = Math.round(600/w * w); w = 600; }
			
			var html = "<a href='#' onclick='popup.hide();return false'><img src='"+img+"' width='"+w+"' height='"+h+"' border='0' alt='Нажмите для закрытия' title='Нажмите для закрытия' /></a>"+name;
			var obj = $("."+popup.wClass);
			obj.css("width",w+"px");
			obj.css("height",h+"px");
			obj.css("margin-left",(-1)*Math.round(w/2)+"px");
			obj.css("margin-top",(-1)*Math.round(h/2)+"px");
			$("."+this.wClass).removeClass(this.lClass);
			obj.html(html);
			obj.css("height","auto");
			clearInterval(popup.img_int);
			clearInterval(popup.h_int);
		};

		this.img_int = setInterval(function(){ 
			if (!i.complete) i.src = img;
		},500);
		
		this.h_int = setInterval(function(){
			if (i.complete) getHTML();
		},500);
	}
}

function createSlideshow(images) { 
	var html = [], str="";
	if (images.length > requiredImg) {
		for (var i in images) { 
			str = "<a onclick=\"popup.show('/data/photo/pic/"+images[i]["file"]+"','"+images[i]["name"]+"');return false;\" href=\"/data/photo/pic/" + images[i]["file"]+ "\" title=\"" + images[i]["name"] + "\"><img style=\"display:block;margin:0 auto;height:100%;border:0;\" src=\"" + sImages[i] + "\" alt=\""+ images[i]["name"] + "\" title=\""+ images[i]["name"] + "\"/></a>";
			html.push(str);
		}
		html = html.join(""); 
		
		if (html != "") {
			$('#Slideshow').remove();
			$('#Slideshow-box').append("<div id=\"Slideshow\"></div>");
			$('#Slideshow').html(html);
		}
		
		if (html)
		$('#Slideshow').slideshow({
			width: 300,
			height: 240,
			index: 0,
			content: {
				'nextclick': false,	// bind content click next slide
				'playclick': false,	// bind content click play/stop
				'playframe': false,	// show frame "Play Now!"
				'imgresize': true,	// resize image to slideshow window
				'imgcenter': true,	// set image to center // TODO
				'imgajax'  : false,	// load images from links
				'linkajax' : false	// load html from links
			},
			controls: {				// show/hide controls elements
				'hide'   : false,		// show controls bar on mouse hover   
				'first'  : false,		// goto first frame
				'prev'   : true,		// goto previouse frame (if it first go to last)
				'play'   : true,		// play slideshow
				'next'   : true,		// goto next frame (if it last go to first)
				'last'   : false,		// goto last frame
				'help'   : false,		// show help message
				'counter': true		// show slide counter
			},
			slideshow: {
				'time' : 3000,		// time out beetwen
				'title': true,		// show title
				'panel': true,		// show controls panel
				'play' : true		// play slideshow

			}

		});
	}
}
