﻿var AA = {

	start: function() {
		this.tweakLayout();
		this.tweakButtons();
		this.startRotator();
		this.addPanesFunctionality();
		this.addFunctionalities();
		this.addLightbox();
		this.rotateBoxes();
	},
	
	
	tweakLayout: function() {
		$('html').addClass('js');
		
		// Sign inputs for IE
		if ($.browser.msie) {
			$('input').each(function() {
				if ($(this).attr('type') == 'text') {
					$(this).addClass('text');
				}
				else if ($(this).attr('type') == 'password') {
					$(this).addClass('password');
				}
				else if ($(this).attr('type') == 'checkbox') {
					$(this).addClass('checkbox');
				}
				else if ($(this).attr('type') == 'radio') {
					$(this).addClass('radio');
				}
			});
		}
		
		// Fix png links in IE6
		if ($.browser.msie && /MSIE 6.0/.test(navigator.userAgent)) {
			// logo
			if (document.getElementById('name')) {
				if ($('#name a').length) {
					var href = $('#name a:first').attr('href');
					$('#masthead').append('<a href="' + href + '" id="logo-fix"></a>');
				}
			}
		}
		
		// Give hover
		function giveHover($el) {
			$el.hover(function() {
				$el.addClass('fHover');
			}, function() {
				$el.removeClass('fHover');
			});
		}
		if ($.browser.msie && /MSIE 6.0/.test(navigator.userAgent)) {

		}
		
	},
	
	tweakButtons: function() {
		$('input.button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			var val = $submit.attr('value');
			$submit.after('<a href="#" class="' + classes + '">' + val + '</span></a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
	},
	
	addFunctionalities: function() {
		//add target _blank
		$('a.blank').attr('target', '_blank');
	},
	
	startRotator: function() {
		$('#rotator').each(function() {
			function changeTo(x) {
				inTransition = true;
				var $old = $rotator.find('> a.active');
				$old.removeClass('active');
				$pager.find('li.selected').removeClass('selected');
				$pager.find('li:eq(' + x + ')').addClass('selected');
				$rotator.find('> a:eq(' + x + ')').addClass('active').fadeIn(500, function() {
					$old.hide();
					inTransition = false;
				});
			}
			
			var inTransition = false;
			var $rotator = $(this);
			var $pages = $rotator.find('> a');
			if ($pages.length > 1) {
				$rotator.addClass('rotating');
				var html = '<ul class="rot-pager">';
				$pages.each(function(i) {
					if (i > 0) {
						$(this).hide();
						html += '<li><a href="#">' + (i+1) + '</a></li>';
					}
					else {
						$(this).addClass('active');
						html += '<li class="selected"><a href="#">' + (i+1) + '</a></li>';
					}
				});
				html += '</ul>';
				$rotator.append(html);
				var $pager = $rotator.find('.rot-pager');
				$pager.find('li').each(function(i) {
					var $li = $(this);
					$li.find('a').click(function() {
						if (!inTransition && !$li.hasClass('selected')) {
							changeTo(i);
							clearInterval(rotatorInterval);
							rotate();
						}
						return false;
					});
				});
			}
			
			
			function rotate() {
				var rot = $rotator.find('.active').prevAll().length + 1;
				rotatorInterval = setInterval(function() {
					changeTo(rot++);
					if (rot > $pages.length - 1) {
						rot = 0;
					}
				}, 3000);
			}
			
			rotate();
			
		});
	},
	
	addPanesFunctionality: function() {
		$('#panes').each(function() {
			function changeTo(x) {
				$tabs_ul.find('.selected').removeClass('selected');
				$tabs_ul.find('> li:eq(' + x + ')').addClass('selected');
				$panes_ul.find('.active').removeClass('active');
				$panes_ul.find('> li:eq(' + x + ')').addClass('active');
			}
			
			var $this = $(this);
			var $tabs_ul = $this.find('.tabs');
			var $tabs = $tabs_ul.find('> li');
			var $panes_ul = $this.find('.panes');
			var $panes = $panes_ul.find('> li');
			var max = 0;
			$panes.each(function(i) {
				var $pane = $(this);
				if (i == 0) {
					$pane.addClass('active');
				}
			});
			
			$tabs.each(function(i) {
				var $li = $(this);
				var $a = $li.find('a');
				if (i == 0) {
					$li.addClass('selected');
				}
				$a.click(function() {
					var el = $(this).attr('rel');
					
					changeTo(i);
					if (!$tabs_ul.find('li:eq(' + i + ')').hasClass('gotten')) {
						$panes_ul.append('<img id="panes-loader" src="' + siteUrl + 'static/panes-loader.gif" />');
						$.ajax({
					   		type: "POST",
					   		url: siteUrl + 'ajax-element',
					   		data: "element=" + el,
					   		dataType: "json",
					   		success: function(data){
					   			if (data) {
									$('#panes-loader').remove();
									var html = '<ul>';
									for (j = 0; j < data.length; j++) {
										html += '<li><a href="' + siteUrl + depolonize(data[j].type) + '/' + data[j].url_title + '/' + data[j].id + '/1">' + data[j].title + '</a></li>';
									}
									html += '</ul>';
									$panes_ul.find('> li:eq(' + i + ')').html(html);
									$li.addClass('gotten');
								}
					   		}
						});
					}
					return false;
				});
				
				function depolonize(str) {
					return str.replace(/ę/g, 'e').replace(/ł/g, 'l').replace('sprzet', 'video').replace('news', 'wiadomosc');
				}
				
			});
			
			//select aa3 as deafult pane
			$('#panes .tabs li:last a').click();
		});
		
		//Rotate top banners
		function rotateBanners($bann1, $bann2, time) {
			var odd = true;
			var topBannersInterval = setInterval(function() {
				if (odd) {
					$bann1.hide();
					$bann2.show();
				}
				else {
					$bann2.hide();
					$bann1.show();
				}
				odd = !odd;
			}, time);
		}
		// $('#top-banners').each(function() {	
			// var $this = $(this);
			// rotateBanners($this.find('.top-banner:first'), $this.find('.top-banner:last'), 10000);
		// });
		$('#sky-banners').each(function() {	
			var $this = $(this);
			rotateBanners($this.find('.sky:first'), $this.find('.sky:last'), 10000);
		});
		$('#rectangle').each(function() {	
			var $this = $(this);
			rotateBanners($this.find('.rect:first'), $this.find('.rect:last'), 10000);
		});
		
		
	},
	
	rotateBoxes: function() {
		if (document.getElementById('quote-of-the-day') && document.getElementById('know-how')) {
			var $box1 = $('#quote-of-the-day');
			var $box2 = $('#know-how');
			var height = $box1.find('.inner:first').height();
			if (height < $box2.find('.inner:first').height()) {
				height = $box2.find('.inner:first').height();
			}
			$box1.find('.inner:first').height(height).find('p').addClass('hid');
			$box1.find('p').addClass('hid');
			$box2.find('.inner:first').height(height);
			$box2.hide();
			AA.typeWriter($box1.find('p:first'));
			var odd = true;
			var boxInterval = setInterval(function() {
				if (odd) {
					$box1.hide();
					$box1.find('p:first').hide();
					$box2.show();
					AA.typeWriter($box2.find('p:first'));
				}
				else {
					$box1.show();
					$box2.hide();
					AA.typeWriter($box1.find('p:first'));
				}
				odd = !odd;
			}, 10000);
		}
	},
	
	typeWriter: function($el) {
		var text = $el.text();
		$el.text('');
		$el.show();
		var i = 0;
		var twInterval = setInterval(function() {
			$el.text(text.substring(0,i++));
			if (i == text.length + 1) {
				clearInterval(twInterval);
			}
		}, 30);
	},
	
	addLightbox: function() {
		$('a.lightbox').fancybox();
		$('#gallery-list .gallery-elements li a').fancybox({
			'overlayShow'			: false,
			'zoomSpeedIn'			: 600,
			'zoomSpeedOut'			: 500,
			'easingIn'				: 'easeOutBack',
			'easingOut'				: 'easeInBack',
			'imageScale'			: true
		});
		$('.news .news-gallery li a').fancybox({
			'overlayShow'			: false,
			'zoomSpeedIn'			: 600,
			'zoomSpeedOut'			: 500,
			'easingIn'				: 'easeOutBack',
			'easingOut'				: 'easeInBack',
			'imageScale'			: true
		});
	}
	
}

$(function() {
	AA.start();
});