jQuery(document).ready(function(){
	jQuery(".wrapper a img, #footer a img, .flickr_badge_image").fadeTo("slow", 1.0); // This sets the opacity of the thumbs to fade down to 100% when the page loads

	jQuery(".wrapper a img, #footer a img, .flickr_badge_image").hover(function(){
		jQuery(this).fadeTo("slow", 0.6); // This should set the opacity to 60% on hover
	},function(){
   		jQuery(this).fadeTo("slow", 1.0); // This should set the opacity back to 100% on mouseout
	});
});

/*-----------------------------------------------------------------------------------*/
/*	Filter States
/*-----------------------------------------------------------------------------------*/

	var filterLinks = jQuery('#filter li');
	
	filterLinks.click( function(e) {
	
		filterLinks.removeClass('active');
		
		filterLinks.not(this).find('span.border').fadeOut(100);
		
		jQuery(this).addClass('active');
		
		e.preventDefault();
	});
	
	filterLinks.hover( function() {
		jQuery(this).not('.active').find('.border').stop().css({
			opacity: 0,
			display: 'block'
		}).animate({
			opacity: 1
		}, 150);
			
	}, function() {
		jQuery(this).not('.active').find('.border').stop().fadeOut(150);
	});

	
/*-----------------------------------------------------------------------------------*/
/*	Portfolio Sorting
/*-----------------------------------------------------------------------------------*/
	
	if (jQuery().quicksand) {

		(function($) {
			
			$.fn.sorted = function(customOptions) {
				var options = {
					reversed: false,
					by: function(a) {
						return a.text();
					}
				};
		
				$.extend(options, customOptions);
		
				$data = jQuery(this);
				arr = $data.get();
				arr.sort(function(a, b) {
		
					var valA = options.by($(a));
					var valB = options.by($(b));
			
					if (options.reversed) {
						return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
					} else {		
						return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
					}
			
				});
		
				return $(arr);
		
			};
		
		})(jQuery);
		
		jQuery(function() {
		
			var read_button = function(class_names) {
				
				var r = {
					selected: false,
					type: 0
				};
				
				for (var i=0; i < class_names.length; i++) {
					
					if (class_names[i].indexOf('selected-') == 0) {
						r.selected = true;
					}
				
					if (class_names[i].indexOf('segment-') == 0) {
						r.segment = class_names[i].split('-')[1];
					}
				};
				
				return r;
				
			};
		
			var determine_sort = function($buttons) {
				var $selected = $buttons.parent().filter('[class*="selected-"]');
				return $selected.find('a').attr('data-value');
			};
		
			var determine_kind = function($buttons) {
				var $selected = $buttons.parent().filter('[class*="selected-"]');
				return $selected.find('a').attr('data-value');
			};
		
			var $preferences = {
				duration: 500,
				adjustHeight: 'dynamic'
			}
		
			var $list = jQuery('.grid');
			var $data = $list.clone();
		
			var $controls = jQuery('#filter');
		
			$controls.each(function(i) {
		
				var $control = jQuery(this);
				var $buttons = $control.find('a');
		
				$buttons.bind('click', function(e) {
		
					var $button = jQuery(this);
					var $button_container = $button.parent();
					
					var button_properties = read_button($button_container.attr('class').split(' '));      
					var selected = button_properties.selected;
					var button_segment = button_properties.segment;
		
					if (!selected) {
		
						$buttons.parent().removeClass();
						$button_container.addClass('selected-' + button_segment);
		
						var sorting_type = determine_sort($controls.eq(1).find('a'));
						var sorting_kind = determine_kind($controls.eq(0).find('a'));
		
						if (sorting_kind == 'all') {
							var $filtered_data = $data.find('li');
						} else {
							var $filtered_data = $data.find('li.' + sorting_kind);
						}
		
						var $sorted_data = $filtered_data.sorted({
							by: function(v) {
								return parseInt(jQuery(v).find('.count').text());
							}
						});
		
						$list.quicksand($sorted_data, $preferences, function () {
						});
						
						//console.log($sorted_data);
			
					}
			
					e.preventDefault();
					
				});
			
			}); 
			
		});
	
	}


/***************************************************
		DROP DOWN JAVASCRIPT
***************************************************/

jQuery(document).ready(function() {
	jQuery("div.panel_button").click(function(){
		jQuery("div#panel").animate({
			height: "250px"
		})
		.animate({
			height: "200px"
		}, "fast");
		jQuery("div.panel_button").toggle();
	
	});	
	
   jQuery("div#hide_button").click(function(){
		$("div#panel").animate({
			height: "0px"
		}, "fast");
		
	
   });	
	
});

/***************************************************
				  SOCIAL ICON HOVERS
***************************************************/


(function(jQuery) {
    jQuery.fn.tipsy = function(options) {

        options = jQuery.extend({}, jQuery.fn.tipsy.defaults, options);
        
        return this.each(function() {
            
            var opts = jQuery.fn.tipsy.elementOptions(this, options);
            
            jQuery(this).hover(function() {

                jQuery.data(this, 'cancel.tipsy', true);

                var tip = jQuery.data(this, 'active.tipsy');
                if (!tip) {
                    tip = jQuery('<div class="tipsy"><div class="tipsy-inner"/></div>');
                    tip.css({position: 'absolute', zIndex: 100000});
                    jQuery.data(this, 'active.tipsy', tip);
                }

                if (jQuery(this).attr('title') || typeof(jQuery(this).attr('original-title')) != 'string') {
                    jQuery(this).attr('original-title', jQuery(this).attr('title') || '').removeAttr('title');
                }

                var title;
                if (typeof opts.title == 'string') {
                    title = jQuery(this).attr(opts.title == 'title' ? 'original-title' : opts.title);
                } else if (typeof opts.title == 'function') {
                    title = opts.title.call(this);
                }

                tip.find('.tipsy-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);

                var pos = jQuery.extend({}, jQuery(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                tip.get(0).className = 'tipsy'; // reset classname in case of dynamic gravity
                tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
                var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
                var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(this) : opts.gravity;

                switch (gravity.charAt(0)) {
                    case 'n':
                        tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                        break;
                    case 's':
                        tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                        break;
                    case 'e':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                        break;
                    case 'w':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                        break;
                }

                if (opts.fade) {
                    tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1.0});
                } else {
                    tip.css({visibility: 'visible'});
                }

            }, function() {
                jQuery.data(this, 'cancel.tipsy', false);
                var self = this;
                setTimeout(function() {
                    if (jQuery.data(this, 'cancel.tipsy')) return;
                    var tip = jQuery.data(self, 'active.tipsy');
                    if (opts.fade) {
                        tip.stop().fadeOut(function() { jQuery(this).remove(); });
                    } else {
                        tip.remove();
                    }
                }, 100);

            });
            
        });
        
    };
    
    // Overwrite this method to provide options on a per-element basis.
    // For example, you could store the gravity in a 'tipsy-gravity' attribute:
    // return jQuery.extend({}, options, {gravity: jQuery(ele).attr('tipsy-gravity') || 'n' });
    // (remember - do not modify 'options' in place!)
    jQuery.fn.tipsy.elementOptions = function(ele, options) {
        return jQuery.metadata ? jQuery.extend({}, options, jQuery(ele).metadata()) : options;
    };
    
    jQuery.fn.tipsy.defaults = {
        fade: false,
        fallback: '',
        gravity: 'e',
        html: false,
        title: 'title'
    };
    
    jQuery.fn.tipsy.autoNS = function() {
        return jQuery(this).offset().top > (jQuery(document).scrollTop() + jQuery(window).height() / 2) ? 's' : 'n';
    };
    
    jQuery.fn.tipsy.autoWE = function() {
        return jQuery(this).offset().left > (jQuery(document).scrollLeft() + jQuery(window).width() / 2) ? 'e' : 'w';
    };
    
})(jQuery);
