/***************************
IT-Serve 2011. 
Jquery Settings and Preferences.

Includes: 	External Links
			Dynamic Nav Class
			localScroll
			Tooltips
			LavaLamp			
			Quicksand
			Form Validation
			
Form Placeholder text JS in index.html
****************************/
/*
 * Set and remove the main nav's "clicked" class dynamically
 */
/*<![CDATA[*/
var Lst;
function NavClicks(obj){
 if (typeof(obj)=='string') obj=document.getElementById(obj);
 if (Lst) Lst.className='';
 obj.className='clicked';
 Lst=obj;
}
/*]]>*/



$(document).ready(function(){
	/*
	 * LocalScroll
	 */
	   // Set the ID containing the nav for local scroll 					   
	  $('#header-content').localScroll();
	/*
	 * Tooltips 
	 */
	$("#infographics img[title]").tooltip({
		offset: [10, 20],
		effect: 'slide'	
		})
		//This line enables dynamic use of the tooltip underneath the images when there is no space above
		//.dynamic({ bottom: { direction: 'down', bounce: true } });   
	
	/*
	 * lavaLamp preferences
	 */
	$(function() {
				$("#lava-one, #lava-two, #lava-three").lavaLamp({
					fx: "swing",
					speed: 350,
					click: function(event, menuItem) {
						return false;
					}
				});
			}); 
	/* 
	 * Quicksand preferences for multiple data-set
	 */
	(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');
	  };
	
	 // Set easing, scaling and duration preferences here
		  var $preferences = {
			duration: 600,
			easing: 'easeInOutQuad',
			adjustHeight: 'dynamic',
			useScaling: false
		  };
	  
	  var $list = $('#shuffle');
	  var $data = $list.clone();
	  
	  var $controls = $('ul.gallery-subnav ');
	  
	  $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-0').removeClass('selected-1').removeClass('selected-2');
			$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);
			}
			
			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, $preferences);
			
		  }
		  
		  e.preventDefault();
		});
		
	  }); 
	  
	});
	/*
	 * Form Validation Settings
	 */
			  
	 var validator = $("#contactform").validate({ 
        rules: { 
            fullname: { 
                required: true, 
                minlength: 6               
            },
			telephone: { 
                required: true, 
                minlength: 6               
            },
            email: { 
                required: true, 
                email: true 
            }
		},
        messages: { 
            //enter custom error messages here - currently using defaults as they offer character limit hints and a required status
            },
			
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) {
			//this line appends the error message text to an auto generated label element next to the parent - in this case the input field
                error.appendTo( element.parent() ); 
        }, 
        /* specifying a submitHandler prevents the default submit, good for the demo 
        submitHandler: function() { 
             
        },*/
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        } 
    }); 

}); /* End $(document).ready(function(){ */
