/* ---------------------------------------------------------------------------------------------------
	
	Custom JavaScript
	
--------------------------------------------------------------------------------------------------- */

jQuery(document).ready(function($){
	
	$('#sidebar .separator:last:not(:first)').remove();
	
	/* ---------------------------------------------------------------------------------------------------
		Wrapped
	--------------------------------------------------------------------------------------------------- */
	$('.wrapped').mouseenter(function(){
		$(this).stop().animate({ borderTopColor: '#545454', borderLeftColor: '#545454', borderRightColor: '#545454', borderBottomColor: '#545454 ' }, 500);
	}).mouseleave(function(){
		$(this).stop().animate({ borderTopColor: '#eee', borderLeftColor: '#eee', borderRightColor: '#eee', borderBottomColor: '#eee ' }, 500);
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Social
	--------------------------------------------------------------------------------------------------- */
	$('#header-bar-social a').mouseenter(function(){
		$(this).stop().animate({ opacity : 0.5 }, 200);
	}).mouseleave(function(){
		$(this).stop().animate({ opacity : 0.2 }, 200);
	});
	
	
	/* ---------------------------------------------------------------------------------------------------
		Navigation
	--------------------------------------------------------------------------------------------------- */
	$("ul.sf-menu").superfish({
		autoArrows	: false,
		animation	: {height:'show'},
		speed		: 'fast'
	}); 
	
	
	/* ---------------------------------------------------------------------------------------------------
		Twitter
	--------------------------------------------------------------------------------------------------- */

	var twitter_profile;
	var twitter_amount;

	$('.recent-tweets').each(function(){
		
		twitter_profile = $(this).find('input.twitter-profile').val();
		twitter_amount = $(this).find('input.twitter-amount').val();
		
		$(this).tweet({
			username: twitter_profile,
			join_text: 'auto',
			avatar_size: 32,
			count: twitter_amount,
			auto_join_text_default: '', 
			auto_join_text_ed: '',
			auto_join_text_ing: '',
			auto_join_text_reply: '',
			auto_join_text_url: '',
			loading_text: 'Loading tweets...',
			template: '{time}{join}{text}'
		});
	});
	
	
	/* ---------------------------------------------------------------------------------------------------
		Flickr
	--------------------------------------------------------------------------------------------------- */
	
	var flickr_profile;
	var flickr_amount;

	$('.flickr-stream').each(function(){
		
		flickr_profile = $(this).find('input.flickr-profile').val();
		flickr_amount = $(this).find('input.flickr-amount').val();
		
		$(this).html('');
		
		$(this).jflickrfeed({
			limit: flickr_amount,
			qstrings: { id: flickr_profile },
			itemTemplate: 
			'<li class="flickr-stream-item">' +
				'<a href="{{image_b}}" class="fancybox" rel="group_flicker"><img src="{{image_s}}" alt="{{title}}" /></a>' +
			'</li>'
		});

	});
	
	/* ---------------------------------------------------------------------------------------------------
		Tabs
	--------------------------------------------------------------------------------------------------- */
	$('.tabs-container .tab-content:first-child').siblings('.tab-content').hide();
	$('.tabs-nav li:first-child').addClass('active');
	
	$('.tabs-container .tabs-nav a').live('click', function(e){
		
		e.preventDefault();
		
		$(this).parents('li').addClass('active').siblings('li.active').removeClass('active');
		var tab_id = $(this).parents('li').index();
		$(this).parents('.tabs-container').find('.tab-content').eq(tab_id).show().siblings('.tab-content').hide();
	
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Scroll to top
	--------------------------------------------------------------------------------------------------- */
	$('#footer-to-top-link').click(function () {
		$('body,html').animate({
			scrollTop: 0
		}, 800);
		return false;
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Lightbox container slider
	--------------------------------------------------------------------------------------------------- */
	$('.portfolio-fancybox-container-inner').each(function(){
		var portfolio_single_slider_autoplay = parseInt($('#portfolio-single-slider-autoplay').val());
		$(this).show();
		$(this).cycle({
			fx:	'fade',
			pager:  '',
			timeout: portfolio_single_slider_autoplay
		});
	});
	
	$('.portfolio-fancybox-action-prev').live('click', function(e){
		
		e.preventDefault();
		$(this).parents('.portfolio-fancybox-container-inner').cycle('prev');
		
	});
	
	$('.portfolio-fancybox-action-next').live('click', function(e){
		
		e.preventDefault();
		$(this).parents('.portfolio-fancybox-container-inner').cycle('next');
		
	});
	
	$('.portfolio-fancybox-actions a').live('mouseenter', function(){
		$(this).stop().animate({ opacity : 1 }); 
	}).live('mouseleave', function(){
		$(this).stop().animate({ opacity : 0.4 });
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Portfolio Slider
	--------------------------------------------------------------------------------------------------- */
	$('#portfolio-single-slider').cycle({
		fx:	'fade',
		pager:  '#portfolio-single-slider-nav',
		timeout: 0
	});
	
	$('#portfolio-single-slider-prev').live('click', function(e){
		
		e.preventDefault();
		$('#portfolio-single-slider').cycle('prev');
		
	});
	
	$('#portfolio-single-slider-next').live('click', function(e){
		
		e.preventDefault();
		$('#portfolio-single-slider').cycle('next');
		
	});
	
	/* --------------------------------------------------
		Portfolio - Filter - Quicksand
	-------------------------------------------------- */
	$('#portfolio-filter a').click(function(e) {
		e.preventDefault();  
		$('.portfolio-filter-active').removeClass('portfolio-filter-active');
		$(this).addClass('portfolio-filter-active');
		$.get( $(this).attr('href'), function(data) {
			$('.portfolio-quicksand').quicksand( $(data).find('li'), { adjustHeight: 'dynamic' }, function(){
				
				$('a.fancybox').prettyPhoto({
					show_title : false
				});
				
				$('.portfolio-fancybox-container-inner').each(function(){
					$(this).show();
					$(this).cycle({
						fx:	'fade',
						pager:  '',
						timeout: 0
					});
				});
				
			});
		});  
	});
	
	
	/* ---------------------------------------------------------------------------------------------------
		Fancybox
	--------------------------------------------------------------------------------------------------- */
	$('#posts-slider a.fancybox').each(function(){
		
		var rel_attr = $(this).attr('rel');
		rel_attr = 'posts_slider_' + rel_attr;
		
		$(this).attr('rel', rel_attr);
		
	});
	
	$('#content a.fancybox').each(function(){
		
		var rel_attr = $(this).attr('rel');
		rel_attr = 'content_' + rel_attr;
		
		$(this).attr('rel', rel_attr);
		
	});
	
	$('a.fancybox').prettyPhoto({
		show_title : false
	});
	
	$('.portfolio-fancybox-init').live('click', function(e){
		
		e.preventDefault();
		$(this).parents('.portfolio-post-entry ').find('.portfolio-fancybox-container a:first').click();
		
	});

	
	/* ---------------------------------------------------------------------------------------------------
		Contact Form
	--------------------------------------------------------------------------------------------------- */
	$('.contact-form form').submit(function(e){
	
		e.preventDefault();
		
		var submit_path = $(this).attr('action');
		
		var name = $(this).find('.contact-form-name');
		var email = $(this).find('.contact-form-email');
		var message = $(this).find('.contact-form-message');
		
		var status = true;
		
		name.removeClass('contact-form-invalid');
		email.removeClass('contact-form-invalid');
		message.removeClass('contact-form-invalid');
		
		if(name.val() == ''){
			name.addClass('contact-form-invalid');
			status = false;
		}
		
		if(email.val() == ''){
			email.addClass('contact-form-invalid');
			status = false;
		}
		
		if(message.val() == ''){
			message.addClass('contact-form-invalid');
			status = false;
		}
		
		if(status == true){
			
			$(this).parents('.contact-form').load(submit_path, $(this).serialize());
			
		}
	
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Toggle
	--------------------------------------------------------------------------------------------------- */
	$('.content-box-toggle').each(function(){
		
		var content_box = $(this).parents('.content-box');
		
		$(this).height($(this).height());
		
		if(content_box.hasClass('content-box-toggle-state-closed')){
			content_box.addClass('content-box-toggle-closed');
			content_box.find('.content-box-content').hide();
		}else{
			content_box.addClass('content-box-toggle-open');
		}		
		
	});
	
	$('.content-box-toggle-open .content-box-toggle').live('click', function(e){
		
		e.preventDefault();
		
		var content_box = $(this).parents('.content-box');
		content_box.find('.content-box-content').slideUp(200);
		
		content_box.removeClass('content-box-toggle-open').addClass('content-box-toggle-closed');
		
	});
	
	$('.content-box-toggle-closed .content-box-toggle').live('click', function(e){
		
		e.preventDefault();
		
		var content_box = $(this).parents('.content-box');
		content_box.find('.content-box-content').slideDown(200);
		
		content_box.removeClass('content-box-toggle-closed').addClass('content-box-toggle-open');
		
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Search
	--------------------------------------------------------------------------------------------------- */
	$('input[name="s"]').focus(function(){
		
		var search_text = $(this).val();
		var search_alt = $(this).attr('alt');
		
		if(search_text == search_alt){
			$(this).val('');
		}
		
	});
	
	$('input[name="s"]').blur(function(){
		
		var search_text = $(this).val();
		var search_alt = $(this).attr('alt');
		
		if(search_text == ''){
			$(this).val(search_alt);
		}
		
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Posts Slider Hover Animation
	--------------------------------------------------------------------------------------------------- */
	$('#posts-slider .post-entry-special').mouseenter(function(){
		$(this).stop().animate({ marginTop : 0 }, 100);
	}).mouseleave(function(){
		$(this).stop().animate({ marginTop : 10 }, 100);
	});
	
});

jQuery(window).load(function(){
	
	/* ---------------------------------------------------------------------------------------------------
		Accordion
	--------------------------------------------------------------------------------------------------- */
	if(jQuery('#accordion').length){
		
		var accordion_width = jQuery('#accordion').width();
		var accordion_spacing = 5;
		var accordion_item_amount = jQuery('#accordion-inner li').length;
		var accordion_item_width = accordion_width / accordion_item_amount - (accordion_spacing - (accordion_spacing / accordion_item_amount));
		
		jQuery('#accordion-inner li').css({ 'width' : accordion_item_width });
		
		jQuery('#accordion-inner').kwicks({  
			max : 593,  
			spacing : accordion_spacing  
		});
		
		jQuery('#slider-load').fadeOut(400, function(){
			
			jQuery('#accordion-inner').fadeIn(400);
			var accordion_height = jQuery('#accordion-inner img').height();
			jQuery('#accordion-inner').height(accordion_height);
			
		});
		
		jQuery('#accordion-inner li').mouseenter(function(){
			jQuery(this).find('.accordion-slide-info').stop().animate({ opacity : 1 }, 500);
		}).mouseleave(function(){
			jQuery(this).find('.accordion-slide-info').stop().animate({ opacity : 0 }, 500);
		});
	
	}
	
	function header_slider_before(curr, next, opts, fwd) {
		
		var index = opts.currSlide;
		//get the height of the current slide
		var $ht = jQuery(this).height();
		//set the container's height to that of the current slide
		jQuery(this).parent().animate({height: $ht}, 200);
		
		/* Change active element */
		if(fwd == true){
			var current_item = jQuery('.active-slider-nav-item').attr('jcarouselindex');
			if(current_item > 2){ jQuery('#slider-nav-container .jcarousel-next').click(); }
			jQuery('.active-slider-nav-item').removeClass('active-slider-nav-item').next('li').addClass('active-slider-nav-item').find('img').stop().animate({ opacity : 1 }, 400).parents('li').siblings('li').find('img').stop().animate({ opacity : 0.4 }, 400);
		}else{
			jQuery('.active-slider-nav-item').removeClass('active-slider-nav-item').prev('li').addClass('active-slider-nav-item').find('img').stop().animate({ opacity : 1 }, 400).parents('li').siblings('li').find('img').stop().animate({ opacity : 0.4 }, 400);
		}
		
	}
	
	if(jQuery('#posts-slider').length){
	
		jQuery('#slider-prev, #slider-next').show();
	
		jQuery('#slider-load').animate({opacity : 0, height : 0}, 500, function(){
			
			jQuery('#posts-slider').animate({ height : jQuery('#slider-inner').height() }, 500, function(){
			
				jQuery('#slider-inner').fadeIn(500, function(){
					
					if(jQuery('#posts-slider').length){
					
						if(jQuery('#posts-slider .one-half').length){
							
							if(jQuery('#posts-slider .one-half').length > 2){
								
								jQuery('#posts-slider #slider-next').animate({ opacity : 1, right : -70 }, 200);
								jQuery('#posts-slider #slider-prev').animate({ opacity : 1, left : -70 }, 200);
								
							}
							
						}else if(jQuery('#posts-slider .one-third').length){
						
							if(jQuery('#posts-slider .one-third').length > 3){
								
								jQuery('#posts-slider #slider-next').animate({ opacity : 1, right : -70 }, 200);
								jQuery('#posts-slider #slider-prev').animate({ opacity : 1, left : -70 }, 200);
								
							}
						
						}else if(jQuery('#posts-slider .one-fourth').length){
							
							if(jQuery('#posts-slider .one-fourth').length > 4){
								
								jQuery('#posts-slider #slider-next').animate({ opacity : 1, right : -70 }, 200);
								jQuery('#posts-slider #slider-prev').animate({ opacity : 1, left : -70 }, 200);
								
							}
							
						}
						
					}
					
				});
				
				jQuery('#posts-slider .portfolio-fancybox-container-inner').each(function(){
					jQuery(this).show();
					jQuery(this).cycle({
						fx:	'fade',
						pager:  '',
						timeout: 0
					});
				});
				
			});
			
		});
		
	}else if(jQuery('#slider').length){
		
		jQuery('#slider-nav-prev, #slider-nav-next').css({ opacity : 0.5 });
		
		jQuery('#slider-nav-prev, #slider-nav-next').mouseenter(function(){
			jQuery(this).stop().animate({ opacity : 1 }, 400);
		}).mouseleave(function(){
			jQuery(this).stop().animate({ opacity : 0.5 }, 400);
		});
		
		jQuery('#slider').css({ 'background-image' : 'none' });
		
		jQuery('#slider-inner').height(1).show();
	
		var top_slider_autoplay_speed = parseInt(jQuery('#top-slider-autoplay-speed').val());
	
		jQuery('#slider #slider-inner').cycle({
			fx:	'scrollHorz',		
			timeout: top_slider_autoplay_speed,
			before: header_slider_before,
			speed: 300,
			pager:  '#slider-nav', 
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '#slider-nav li:eq(' + idx + ') a'; 
			} 
		});
		
		jQuery('#slider-nav-container').show();
		jQuery('#slider-nav').jcarousel({
			scroll : 1,
			wrap : 'circular'
		});
		
	}else if(jQuery('#accordion').length){
		
		
		
	}
	
	/* ---------------------------------------------------------------------------------------------------
		Header Slider
	--------------------------------------------------------------------------------------------------- */
	
	var jcarousel_wrap = jQuery('#posts-slider-wrap').val();
	var jcarousel_item_width = jQuery('#slider-inner li').outerWidth(true);
	jQuery('#posts-slider #slider-inner').jcarousel({
		itemFallbackDimension : jcarousel_item_width,
		scroll : 1,
		wrap : jcarousel_wrap
	});
	
	jQuery('#slider-nav li:first').addClass('active-slider-nav-item');
	
	jQuery('#slider-nav li:not(.active-slider-nav-item)').live('mouseenter', function(){
		jQuery(this).find('img').stop().animate({ opacity : 0.8 }, 400);
	}).live('mouseleave', function(){
		jQuery(this).find('img').stop().animate({ opacity : 0.5 }, 400);
	});	
	
	jQuery('#slider-nav li').live('click', function(){
		
		jQuery('.active-slider-nav-item').removeClass('active-slider-nav-item');
		jQuery(this).addClass('active-slider-nav-item').find('img').stop().animate({ opacity : 1 }, 400);;
		jQuery(this).siblings('li').find('img').stop().animate({ opacity : 0.5 }, 400);
		
	});
	
	jQuery('#slider-container #slider-prev').live('click', function(e){
		
		e.preventDefault();
		jQuery('#slider-inner').cycle('prev');
		jQuery('#slider-nav-container .jcarousel-prev').click();
		
	});
	
	jQuery('#slider-container #slider-next').live('click', function(e){
		
		e.preventDefault();
		jQuery('#slider-inner').cycle('next');
		jQuery('#slider-nav-container .jcarousel-next').click();
		
	});
	
	jQuery('#posts-slider #slider-prev').live('click', function(e){
		
		e.preventDefault();
		jQuery('.jcarousel-prev').click();
		
	});
	
	jQuery('#posts-slider #slider-next').live('click', function(e){
		
		e.preventDefault();
		jQuery('.jcarousel-next').click();
		
	});
	
	jQuery('#slider-nav-prev').live('click', function(e){
		
		jQuery('#slider-nav-container .jcarousel-prev').click();
		e.preventDefault();
		
	});
	
	jQuery('#slider-nav-next').live('click', function(e){
		
		e.preventDefault();
		jQuery('#slider-nav-container .jcarousel-next').click();
		
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Flickr fancybox
	--------------------------------------------------------------------------------------------------- */
	jQuery(".flickr-stream a.fancybox").prettyPhoto({
		show_title : false
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Testimonials
	--------------------------------------------------------------------------------------------------- */
	jQuery('.testimonials-scroller').each(function(){
		var testimonials_delay = jQuery(this).parents('.testimonials-scroller-container').find('input.testimonials-delay').val();
		jQuery(this).show();
		jQuery(this).cycle({
			fx:	'fade',		
			timeout: testimonials_delay
		});
	});
	
	/* ---------------------------------------------------------------------------------------------------
		Slider Posts
	--------------------------------------------------------------------------------------------------- */
	jQuery('.slider-posts-widget-container').each(function(){
		jQuery(this).show();
		var slider_posts_widget_speed = parseInt(jQuery(this).parents('.jw-slider-posts-widget').find('input.slider-posts-widget-speed').val());
		jQuery(this).cycle({
			fx:	'scrollHorz',		
			timeout: slider_posts_widget_speed,
			speed: 300
		});
	});
	
	jQuery('.slider-posts-prev').live('click', function(e){
		
		e.preventDefault();
		jQuery(this).parents('.jw-slider-posts-widget').find('.slider-posts-widget-container').cycle('prev');
		
	});
	
	jQuery('.slider-posts-next').live('click', function(e){
		
		e.preventDefault();
		jQuery(this).parents('.jw-slider-posts-widget').find('.slider-posts-widget-container').cycle('next');
		
	});
	
});
