jQuery.easing.jswing=jQuery.easing.swing;
jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,a,c,b,d){return jQuery.easing[jQuery.easing.def](e,a,c,b,d)},easeInQuad:function(e,a,c,b,d){return b*(a/=d)*a+c},easeOutQuad:function(e,a,c,b,d){return-b*(a/=d)*(a-2)+c},easeInOutQuad:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a+c;return-b/2*(--a*(a-2)-1)+c},easeInCubic:function(e,a,c,b,d){return b*(a/=d)*a*a+c},easeOutCubic:function(e,a,c,b,d){return b*((a=a/d-1)*a*a+1)+c},easeInOutCubic:function(e,a,c,b,d){if((a/=d/2)<1)return b/
2*a*a*a+c;return b/2*((a-=2)*a*a+2)+c},easeInQuart:function(e,a,c,b,d){return b*(a/=d)*a*a*a+c},easeOutQuart:function(e,a,c,b,d){return-b*((a=a/d-1)*a*a*a-1)+c},easeInOutQuart:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a+c;return-b/2*((a-=2)*a*a*a-2)+c},easeInQuint:function(e,a,c,b,d){return b*(a/=d)*a*a*a*a+c},easeOutQuint:function(e,a,c,b,d){return b*((a=a/d-1)*a*a*a*a+1)+c},easeInOutQuint:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a*a+c;return b/2*((a-=2)*a*a*a*a+2)+c},easeInSine:function(e,
a,c,b,d){return-b*Math.cos(a/d*(Math.PI/2))+b+c},easeOutSine:function(e,a,c,b,d){return b*Math.sin(a/d*(Math.PI/2))+c},easeInOutSine:function(e,a,c,b,d){return-b/2*(Math.cos(Math.PI*a/d)-1)+c},easeInExpo:function(e,a,c,b,d){return a==0?c:b*Math.pow(2,10*(a/d-1))+c},easeOutExpo:function(e,a,c,b,d){return a==d?c+b:b*(-Math.pow(2,-10*a/d)+1)+c},easeInOutExpo:function(e,a,c,b,d){if(a==0)return c;if(a==d)return c+b;if((a/=d/2)<1)return b/2*Math.pow(2,10*(a-1))+c;return b/2*(-Math.pow(2,-10*--a)+2)+c},
easeInCirc:function(e,a,c,b,d){return-b*(Math.sqrt(1-(a/=d)*a)-1)+c},easeOutCirc:function(e,a,c,b,d){return b*Math.sqrt(1-(a=a/d-1)*a)+c},easeInOutCirc:function(e,a,c,b,d){if((a/=d/2)<1)return-b/2*(Math.sqrt(1-a*a)-1)+c;return b/2*(Math.sqrt(1-(a-=2)*a)+1)+c},easeInElastic:function(e,a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);return-(g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f))+c},easeOutElastic:function(e,
a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);return g*Math.pow(2,-10*a)*Math.sin((a*d-e)*2*Math.PI/f)+b+c},easeInOutElastic:function(e,a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d/2)==2)return c+b;f||(f=d*0.3*1.5);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);if(a<1)return-0.5*g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f)+c;return g*Math.pow(2,-10*(a-=1))*Math.sin((a*
d-e)*2*Math.PI/f)*0.5+b+c},easeInBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;return b*(a/=d)*a*((f+1)*a-f)+c},easeOutBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;return b*((a=a/d-1)*a*((f+1)*a+f)+1)+c},easeInOutBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;if((a/=d/2)<1)return b/2*a*a*(((f*=1.525)+1)*a-f)+c;return b/2*((a-=2)*a*(((f*=1.525)+1)*a+f)+2)+c},easeInBounce:function(e,a,c,b,d){return b-jQuery.easing.easeOutBounce(e,d-a,0,b,d)+c},easeOutBounce:function(e,a,c,b,d){return(a/=
d)<1/2.75?b*7.5625*a*a+c:a<2/2.75?b*(7.5625*(a-=1.5/2.75)*a+0.75)+c:a<2.5/2.75?b*(7.5625*(a-=2.25/2.75)*a+0.9375)+c:b*(7.5625*(a-=2.625/2.75)*a+0.984375)+c},easeInOutBounce:function(e,a,c,b,d){if(a<d/2)return jQuery.easing.easeInBounce(e,a*2,0,b,d)*0.5+c;return jQuery.easing.easeOutBounce(e,a*2-d,0,b,d)*0.5+b*0.5+c}});

function log(a){ try{console.log(a);}catch(e){} }

jQuery.noConflict();
jQuery(document).ready(function($){
	// Mneu setup
	var $nav = $('.nav');
	
	// search box
	var message = $('#top-search').val();
	$('#top-search').focus(function(){
		if($(this).val() == message){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val(message);
		}
	});
	
	// inner slider
	$('.inner-slider').each(function(){
		$(this).cycle({
			delay:parseInt($('.inner-slider-delay', this).remove().text()),
			height:$('.inner-slider-height', this).remove().text()
		});
	});
	
	// tooltips for featured-services
	$('.featured-service').hover(function(){
		$('.tooltip', this).css({
			opacity: 0.5,
			display: 'block',
			bottom: '85px'
		}).animate({
			opacity: 1,
			bottom: '75px'
		},{
			duration: 300
		});
	},function(){
		$('.tooltip', this).animate({
			opacity: 0,
			bottom: '65px'
		},{
			duration: 300,
			complete: function(){
				$(this).hide();
			}
		});
	});
	
	// fix widgets
	$('#inner-footer .footer-column:last').addClass('footer-column-last');
	
	// animate contact form messages
	$('#message-container').hide().fadeIn(1000);
	
	// fix for some browsers
	$('a', $nav).attr('title','');
	
	if($('>.menu', $nav).size() > 0){
		$('>.menu>ul', $nav).unwrap();
	}
	
	$('ul li ul', $nav).each(function(){
		$(this).data('height', $(this).height()); // can be read only after menu has been shown
	}).hide();
	
	$('li:has(ul)', $nav).hover(function(){
		var $ul = $('>ul', this);
		
		var height = $ul.data('height');
		
		$ul.show().css({
			opacity: 0,
			height: '0px',
			overflow: 'visible'
		}).stop().animate({
			opacity: 1,
			height: height+'px'
		}, {
			duration: 400,
			easing: 'easeOutExpo'
		});
		
		if($ul.offset().left + $ul.width() > $(window).width()){
			$ul.css({
				'left': - $ul.width() + 15 + 'px',
				'z-index': 120
			});
		}
	}, function(){
		var $ul = $('>ul', this);
		$ul.stop().animate({
			opacity: 0,
			height: '0px'
		},{
			duration: 400,
			easing: 'easeOutExpo',
			complete: function() { $(this).hide(); }
		});
	});
	
	// apply corner radius
	$('.nav>ul>li>ul>li:last-child').css({
		'-moz-border-radius-bottomleft': '5px',
		'-moz-border-radius-bottomright': '5px',
		'-webkit-border-bottom-left-radius': '5px',
		'-webkit-border-bottom-right-radius': '5px'
	});
	
	$('.nav>ul ul ul>li:first-child').css({
		'-moz-border-radius-topleft': '5px',
		'-moz-border-radius-topright': '5px',
		'-webkit-border-top-left-radius': '5px',
		'-webkit-border-top-right-radius': '5px'
	});
	
	$('.nav>ul ul ul>li:last-child').css({
		'-moz-border-radius-bottomleft': '5px',
		'-moz-border-radius-bottomright': '5px',
		'-webkit-border-bottom-left-radius': '5px',
		'-webkit-border-bottom-right-radius': '5px'
	});
	
	// move last menus to the left if it overflows
	var $lastUl = $('>ul>li:last-child>ul', $nav);
	var lastUlWidth = $lastUl.width();
	var lastLiWidth = $('>ul>li:last-child', $nav).width();
	if(lastUlWidth > lastLiWidth){
		newLeft = lastUlWidth - lastLiWidth;
		$lastUl.css('left', '-' + newLeft + 'px');
	}

	/*
	 * billboard
	 */
	var slides = [];
	var $bb = $('#billboard');
	var rotation = null;
	var autorotateInterval = 5000;
	var totalLoaded = 0;
	var totalImages = $('.slide>img', $bb).length;
	
	if($bb.size() == 1) {
		$('.slide', $bb).remove().each(function(i, el){
			var slide = {
				image: $('img', el).attr('src'),
				descr: $('.descr', el).html()
			};
			slides.push(slide);
		});
	}
	
	loadImage = function(index){
		if(totalImages < 1 || index >= totalImages) return;
		
		var img = $('<img alt="" />').load(function(){
			totalLoaded++;
			//log('Loaded '+totalLoaded+'/'+totalImages);
			if(totalLoaded == totalImages){
				runTheShow();
			}
		});
		
		$(img).attr('src', slides[index].image);
		
		setTimeout('loadImage('+(index+1)+')', 10);
	}
	loadImage(0);
	
	if(slides.length > 0) {
		
		$bb.html('<img src="'+slides[0].image+'"><div class="descr-wrapper"><div class="descr">'+slides[0].descr+'</div></div><div class="navi"><div class="center"></div></div>');
		
		$bb.hide();
		
		$('.descr-wrapper,.descr', $bb).add($bb).css({
			'-moz-border-radius': '5px',
			'-webkit-border-radius': '5px'
		});
		
		$('>img', $bb).hide();
		
		for(i = 0; i < slides.length; i++){
			$('.navi .center', $bb).append('<a href="#" id="uds-bb-'+i+'" class="'+(i==0 ? 'current' : '')+'"></a>');
		}
		
		if(slides.length < 2){
			$('.navi', $bb).hide();
		}
		
		$bb.css('background-image', 'url('+slides[0].image+')');
		
		var swapImage = function(nextNo){
			if(slides[nextNo].image == $('img', $bb).attr('src')) return;
			
			$img = $('>img', $bb);
			$img.attr('src', slides[nextNo].image)
				.show()
				.css({
					opacity: 0,
					position: 'absolute',
					top: '-100px'
				})
				.animate({
					opacity: 1,
					top: '0px'
				}, {
					duration: 700,
					easing: 'easeOutExpo',
					complete: function(){
						$bb.css('background-image', 'url('+slides[nextNo].image+')');
						$img.hide();
					}
				});
				
			$desc = $('.descr-wrapper', $bb);
			if($desc.find('.descr').text().trim() == "" && slides[nextNo].descr.trim() == ""){
				$desc.find('.descr').html(slides[nextNo].descr);
				$desc.css('opacity', 0);
				return;
			}
			
			$desc.animate({
				opacity: 0
			}, {
				duration: 300,			
				easing: 'easeOutCubic',
				step: function(a, object){
					var now = new Date().getTime();
					var scale = 1 - $.easing.easeOutExpo(null, now - object.startTime, 0, 1, object.options.duration);
					$(object.elem).css({
						'-moz-transform': 'scale('+scale+')',
						'-webkit-transform': 'scale('+scale+')'
					});
				},
				complete: function(){
					$desc.find('.descr').html(slides[nextNo].descr);
					if(slides[nextNo].descr.trim() == "") return;
					$desc.animate({
						opacity: 1
					}, {
						duration: 300,
						easing: 'easeOutCubic',
						step: function(a, object){
							var now = new Date().getTime();
							var scale = $.easing.easeOutExpo(null, now - object.startTime, 0, 1, object.options.duration);
							$(object.elem).css({
								'-moz-transform': 'scale('+scale+')',
								'-webkit-transform': 'scale('+scale+')'
							});
						},
						complete: function(){
							if($.browser.msie){
								$desc.css('opacity', 0.9);
							}
						}
					});
				}
			});
		}
		
		nextImage = function (){
			for(i = 0; i < slides.length; i++){
				if($('img', $bb).attr('src') == slides[i].image){
					$('.navi a', $bb).removeClass('current');
					if(i + 1 < slides.length){
						$('.navi a', $bb).eq(i + 1).addClass('current');
						swapImage(i+1);
					} else {
						$('.navi a', $bb).eq(0).addClass('current');
						swapImage(0);
					}
					return;
				}
			}
		}
		
		runTheShow = function(){
			$('#billboard-wrapper .loader').hide();
			$bb.show();
			rotation = setInterval(nextImage, autorotateInterval);
		}
		
		$('.navi a', $bb).click(function(){
			$('.navi a', $bb).removeClass('current');
			$(this).addClass('current');
			swapImage($(this).index());
			return false;
		});
		
		$bb.hover(function(){
			clearInterval(rotation);
		}, function(){
			rotation = setInterval(nextImage, autorotateInterval);
		});
		
		
	}//*/
	
	// portfolio
	function preloadPortfolio(){
		$('.portfolio-item:has(img)').css({
			'background-image': 'url('+template_url+'/images/ajax-loader-portfolio.gif)',
			'background-position': 'center center',
			'background-repeat': 'no-repeat'
		});
		$('.portfolio-item img').each(function(){
			var image = this;
			var src = $(this).attr('src');
			$(image).css('opacity', 0);
			$('<img alt="">').load(function(){
				$(image).parents('.portfolio-item').css('background-image', 'none');
				$(image).animate({'opacity': 1}, 400);
			}).attr('src', src);
		});
	}
	preloadPortfolio();
	
	function setupFancybox(){
		if(typeof $.fancybox != 'function') return;
		
		$('.portfolio-item:not(.no-lightbox) a').fancybox({
			titleShow: false
		});
	}
	setupFancybox();
	
	$('.terms-tag-main').click(function(e){
		$.get( $(this).attr('href'), function(data) {
			$('.portfolio').quicksand( $(data).find('.portfolio-item-wrapper'), {
				adjustHeight: 'dynamic',
				attribute: function(v) {
					return $(v).attr('id');
				}
			}, function() {
				preloadPortfolio();
				setupFancybox();
			});
		});  
		e.preventDefault();
	});
	
	$('.portfolio-item').live('mouseover', function(){
		$('a:has(img):not(:has(.portfolio-hover))', this).append('<span class="portfolio-hover"></span>');
		$('img', this).stop().animate({'opacity': 0.4});
	});
	$('.portfolio-item').live('mouseout', function(){
		$('img', this).stop().animate({'opacity': 1}, {
			complete: function(){
				$(this).css('opacity', 1);
			}
		});
	});
	
	// widget LI animations
	$('ul li a','.widget,.footer-column').hover(function(){
		$(this).stop().animate({'padding-left': '25px'}, 300);
	},function(){
		$(this).stop().animate({'padding-left': '19px'}, 300);
	});
	
	// fix trim for IE
	if(typeof String.prototype.trim !== 'function') {
		String.prototype.trim = function() {
			return this.replace(/^\s+|\s+$/, ''); 
		}
	}
	
	// Sidebar slideshow
	$('.uds-slideshow-widget .images img').each(function(i, el){
		var link = $('<div>' + (i + 1) + '</div>');
		if(i != 0) {
			$(this).hide();
		} else {
			$(link).addClass('active');
		}
		$(link).click(function(event){
			$('.uds-slideshow-widget .control div').removeClass('active');
			$(this).addClass('active');
			
			if($('.uds-slideshow-widget .images img:eq(' + i + ')').is(':visible')) return;
			
			$('.uds-slideshow-widget .images img:visible').animate({opacity: 0}, {
				duration: 800, 
				easing: 'easeOutExpo', 
				complete:function(){
					$(this).hide();
					$('.uds-slideshow-widget').height($(this).height());
				}
			});
			
			$('.uds-slideshow-widget .images img:eq(' + i + ')').show().css({opacity: 0}).animate({opacity: 1},{
				duration: 800,
				easing: 'easeOutExpo'
			});
		});
		$('.uds-slideshow-widget .control').append(link);
	});
	$('.uds-slideshow-widget').height($('.uds-slideshow-widget .images img:first').height());
	$('.uds-slideshow-widget .images img:first').load(function(){
		$('.uds-slideshow-widget').height($('.uds-slideshow-widget .images img:first').height());
	});
});
