Current File : /home/virtualki/22346/old/script/cascade.js
(function($) {
	var langs = $('html').attr('lang');

	if(langs == 'pl') {

	var Tablica = new Array()
	    Tablica['home']  = "Megiw - Sauny, producent saun - zakład obróbki drewna";
		Tablica['tab-1'] = "Megiw - Firma, otoczenie, pasja";
		Tablica['tab-2'] = "Megiw - Metody produkcji, ekologia";
		Tablica['tab-3'] = "Megiw - Aktualności";
		Tablica['tab-4'] = "Megiw - Kontakt";
		Tablica['tab-5'] = "Megiw - Akcesoria i kosmetyki do sauny - projektowanie spa, domki ogrodowe";
		Tablica['tab-6'] = "Megiw - Informacje dla dostawcy";
		Tablica['tab-7'] = "Megiw - Informacje dla odbiorcy";
		Tablica['tab-8'] = "Megiw - Multimedia";
			
	var TablicaDesc = new Array()
		TablicaDesc['home']  = "Megiw Sp. z o.o. to zakład obróbki drewna. Naszą pasją są sałny. Megiw to producent saun domowych oraz dla ośrodków SPA, w tym fińska, na podczerwień. W ofercie również DOMKI ALTANY OGRODOWE, akcesoria z drewna do sauny i ogrodu.";
		TablicaDesc['tab-1'] = "";
		TablicaDesc['tab-2'] = "";
		TablicaDesc['tab-3'] = "";
		TablicaDesc['tab-4'] = "";
		TablicaDesc['tab-5'] = "Najlepsze domki ogrodowe, w tym gotowe pod projektowanie SPA! Tu znajdziesz również akcesoria do sauny oraz najnowsze kosemtyki do sauny";
		TablicaDesc['tab-6'] = "";
		TablicaDesc['tab-7'] = "";
		TablicaDesc['tab-8'] = "";	
	}


	if(langs == 'de') {

	var Tablica = new Array()
	    Tablica['home']  = "Megiw";
		Tablica['tab-1'] = "Megiw";
		Tablica['tab-2'] = "Megiw";
		Tablica['tab-3'] = "Megiw";
		Tablica['tab-4'] = "Megiw";
		Tablica['tab-5'] = "Megiw";
		Tablica['tab-6'] = "Megiw";
		Tablica['tab-7'] = "Megiw";
		Tablica['tab-8'] = "Megiw";
			
	var TablicaDesc = new Array()
		TablicaDesc['home']  = "";
		TablicaDesc['tab-1'] = "";
		TablicaDesc['tab-2'] = "";
		TablicaDesc['tab-3'] = "";
		TablicaDesc['tab-4'] = "";
		TablicaDesc['tab-5'] = "";
		TablicaDesc['tab-6'] = "";
		TablicaDesc['tab-7'] = "";
		TablicaDesc['tab-8'] = "";	
	}
	
	if(langs == 'en') {

	var Tablica = new Array()
	    Tablica['home']  = "Megiw Ltd.";
		Tablica['tab-1'] = "Megiw Ltd.";
		Tablica['tab-2'] = "Megiw Ltd.";
		Tablica['tab-3'] = "Megiw Ltd.";
		Tablica['tab-4'] = "Megiw Ltd.";
		Tablica['tab-5'] = "Megiw Ltd.";
		Tablica['tab-6'] = "Megiw Ltd.";
		Tablica['tab-7'] = "Megiw Ltd.";
		Tablica['tab-8'] = "Megiw Ltd.";
			
	var TablicaDesc = new Array()
		TablicaDesc['home']  = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-1'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-2'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-3'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-4'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-5'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-6'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-7'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";
		TablicaDesc['tab-8'] = "The specialised wood processing plant. Products: sauna for your house, Houses and accessories for the garden, accessories and cosmetics to the sauna.";	
	}
		
		$(".header-logo-link").click(function() {
			document.title = Tablica['home'];
			$('meta[name=description]').attr('content', TablicaDesc['home']);
		});
		
		$(".cascade-close").click(function() {
			document.title = Tablica['home'];
			$('meta[name=description]').attr('content', TablicaDesc['home']);
		});

	var Cascade = function(cascade, page) {

		var $this = this, $body = $(document.body), lang = $('html').attr('lang'), baseUrl = $body.data('baseurl');

		$this.cascade = $(cascade);
		$this.cascadeDistinction = $this.cascade.attr('class').split(' ')[1];
		$this.cascadeParent = $this.cascade.parent('.content');
		$this.cascadeWindow = cascade.find('.cascade-window');
		$this.cascadeElement = cascade.find('.cascade-menu li');
		$this.cascadeNavigation = cascade.find('.cascade-navigation');
		$this.closeButton = cascade.find('.cascade-close');

		$this.enable = false;

		$this.page = page;

		$this.currentPage = -1;
		$this.previousPage = -1;

		$this.cascadeParentOpenMinHeight = 495;
		$this.cascadeParentClosedHeight = 300;
		$this.cascadeWindowWidth = 640;
		$this.cascadeElementMargin = 20;
		
		

		/******************************************************************/

		this.load = function(initLeft) {
			var i = 0,
					k = 0,
					left = 0,
					width = parseInt($this.cascadeElement.first().css('width')),
					image = $this.imageLoad($this.cascadeElement.first());

			image.on('load', function() {
				$this.cascadeElement.each(function() {
					var image = $this.imageLoad($(this).children('a').first());

					left = ($this.initLeft || 0) + (width * k) + ($this.cascadeElementMargin * k);
					$(this).css('left', left);

					k++;

					if(!image) i++;
					else {
						$(image).on('load', function() {
							if ((++i) === $this.cascadeElement.length) {
								var j = 0;

								$this.cascade.removeClass('preloader');
								$this.cascadeElement.css('display', 'block');

								$this.cascadeElement.each(function() {
									$(this).animate({
										height: parseInt($this.cascade.css('height')),
										opacity: 1
									}, getRandom(100, 1000), 'easeInOutQuint', function() {
										if ((++j) === $this.cascadeElement.length) {
											$this.enable = true;
											$this.handleHash();
										};
									});
								});
							};
						});
					}
				});
			});
		};

		/******************************************************************/

		this.imageLoad = function(object) {
			var image = $(document.createElement('img')),
					url = object.css('background-image').substring(4) || 'none';

			if(url !== 'none') {
				url = url.substring(0, url.length - 1).replace(/"/ig, '');

				if ($.browser.msie) image.attr('src', url + '?i=' + getRandom(1, 10000));
				else image.attr('src', url);

				return (image);
			} else
				return false;
		};

		/******************************************************************/

		this.getFirstPage = function() {
			for (var key in $this.page) {
				if ($this.page[key]['main'] === 1) return (key);
			}

			return (false);
		};

		/******************************************************************/

		this.getPrevPage = function() {
			var prev = '';
			for (var key in $this.page) {
				if (key === $this.currentPage && prev !== '') return (prev);
				else if ($this.page[key]['main'] === 1) prev = key;
			};

			return (prev);
		};

		/******************************************************************/

		this.getNextPage = function() {
			var n = false,
					next = $this.getFirstPage();

			for (var key in $this.page) {
				if (n) {
					if ($this.page[key]['main'] === 1) return (key);
				}
				if (key === $this.currentPage) n = key;
			};

			return (next);
		};

		/******************************************************************/

		this.getPage = function(key, property) {
			return ($this.page[key][property]);
		};

		/******************************************************************/

		this.getOpenStartPage = function() {
			for (var key in $this.page) {
				if ($this.page[key]['openStart'] === 1) return (key);
			}

			return (false);
		};

		/******************************************************************/

		this.doHash = function() {
			if (!$this.enable) return (false);
			$this.enable = false;

			var open = $this.isOpen();

			var currentPage = $this.checkHash();
			if (currentPage === false) {
				$this.enable = true;
				return (false);
			}

			$this.currentPage = currentPage;
			if ($this.previousPage === -1) $this.previousPage = $this.currentPage;

			if ($this.currentPage === -1)
				$this.close();
			else if (open)
				$this.close({'onComplete': $this.open });
			else
				$this.open();

			return (true);
		};

		/******************************************************************/

		this.handleHash = function() {
			CascadeController.currentHash = window.location.hash;

			$(window).on('hashchange', function(event) {
				event.preventDefault();

				if ($this.isEnable() === false) return;

				CascadeController.previousHash = CascadeController.currentHash;
				CascadeController.currentHash = window.location.hash;

				if( $this.currentPage !== CascadeController.currentHash.replace( /^#!\//, '') )
					$this.doHash();
			});

			$this.closeButton.on('click', function(e) {
				if($(this).parents('.cascade').attr('class').split(' ')[1] === $this.cascadeDistinction) {
					var another = $this.cascadeDistinction === 'first' ? 'second' : 'first';

					if( CascadeController.cascade[another].isOpen() ) {
						$this.close();
						window.location.href = '#!/' + CascadeController.cascade[another].currentPage;
					} else
						window.location.href = '#!/main';
				}

				return false;
			});

			if (CascadeController.currentHash === '') {
				var start = $this.getOpenStartPage();
				if (start === false) window.location.href = '#!/main';
				else window.location.href = '#!/' + start;
			} else
				$this.doHash();
		};

		/******************************************************************/

		this.checkHash = function() {
			if (CascadeController.currentHash === '#!/main') return (-1);

			for (var id in $this.page) {
				if ('#!/' + id === CascadeController.currentHash) return (id);
			}

			return (false);
		};

		/******************************************************************/

		this.open = function() {
			var i = 0, j = 0, k = 65,
					tab = $this.getPage($this.currentPage, 'tab'),
					pagePath = $this.getPage($this.currentPage, 'html'),
					scriptPath = $this.getPage($this.currentPage, 'js');

			$('#' + tab).css('z-index', 4);
			
			document.title = Tablica[tab];
			$('meta[name=description]').attr('content', TablicaDesc[tab]);
			
			$this.cascadeElement.addClass('closed').animate({
				left: 0
			}, 500, 'easeOutExpo', function() {
				i++;
				
				if (i === $this.cascadeElement.length) {
					var className = $('#' + $this.getPage($this.currentPage, 'tab')).attr('class').split(' ')[0].split('-')[0];

					$this.cascadeWindow.css('opacity', '1').css('display', 'block').attr('class', 'cascade-window ' + className);

					$this.cascadeWindow.animate({
						width: $this.cascadeWindowWidth
					}, 500, 'easeOutExpo', function() {
						$this.cascadeElement.removeClass('closed').not('#'+tab).each(function() { $(this).css({'z-index': 4-(++j), 'top': 0}).animate( { 'top': j*k }, 300, 'easeOutBack' ); });

						$this.showPreloader(true);
						var $content = $this.cascadeWindow.find('.cascade-window-content'),
								fH = 85;
						
						$.get( baseUrl + 'content/page/' + lang + '/' + pagePath, { lang: lang }, function(page) {
							$content.html(page);

							var $sections = $content.find('.layout-50-left section');

							if( $sections.exists() ) {
								var sH = $sections.heighest().height(),
										gH = $content.find('.layout-50-right').height(),
										$accordion = $content.find('.accordion');

								if( $accordion.exists() )
									sH += $accordion.find('.heading').length * $accordion.find('.heading').height() + 24;

								targetHeight = sH > gH ? sH : gH;
							} else
								targetHeight = $content.height() > $this.cascadeParentOpenMinHeight ? $content.height() : $this.cascadeParentOpenMinHeight;

							$this.cascadeParent.animate( { height: targetHeight + fH }, 500, 'easeOutBack' );

							$this.showPreloader(false);
							
							$.getScript(baseUrl + 'content/page/script/main.js', function() {
								if (scriptPath !== '') $.getScript(baseUrl + 'content/page/script/' + scriptPath);
							});

							$this.enable = true;

							$this.previousPage = $this.currentPage;

							$this.createNavigation();
							
						}, 'html');
					});
				};
			});
		};

		/******************************************************************/

		this.close = function(data) {
			$(':input,a').qtip('destroy');

			$this.cascadeWindow.find('.cascade-window-content').html('');

			if ($this.previousPage !== -1) $('#' + $this.getPage($this.previousPage, 'tab') + ' a').attr('href', '#!/' + $this.previousPage);

			$this.cascadeElement.addClass('closed').each(function() {
				$(this).animate( { 'top': 0 }, 300 );
			});
			$this.cascadeParent.animate({height:$this.cascadeParentClosedHeight},500,'easeOutBack');

			$this.cascadeWindow.animate({
				width: '0px',
				opacity: 0
			}, 500, 'easeOutBounce', function() {
				$this.cascadeWindow.css('display', 'none');
				$this.expand(data);
			});
		};

		/******************************************************************/

		this.expand = function(data) {
			var width = parseInt($this.cascadeElement.first().css('width')),
					counter = 0,
					done = 0,
					left = -1 * width;

			$this.cascadeElement.each(function() {
				$(this).css('z-index', 1).removeClass('closed');
				left += width + ((counter++) > 0 ? $this.cascadeElementMargin : 0);

				$(this).animate({
					left: left
				}, 500, 'easeOutExpo', function() {
					done++;
					if (done === $this.cascadeElement.length) {
						if (typeof(data) !== 'undefined') {
							if (typeof(data.onComplete) !== 'undefined') data.onComplete.apply();
							else $this.enable = true;
						} else {
							$this.enable = true;
							$this.currentPage = -1;
						}
					};
				});
			});
		};

		/******************************************************************/

		this.isOpen = function() {
			return ($this.currentPage === -1 ? false : true);
		};

		/******************************************************************/

		this.isEnable = function() {
			if (!$this.enable) {
				window.location.href = CascadeController.previousHash;
				return (false);
			}

			return (true);
		};

		/***********************************************************/

		this.createNavigation = function() {
			var prev = $this.getPrevPage(),
					next = $this.getNextPage();

			$this.cascade.find('.cascade-prev').attr('href', '#!/' + prev);
			$this.cascade.find('.cascade-next').attr('href', '#!/' + next);
		};

		/******************************************************************/

		this.showNavigation = function(show) {
			if ($this.cascadeElement.length > 1) $this.cascadeNavigation.css('display', show ? 'block' : 'none');
		};

		/******************************************************************/

		this.showPreloader = function(show) {
			if (show) $this.cascadeWindow.addClass('cascade-window-prealoder');
			else $this.cascadeWindow.removeClass('cascade-window-prealoder');
		};

	};

	/**************************************************************/

	$.fn.cascade = function(page) {
		var cascade = new Cascade(this, page);
		cascade.load();

		return cascade;
	};
	
})(jQuery);