jQuery.noConflict();
jQuery(document).ready(function(){
	initFormSlider();
	jQuery('#main .page-image').fadeGallery({
		gallery : '.visual-list',
		prev : false,
		next : false,
		play : false,
		pause : false,
		thumbnails : false,
		autoplay : 5000,
		duration : 400
	});
	var dates = jQuery("#datepicker-from, #datepicker-to").datepicker({
		showOn: 'button',
		buttonImage: '/dev/wp-content/themes/Wasini/images/ico-calendar.gif',
		buttonImageOnly: true,
		onSelect: function(selectedDate) {
			var option = this.id == "datepicker-from" ? "minDate" : "maxDate";
			var instance = jQuery(this).data("datepicker");
			var date = jQuery.datepicker.parseDate(instance.settings.dateFormat || jQuery.datepicker._defaults.dateFormat, selectedDate, instance.settings);
			dates.not(this).datepicker("option", option, date);
		}

	});
	jQuery('#main .twocolumns').fadeGallery({
		gallery : '.image-gallery > ul',
		prev : false,
		next : false,
		play : false,
		pause : false,
		thumbnails : '.control-gallery > ul',
		autoplay : 5000,
		duration : 400
	});
});

// form slider
function initFormSlider (){
	var duration = 1000; // in ms
	jQuery('#main div.wpcf7').each(function(){
		var _hold = jQuery(this);
		var opener = jQuery('.btn-open', _hold);
		var slide = jQuery('.slide', _hold);
		opener.click(function(){
			if (jQuery(this).hasClass('btn-active')) {
				var slide_h = slide.height();
				slide.stop().css({
					height:slide_h
				}).animate({
					height:0
				}, duration , function(){
					jQuery(this).css({
						display:'none',
						height:'auto'
					});
				});
				jQuery(this).removeClass('btn-active');
			} else {
				var slide_h = slide.show().height();
				slide.hide().css({
					height:0
				}).animate({
					height:slide_h
				}, duration , function(){
					jQuery(this).css({height:'auto'});
				});
				jQuery(this).addClass('btn-active');
			}
			return false;
		});
		var t_box = _hold.find('div.wpcf7-response-output');
		var t_f = true, t_t;
		_hold.find('input:submit').click(function(){
			if(t_t) clearTimeout(t_t);
			t_t = setTimeout(function(){
				if(t_box.hasClass('wpcf7-validation-errors')){
					_hold.find('.wpcf7-not-valid-tip').fadeOut(200, function(){ jQuery(this).remove();});
					t_box.slideUp(200);
				}
				else{
					var slide_h = slide.height();
					slide.stop().css({
						height:slide_h
					}).animate({
						height:0
					}, duration , function(){
						jQuery(this).css({
							display:'none',
							height:'auto'
						});
					});
					opener.removeClass('btn-active');
					t_box.slideUp(200);
				}
			}, 3000);
		});
		jQuery('body').click(function(e){
			if(!e) e = window.event;
			var _target = (e.target || e.srcElement);
			if (!jQuery(_target).hasClass('slide-form-hold')) {
				var _parent = _target;
				if (!jQuery(_target).parents('.slide-form-hold').length) {
					if (opener.hasClass('btn-active')){
						opener.trigger('click');
					}
				}
			}
		});
		/*jQuery(document).bind('mousedown', function(e){
			if (opener.hasClass('btn-active')){
				e = e || event;
				var t = e.target || e.srcElement;
				t = jQuery(t);
				if(t.parents('div.wpcf7').length == 0 && t.get(0) != jQuery('div.wpcf7').get(0) && t.parents('#ui-datepicker-div').length == 0 && t.get(0) != jQuery('#ui-datepicker-div').get(0)){
					if(t_t) clearTimeout(t_t);
					_hold.find('.wpcf7-not-valid-tip').fadeOut(200, function(){ jQuery(this).remove();});
					t_box.slideUp(200);
					var slide_h = slide.height();
					slide.stop().css({
						height:slide_h
					}).animate({
						height:0
					}, duration , function(){
						jQuery(this).css({
							display:'none',
							height:'auto'
						});
					});
					opener.removeClass('btn-active');
				}
			}
		});*/
	});
}

//fade gallery
jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = jQuery(this),
			gallery = jQuery(options.gallery, holder),
			prev = jQuery(options.prev, holder),
			next = jQuery(options.next, holder),
			play = jQuery(options.play, holder),
			pause = jQuery(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = jQuery(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('li'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && jQuery.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && jQuery.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					jQuery(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						jQuery(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						jQuery(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		
		// thumbnails
		thumbs.each(function(i){
			jQuery(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}
