(function($) {
	$(document).ready(function() {

                $("a[rel^='prettyPhoto']").prettyPhoto();

		$('.testimonials-wrap').cycle({
			'fx' : 'fade',
			'timeout' : 6000,
			'before': onAfter,
			'pager' : '.testimonial-pager'
		});

                $('.testimonials-wrap2').cycle({
			'fx' : 'fade',
			'timeout' : 0,
			'before': onAfter,
			'pager' : '.testimonial-pager2'
		});
		
		$('.featured.circle').cycle({
			'fx' : 'fade',
			'timeout' : 5000,
			'pager' : '#slider-pager'
		});
		
		$('.circle-item').live('mouseenter', function() { // mousein
					$(this).find('.circle-meta').fadeIn(250);
				}).live('mouseleave', function() { // mouseout
					$(this).find('.circle-meta').fadeOut(250);
				});
		
		$('#navigation').superfish({ 
		            delay:       800,                            // one second delay on mouseout 
		            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		            speed:       'fast',                          // faster animation speed 
		            autoArrows:  false,                           // disable generation of arrow mark-up 
					dropShadows:   false   						// turn on drop shadows
		});
		$('#navigation li ul li:last-child').addClass('last-item');
		
		//center menu under main menu item
		/*$('.sf-menu > li').each(function() {
			var $ul = $(this).children('ul');
			console.log($ul);
			var width = $(this).width();
			console.log(width);
			$ul.css('left', (-((200 - width - 40) / 2)) + 'px')
		});*/
		
		if (jQuery().jcarousel) {
			jQuery("#blog .featured-posts").jcarousel({
			        scroll: 1,
			        initCallback: bcCarouselInit

			    });
		}
		
		function bcCarouselInit(carousel) {
			var $pager = $("<div />").addClass("featured-pager"),
				count = $("#blog .featured-posts").find('li.post').length - 1,
				i,
				active = "";
				
				for (i = 1; i <= count; i++ ) {
					if (i == 1) {
						active  = ' class="activeSlide"';
					}
					$pager.append('<a href="#"'+active+'>'+i+'</a>');
					active = "";
				}
				$pager.insertAfter("#blog .featured-posts");
				
				$('#blog .featured-pager a').bind('click', function() {
						$('#blog .featured-pager a').removeClass('activeSlide');
						$(this).addClass('activeSlide');
				        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				        return false;
				});
		}
				
		if (jQuery().roundabout) {
            $('.featured.caroussel').roundabout({
                autoplay: true,
                autoplayDuration: 5000,
                autoplayPauseOnHover: true,
                minScale: 0.6,
                mixScale: 0.6,
                minOpacity: 1.0,
                btnNext: '#next-slide',
                btnPrev: '#prev-slide',
                easing: 'easeInOut'
							
                                                             
            });

				// count and prepare few stuff
				var	count = $('.roundabout-moveable-item').size(),
					pager = '';

				// prepares the control nav
				for( var i = 1; i <= count; i++ )
				{
					pager = pager+'<a'+((i==1) ? ' class="activeSlide"' : '')+' href="#" data-id="' + (i-1) + '"></a>';
				}

				// adds the control nav
				pager = '<div id="slider-pager-wrap"><div id="slider-pager">'+pager+'</div></div>';
				$(pager).insertAfter($('.caroussel'));

				$('#slider-pager a').click(function() {
					$('.featured.caroussel').roundabout_animateToChild( $(this).attr('data-id') );
					$('#slider-pager a').removeClass('activeSlide');
					$(this).addClass('activeSlide');
					return false;
				});
		}
		
		$('.portfolio-single .portfolio-item-images .portfolio-images').cycle({
			'fx' : 'fade',
			'timeout' : 0,
			'before': onAfter,
			'pager' : '#portfolio-pager'
		});
		
		
		function onAfter(curr, next, opts, fwd) {
		var index = opts.currSlide;
		$('#prev')[index == 0 ? 'hide' : 'show']();
		$('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		//get the height of the current slide
		var $ht = $(this).height();
		//set the container's height to that of the current slide
		$(this).parent().animate({
			'height' : $ht
		});
		}
		
		// Tabs
		// When page loads...
		$(".tab-content").hide(); //Hide all content
		$("ul.sidebar-tabs li:first").addClass("active").show(); //Activate first tab
		$(".tab-content:first").show(); //Show first tab content

		// On Click Event
		$("ul.sidebar-tabs li").click(function() {

			$("ul.sidebar-tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".tab-content").hide(); //Hide all tab content

			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
				
		// Clone applications to get a second collection
		var $data = $(".portfolio").clone();

		//NOTE: Only filter on the main portfolio page, not on the subcategory pages
		$('.portfolio-selector li').click(function(e) {
			
			$(".filter li").removeClass("active");	
			// Use the last category class as the category to filter by. This means that multiple categories are not supported (yet)
			var filterClass=$(this).attr('class').split(' ').slice(-1)[0];

			if (filterClass == 'all-projects') {
				var $filteredData = $data.find('.portfolio-item');
			} else {
				var $filteredData = $data.find('.portfolio-item[data-type*=' + filterClass + ']');
			}
			//$filteredData.find('.portfolio-item').removeClass('last');
			//$filteredData.find('.portfolio-item:nth-child(2n)').addClass('last');
			$(".portfolio").quicksand($filteredData, {
				duration: 800,
				easing: 'swing',
				adjustHeight: 'dynamic'
			}, function() {

			});
			$(this).addClass("active"); 			
			return false;
		});
		
		$.validator.setDefaults({
			submitHandler: function() {
				$('#contact-form').remove('.responseText');
				$('#contact-form').append('<h3 class="responseText">Sending message...</h3>');
				
				var name = $("#name").val();  
				var email = $("#email").val(); 
				var message = $("#comment").val(); 

				var dataString = 'name='+ name + '&email=' + email + '&message=' + message;  

					$.ajax({

					url: "mail.php",
					data: dataString,
					type: "GET",
					success: function(responseText) {
							$('.responseText').text(responseText);

							}
					});
			}
		});

		$("#contact-form .comment-form").validate();
		
		$('.pricing-table tr:first-child th:first-child').addClass('top-left');
		$('.pricing-table tr:first-child th:last-child').addClass('top-right');
		$('.pricing-table tr:last-child td:first-child').addClass('bottom-left');
		$('.pricing-table tr:last-child td:last-child').addClass('bottom-right');
		
		// Mega menu code
		$('#navigation li').each(function() {
			if($(this).find('.mega-menu').length > 0) {
				var pos = $(this).position(),
					width = $(this).width(),
					bgOffset = pos.left + width/2;
				$(this).find('.mega-menu').css({'left' : '-' + pos.left + 'px',
												'background-position' : bgOffset + 'px 0px'});
			}
		});
		
		
	});
	
	if (jQuery().nivoSlider) {
		$(window).load(function() {
	        $('.featured.nivo').nivoSlider({
				'directionNavHide' : true
			});
	    });
	}
	
})(jQuery);
