jQuery(document).ready(function($){
	vc_tabsBehaviour();
	vc_slidersBehaviour();
	vc_twitterBehaviour();
	vc_carouselBehaviour();
	
	/*** Toggle click (FAQ) ***/
	$(".wpb_toggle").click(function(e) {
		if ($(this).hasClass('wpb_toggle_title_active')) {
			$(this).removeClass('wpb_toggle_title_active').next().hide();
		} else {
			$(this).addClass('wpb_toggle_title_active').next().show();
		}
		
	});
	
	/** prettyPhoto initialization */
	$("a.prettyPhoto, a.prettyphoto").prettyPhoto();
}); // end jQuery(document).ready



function vc_twitterBehaviour() {
	jQuery('.wpb_twitter_widget .tweets').each(function(index) {
		var this_element = jQuery(this),
			tw_name = this_element.parent().find('.tw_name').text();
			tw_count = this_element.parent().find('.tw_count').text();
		
		this_element.tweet({
			username: tw_name,
			join_text: "auto",
			avatar_size: 0,
			count: tw_count,
			template: "{avatar}{join}{text}{time}",
			auto_join_text_default: "",
			auto_join_text_ed: "",
			auto_join_text_ing: "",
			auto_join_text_reply: "",
			auto_join_text_url: "",
			loading_text: '<span class="loading_tweets">loading tweets...</span>'
        });
	});
}

function vc_slidersBehaviour() {
	//var sliders_count = 0;
	jQuery('.wpb_gallery .wpb_gallery_slides').each(function(index) {
		var this_element = jQuery(this);
		var ss_count = 0;
		
		if (this_element.hasClass('wpb_slider_fading')) {
			var sliderSpeed = 500, sliderTimeout = 5000, slider_fx = 'fade';
			var current_ss;
			
			function slideshowOnBefore(currSlideElement, nextSlideElement, options) {
				jQuery(nextSlideElement).find("div.description").animate({"opacity": 0}, 0);
			}
			
			function slideshowOnAfter(currSlideElement, nextSlideElement, options) {
				jQuery(nextSlideElement).find("div.description").animate({"opacity": 1}, 2000);
			}
			
			this_element
			.before('<div class="ss_nav ss_nav_'+ss_count+'">')
			.cycle({
				fx: slider_fx, // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				pause: 1,
				speed: sliderSpeed,
				timeout: sliderTimeout,
				delay: -ss_count * 1000,
				before: slideshowOnBefore,
				after:slideshowOnAfter,
				pager:  '.ss_nav_'+ss_count
			})
			.find('.description').width(jQuery(this).width() - 20);
			ss_count++;
			
			var simg;
			var max_h, max_w;
			max_h = max_w = 0;
			
			jQuery(this).find("img").each(function(){
				var simg = new Image();
				simg.src = jQuery(this).attr('src');
				simg.onload = function() {
					if (simg.height > max_h) { max_h = simg.height; }
					if (simg.width > max_w) { max_w = simg.width; }
					
					jQuery(current_ss).css({
						"width" : max_w,
						"height" : max_h
					});
				}
			});
			/*
			$j(".small_slideshow").each(function(){
				current_ss = $j(this);
				$j(this)
				.wrap('<div class="relative"></div>')
				.before('<div class="ss_nav ss_nav_'+ss_count+'">')
				.cycle({
					fx: slideEffect, // choose your transition type, ex: fade, scrollUp, shuffle, etc...
					pause: 1,
					speed: slideShowSpeed,
					timeout: slideShowTimeout,
					delay: -ss_count * 1000,
					before: slideshowOnBefore,
					after:slideshowOnAfter,
		//			easing: slideTransitionEffect,
					pager:  '.ss_nav_'+ss_count
				})
				.find('.description').width($j(this).width() - 20);
				ss_count++;
				var simg;
				var max_h, max_w;
				max_h = max_w = 0;
				$j(this).find("img").each(function(){
					var simg = new Image();
					simg.src = $j(this).attr('src');
					simg.onload = function() {
						if (simg.height > max_h) { max_h = simg.height; }
						if (simg.width > max_w) { max_w = simg.width; }
						
						$j(current_ss).css({
							"width" : max_w,
							"height" : max_h
						});
					}
				});
			});*/
			
			/*this_element.cycle({
				fx: slider_fx,
				timeout: sliderTimeout+sliderSpeed,
				speed: sliderSpeed,
				containerResize: 1
			});*/
		}
		
		else if (this_element.hasClass('wpb_slider_nivo')) {
			this_element.nivoSlider({
				effect:'boxRainGrow,boxRain,boxRainReverse,boxRainGrowReverse', // Specify sets like: 'fold,fade,sliceDown'
				slices:15, // For slice animations
				boxCols: 8, // For box animations
				boxRows: 4, // For box animations
				animSpeed:800, // Slide transition speed
				pauseTime:7000, // How long each slide will show
				startSlide:0, // Set starting Slide (0 index)
				directionNav:true, // Next & Prev navigation
				directionNavHide:true, // Only show on hover
				controlNav:true, // 1,2,3... navigation
				keyboardNav:false, // Use left & right arrows
				pauseOnHover:true, // Stop animation while hovering
				manualAdvance:false, // Force manual transitions
				prevText: 'Prev', // Prev directionNav text
				nextText: 'Next' // Next directionNav text
			});
		}
	});
}

function vc_tabsBehaviour() {
	var tabSpeed = 500,
		tab_fx = 'none',
		tabs_count = 0;
	
	jQuery(".wpb_tabs").each(function(){
		jQuery(this).children().addClass("tab");
		jQuery(this)
		.attr("id", "wpb_tabs_"+tabs_count)
		.before('<div class="wpb_tabs_nav wpb_tabs_nav_'+tabs_count+'">')
		.cycle({
			fx: tab_fx,
			timeout: 0,
			speed: tabSpeed,
			containerResize: 1,
			before:  tabsOnBefore, 
			pager:  '.wpb_tabs_nav_'+tabs_count
		});
		var tab = jQuery(this);
		jQuery(".wpb_tabs_nav_"+tabs_count+" a").each(function(){
			var currentTabIndex = jQuery(this).prevAll().length;
			var tabTitle = jQuery("#wpb_tabs_"+tabs_count+" span.tab-title").eq(currentTabIndex).html();
			jQuery(this).html(tabTitle);
		});
		var tabtitle = jQuery(this).find("span").html();
		tabs_count++;
	});
	
	function tabsOnBefore(currSlideElement, nextSlideElement, options) {
		var tabHeight = jQuery(nextSlideElement).outerHeight() + parseInt(jQuery(nextSlideElement).css("margin-top").replace(/[^\d\.]/g, ''));
		jQuery(nextSlideElement).parent().css({"height": tabHeight+"px"});
	}
}

function vc_carouselBehaviour() {
	jQuery(".wpb_carousel").each(function(){
		var carousel_width = jQuery(this).width(),
			visible_count = getColumnsCount(jQuery(this));
		jQuery(this).find('.wpb_wrapper').jCarouselLite({
	        btnNext: ".next",
	        btnPrev: ".prev",
	        visible: visible_count
	    })
	    .width(carousel_width);
	});
}
function getColumnsCount(el) {
	var find = false,
		i = 1;
		
	while (find == false) {
		if (el.hasClass('columns_count_'+i)) {
			find = true;
			return i;
		}
		i++;
	}
}
