/* === Cufon Font replace === */

Cufon.replace('nav a', {hover: true})('.media-wrap #filtering-nav a', {hover: true})('.media-wrap #media-cat-nav a', {hover: true});
Cufon.set('fontFamily', 'Museo 500').replace('nav a')('nav label')('article#media-hero p')('aside a#media-cat');
Cufon.set('fontFamily', 'Museo 300').replace('.media-wrap #filtering-nav a')('.media-wrap aside h3')('#media-content h2')('#media-content h3')('#media-content h4')('#media-content h5')('#media-content h6');

/* === Fanybox === */

$(function() {
	$("a.zoom").fancybox();
	$("img.zoom").parent().fancybox();
});

/* === Fader === */

$(function() {
	$.fn.heroSwap = function(options) {
		var o = $.extend({}, $.fn.heroSwap.defaults, options);
		o.container = '#' + $(this).attr('id');
		o.container_static = $(this).attr('id');
		o.animation_ref = o.container_static + '_' + o.animation_ref;
		var timer, continue_cycle = true, next_hero, current_hero = o.default_hero;
		var heroes = $(this).find(o.content_class + " li");
		var navs = $(this).find(o.nav_class + " li");
		var swap = function(a,b) {
			if(a != b) {
				set_active(b);
				var fout = $(heroes[a]);
				var fin = $(heroes[b]);
				fout.fadeOut(o.fade_time);
				fin.fadeIn(o.fade_time);
			}
		}
		var set_active = function(i)
		{
			$(navs[current_hero]).removeClass('active');
			current_hero = i;
			$(navs[i]).addClass('active');
		}
		var get_next_hero = function(i)
		{
			var next = current_hero + 1;
			if(next+1 > heroes.length)
			{
				next_hero = 0;
			}
			else
			{
				next_hero = next;
			}
		}
		var cycle = function(type) {
			if (continue_cycle == true) {
				if (heroes.length >= 1) {
					get_next_hero(current_hero);
					
					timer = setTimeout(function(){
						swap(current_hero, next_hero);
						cycle();
					}, o.cycle_delay);
				}
			}
		}
		if(o.cycle == true) {
			cycle(o.type);
		}
		$(this).find(o.nav_class + " li").each(function(i) {
			$(heroes[i]).css({
				'position':'absolute',
				'top':'0',
				'left':'0',
				'display':'list-item',
				'overflow':'hidden'
			});
			
			if(i != current_hero)
			{
				$(heroes[i]).css({
					'display':'none'
				});
			}
			$(this).click(function() {
				var clicked_nav = navs.index(this);
				if(o.cycle == true)
				{
					clearTimeout(timer);
					continue_cycle = false;
				}
				swap(current_hero, clicked_nav);
				return false;
			});
		});
		set_active(o.default_hero);
	};
	$.fn.heroSwap.defaults = {
		default_hero	: 0,
		container		: "#hero",
		nav_class		: ".hero-nav",
		content_class	: ".hero-content",
		animation_ref	: "in_anim_0o0o",
		cycle			: true,
		cycle_delay		: 4000,
		type			: 'fade',
		fade_time		: 1500
	};
});

/* === Panel Navigation === */

$(function() {
   
   if (!$.browser.msie) {
	$("header").css({ opacity: 0.95, height: 85});
	$("header ul a").click(function () {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
			$("header").addClass("selected").delay(100).animate({height: "325", opacity: 0.98}, "slow");
			$("header ul").css({height:'250px'});
		}
	});
	  $("header").mouseleave(function () {
		$(this).removeClass("selected").delay(500).animate({height: "85", opacity: 0.95}, "slow");
	  }
	);
	}
	else{
	$("header").css({ height: 85});
	$("header ul a").click(function () {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
			$("header").addClass("selected").delay(100).animate({height: "325"}, "slow");
			$("header ul").css({height:'250px'});
		}
	});
	  $("header").mouseleave(function () {
		$(this).removeClass("selected").delay(500).animate({height: "85"}, "slow");
	  }
	);	
	}
});
$(function(){
	$("header ul li").hover(function() {
			$(this).addClass("nav-hover");
		},
		function() {
			$(this).removeClass("nav-hover");
		}
	)
});

/* === Smooth Scroll === */

$(function(){
    $('a[href*=#]').click(function() {
    	if($(this).hasClass('no-scroll'))return false;
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);   
                return false; 
            } 
        } 
    }); 
});


/* === Masonry Nav Active === */

$(function(){
	$('#filtering-nav a').click(function(){
		$("#filtering-nav li.active a").parent().removeClass("active");
		$(this).parent().addClass("active");
	});
});

/* === Forms Hover === */

$(function(){
	$("input,textarea").focus(function(){
		$(this).addClass("form-active");
	});
});
$(function(){
	$("input,textarea").blur(function(){
		$(this).removeClass("form-active");
	});
});


/* === Comment Hover === */

$(function(){
	$('#submit').hover(function(){
		$("#commentform h4").addClass("hover");
	},
	function(){
		$("#commentform h4").removeClass("hover");
	});
});

/* === Showcase Masonry === */

$(function(){
  var 
	speed = 1000,   // animation speed
	$wall = $('#showcase').find('.wrap'),

	masonryOptions = {         // initial masonry options
	  columnWidth: 119, 
	  itemSelector: '.box:not(.invis)',
	  animate: true,
	  animationOptions: {
		duration: speed,
		queue: false
	  }
	};

  // run on window.load so we can capture any incoming hashes
  $(window).load(function(){
	// run masonry on start-up to capture all the boxes we'll need
	$wall.masonry(masonryOptions);
	if ( window.location.hash ) {
	  // get rid of the '#' from the hash
	  var possibleFilterClass = window.location.hash.replace('#', '');
	  switch (possibleFilterClass) {
	  // if the hash matches the following words
	  case 'red' : case 'gray' : case 'black' : 
		// set masonry options animate to false
		masonryOptions.animate = false;
		// hide boxes that don't match the filter class
		$wall.children().not('.'+possibleFilterClass)
		  .toggleClass('invis').hide();
		// run masonry again, this time with the necessary stuff hidden
		$wall.masonry(masonryOptions);
		break;
	  }
	}

  });

  $('#filtering-nav a').click(function(){
	var 
	  color = $(this).attr('class'),
	  filterClass = '.' + color;
	;

	if(filterClass=='.all') {
	  // show all hidden boxes
	  $wall.children('.invis')
		.toggleClass('invis').fadeIn(speed);
	} else {
	  // hide visible boxes 
	  $wall.children().not(filterClass).not('.invis')
		.toggleClass('invis').fadeOut(speed);
	  // show hidden boxes
	  $wall.children(filterClass+'.invis')
		.toggleClass('invis').fadeIn(speed);

	}
	$wall.masonry({ animate: true });
	// set hash in URL
	window.location.hash = color;
	return false;
  });

});

/* === Media === */

/*window.onscroll = function() {
    if( window.XMLHttpRequest ) {
        if (document.documentElement.scrollTop > 734 || self.pageYOffset > 734) {
            $('#aside-wrap').css('position','fixed');
            $('#aside-wrap').css('top','20px');
        } else if (document.documentElement.scrollTop < 734 || self.pageYOffset < 734) {
            $('#aside-wrap').css('position','absolute');
            $('#aside-wrap').css('top','754px');
        }
    }
}*/
