/*!
	Htmlbox v1.68 - The ultimate lightweight Lightbox clone
	(c) 2007-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/

var Htmlbox = (function() {

	// Global variables, accessible to Htmlbox only
	var win = window, ie6 = Browser.Engine.trident4, options, pages, activePage = -1, compatibleOverlay, middle, centerWidth, centerHeight,

	// DOM elements
	overlay, center, page, bottomContainer, closeLink,

	// Effects
	fxOverlay, fxResize, fxPage;

	/*
		Initialization
	*/

	win.addEvent("domready", function() {
		// Append the Htmlbox HTML code at the bottom of the document
		$(document.body).adopt(
			$$(
				overlay = new Element("div", {id: "hbOverlay", events: {click: close}}),
				center = new Element("div", {id: "hbCenter"}),
				bottomContainer = new Element("div", {id: "hbBottomContainer"})
			).setStyle("display", "none")
		);
		
		closeLink = new Element("a", {id: "hbCloseLink", href: "#", events: {click: close}});

		page = new Element("div", {id: "hbPage"}).injectInside(center).adopt();

	});


	/*
		Internal functions
	*/

	function position() {
		var scroll = win.getScroll(), size = win.getSize();
		$$(center, bottomContainer).setStyle("left", scroll.x + (size.x / 2));
		if (compatibleOverlay) overlay.setStyles({left: scroll.x, top: scroll.y, width: size.x, height: size.y});
	}

	function setup(open) {
		["object", ie6 ? "select" : "embed"].forEach(function(tag) {
			Array.forEach(document.getElementsByTagName(tag), function(el) {
				if (open) el._htmlbox = el.style.visibility;
				el.style.visibility = open ? "hidden" : el._htmlbox;
			});
		});

		overlay.style.display = open ? "" : "none";

		var fn = open ? "addEvent" : "removeEvent";
		win[fn]("scroll", position)[fn]("resize", position);
		//document[fn]("keydown", keyDown);
	}

	function changePage(pageIndex) {
		if (pageIndex >= 0) {
			window.location.hash = pages[pageIndex][0].replace(window.location.protocol + "//" + window.location.hostname + "/", "").replace("/", "");
			activePage = pageIndex;

			/*stop();
			center.className = "hbLoading";*/
			
			var req = new Request({url: "/ajax/" + pages[pageIndex][0].replace("http://e1.nikru.com", "").replace("#", ""), 
				onSuccess: function(html) {
					animateBox(html);
					url = this.options.url.replace("http://e1.nikru.com", "").replace("/ajax", "");
					replaceLinks();
					var pageTracker;
					if (pageTracker != undefined)
						pageTracker._trackPageview(url);
					
					contact_form7(jQuery);
					
				},
				onFailure: function(html) {
					if (!this.options.url.contains("http://") && !this.options.url.contains("#") && html.status != 301 && html.status != 302)
					{
						animateBox(html.responseText);
						url = this.options.url.replace("http://e1.nikru.com", "").replace("/ajax", "");
						replaceLinks();
						pageTracker._trackPageview(url);
					}
				},
				evalScripts: true,
				method: 'get'
			});
			req.send();
		}

		return false;
	}

	function animateBox(data) {
		if (page.innerHTML == "")
		{
			$$(page).setStyle("display", "");
			center.className = "";
			fxPage.set(0);
			centerWidth = 970;
			centerHeight = win.getHeight() - 50;
			page.innerHTML = data + "<br /><br /><br />";
			page.appendChild(closeLink);
	
			var top = Math.max(0, middle - (centerHeight / 2)), fn;
			/*if (center.offsetHeight != centerHeight) {
				fxResize.start({height: centerHeight, top: top});
			}*/
			if (center.offsetWidth != centerWidth) {
				fxResize.start({width: centerWidth, marginLeft: -centerWidth/2});
			}
			fn = function() {
				bottomContainer.setStyles({width: centerWidth, bottom: top + centerHeight, marginLeft: -centerWidth/2, visibility: "hidden", display: ""});
				fxPage.start(1);
			};
			fn();
			//if (fxResize.check(fn)) fn();
		} else {
			page.innerHTML = data + "<br /><br /><br />";
			page.appendChild(closeLink);			
		}
		
		if ($('imageFormSubmit'))
		{
			fadeSubmitted = new Fx.Tween($('form-submitted'), {property: "opacity", duration: 400});
			$('form-submitted').setStyle("display", "none");
			fadeSubmitted.set(0);
			$('contact-form').addEvent('submit', function(e) {
				new Event(e).stop();
			
				var sender = new Request();
				sender.onSuccess=function(responseText, responseXML){
					if (responseText != '1')
					{
						alert(responseText);
					} else {
						$('form-submitted').setStyle("display", "");
						fadeSubmitted = new Fx.Tween($('form-submitted'), {property: "opacity", duration: 400});
						fadeSubmitted.set(0).start(1);
					}
				};
				sender.send({data: this, url: this.get('action')});
			});
		}
		Rounded("rounded", 6, 6);
		Htmlbox.scanPage();
		oldHash = window.location.hash;
	}

	function stop() {
		fxResize.cancel();
		fxPage.cancel();
		$$(page, bottomContainer).setStyle("display", "none");
	}

	function close() {
		if (activePage >= 0) {
			window.location.hash = "#";
			stop();
			activePage = -1;
			center.style.display = "none";
			page.innerHTML = "";
			fxOverlay.cancel().chain(setup).start(0);
		}

		return false;
	}


	/*
		API
	*/

	Element.implement({
		htmlbox: function(_options, linkMapper) {
			// The processing of a single element is similar to the processing of a collection with a single element
			$$(this).htmlbox(_options, linkMapper);

			return this;
		}
	});

	Elements.implement({
		/*
			options:	Optional options object, see Htmlbox.open()
			linkMapper:	Optional function taking a link DOM element and an index as arguments and returning an array containing 2 elements:
					the page URL and the page caption (may contain HTML)
			linksFilter:	Optional function taking a link DOM element and an index as arguments and returning true if the element is part of
					the page collection that will be shown on click, false if not. "this" refers to the element that was clicked.
					This function must always return true when the DOM element argument is "this".
		*/
		htmlbox: function(_options, linkMapper, linksFilter) {
			linkMapper = linkMapper || function(el) {
				return [el.href, el.title, el.id];
			};

			linksFilter = linksFilter || function() {
				return true;
			};

			var links = this;

			links.removeEvents("click").addEvent("click", function() {
				// Build the list of pages that will be displayed
				var filteredLinks = links.filter(linksFilter, this);
				return Htmlbox.open(filteredLinks.map(linkMapper), filteredLinks.indexOf(this), _options);
			});

			return links;
		}
	});

	return {
		open: function(_pages, startPage, _options) {
			if (center.style.display == "none")
			{
				options = $extend({
					loop: false,				// Allows to navigate between first and last pages
					overlayOpacity: 0.8,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
					overlayFadeDuration: 400,		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
					resizeDuration: 400,			// Duration of each of the box resize animations (in milliseconds)
					resizeTransition: false,		// false uses the mootools default transition
					initialWidth: 250,			// Initial width of the box (in pixels)
					initialHeight: 250,			// Initial height of the box (in pixels)
					pageFadeDuration: 400,			// Duration of the page fade-in animation (in milliseconds)
					captionAnimationDuration: 400,		// Duration of the caption animation (in milliseconds)
					counterText: "Page {x} of {y}"	// Translate or change as you wish, or set it to false to disable counter text for page groups
				}, _options || {});
				
				// Setup effects
				fxOverlay = new Fx.Tween(overlay, {property: "opacity", duration: options.overlayFadeDuration});
				fxResize = new Fx.Morph(center, $extend({duration: options.resizeDuration, link: "chain"}, options.resizeTransition ? {transition: options.resizeTransition} : {}));
				fxPage = new Fx.Tween(page, {property: "opacity", duration: options.pageFadeDuration});
				middle = win.getScrollTop() + (win.getHeight() / 2);
				centerWidth = options.initialWidth;
				centerHeight = options.initialHeight;
				center.setStyles({/*top: Math.max(0, middle - (centerHeight / 2)), */width: centerWidth, /*height: centerHeight,*/ marginLeft: -centerWidth/2, display: ""});
				compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
				if (compatibleOverlay) overlay.style.position = "absolute";
				fxOverlay.set(0).start(options.overlayOpacity);
				position();
				setup(1);
			}
			
			if (typeof _pages == "string") {
				_pages = [[_pages, startPage]];
				startPage = 0;
			}
			
			pages = _pages;
			options.loop = options.loop && (pages.length > 1);
			
			return changePage(startPage);
		}
	};

})();

// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
Htmlbox.scanPage = function() {
	$$(document.links).filter(function(el) {
		return el.rel && el.rel.test(/^htmlbox/i);
	}).htmlbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
};
window.addEvent("domready", Htmlbox.scanPage);
