﻿Sportpark = {};

Sportpark.Lightbox = function() {
	var options = ({
		width: '500px',
		height: '500px'
	}, arguments[0] || { });
	
	jQuery.extend(this, options);
	
	this.lightbox = null;
	this.bg = null;
	this.container = null;
	this.content = null;
	this.spinner = null;
	this.cururl = null;
	
	this.dimW = '';
	this.dimH = '';
	
	if(this.width != 'auto') {
		this.dimW = (this.width.indexOf('%') != -1) ? '%' : 'px';
		this.width = this.width.split('px')[0].split('%')[0];
	}
	if(this.height != 'auto') {
		this.dimH = (this.height.indexOf('%') != -1) ? '%' : 'px';
		this.height = this.height.split('px')[0].split('%')[0];
	}

	this.setup();
}

Sportpark.Lightbox.prototype = {
	setup: function() {
		this.createDom();
	},
	
	createDom: function() {
		var lightbox = $('<div class="lightbox"><img class="lightbox-spinner" src="/site/images/spinner.gif" /><div class="lightbox-bg"></div><div class="lightbox-container" style="width:'+((this.width) ? this.width+this.dimW : '500px')+'; height:'+((this.height) ? this.height+this.dimH : 'auto')+'; margin-left:-'+((this.width) ? Math.round(this.width/2)+this.dimW : '250px')+';"><div class="lightbox-bgc"></div><div class="lightbox-content"><a class="lightbox-close" onclick="photolightbox.hide();">закрыть окно</a><div class="lightbox-indent"></div></div></div></div>').appendTo('body');
		this.lightbox = $('.lightbox');
		this.bg = $('.lightbox-bg');
		this.container = $('.lightbox-container');
		this.content = $('.lightbox-indent');
		this.spinner = $('.lightbox-spinner');
		
		this.lightbox.css('left','-10000px');
		this.bg.css('left','-10000px');
		this.bg.fadeTo(1, 0.0);
	},
	
	show: function(url) {
		this.lightbox.css('left','0px');
		this.bg.css('left','0px');
		var parent = this;
		this.bg.fadeTo("slow", 0.75, function () {
        	parent.getData(url);
      	});
	},
	
	getData: function(url) {
		var spinner = this.spinner;
		var parent = this;
		this.cururl = url;
		spinner.show();
		this.content.load(url, '', function(){
			spinner.hide();
			parent.showcont();
			parent.setListeners();
		});
	},
	
	sendData: function(url) {
		var spinner = this.spinner;
		var parent = this;
		spinner.show();
		
		var controller = this;
		$.post(url, this.harvestData(), function(content) { controller.dataSendResultHandler(content); }, 'html');
	},
	
	dataSendResultHandler: function(content) {
		
		console.log(content);
		
		this.content.empty();
		this.content.append($(content));
		
		this.spinner.hide();
		this.showcont();
		this.setListeners();
	},
	
	harvestData: function() {
		var returnObj = {};
		var dummy = $('input[name], textarea[name]');
		
		for(var i=0; i<dummy.length;i++) {
			returnObj[dummy[i].name] = dummy[i].value;
		}
		
		return returnObj;
	},
	
	showcont: function() {
		var parent = this;
		this.container.fadeIn("slow", function() {
			parent.fixHeight();
		});
	},

	setListeners: function() {
		var parent = this;
		try {
			var next = $("#foto-next").attr("href");
			$("#foto-next").removeAttr("href");
			$("#foto-next").click(function() {
				parent.getData(next);
			});
		} catch(error) {
			//throw(error);
		}
		try {
			var prew = $("#foto-back").attr("href");
			$("#foto-back").removeAttr("href");
			$("#foto-back").click(function() {
				parent.getData(prew);
			});
		} catch(error) {
			//throw(error);
		}
		try {
			var submit = $("#foto-submit").attr("href");
			$("#foto-submit").removeAttr("href");
			$("#foto-submit").click(function() {
				parent.sendData(submit);
			});
		} catch(error) {
			//throw(error);
		}
	},
	
	fixHeight: function() {
		/*if(this.container.height() > this.bg.height()) {
			this.container.css('top','100px');
		} else {
			this.container.css('top','50%');
			this.container.css('margin-top','-'+Math.round(this.container.height()/2)+'px');
		}*/
		this.container.css('top','100px');
	},
	
	hide: function() {
		var parent = this;
		this.container.fadeOut("fast", function () {
        	parent.bg.fadeTo("fast", 0.0, function () {
	        	parent.lightbox.css('left','-10000px');
				parent.bg.css('left','-10000px');
	      	});
      	});
	}
};

