﻿/* Previewbox library */
/* Last edited on: 26-02-10 */

/* Creating Previewbox namespace */

var Previewbox = null;

$(function() {
	Previewbox = new Previewbox();
});

/* Creating Previewbox namespace */

Previewbox = function() {

	/* Global variables */
	var parent = this;	
	this.targetsList = [];
	this.container = "body";
	this.previewbox = null;
	this.defaultW = null;
	this.defaultH = null;
	this.currentEl = null;
	
	/* Customisation variables */
	this.blockMaxW = 900;
	//this.blockMaxH = 600;
	this.blockMaxH = 720;
	
	/* Text variables */
	this.closeText = "Закрыть";
	
	/* Cached objects */
	this.cpContent = null;
	this.cpContainer = null;
	this.cimageGallery = null;
	
	/* Initialisation */
	this.setup();
},

Previewbox.prototype = {
	setup: function() {
		this.captureLinks();
		this.createDom();
		this.setListeners();
	},
	
	captureLinks: function() {
		var parent = this;
		
		$("a").each(function() {
			var el = $(this);
			if(el.attr("rel") && el.attr("rel").indexOf("previewbox") != -1) {
				var dummyVar = $(this).attr("href").split("/");
				dummyVar = (dummyVar[dummyVar.length-1] == "") ? dummyVar[dummyVar.length-2] : dummyVar[dummyVar.length-1];
				
				var options = (el.attr("rel").split("previewbox[")[1] == undefined) ? null : el.attr("rel").split("previewbox[")[1].split("]")[0];
				
				if(options != "cancel_url") {
					if(dummyVar.split(".")[1] == undefined) {
						parent.targetsList.push({
							el: this,
							type: "ajax",
							options: options
						});
					} else {
						parent.targetsList.push({
							el: this,
							type: "img",
							options: options
						});
					}
				} else {
					el.data("href", el.attr("href"));
					el.removeAttr("href").removeAttr("rel").css("cursor", "pointer");
				}
			}
		});
		
		this.targetsList = $(this.targetsList);
		this.targetsList.each(function() {
			var el = $(this.el);
			var type = this.type;
			var options = this.options;
			el.data("href", el.attr("href"));
			el.removeAttr("href").removeAttr("rel").css("cursor", "pointer");
			el.click(function() {
				parent.onClick(this, type, options);
			});
		});
	},
	
	createDom: function() {
		this.previewbox = $("<div>").html('<div class="p-bg"></div>'+
			'<div id="p-container" class="p-container">'+
				'<a class="p-close" onclick="Previewbox.hide();"><span>Закрыть</span></a>'+
				'<div class="p-content-container">'+
					'<div id="p-content" class="p-content"></div>'+
					'<div class="p-preloader"></div>'+
				'</div>'+
				'<div class="p-t"><div></div></div>'+
				'<div class="p-r"><div></div></div>'+
				'<div class="p-b"><div></div></div>'+
				'<div class="p-l"><div></div></div>'+
			'</div>').addClass("previewbox").addClass("launch").hide().appendTo(this.container);
		this.cpContent = $("#p-content");
		this.cpContainer = $("#p-container");
	},
	
	setListeners: function() {
		var parent = this;
		
		/* Preloader */
		this.previewbox.bind("loadStart", function() {
			parent.startPreloading();
		}).bind("loadStop", function() {
			parent.stopPreloading();
		});
	},
	
	getContent: function(url, type, options, params, callback) {
		var parent = this;
		var date = new Date();
		
		switch(type) {
			case "ajax":
					switch(options) {
						case "image-gallery":
							parent.createGallery(url);
						break;
						case "method:post":
							$.post(url, params, function(data) {
								parent.cpContent.html(data);
								if(callback) { callback(); }
								parent.setSize(".previewbox .p-content .p-dimensions");
							});
						break;
						default:
							$.get(url, function(data) {
								parent.cpContent.html(data);
								if(callback) { callback(); }
								parent.setSize(".previewbox .p-content .p-dimensions");
							});
					}
					
			break;
			case "img":
					parent.cpContent.empty().append($("<img>").attr("src", url+"?uid="+date.getTime()).load(function() {
						parent.setSize(".previewbox .p-content img");
					}));
			break;
		}
	},
	
	createGallery: function(url) {
		try {
			this.cimageGallery = new ImageGallery(this);
			this.cimageGallery.build(url);
		} catch(e) {
			Previewbox.hide(function() {
				alert('"Image Gallery" module is not defined');
			});
		}
	},
	
	setSize: function(selector, callback, obj) {
		var parent = this;
		var selector = $(selector);
		if(selector.length > 0) {
			var container = this.cpContainer;
			this.defaultW = (!this.defaultW) ? container.width() : this.defaultW;
			this.defaultH = (!this.defaultH) ? container.height() : this.defaultH;
			
			if(selector.width() > selector.height()) {
				if(selector.width() > this.blockMaxW) {
					selector.width(this.blockMaxW);
				}
			} else {
				if(selector.height() > this.blockMaxH) {
					selector.height(this.blockMaxH).css("overflow-y", "scroll");
				}
			}
			
			var width = (selector.css("width").split("px")[0] < 100) ? 100 : selector.css("width").split("px")[0];
			var height = (selector.css("height").split("px")[0] < 75) ? 75 : selector.css("height").split("px")[0];
			
			width = (width == "auto") ? selector.width() : width;
			height = (height == "auto") ? selector.height() : height;
			
			var mtop = (this.currentEl.offset().top < height/2) ? this.currentEl.offset().top-50 : height/2;
			
			container.animate({
				"width": width+"px",
				"height": height+"px",
				"marginLeft": -(width/2)+"px",
				"marginTop": -(mtop)+"px"
			}, "fast", function() {
				parent.previewbox.trigger("loadStop");
				if(callback) { callback(obj); }
			});
		}
	},
	
	onClick: function(el, type, options) {
		this.show(el, type, options, null, null);
	},
	
	show: function(el, type, options, params, callback) {
		var parent = this;
		var el = $(el);
		this.currentEl = el;
		
		if(this.previewbox.hasClass("launch")) {
			this.previewbox.children().filter(".p-container").css({
				"marginTop": "-50px",
				"marginLeft": "-50px"
			});
			this.previewbox.removeClass("launch");
		}
		this.previewbox.children().filter(".p-container").animate({
			"top": ((Math.round(el.offset().top+(el.height()/2)) < 0) ? 50 : Math.round(el.offset().top+(el.height()/2)))+"px"
		}, "slow");
		
		this.previewbox.trigger("loadStart").fadeIn("slow", function() {
			parent.getContent(el.data("href"), type, options, params, callback);
		});
	},
	
	hide: function(callback) {
		var parent = this;
		this.previewbox.addClass("closing").fadeOut("slow", function() {
			$(this).removeClass("closing");
			parent.resetState();
			if(callback) { callback(); }
		});
	},
	
	openUrl: function(url, callback) {
		var el = $("<div>").data("href", url);
		this.show(el, "ajax", null, null, callback);
	},
	
	openUrlfromDom: function(linkEl, callback) {
		var el = $(linkEl);
		var dummyUrl = el.data("href").split("?");
		el.data("href", dummyUrl[0]+"/ajax?"+dummyUrl[1]);
		this.show(el, "ajax", null, null, callback);
	},
	
	submitForm: function(form, callback) {
		var el = $(form)
		var returnObj = {};
		var sendEl = $("<div>").data("href", el.attr("action")+"/ajax");
		
		el.attr("id", "previewbox-tmp-class-form");
		$("#previewbox-tmp-class-form input").each(function() {
			var inputEl = $(this);
			if(inputEl.attr("name") != "") {
				returnObj[inputEl.attr("name")] = inputEl.val();
			}
		}).removeAttr("id");
		
		this.show(sendEl, "ajax", "method:post", returnObj, callback);
	},
	
	catchUrls: function(selector) {
		var parent = this;
		$(selector).each(function() {
			var el = $(this);
			el.data("href", el.attr("href"));
			el.removeAttr("href").removeAttr("rel").css("cursor", "pointer");
			el.click(function() {
				parent.hide();
				parent.show(el, "ajax", null, null, null);
			})
			
		});
	},
	
	resetState: function() {
		this.cpContainer.css({
			"width": this.defaultW,
			"height": this.defaultH,
			"marginLeft": -(this.defaultW/2)+"px",
			"marginTop": -(this.defaultH/2)+"px"
		});
	},
	
	startPreloading: function() {
		this.previewbox.addClass("loading");
	},
	
	stopPreloading: function() {
		this.previewbox.removeClass("loading");
	}
}
