/**
 * Common javascript functions for Chorizo (the CMS).
 * Site specific functions should be placed in the scripts file for the template.
 * 
 * @author Damon Skelhorn
 */

$(function(){
	Common.init();
});

var Common = {
		
	AjaxURL: 'ajax/',
	
	init: function(){
		this.emailSubscribe();
		this.homepageImages();
		$('noscript').remove();
		this.addFavorite();
	},
	
	addFavorite: function(){
		$('a.addFavorite').click(function(){
			if(document.all) window.external.AddFavorite(location.href,document.title); 		
			else if(window.sidebar) window.sidebar.addPanel(document.title,location.href,'');
			return false;
		});
	},
	
	emailSubscribe: function(){
		$('#emailSubscribe').click(function(){
			Common.processEmailSubscribe();
			return false;
		});
		$('#email').keypress(function(e){
			if(e.which == 13) {
				Common.processEmailSubscribe();
				return false;
			}
		});
	}, 
	
	processEmailSubscribe: function(){
		$('#errorMessage').remove();
		var postData = {email: $('#email').val()};
		$.post(Common.AjaxURL + 'newsletter.php', postData, function(data){
			if(data.Status == 'OK'){
				// ok
				$('#emailSubscribe').parent().prepend('<p id="errorMessage">You have subscribed to our newsletter. Thank you!</p>');
			}
			else {
				// failed.
				$('#emailSubscribe').parent().prepend('<p id="errorMessage">Invalid email address</p>');
			}
		}, 'json');
	},
	
	homepageImages: function(){
		if($('div#homepageImagesContainer').length){
			$('div#homepageImagesContainer').cycle({fx: 'fade', speed: 5000});
		}
	}
	
};

