$(function(){

	// Slides preferances
	$('.slides').slides({
		preload: true,
		preloadImage: 'http://lobinas.eu/kiloo/demetra/images/loading.gif',
		play: 6000,
		pause: 2500,
		hoverPause: true,
		generatePagination: false
	});
	
	// Clear textarea value on focus
	$(".comments_form textarea")
	  .focus(function() {
	        if (this.value === this.defaultValue) {
	            this.value = '';
	        }
	  })
	  .blur(function() {
	        if (this.value === '') {
	            this.value = this.defaultValue;
	        }
	});
	
	// Zebra rows for chart table
	$('.chart tr:odd').addClass('odd');
	$('.chart tr:even').addClass('even');
	
	// Handle 'Book Analysis' form
	(function() {
 		
 		// Function to get $_GET array
 		function getQueryParams(qs) {
		    qs = qs.split("+").join(" ");
		    var params = {},
		        tokens,
		        re = /[?&]?([^=]+)=([^&]*)/g;
		
		    while (tokens = re.exec(qs)) {
		        params[decodeURIComponent(tokens[1])]
		            = decodeURIComponent(tokens[2]);
		    }
		
		    return params;
		}

		var $_GET = getQueryParams(document.location.search);		
		var product = $_GET['a'];
		
		// Make it an index
		product--;
		
		// Check selected checkbox
		var $product = $('.products .wpcf7-list-item:eq(' + product +') label input');
		$product.prop('checked', true);		
		
	})();
	
	
	// Handle print links
	(function() {
 		 var $print = $('.print');
 		 
 		 $print.show();
 		 
 		 $print.click(function(){
 		 	window.print();
 		 });
 		 
	})();
		
	//$(".entry a:not(.rss, .nofancy) img").parent().fancybox();
	//$("a.swf").fancybox();
    
    // Be Fancy
    (function($) {
	    $.fn.befancy = function(options) {
	    	debug(this);
			var opts = $.extend({}, $.fn.befancy.defaults, options);
			
			return this.each(function() {
				$this = $(this);
				// build element specific options
				var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
				
				var parent = $this.parent();
				var parentLink = parent.attr('href');
				//log(parentLink);
				var imageFormat = parentLink.substr(-4,4);
				log('Format:' + imageFormat);
				
				//var imageLink = $this.attr('src');
				//log(imageLink);

				if(isimage(imageFormat)) {
					parent.addClass('fancy');
				}				
				
				//console.log(parent);
				
				// update element styles
				
				/*$this.css({
					backgroundColor: 'red'
				});*/
			});
			
			// private function for checking if parent links to image
			function isimage(imageFormat) {
				if(imageFormat=='.jpg' || imageFormat=='.gif' || imageFormat=='.png' || imageFormat=="jpeg") {
					return true;
				}
				else{
					return false;
				}
			};
			
			// private function for debugging
			function debug($obj) {
				if (window.console && window.console.log)
					window.console.log('hilight selection count: ' + $obj.size());
			};
			function log($obj) {
				if (window.console && window.console.log)
					window.console.log($obj);
			};
	    };
	    
	    $.fn.befancy.defaults = {
		 	//class: 'fancy';
		};
	})(jQuery);
	
	// add class .fancy for a img where src attributes match
	$('.entry a img').befancy();
	$('.entry a.fancy').fancybox();
	
	
	function filterPath(string) {
	  return string
	    .replace(/^\//,'')
	    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	    .replace(/\/$/,'');
	  }
	  var locationPath = filterPath(location.pathname);
	  var scrollElem = scrollableElement('html', 'body');
	
	  $('a[href*=#]').each(function() {
	    var thisPath = filterPath(this.pathname) || locationPath;
	    if (  locationPath == thisPath
	    && (location.hostname == this.hostname || !this.hostname)
	    && this.hash.replace(/#/,'') ) {
	      var $target = $(this.hash), target = this.hash;
	      if (target) {
	        var targetOffset = $target.offset().top;
	        $(this).click(function(event) {
	          event.preventDefault();
	          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
	            location.hash = target;
	          });
	        });
	      }
	    }
	  });
	
	  // use the first element that is "scrollable"
	  function scrollableElement(els) {
	    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
	      var el = arguments[i],
	          $scrollElement = $(el);
	      if ($scrollElement.scrollTop()> 0) {
	        return el;
	      } else {
	        $scrollElement.scrollTop(1);
	        var isScrollable = $scrollElement.scrollTop()> 0;
	        $scrollElement.scrollTop(0);
	        if (isScrollable) {
	          return el;
	        }
	      }
	    }
	    return [];
  	}
  	

	
});
