//jquery.knorr_gr.js
(function($) {
	$.log = ('console' in window && 'log' in console) ? function(){ console.log(arguments[0]) }: function() { }
})(jQuery);

$(function() {
	if ($.fn.pngFix) $('p.logo, p.promo').pngFix();
	
	//Load the fancybox stuff
	if ($.fn.fancybox) {
		var simpleFancyOptions = {
				'overlayOpacity': 0.4,
				'overlayShow': true
			};

		var $photogallery = $("a.photogallery"); 
		var galleries = new Array();
		$photogallery.each(function() {
			var rel = this.rel;
			if (!(galleries[rel])) {
				galleries[rel] = new Object();
				var $g = $("a.photogallery[rel='" + rel + "']"); 
				$g.fancybox(simpleFancyOptions);
				$.log($g.length + 'images with rel ' + rel + ' has been fancyboxized');
			}
		});
		
		$('a.inlinefancybox').each(function() {
			var $this = $(this);
			var id = $this.attr('href');
			var $t = $(id);
			var o = $.extend(simpleFancyOptions, {
				frameWidth: $t.width(),
				frameHeight: $t.height(),
				hideOnContentClick: false
			});
			$this.fancybox(o);
			$.log('Inline fancyboxizing ' + this + ' with target: ' + id + '(' + $t.width() + 'x' + $t.height() + ')');
		});
		
		//In case of a window location with a hash click the apropriate a:
		if (window.location.hash) {
			var hash = window.location.hash.toString();
			if (hash.length > 0) {
				$.log('Trying to click a[href=' + hash + ']');
				var $a = $('a[href=' + hash + ']').click();
			}
		}
	}

	$("a[href^=#submit]").click(function() {
		var $this = $(this);
		var $form = $('#' + this.rel);
		var href = $this.attr('href');
		var index = href.indexOf('?'); 
		if (index > 0) {
			href = href.substring(index + 1);
			var entries = href.split(/=|&/);
			var i = 0;
			for (var i = 0; i < entries.length; i++) {
				var name = entries[i];
				var value = entries[++i];
				$.log("Trying to set the input with name '" + name + "' the value '" + value + "'");
				$form.find('[name=' + name + ']').val(value);
			}
		}
		$form.submit();
		return false;
	});
	
	$('form#newsletter').submit(function() {
		$.log('form#newsletter submit');
		$this = $(this);
		$this.fadeOut();
		$.getJSON($this.attr('action'), 
			{email: $this.find('input[name=email]').val(), valid:$this.find('input[name=valid]').val()}, 
			function(data) {
				$this.hide();
				var $msg = $this.next().next();
				if (!data.registered) $msg = $msg.next();
				$msg.find('strong').html(data.email).end().fadeIn();
			}
		);
		return false;
	});
	
	$('form').submit(function() {
		var $this = $(this);
		$this.find('li.mandatory').removeClass('warning');
		
		var validationErrors = 0;
		$this.find('li.mandatory .field')
			.each(function() {
				var $f = $(this);
				var value = $f.val();
				if (value == '' || value == 0) {
					++validationErrors;
					$.log ('Validation error for field with name: ' + $f.attr('name'));
					$f.parents('li.mandatory').addClass('warning');
				}
			});
		
		if (validationErrors > 0) {
			alert('Παρακαλώ συμπλήρώστε σωστά όλα τα πεδία')
			return false;
		}
		return true;
	});

	$('.minheight_300').each(function() {
		var $this = $(this);
		var h = $this.height();
		if (h < 300) $this.height(300);
		$.log('Found .minheight_300 with a height of ' + h + 'px. New height: ' + $this.height() + 'px');
	});
	
	var $recipesearch = $('#recipe_search_form');
	if ($recipesearch.length == 1) {
		$recipesearch.submit(function() {
			var $tag = $recipesearch.find("select[name='tag.surrogate']");
			var $title = $recipesearch.find('input[name=title]');
			var url = $recipesearch.attr('action');
			if ($tag.val().length > 0) {
				url = url.replace('search', $tag.val());
			}
			if ($title.val().length == 0) {
				window.location = url;
				return false;
			}
			else {
				$recipesearch.attr('action', url);
				$tag.remove();
				return true;
			}
		});
	}

	$bmi = $('#bmi');
	if ($bmi.length == 1) {
		$bmi.find('#bmi_calc').submit(function() {
			var w = $bmi.find('#bmi_weight').val();
			if (isNaN(w)) {
				alert('Πρέπει να συμπήρώσεται σωστά το βάρος σας');
				return false;
			}
			
			var h = $bmi.find('#bmi_height').val();
			if (isNaN(h)) {
				alert('Πρέπει να συμπληρώσεται σωστά το ύψος σας');
				return false;
			}
			
			//In any other case we can compute the bmi:
			h = h/100;
			var bmi = w / (h * h);
			var rbmi = Math.round(bmi * 10)/10; //round the bmi in order to have 1 demical
			if (rbmi < 10) rbmi = Math.round(bmi * 100)/100;
			$.log('Computed bmi: ' + bmi + ', rounded: ' + rbmi);
			$bmi.find('#computed_bmi').html(rbmi);
			
			//Now we hide the bmi form and show the results div:
			$bmi.find('#bmi_calc').hide();
			$bmi.find('.results').show();
			return false;
		});
		
		$bmi.find('a[href=#toggle_bmi]').click(function() {
			$bmi.find('.results').hide();
			$bmi.find('#bmi_calc').show();
			return false;
		});
	}
	
	try {
		var pageTracker = _gat._getTracker("UA-720559-23");
		pageTracker._trackPageview();
		$.log('Google Analytics: page has been tracked');
	} catch(err) {
		$.log('Google Analytics: could not track page because of: ' + err);
	}
});
