var Site = {
	sponsorshipSpecialFields: null,

	start: function() {

		if ($('kwicks-container')) Site.parseKwicks();

		if ($('login-form')) {

			$('login-button').addEvent("click", function(e) {
				e = new Event(e).stop();
				$('login-form').submit();
			});
		}

		if ( $$('input.sponsorship_level').length > 0 ) {
			Site.attachSponsorshipEvents($$('input.sponsorship_level'));
			updateSponsorshipLevel();
		}

		if ( $$('#login input.text')[0] ) {
			Site.attachLoginEvents();
		}

		if ( $('art_single') ) {
			Site.attachArtFormEvents();
			toggleCCFields();
		}

		if ($('home-slider')) {
			new InfiniSlider({
				container: $('slider-items'),
				previousButton: 'slider-prev-link',
				nextButton: 'slider-next-link',
				showItems: 3
			});

			new InfiniSlider({
				container: $('cs-items'),
				previousButton: 'cs-prev-link',
				nextButton: 'cs-next-link',
				indexItems: '#cs-dots li'
			});
		}
	},


	parseKwicks: function() {

		var szNormal = 150, szSmall = 150, szFull = 300;

		var kwicks = $$("#kwicks .kwick");
		var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
		kwicks.each(function(kwick, i) {
			kwick.addEvent("mouseenter", function(event) {
				var o = {};
				o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
				kwicks.each(function(other, j) {
					if(i != j) {
						var w = other.getStyle("width").toInt();
						if(w != szSmall) o[j] = {width: [w, szSmall]};
					}
				});
				fx.start(o);
			});
			kwick.addEvent("click", function(event) {
				window.open(kwick.getElement('a').getProperty('href'),'_self');
			});
		});

		$("kwicks").addEvent("mouseleave", function(event) {
			var o = {};
			kwicks.each(function(kwick, j) {
				if ( j == 0 ) {
					o[j] = {'width': [kwick.getStyle('width').toInt(), szFull]};
				} else {
					o[j] = {'width': [kwick.getStyle('width').toInt(), szNormal]};
				}
			});
			fx.start(o);
		})

	},


	attachSponsorshipEvents: function(fields) {
		fields.each(function(field, idx) {
			field.addEvent('click', function() {
				updateSponsorshipLevel();
			});
		});
	},


	attachLoginEvents: function() {
		var username = $('login-username');
		var password = $('login-password');

		username.addEvent('focus', function() {
			if ( this.value == 'PUPdate Login' )
				this.value = '';
		});
		username.addEvent('blur', function() {
			if ( this.value == '' )
				this.value = 'PUPdate Login';
		});
		password.addEvent('focus', function() {
			if ( this.value == 'Password' )
				this.value = '';
		});
		password.addEvent('blur', function() {
			if ( this.value == '' ) {
				this.value = 'Password';
			}
		});
	},


	attachArtFormEvents: function() {
		$('art_single').addEvent('click', function() {		toggleCCFields();	});
		$('art_recur').addEvent('click', function() {		toggleCCFields();	});
	},
};
window.addEvent('load', Site.start);



function updateSponsorshipLevel() {
	var form = document.sponsorship_form;
	var annual = 0;
	var monthly = 0;

	if ( form.sponsorship_type[0].checked ) {	annual = 250;		monthly = 21;		}
	if ( form.sponsorship_type[1].checked ) {	annual = 600;		monthly = 50;		}
	if ( form.sponsorship_type[2].checked ) {	annual = 30000;		monthly = 0;		}
	if ( form.sponsorship_type[3].checked ) {	annual = 1500;		monthly = 125;		}
	if ( form.sponsorship_type[4].checked ) {	annual = 1500;		monthly = 125;		}
	if ( form.sponsorship_type[5].checked ) {	annual = 1500;		monthly = 125;		}
	if ( form.sponsorship_type[6].checked ) {	annual = 3000;		monthly = 250;		}
	if ( form.sponsorship_type[7].checked ) {	annual = 3000;		monthly = 250;		}
	if ( form.sponsorship_type[8].checked ) {	annual = 3000;		monthly = 250;		}
	if ( form.sponsorship_type[9].checked ) {	annual = 5000;		monthly = 420;		}
	if ( form.sponsorship_type[10].checked ) {	annual = 5000;		monthly = 420;		}
	if ( form.sponsorship_type[11].checked ) {	annual = 5000;		monthly = 420;		}


	if ( form.sponsorship_interval[0].checked ) {
		$('contribution_amount').set('text', '$' + annual + ' annually');
	} else {
		if ( form.sponsorship_type[2].checked ) {
			form.sponsorship_interval[0].checked = true;
			$('contribution_amount').set('text', '$' + annual + ' annually');
			alert('Puppy Partner sponsorship is not available for monthly contributions.');
			$('cc_fields').setStyle('display', '');
		} else {
			$('contribution_amount').set('text', '$' + monthly + ' monthly');
		}
	}

}


function toggleCCFields() {
	if ( $('art_single').checked ) {
		$('cc_fields').setStyle('display', '');
	} else {
		$('cc_fields').setStyle('display', 'none');
	}
}


function increaseQty() {
	var curQty = parseInt($('product_qty').value);

	if ( curQty > 0 ) {
		$('product_qty').value = curQty + 1;
	} else {
		$('product_qty').value = 1;
	}
}

function decreaseQty() {
	var curQty = parseInt($('product_qty').value);

	if ( curQty > 1 ) {
		$('product_qty').value = curQty - 1;
	} else {
		$('product_qty').value = 1;
	}
}

// Validate sponsorship form special fields
/*
function validateSponsorshipForm(form) {
	if ( validateForm(form) ) {
		if ( form.email.value != form.confirm_email.value ) {
			alert('Email and confirm email fields do not match.');
			return false;
		}

		if ( form.password.value != form.confirm_pass.value ) {
			alert('Password and confirm password fields do not match.');
			return false;
		}

		if ( form.password.value.length < 6 ) {
			alert('Your selected password is too short, passwords must be at least 6 characters.');
			return false;
		}

		return true;
	}

	return false;
}
*/

// Validate wine club form special fields
function validateWineForm(form) {
	if ( validateForm(form) ) {
		if ( form.password.value != form.confirm_pass.value ) {
			alert('Password and confirm password fields do not match.');
			return false;
		}

		if ( form.password.value.length < 6 ) {
			alert('Your selected password is too short, passwords must be at least 6 characters.');
			return false;
		}

		return true;
	}

	return false;
}

// XHTML Strict is awesome, silence critics
function openExternalLink(url) {
	window.open(url, '_external');
}
