(function($){
	$(function() {
		
		var slicesCount = 10;
		var speed = 2000;
		var pause = 12000;
		var fadeing = false;
		var ease = 'swing';
		
		$('.promise-box').each(function(){
			
			
			var $this = $(this);
			var $ul = $('ul', this);
			var $li = $('li', $ul);
			var current = 0;
			var width = $li.width();
			
			
			var render = function($this) {
				
				$('.slice-wrapper', $this).css({
					'width': (100 / slicesCount) + '%',
					'float': 'left',
					'overflow': 'hidden',
					'position': 'relative'
				}).each(function(id){
					
					$('div:first', this).width(width);
					this.style.display = 'block';
					this.scrollLeft = parseInt((width/slicesCount)* id);
					
				});
			
			}
			
			
			$ul.height($ul.height());
			$li.css({'top': 0, 'position': 'absolute', 'left': 0}).not('li:first').hide();
			$this.addClass('active');
			
			
			$li.each(function(id){
				var $this = $(this);
				$this.css({
					'height': $this.height()
				});
				
				var html = '<div class="slice-wrapper"><div>' + $this.html() + '</div></div>';
				var newHTML = ['<div class="original-wrapper">' + $this.html() + '</div>'];
				for(var i = 0; i < slicesCount; ++i) {
					newHTML.push(html);
				}
				$this.html(newHTML.join(''));
				render($this);
				
			});
			
			
		
			
			var change = function() {
				if( !fadeing ) {
					fadeing = true;
					var old = $( $li.get(current) )
					
					++current;
					if( current == $li.length ) {
						current = 0;
					}
					var next = $( $li.get(current) );
					
					next.css('z-index', 2);
					old.css('z-index', 1);
					
					var newSlices = $('.slice-wrapper', next)
					var oldSlices = $('.slice-wrapper', old)
					
					newSlices.css('opacity', 0);
					
					
					next.css('display', 'block');
					
					$('.original-wrapper', next).hide();
					$('.original-wrapper', old).hide();
					
					render(next);
					
					//newSlices.show();
					//oldSlices.show();
				

					var currentSlice = 0;
					for(var i = 0; i < slicesCount; ++i) {
						setTimeout(function(){
							
							$(newSlices.get(currentSlice)).animate({'opacity': 1}, speed, ease);
							
							$(oldSlices.get(currentSlice++)).animate({'opacity': 0}, speed, ease,currentSlice == slicesCount - 1 ? function(){
								//$('.original-wrapper', next).show();
								fadeing = false;
							} : null);
							
						}, i * 100);
					}
				}
				
			}
			
			$this.height(205);
			
			setInterval(function(){
				change();
			}, pause);
		

			
		});
		
		$(".calculator").click(function() {
			
			var width = 800;
			var height = 600;
			var left = parseInt( (screen.availWidth - width) / 2 );
			if(left < 0) {
				left = 0;
			}
			var top = parseInt( (screen.availHeight - height) / 2 );
			if(top < 0) {
				top = 0;
			}
			
			window.open(this.href, "child", "width="+ width +"px, height="+ height +"px, resizable, scrollbars, screenX="+ left +"px, screenY="+ top +"px, left="+ left +"px, top="+ top +"px");
			
			return false;
		});
		
		/*
		$(function() {
			$("#calculator").fancybox({
				'width'				: 800,
				'height'			: '75%',
				'autoScale'			: false,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'autoDimensions'	: false,
				'type'				: 'iframe',
				'onComplete': function() {
					
					/*
					var wrapper = $('#fancybox-inner');
					
					var newContent = $('#formc', wrapper).clone();
					
					//wrapper.html('').append(newContent);
					
					$('a', newContent).click(function(){
						window.open(this.href);
						return false;
					});
					///
					
				}
			});
			
			
		});
		*/
		
		
		var active = $("ul#nav li.active");
		
		$("ul#nav").children("li").children("ul").hover(
			function() {
				$(this).parent("li").addClass("active");
				active.addClass("active");
			},
			
			function() {
				$(this).parent("li").removeClass("active");
				active.addClass("active");
			}
		);
		
		$("ul#nav").children("li").hover(
			function() {
				active.children("ul").hide();
				$(this).children("ul").show();
				$(this).addClass("active");
				active.addClass("active");
			},
			
			function() {
				$(this).children("ul").hide();
				active.children("ul").show();
				$(this).removeClass("active");
				active.addClass("active");
			}
		);
		
		/*
		$('a.calculator').click(function(){
			popup('webcalc/calculator.php','calculator',1024,800);

			return false;
		});
		*/
		
		$('.print a').click(function(){
			window.print();
			return false;
			
		});
		
	
	});
	
})(jQuery);



function popup(mypage,myname,w,h){
	winl = (screen.width-w)/2;
	wint = (screen.height-h)/2;
	settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,toolbar=no'
	window.open(mypage,myname,settings);
}