jQuery(function(){
	// Rating system
    jQuery('form.rating').rating({
    	onRated: function(frm, div) {
    		jQuery(div).fadeOut(500, function() {
    			jQuery(div).find('div, label').remove();
    			jQuery(div).append(jQuery('<div class="thankyou">Thank you for you vote!</div>')).fadeIn(500);
    		});
    	}
    });
    
    // iPhone styled checkboxes
    jQuery('.newsletters :checkbox').iphoneStyle({
    	checkedLabel: 'Yes',
    	uncheckedLabel: 'No'
    	//onChange: Ngala.postNewsletterForm
    });
    /*jQuery('.gender-checkbox :checkbox').iphoneStyle({
    	checkedLabel: 'Male',
    	uncheckedLabel: 'Female'
    });*/
    
    // Lets make the newsletter checkboxes automatic
    jQuery('#frm_newsletter :checkbox').bind('change', function() { Ngala.postNewsletterForm(this); });
    
	// Reset Font Size
	var originalFontSize = 0.8; //$('html').css('font-size');
	var currentFontSize = 0.8;
	
	// Make some buttons printable
	jQuery('*[rel=print]').bind('click', function(){ window.print(); return false;});
	
	$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
	});
	// Increase Font Size
	$(".increaseFont").click(function(){
		//var currentFontSize = $('body').css('font-size');
		//var currentFontSizeNum = parseFloat(currentFontSize, 10);
		currentFontSize *= 1.2;
		$('body').css('font-size', currentFontSize+'em');
		return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		currentFontSize *= 0.8;
		$('body').css('font-size', currentFontSize+'em');
		return false;
	});
	
	// Some T&C Acceptance stuff on the forms
	jQuery('#waitinglistForm').each(function() {
		jQuery('#'+jQuery('#waitinglistSubit button[type=button]').attr('name')).hide();
		
		jQuery('#waitinglistSubit button[type=button]').bind('click', function() {
			jQuery(this).slideUp();
			jQuery('#'+jQuery(this).attr('name')).slideDown();
		});
	});
	
	// Put in some functionality to remove the search text
	jQuery('form.search-bar input.input').bind('focus', function() {
		// Blank the text box if it's our usual text
		if (this.value == 'Search by keyword') this.value = '';
	});
	jQuery('form.search-bar input.input').bind('blur', function() {
		if (!this.value) this.value = 'Search by keyword';
	});
});

var Ngala = {
	ajaxNewsletter: null,
	
	defaults:{
		superfish:{ // menu
			autoArrows: false
		}
	},
	
	/**
	 * Called when the location field changes
	 *
	 * @param HTMLSelectElement selectObj
	 * @return boolean
	 */
	changeLocation: function(selectObj) {
		// Disable or enable the other options
		return true;
	},
	
	postNewsletterForm: function(elm) {
		var data = {};
		
		// Kill any previous updates
		if (Ngala.ajaxNewsletter != null) Ngala.ajaxNewsletter.abort();
		
		for(var index=0;index<elm.form.elements.length;index++){
			if (elm.form.elements[index].tagName != 'INPUT') continue;
			if (elm.form.elements[index].type == 'checkbox' && !elm.form.elements[index].checked) continue;
			data[elm.form.elements[index].name] = elm.form.elements[index].value;
		}
		
		Ngala.ajaxNewsletter = jQuery.post(elm.form.action, data);
	}
}