/**
 * MMA Connect jQuery functions
 *
 * @author Martin Bean <martin@mcbwebdesign.co.uk>
 */

$(document).ready(function() {
	externalLinks();
	ticketsDropdown();
	insuranceForm();
	sfHover();
	$('#navigation li:first').addClass('first');
});

// handles external links; opening in new windows and modifying classes
function externalLinks() {
	$('a[rel="external"]').each(function() {
		var new_title = $(this).attr('title');
		$(this).addClass('external');
		$(this).attr('title', new_title + ' (opens in a new window)');
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	});
};

// 
function ticketsDropdown() {
	$('#ticket_type').change(function() {
		var promotion = $('#promotion').val();
		var event_name = $('#event_name').val();
		var price = this.value;
		var ticket_name = this.options[this.selectedIndex].text;
		$('input[name=price]').val(price);
		$('input[name=product]').val(promotion+' '+event_name+' '+ticket_name);
	});
	$('form#buy_tickets').submit(function() {
		if ($('#ticket_price').val()=='' || $('#ticket_price').val()=='') {
			alert('Please choose a ticket option before proceeding.');
			return false;
		} else {
			return true;
		}
	});
};

//
function insuranceForm() {
	$('#package').change(function() {
		$('#package_price').val(this.value);
		$('#package_name').val(this.options[this.selectedIndex].text);
	});
	$('#insurance_packages').submit(function() {
		if ($('#package_price').val()==null || $('#package_price').val()=='') {
			alert('Please choose an insurance package before proceeding.');
			return false;
		} else {
			return true;
		}
	});
};

// 
function sfHover() {
    var sfEls = document.getElementById("navigation").getElementsByTagName("li");
    for (var i=0; i<sfEls.length; i++) {
    	sfEls[i].onmouseover=function() {
    		this.className+=" sfhover";
    	}
    	sfEls[i].onmouseout=function() {
    		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    	}
    }
};