var gallery = {
	initialize: function(images) {
		this.images = undefined;
		this.updateGallery;
		this.images = images;
		this.current = 0;
		this.length = images.length;
		if (this.images == undefined) {
			$('idPopup4Total').innerHTML = 0;
		} else {
			$('idPopup4Total').innerHTML = this.length;
		}
		this.updateGallery();
	},
	
	updateGallery: function() {
		if (this.images == undefined) {
			$('idPopup4Current').innerHTML = 0;
			$('idPopup4ImgBig').src = '';
			for (var i = 1; i <= 5; ++i) {
				$('idPopup4ImgSmall' + i).src = '';
				$('idPopup4ImgSmall' + i).style.visibility = "hidden";
				$('idPopup4ImgSmall' + i + "Link").onclick = undefined;
			}
		} else {
			$('idPopup4Current').innerHTML = this.current + 1;
			$('idPopup4ImgBig').src = this.images[this.current].link;
			$('idPopup4ImgBig').alt = this.images[this.current].alt;
			$('idPopup4ImgBig').title = this.images[this.current].alt;
			var first;
			if (this.length <= 5) {
				first = 0;
			} else if (this.current == this.length - 1) {
				first = this.current - 4;
			} else if (this.current == this.length - 2) {
				first = this.current - 3;
			} else if (this.current == 0 || this.current == 1) {
				first = 0;
			} else {
				first = this.current - 2;
			}
			
			for (var i = first, counter = 1; i < Math.min(first + 5, this.length); ++i, ++counter) {
				$('idPopup4ImgSmall' + counter).src = this.images[i].thumb;
				$('idPopup4ImgSmall' + counter).alt = this.images[i].alt;
				$('idPopup4ImgSmall' + counter).title = this.images[i].alt;
				if (this.images[i].thumb != "") {
					$('idPopup4ImgSmall' + counter).style.visibility = 'visible';
					$('idPopup4ImgSmall' + counter + "Link").onclick = Function('gallery.select('+i+');');
				} else {
					$('idPopup4ImgSmall' + counter).style.visibility = 'hidden';
					$('idPopup4ImgSmall' + counter + "Link").onclick = undefined;
				}
			}
		}
	},
	
	next: function() {
		this.current++;
		if (this.current >= this.length) {
			this.current = this.length - 1;
		} else {
			this.updateGallery();
		}
		return false;
	},
	
	previous: function() {
		this.current--;
		if (this.current < 0) {
			this.current = 0;
		} else {
			this.updateGallery();
		}
		return false;
	},
	
	nextPage: function() {
		this.current += 5;
		if (this.current >= this.length) {
			this.current = this.length - 1;
		}
		this.updateGallery();
		return false;
	},
	
	previousPage: function() {
		this.current -= 5;
		if (this.current < 0) {
			this.current = 0;
		}
		this.updateGallery();
		return false;
	},
	
	select: function(id) {
		this.current = id;
		if (this.current < 0) {
			this.current = 0;
		}
		if (this.current >= this.length) {
			this.current = this.length - 1;
		}
		this.updateGallery();
		return false;
	}
}

/**
 * @param {Number} npcId
 * @param {Number} linkId
 * @param {Boolean} hide 
 */
function showDialogue(npcId, linkId, hide) {
	var data;
	if (linkId == undefined && npcId == undefined) {
		data = Object.toQueryString({
			nodata: 'nodata'
		});
	} else if (linkId != undefined) {
		data = Object.toQueryString({
			linkid: linkId
		})
	} else {
		data = Object.toQueryString({
			npcid: npcId
		})
	}
	
	var ajax = new Ajax('/plugins/dialogueajax.php?rand=' + Math.random(),
		{
			data: data,
			method: 'post',
			onSuccess: function(response) {
				this.json = Json.evaluate(response, true);
				if (!this.json) {
					alert('Ошибка.');
					return false;
				}
				if (!this.json.success) {
					if (this.json.message) {
						alert('Ошибка: ' + this.json.message);
					} else {
						alert('Неизвестная ошибка');
					}
					return false;
				}
				if (this.json.score != undefined){ 
					$('score').innerHTML = this.json.score;
				}
				if (this.json.friends != undefined){ 
					$('friendspics').innerHTML = this.json.friends;
				}
				if (this.json.perc != undefined){ 
					$('perc_'+this.json.npcid).innerHTML = this.json.perc;
				}
				switch (this.json.cuetype) {
					case 0:
					case "0": // Нет реплики
						break;
						
					case 1:
					case "1": //Обычный
						if (this.json.text == "") {
							$('idPopup1TextFrame').style.display = 'none';
						} else {
							$('idPopup1TextFrame').style.display = 'block';
							$('idPopup1Text').innerHTML = this.json.text;
						}
						if (this.json.description == "") {
							$('idPopup1DescriptionFrame').style.display = 'none';
						} else {
							$('idPopup1DescriptionFrame').style.display = 'block';
							$('idPopup1Description').innerHTML = this.json.description;
						}
						$('idPopup1NpcName').innerHTML = this.json.npcname;
						if (this.json.npcimage == "") {
							$('idPopup1NpcImage').style.display = 'none';
						} else {
							$('idPopup1NpcImage').src = this.json.npcimage;
							$('idPopup1NpcImage').style.display = 'inline';
						}
						$('idPopup1YourName').innerHTML = this.json.yourname;
						var answerscount = this.json.answers.length;
						var column1 = "";
						var column2 = "";
						var variant;
						var showstr;
						for (var i = 0; i < answerscount; ++i) {
							if (this.json.answers[i].terminal != "1") {
								showstr = "showDialogue(" + npcId + ", " + this.json.answers[i].link +  ", 0);";
							} else {
								showstr = "showDialogue(" + npcId + ", " + this.json.answers[i].link +  ", 1);";
							}
							variant = "<div class=\"n\">" + (i + 1) + ".</div>"
							        + "<div class=\"text\"><a href=\"#\" onclick=\"popup.hide({id:'idPopup1'});"
							        + showstr + "\">" 
							        + this.json.answers[i].linkname + "</a></div>";
							if (i >= answerscount / 2) {
								column2 += variant;
							} else {
								column1 += variant;
							}
						}
						$('idPopup1Column1').innerHTML = column1;
						$('idPopup1Column2').innerHTML = column2;
						if(hide !=1){
						popup.show({id:'idPopup1',t:50,o:'no'});}
						break;
						
					case 2:
					case "2": //Аудио
						$('idPopup3Title').innerHTML = this.json.title;
						$('idPopup3Frame').innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"350\" height=\"30\" id=\"ncpi_videoPlayer\" align=\"middle\">"
							+ "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"
							+ "<param name=\"allowFullScreen\" value=\"false\" />"
							+ "<param name=\"movie\" value=\"/webroot/delivery/swf/audio.swf?audio_file="
							+ this.json.link
							+ "\" />"
							+ "<param name=\"quality\" value=\"high\" />"
							+ "<param name=\"bgcolor\" value=\"#ffffff\" />"
							+ "<embed src=\"/webroot/delivery/swf/audio.swf?audio_file="
							+ this.json.link
							+ "\" quality=\"high\" bgcolor=\"#ffffff\" width=\"350\" height=\"30\" name=\"ncpi_videoPlayer\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />"
							+ "</object>";
						if(hide !=1){
						popup.show({id:'idPopup3',t:240,o:'no'});}
						break;
					
					case 3:
					case "3": //Видео
						$('idPopup2Title').innerHTML = this.json.title;
						$('idPopup2Frame').innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"350\" height=\"280\" id=\"ncpi_videoPlayer\" align=\"middle\">"
							+ "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"
							+ "<param name=\"allowFullScreen\" value=\"false\" />"
							+ "<param name=\"movie\" value=\"/webroot/delivery/swf/video.swf?video_file="
							+ this.json.link
							+ "\" />"
							+ "<param name=\"quality\" value=\"high\" />"
							+ "<param name=\"bgcolor\" value=\"#ffffff\" />"
							+ "<embed src=\"/webroot/delivery/swf/video.swf?video_file="
							+ this.json.link
							+ "\" quality=\"high\" bgcolor=\"#ffffff\" width=\"350\" height=\"280\" name=\"ncpi_videoPlayer\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />"
							+ "</object>";
						if(hide !=1){	
						popup.show({id: 'idPopup2',t:100,o:'no'});}
						break;
					
					case 4:
					case "4": //HTML
						$('idPopup5Title').innerHTML = this.json.title;
						$('idPopup5Description').innerHTML = this.json.description;
						if(hide !=1){
						popup.show({id:'idPopup5',t:80,o:'no'});}
						break;
					
					case 5:
					case "5": //Фотогалерея
						$('idPopup4Title').innerHTML = this.json.title;
						gallery.initialize(this.json.images);
						gallery.updateGallery();
						if (hide != 1) {
							popup.show({id:'idPopup4',t:10,o:'no'});
						}
						break;
						
					case 203:
					case "203": //Redirect
						document.location.href = this.json.location;
						break;
						
					default:
						//alert('Неизвестный тип');
				}
				return false;
			},
			onFailure: function() {
				//alert('Ошибочка вышла...');
			}
		}
	);
	ajax.request();
	return false;
}

function sendAction(action) {
	var ajax = new Ajax('/plugins/dialogueajax.php?rand=' + Math.random(),
		{
			data: Object.toQueryString({
				action: action
			}),
			method: 'post',
			onSuccess: function(response) {
			}
		}
	);
	ajax.request();
	return false;		
}
