/************************************************************************/// /* SORTING *//************************************************************************/

(function($) {
	$.fn.sorted = function(customOptions) {
		var options = {
			reversed: false,
			by: function(a) {
				return a.text();
			}
		};
		$.extend(options, customOptions);
	
		$data = $(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);

$(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: 800,
    easing: 'easeInOutQuad',
    adjustHeight: false
  };
  
  var $list = $('#gallery');
  var $data = $list.clone();
  
  var $controls = $('ul.gallerynav');
  
  $controls.each(function(i) {
    
    var $control = $(this);
    var $buttons = $control.find('a');
    
    $buttons.bind('click', function(e) {
      
      var $button = $(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('selected-1'); $button_container.addClass('selected-' + 1);
        
        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);
        }
        
        if (sorting_type == 'size') {
          var $sorted_data = $filtered_data.sorted({
            by: function(v) {
              return parseFloat($(v).find('span').text());
            }
          });
        } else {
          var $sorted_data = $filtered_data.sorted({
            by: function(v) {
              return $(v).find('strong').text().toLowerCase();
            }
          });
        }
        
        $list.quicksand($sorted_data, {
      duration: 800,
      easing: 'easeInOutQuad'
    }, function() {

                                $("a.fancybox").fancybox();
                                $("a.videoframe").click(function() {
                 $.fancybox({
                  'padding'             : 10,
                  'autoScale'   : false,
                  'transitionIn'        : 'none',
                  'transitionOut'       : 'none',
                  'title'               : this.title,
                  'width'               : 640,
                  'height'              : 385,
                  'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                  'type'                : 'swf',    // <--add a comma here
                  'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
                  });
                 return false;

            });
                                $('.boxgrid.caption').hover(function(){
					$(".cover", this).stop().animate({top:'110px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'140px'},{queue:false,duration:160});
				});
                        });
        
      } 
      
      
           e.preventDefault();
      
      
    });
    
  }); 

  
});

			
/************************************************************************/// // NIVO SLIDER ///************************************************************************/
			






/************************************************************************/// // fancybox ///************************************************************************/

$(document).ready(function(){
				$(".fancybox").fancybox();
				$("a.videoframe").click(function() {
                 $.fancybox({
                  'padding'             : 10,
                  'autoScale'   : false,
                  'transitionIn'        : 'none',
                  'transitionOut'       : 'none',
                  'title'               : this.title,
                  'width'               : 640,
                  'height'              : 385,
                  'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                  'type'                : 'swf',    // <--add a comma here
                  'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
                  });
                 return false;

            });

			});
			
/************************************************************************/// // CAPTION ///************************************************************************/
			
$(document).ready(function(){
				//Caption Sliding (Partially Hidden to Visible)
				$('.boxgrid.caption').hover(function(){
					$(".cover", this).stop().animate({top:'110px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'140px'},{queue:false,duration:160});
				});
			});
			

			
/************************************************************************/// // NEWS ///************************************************************************/

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {

    jQuery('#newslist-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#newslist-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

// Ride the carousel...
jQuery(document).ready(function() {
    jQuery("#newslist").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
        vertical: true
    });
});


      $(document).ready(function(){

        $('a.menuitem[href*=#]').click(function() {

          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;

            }

          }

        });

      });

/************************************************************************/// HOVER EFFECT /************************************************************************/
			
// If the HTML document is ready to be manipulated
$(document).ready(function(){
	// Add the hover handler to the link
	$("ul.social li a").hover(
		function(){
			$(this).find("img").animate({top : '-4px'}, 200);
		},
		function(){ 
			$(this).find("img").animate({top : '0px'}, 200);
		}
	);
});

/************************************************************************/// SCROLL /************************************************************************/

$(function()
			{
				// this initialises the demo scollpanes and makes it reinitialise itself once it's images have loaded...
				var settings = {
					scrollbarWidth: 4,
					dragMaxHeight: 100
				};
				$('.scroll-pane')
					.jScrollPane(settings)
					$.featureList(
				$("#newslist ul li a"),
				$(".entry"), {
					start_item	:	0,
					transition_interval : 0
				}
			);
			});

/************************************************************************/// MENU /************************************************************************/

$(document).ready(function(){
            
            $("ul#menu li a").blend({speed:700});
            
        });
        
