$(window).load(function() {

	// $('#header .search, #header .search form').css({ 'min-width': '' })

	$('.partner .logo img').vAlign()
	$('.partner-bar li img').vAlign()
	$('.catalog-list .thumbnail img').vAlign();
	$('.catalog .gallery .first a img').vAlign();
	
	if( $('#home-slider').size() ) {
		
		$('#home-slider').slides({
			generateNextPrev: true,
			generatePagination: false,
			play: 5000,
			pause: 5000,
			slideEasing: "easeInOutExpo"
		})
		
		var lis = $(".partner-bar ul li");
			for(var i = 0; i < lis.length; i+=5) {
			  lis.slice(i, i+5).wrapAll('<div class="group"></div>');
			}
			
		$('.partner-bar').slides({
			generateNextPrev: true,
			generatePagination: false,
			play: 5000,
			pause: 5000,
			slideEasing: "easeInOutExpo"
		})
		
	}

	var sendW = $('.send-form').width(),
		sendH = $('.send-form').height()

		var urlhash = window.location.hash;

		if( urlhash == "#comanda" ) {
			$('.buy a').fancybox({
		'type': 'inline',
		'autoDimensions': false,
		'width': sendW,
		'height': sendH
	}).trigger('click')
		}

	$('.buy a').fancybox({
		'type': 'inline',
		'autoDimensions': false,
		'width': sendW,
		'height': sendH
	})

	$('#mce-EMAIL[title]').qtip({
		style: {
			classes: 'ui-tooltip-dark ui-tooltip-rounded'
		},
		position: {
	      my: 'bottom left', 
	      at: 'top left'
	   }
	})
})

$(document).ready(function(){

	$('#header .search input.text').focusin(function() {
		$(this).animate({ 'width': '178px' });
		$(this).parent().addClass('focus');
	}).focusout(function() {
		$(this).animate({ 'width': '68px' });
		$(this).parent().removeClass('focus');
	});
	
	$('.partner-bar li a').hover(function() {
	
		$(this).children('.bw').hide()
		$(this).children('.color').show()
	
	}, function() {
	
		$(this).children('.color').hide()
		$(this).children('.bw').show()
	
	})
	
	if( $('.catalog').size() || $('.photo-gallery').size() ) {
//		$('.catalog .gallery .first a').fancybox();	
		$("a[rel=prod-pic]").fancybox();
		$('.photo-gallery a').fancybox();	
	}
	
	$('.catalog #main .media a:eq(2), .catalog #main .media a:eq(5)').addClass('third')
	
	$('.catalog-list li:nth-child(3n+3)').addClass('third')
	
	$('#sidebar .nav > li').has('ul').addClass('has-sub').children('ul').addClass('sub-nav');
	
	$('#contact-form-ro, #quip-comment-box-qcom').submit(function() {
	
		$(this).find('input[type="text"], textarea').each(function() {
		
			var usedinput = $(this);	
			var usedinputtitle = $(this).attr('title');
			
			if( usedinput.val() == usedinputtitle ) {
			
				usedinput.attr('value', "")
			
			}
		
		})
	
	})
	
	$('#lang-select').hover(function() {
	
		$('#lang-select .options').show()
	
	}, function() {
		
		$('#lang-select .options').hide()
	
	})
	
	$('.catalog .sub-nav').each(function() {
	
		$(this).append('<div class="bottom"></div')
	
	})
		
	$('#main .photo-gallery li:nth-child(4n)').addClass('fourth')
	
	$('.form input[type="text"], .form textarea').each(function() {
	
				var usedinput = $(this);		
				var usedinputtitle = $(this).attr('title');
	
	
	
				function addTitle() { 
					if(usedinput.val() == "") {
					$(usedinput).val(usedinputtitle);
				}};
	
				function remove() {
					if(usedinput.val() == usedinputtitle) {
						$(usedinput).val("");
					}
				}
	
				addTitle();
	
				$(this).blur(addTitle).focus(remove);
	
			});
	
	$(".catalog-list li").click(function(){
	     window.location=$(this).find("a").attr("href");
	     return false;
	});
	
	$('.catalog #main .catalog-list li').hover(function() {
	
		$(this).children('.i').stop(true, false).animate({ 'bottom': 0 }, '300', 'easeInOutExpo')
	
	}, function() {
	
		$(this).children('.i').stop(true, false).animate({ 'bottom': '-208px' }, '300', 'easeInOutExpo')
	
	})
	
	$('.program').click(function() {
	
		if( $(this).hasClass('open') ) {
		
			$('.program.open').find('.more').slideUp().end().find('.ico').animate({ 'bottom': 44 }).end().removeClass('open');
		
		} else {
			
			$('.program.open').find('.more').slideUp().end().find('.ico').animate({ 'bottom': 44 }).end().removeClass('open');
			
			$(this).addClass('open');
			$(this).find('.more').slideDown();
			$(this).find('.ico').animate({ 'bottom': 10 })
		
		}
	
		return false;
	
	})
	
	$('.form select').tzSelect();
	
	$('.address-title').click(function() {
		
			var nextElem = $(this).next();

		if( $(this).hasClass('active') ) {
			$(this).removeClass('active').next().slideUp('300', function() { nextElem.removeClass('active') })
		} else {
			var nextActiveElem = $('.address-title.active').next();
			$('.address-title.active').removeClass('active').next().slideUp('300', function() { nextActiveElem.removeClass('active') })
			$(this).addClass('active').next().slideDown('300', function() { nextElem.addClass('active') });
		}
		
	
		
		return false;
	})
	
	$('.catalog .has-sub').hover(function() {
		
		$(this).children('a').addClass('hover');
		$(this).children('ul').show();
	
	}, function() {
	
		$(this).children('a').removeClass('hover');
		$(this).children('ul').hide();
	
	})
	
	$('.faq #main h2').each(function() {
		$(this).nextUntil('h2').wrapAll('<div class="accordion-content" />')
	})
	$('.faq .accordion-content').hide()
	$('.faq #main h2').each(function() {
		 $(this).replaceWith('<h2><a href="#">' + $(this).text() + '</a></h2>');
	})
	
	$('.faq #main h2 a').click(function() {
		var par = $(this).parent()
	
		if( par.hasClass('open') ) {			
			par.next().slideUp('300', function() {
				par.removeClass('open')
			})
		} else {
			var cur = $('.faq #main h2.open')
			cur.next().slideUp('300', function() {
				cur.removeClass('open')
			})
			par.addClass('open').next().slideDown('300')
			
		}
		return false;
	})
	
});

(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = Math.ceil((ph-ah) / 2);
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

