
jQuery.extend({
	ivva : {
		urlIncrementPhotoView: null,
		urlPhotoAdd: null,
		urlPhotoRemove: null,

		locationAnchor: {
			parsedQuery: null,
			isParsed: false,

			newQuery: null,
			isNewQueryInited: false,

			set: function(name, value) {
				if (!this.isNewQueryInited) this._init();
				this.newQuery = this.newQuery.set(name, value);
			},
			remove: function(name) {

			},
			get: function(name) {
				if (!this.isParsed) this._parse();
				return this.parsedQuery[name];
			},
			_parse: function() {
				var anchor = $.url.attr('anchor');

				if (anchor == null) {
					this.parsedQuery = [];
				} else {
					this.parsedQuery = $.parseQuery(anchor);
				}

				this.isParsed = true;
			},
			reparse: function() {
				this._parse();
			},
			_init: function() {
				this.newQuery = $.query.empty();

				if (!this.isParsed) this._parse();

				for(var i in this.parsedQuery) {
					this.newQuery = this.newQuery.set(i, this.parsedQuery[i]);
				}

				this.isNewQueryInited = true;
			},
			finish: function() {
				if (!this.isNewQueryInited) this._init();
				var query = this.newQuery.toString();
				window.location.hash = '#' + query;
			}
		},
		gallery : {
			workingWithPhotoSrc: null,
			cartButtonsSet: false,
			enableDetailButtons: true,

			incrementPhotoView: function(imagePath) {
				var url = $.ivva.urlIncrementPhotoView;
				if (imagePath == null || url == null) {
					return;
				}

				var ajaxOptions = {
					url: url,
					data: {'imagePath': imagePath},
					type: "post",
					dataType: "json"
				};

				$.ajax(ajaxOptions);
			},

			lightbox_onInitCallStart: function(jQueryMatchedObj) {
				$.ivva.locationAnchor.reparse();
				var locationHash = $.ivva.locationAnchor.get('i');
				var objClicked = null;
				if (locationHash != null) {
					jQueryMatchedObj.each(function() {
						var elId = $(this).attr('id');
						var imgHash = elId.slice(5) + '.jpg';
						if (imgHash == locationHash) {
							objClicked = this;
						}
					});
				}
				return objClicked;
			},

			lightbox_onImageRender: function(el) {
				$.ivva.gallery.cartButtonsSet = false;
				$.ivva.gallery.incrementPhotoView(el.attr('src'));

				var elLink = $("a[href='" + el.attr('src') + "']");
				if (elLink.length <= 0) {
					return;
				}

				$.ivva.gallery.workingWithPhotoSrc = el.attr('src');

				var elId = elLink.attr('id');
				var imgName = elId.slice(5) + '.jpg';
				$.ivva.locationAnchor.set('i', imgName);

				// change page
				var parent = elLink.closest('.photoPage');
				if (parent.length > 0) {
					var pageNum = parent.attr('id').slice(9);
					var url = null;
					$('.photoSetPage').each(function(){
						if ($(this).attr('id').slice(12) == pageNum) {
							url = $(this).attr('href');
						}
					});

					if (url != null) {
						$.ivva.gallery.cartButtonsSet = true;

						// ajax options
						ajaxOptions = {
							url: url,
							type: "get",
							success: function(payload) {
								jQuery.nette.success(payload);
								$.ivva.gallery.lightbox_cart_buttons($("a[href='" + $.ivva.gallery.workingWithPhotoSrc + "']"));
							}
						};

						// make call
						$.ajax(ajaxOptions);

						$("#ajax-spinner").show();
					}

					$.ivva.locationAnchor.set('p', parent.attr('id').slice(9));
				}

				$.ivva.locationAnchor.finish();
				$.ivva.locationAnchor.reparse();

				if (!$.ivva.gallery.cartButtonsSet) {
					$.ivva.gallery.lightbox_cart_buttons(elLink);
				}

			},

			lightbox_cart_buttons: function(elLink) {
				$('#lightbox-secNav-btnPut').expire('click');
				$('#lightbox-secNav-btnRemove').expire('click');

				if ($('a.buy').length > 0 && $.ivva.gallery.enableDetailButtons) {
					var elId = elLink.attr('id');

					var cell = elLink.parent('.inCart');
					if (cell.length > 0) {
						$('#lightbox-secNav-btnPut').hide();
						$('#lightbox-secNav-btnRemove').show();
					} else {
						$('#lightbox-secNav-btnRemove').hide();
						$('#lightbox-secNav-btnPut').show();
					}

					$('#lightbox-secNav-btnPut').livequery('click', function() {
						var pageNum = $.ivva.locationAnchor.get('p');
						var link = $(this).attr('href');
						link += '&photoId=' + elId.slice(5);

						if (pageNum > 0) {
							link += '&galleries-page=' + pageNum;
						}

						var ajaxOptions = {
							url:  link,
							type: "get",
							success: function(payload) {
								jQuery.nette.success(payload);
								$('#lightbox-secNav-btnPut').hide();
								$('#lightbox-secNav-btnRemove').show();
							},
							dataType: "json"
						};

						$.ajax(ajaxOptions);

					    // zobrazení spinneru a nastavení jeho pozice
					    $("#ajax-spinner").show();

					    return false;
					});
					$('#lightbox-secNav-btnRemove').livequery('click', function() {
						var pageNum = $.ivva.locationAnchor.get('p');
						var link = $(this).attr('href');
						link += '&photoId=' + elId.slice(5);

						if (pageNum > 0) {
							link += '&galleries-page=' + pageNum;
						}

						var ajaxOptions = {
							url:  link,
							type: "get",
							success: function(payload) {
								jQuery.nette.success(payload);
								$('#lightbox-secNav-btnRemove').hide();
								$('#lightbox-secNav-btnPut').show();
							},
							dataType: "json"
						};

						$.ajax(ajaxOptions);

					    // zobrazení spinneru a nastavení jeho pozice
					    $("#ajax-spinner").show();

					    return false;
					});
				}
			},

			lightbox_onFinish: function() {
				var q = $.parseQuery($.url.attr('anchor'));
				var newQ = $.query.empty();
				if (q.p != null) {
					newQ = newQ.set('p', q.p);
				}
				window.location.hash = '#' + newQ.toString();

				$.ivva.gallery.lightboxize();
			},

			lightboxize: function(hardlink, incrementPhotoView) {
				$('a.lightbox').lightBox({
					imageLoading: baseUri + 'images/lightbox/lightbox-ico-loading.gif',
					imageBtnPrev: baseUri + 'images/lightbox/lightbox-btn-prev.gif',
					imageBtnNext: baseUri + 'images/lightbox/lightbox-btn-next.gif',
					imageBtnClose: baseUri + 'images/lightbox/lightbox-btn-close.gif',
					imageBtnPut: baseUri + 'images/design/icon_16_cart_put.png',
					imageBtnRemove: baseUri + 'images/design/icon_16_cart_remove.png',
					urlBtnPut: $.ivva.urlPhotoAdd,
					urlBtnRemove: $.ivva.urlPhotoRemove,
					imageBlank: baseUri + 'images/lightbox/lightbox-blank.gif',
					onInitCallStart: $.ivva.gallery.lightbox_onInitCallStart,
					onImageRender: $.ivva.gallery.lightbox_onImageRender,
					onFinish: $.ivva.gallery.lightbox_onFinish,
					txtImage: 'Fotografie',
					txtOf: 'z'
				});
			},

			initPage: function() {
				// change gallery page
				var pageNum = $.parseQuery($.url.attr('anchor')).p;
				if (pageNum != null) {
					var url = null;
					$('.photoSetPage').each(function(){
						if ($(this).attr('id').slice(12) == pageNum) {
							url = $(this).attr('href');
						}
					});

					if (url != null) {
						// ajax options
						ajaxOptions = {
							url: url,
							type: "get"
						};

						// make call
						$.ajax(ajaxOptions);

						if ($.parseQuery($.url.attr('anchor')).i == null) {
							$("#ajax-spinner").show();
						}
					}
				}
			},

			initPhotoBuyIcon: function() {
				// Buy icon next to the photo
				$('table.photosThumbs a.lightbox').livequery('mouseover', function() {
					$('table.photosThumbs a#' + $(this).attr('id') + 'Buy').css('display', 'block');
				});
				$('table.photosThumbs a.lightbox').livequery('mouseout', function() {
					$('table.photosThumbs a#' + $(this).attr('id') + 'Buy').hide();
				});

				$('table.photosThumbs a.buy').livequery('mouseover', function() {
					$(this).css('display', 'block');
				});
				$('table.photosThumbs a.buy').livequery('mouseout', function() {
					$(this).hide();
				});
				$('table.photosThumbs a.buy').livequery('click', function() {
					var pageNum = $.parseQuery($.url.attr('anchor')).p;

					var link = $(this).attr('href');

					if (pageNum > 0) {
						link = link + '&galleries-page=' + pageNum;
					}

					var ajaxOptions = {
						url:  link,
						type: "get",
						success: jQuery.nette.success,
						dataType: "json"
					};

					$.ajax(ajaxOptions);

				    // zobrazení spinneru a nastavení jeho pozice
				    $("#ajax-spinner").show();

				    return false;
				});
			}
		},

		slideshowize: function() {
			$.getJSON(slideshowSinglePhotoTimeUrl, function(data){
				$('#slideshow table').show();
				$('#slideshow').slideshow({
					width:148,
					height:223,
					play:true,
					time:data.slideshowSinglePhotoTime * 1000,
					panel:false,
					playframe:false,
					title:false
				});
			});
		},

		ajaxizeLinks: function() {
			$('.ajaxLink').live('click', function() {

				// ajax options
				var ajaxOptions = {
					url: $(this).attr('href'),
					type: "get"
				};

				// make call
				$.ajax(ajaxOptions);

			    // zobrazení spinneru
			    $("#ajax-spinner").show();

				return false;
			});
		},

		ajaxSuccess: function(payload) {
			jQuery.nette.success(payload);
			jQuery.ivva.gallery.lightboxize();
		},

		startup: function() {
		    // vhodně nastylovaný div vložím po načtení stránky
		    $('<div id="ajax-spinner"></div>').appendTo("body").ajaxStop(function () {
		        // a při události ajaxStop spinner schovám a nastavím mu původní pozici
		        $(this).hide().css({
		            position: "fixed",
		            left: "50%",
		            top: "50%"
		        });
		    }).hide();

			$.ivva.gallery.lightboxize();
			$.ivva.gallery.initPage();
			$.ivva.slideshowize();
			$.ivva.gallery.initPhotoBuyIcon();
			$.ivva.ajaxizeLinks();
		}

	}
});
