URCHIN = typeof(pageTracker)=='undefined'?true:false;
	$('.track').each(function() {
		var tag = '/'+this.name.replace(/:/g,'/');
		if($(this).is('form')) {
			$(this).submit(function() {
				URCHIN?urchinTracker(tag):pageTracker._trackEvent('form', 'submit', tag);
			});
		} else {
			$(this).click(function() {
				URCHIN?urchinTracker(tag):pageTracker._trackPageview(tag);				   
			});
		}		
	});

$(function() {
	$('form').initializeForm();
	$('.validate').formValidate(); 
	$('.vevent').eventPreview();
	$('.news').newsPreview();
	$('.package').packagePreview();
});

(function($) {		  
	$.fn.eventPreview = function(options) {
		var modifier = ($('body').hasClass('premier')?'-premier':'');
		var defaults = {
			hideDetails : '<img src="/images/package/gfx/hide-details'+modifier+'.gif" alt="hide details" />',
			viewDetails : '<img src="/images/package/gfx/view-details'+modifier+'.gif" alt="view details" />'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.event-details a.toggle', this).toggle(function() {
				var el = $(this);
				$('.share-panel').hide();
				$.get('/includes/events-calendar-details.php', { event_id: this.id.substr(3), channel: this.rel, lang: 'en' },function(data) { 
					el.html(opts.hideDetails).parent().addClass('active').parent().children('.event-description').find('div').hide().html(data).slideDown('slow');
				});
				return false;								   
			},function() { 
				$('.share-panel').hide();
				$(this).html(opts.viewDetails).parent().removeClass('active').parent().children('.event-description').find('div').slideUp('slow', function() { 
					$(this).html(''); 
				});
				return false;								   
			});
		});
	};

	$.fn.packagePreview = function(options) {
		var modifier = ($('body').hasClass('premier')?'-premier':'');
		var defaults = {
			hideDetails : '<img src="/images/package/gfx/hide-details'+modifier+'.gif" alt="hide details" />',
			viewDetails : '<img src="/images/package/gfx/view-details'+modifier+'.gif" alt="view details" />'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.package-details a', this).toggle(function() {
				$('.share-panel').hide();
				var el = $(this);
				$.get('/includes/package-details.php', { package_id: this.id.substr(3), channel: this.rel, lang: 'en' },function(data) { 
					el.html(opts.hideDetails).parent().addClass('active').parent().parent().find('.package-long').hide().html(data).slideDown('slow');
				});
				return false;								   
			},function() { 
				$('.share-panel').hide();
				$(this).html(opts.viewDetails).parent().removeClass('active').parent().parent().find('.package-long').slideUp('slow', function() { 
					$(this).html(''); 
				});
				return false;								   
			});
		});
	};

	$.fn.newsPreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Full Post',
			viewDetails : 'View Full Post'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.post-details a', this).toggle(function() {
				var el = $(this);
				$('.share-panel').hide();
				$.get('/direct/scrapbook-details.php', { event_id: this.id.substr(3), channel: this.rel, lang: 'en' },function(data) { 
					el.text(opts.hideDetails).parent().addClass('active').parent().children('.post-description').hide().html(data).slideDown('slow');
				});
				return false;								   
			},function() { 
				$('.share-panel').hide();
				$(this).text(opts.viewDetails).parent().removeClass('active').parent().children('.post-description').slideUp('slow', function() { 
					$(this).html(''); 
				});
				return false;								   
			});
		});
	};


	$.fn.initializeForm = function() {
		return this.each(function() {
			var form = $(this);
			
			/*
			if(IE6) {
				$(".textfield")
					.focus(function() { $(this).css({backgroundColor: "#fdfcfa"}); })
					.blur(function() { $(this).css({backgroundColor: "#ffffff"}); });			
			}
			*/
			
			$(".date-picker", form).each(function() {
				$(this).datepicker({ 
					duration: "", 
					showOn: "both",
					beforeShow: setDatePicker,
					hideIfNoPrevNext: true,
					buttonImage: "/images/icon-cal.gif", 
					buttonImageOnly: true 
				});
				
				if($(this).val()=='mm/dd/yyyy') {
					$(this).val('');
				}
				else {
					if($(this).hasClass('date-end')&&$(this).hasClass('update-blocks'))
						getRange();
				}
			}).attr("readonly", "readonly");
			
			function setDatePicker(input) {
				if(($(input).hasClass('date-begin')||$(input).hasClass('date-end'))&&$(input).hasClass('update-blocks')){
					return {
						minDate: ( setDates(input, 1) ), 
						maxDate: ( setDates(input, 0) ),
						onClose: getRange
					}; 
				} else if($(input).hasClass('date-begin')||$(input).hasClass('date-end')) {
					return {
						minDate: ( setDates(input, 1) ), 
						maxDate: ( setDates(input, 0) )
					};				
				} 
			} 
			
			function setDates(input, minDate) {
				if( ($(input).hasClass('date-begin') && !$('.date-end', $(input).parent().next()).length) ||
					($(input).hasClass('date-end') && !$('.date-begin', $(input).parent().prev()).length) ) {
					return minDate ? new Date() : null;
				} else if( $(input).hasClass('date-begin') ) {
					return minDate ? new Date() : ($('.date-end', $(input).parent().next()).datepicker("getDate") ? new Date($('.date-end', $(input).parent().next()).datepicker("getDate").getTime() - 1000*60*60*24) : null);
				} else if( $(input).hasClass('date-end') ) {
					return minDate ? ($('.date-begin', $(input).parent().prev()).datepicker("getDate") ? new Date($('.date-begin', $(input).parent().prev()).datepicker("getDate").getTime() + 1000*60*60*24) : '+1d' ) : null;
				}
			}
			
			function getRange() {
				var start = $(".date-begin", form).datepicker("getDate");
				var end = $(".date-end", form).datepicker("getDate");
				
				if(start&&end) {
					var one_day=1000*60*60*24;
					var days = Math.ceil((end.getTime()-start.getTime())/(one_day))					
					createBlock(start, days, form);
					createMeeting(start, days, form);
				}
			}

			// hide all "other" fields
			var othersTextfields = $("ul.checkboxgroup input.other").not(':radio, :checkbox');
			othersTextfields.each(function() {
			    if ( !$(this).siblings('input.other').attr('checked') ) {
				$(this).hide();
			    }
			});
			$("ul.checkboxgroup input:not(:text)", form).click(function() {
				if($(this).hasClass('other')) {
					$(this).siblings().addClass('required').show();
					if(!$(this).is(':checked')) {
						$('.validation-error', $(this).parents('ul')).removeError(form);
						$(this).siblings().removeClass('required').filter(':text').hide();
					}
				} else if($(this).is(':radio')) {
					$('.validation-error', $(this).parents('ul')).removeError(form);
					$('.other', $(this).parents('ul')).siblings().removeClass('required').filter(':text').hide();
				}
			});
			
			$("div.select-other", form).hide();
			// if "Other" is selected, then show it's "other" field
			$("select.select-other", form).change( function () {
				var el = $(this).parent().next()
				if($(this).val() == "--") {
					el.show().children('label, input').addClass("required");
				} else {
					el.hide().children().removeClass("required").find("input").val("");
					$('.validation-error', el).removeError(form);
				}
			});

		});
	};
	
	$.fn.formValidate = function() {
		return this.each(function() {
			var form = $(this);
			$('.submit', form).click(function() {
				var _errors = '';
				//Removes validation errors from previous submit
				$('.validation-error', form).removeError(form);	
				
				$('.required:not(label)', form).each(function() {
					var valid = true;
					if($(this).is(':input')) {
						if($(this).hasClass('multi-dropdown')){
							var o = $(this).val();
						}else{
							var o = $.trim($(this).val());
						}
						
						
						if($(this).hasClass('multi-dropdown')){
							if(o==null){
								valid = !valid;	
							}
						}
						else if($(this).hasClass('email')) {
							// if it's an email address make sure the email is valid using both regular expressions
							valid = /^[a-z0-9_+.-]+\@(?:[a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test(o);
							if($(this).hasClass('confirm-email')&&valid) {
								var prev = $.trim($(this).parents('.field').prev().children('.email').val());
								if(o!=prev)
									valid=!valid;
							}
						} else if (o.replace(/(?:^\s+)|(?:\s+$)/g,'').length < 1) {
							// if not an email address take out funky characters and see if its still blank
							valid = !valid;
						}
					}
					else if($(this).is('ul')) {
						valid = false;
						
						$(':radio, :checkbox', this).each(function() {
							if($(this).is(':checked'))
								valid = true;
						});
					}
					
					if(!valid) {
						if(form.hasClass('inline')) {
							$(this).parents('.field').children('label').addClass('validation-error');
						} else {
							var warn = $('<img />').attr('src', '/images/icon-warning.gif').addClass('validation-error');
							
							if( $(this).is('ul') )
								$('li:first', this).append(warn);
							else 
								$(this).parent().append(warn);
						}
													
						if(_errors == '')
							_errors = $(this).attr("id");
					}
				});
				if(_errors.length) {
					var lbl = $('#'+_errors).parents('.field').children('label');
					if(lbl[0].title.length)
						alert(lbl[0].title);
					else 
						alert( "A valid " + lbl.html().replace(/[*:]/g, '').replace(/<[^>]+>.*<[^>]+>/g, '') + " is required." );
					$('#'+_errors, form).focus();
					return false;			
				}	
			});
		});
	};
	
	$.fn.removeError = function(form) {
		return this.each(function() {
			if ( form.hasClass('inline') ) {
				$(this).removeClass('validation-error');
			}
			else {
				$(this).remove();
			}
		});
	};
	
})(jQuery);

function createBlock(date, days, form) {
	$("#block-details", form).remove();
	var tbody = $('<tbody />').attr('id', 'block-details');
	
	for(var i=0; i<=days; i++) {
		var newDate = $.datepicker.formatDate('D, m/d', new Date(date.getFullYear(), date.getMonth(), date.getDate()+i) );
		
		tbody.append('<tr>\n\
					<th scope="row">' + newDate + '</th>\n\
					<td><input type="text" class="textfield single" maxlength="5" value="0" id="single_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][single]" /></td>\n\
					<td><input type="text" class="textfield dbl" maxlength="5" value="0" id="dbl_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][dbl]" /></td>\n\
					<td><input type="text" class="textfield suite" maxlength="5" value="0" id="suite_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][suite]" /></td>\n\
					<td><input type="text" class="textfield readonly day-total" readonly="readonly" value="0" id="day_' + (i+1) + '_total" name="room_block[day_' + (i+1) + '][total]" /></td>\n\
					</tr>');
	}
	//Add the totals row
	tbody.append('<tr><th scope="row">Room Total</th>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="single_rooms_total" name="room_block[single][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="dbl_rooms_total" name="room_block[dbl][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="suite_rooms_total" name="room_block[suite][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="total_rooms" name="total_rooms" /></td>\n\
		</tr>');

	//add tbody to existing table
	$("#block-requirements", form).append(tbody);
	$('td input:not(.readonly)', tbody).focus(function() { $(this).val(''); })
				  .blur(function() { if($(this).val()=='') $(this).val(0); })
				  .change(function() { calcTotal(this); });
}

function createMeeting(date, days, form) {
	$("#meeting-details", form).remove();
	var tbody = $('<tbody />').attr('id', 'meeting-details');
	
	// For Each Number of Days that need information a row is created for each
	for(var i=0; i<=days; i++) {
		var newDate = $.datepicker.formatDate('D, m/d', new Date(date.getFullYear(), date.getMonth(), date.getDate()+i) );
		
		// Create a row with a cell for the day, room type and breakout room info
		tbody.append('<tr>\n\
			<th scope="row">' + newDate + '</th>\n\
			<td><select name="meeting_requirements[day_' + (i+1) + '][room_setup]">\n\
				<option value="Classroom" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Classroom</option>\n\
				<option value="Boardroom" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Boardroom</option>\n\
				<option value="Theater" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Theater</option>\n\
				<option value="U-Shaped" name="meeting_requirements[day_' + (i+1) + '][room_setup]">U-Shaped</option>\n\
				<option value="Hollow Sq." name="meeting_requirements[day_' + (i+1) + '][room_setup]">Hollow Sq.</option>\n\
				<option value="Rounds" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Rounds</option>\n\
				<option value="Reception" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Reception</option>\n\
				</select></td>\n\
			<td><input type="text" class="textfield" maxlength="5" id="breakout_rooms_day_' + (i+1) + '" name="meeting_requirements[day_'+(i+1)+'][breakout_rooms]" /></td>\n\
			<td><input type="text" class="textfield" maxlength="5" id="breakout_people_day_' + (i+1) + '" name="meeting_requirements[day_'+(i+1)+'][breakout_people]" /></td>\n\
			<td><select name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">\n\
				<option value="Classroom" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Classroom</option>\n\
				<option value="Boardroom" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Boardroom</option>\n\
				<option value="Theater" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Theater</option>\n\
				<option value="U-Shaped" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">U-Shaped</option>\n\
				<option value="Hollow Sq." name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Hollow Sq.</option>\n\
				<option value="Rounds" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Rounds</option>\n\
				<option value="Reception" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Reception</option>\n\
				</select></td>\n\
			</tr>');
	}
	//add tbody to existing table
	$("#meeting-requirements", form).append(tbody);
}

function calcTotal(field) {
	var row = $(field).parents('tr');
	var tbody = $(field).parents('tbody')
	var rTotal=sTotal=dTotal=suite=total=0;
	
	$('input:not(.readonly)', row).each(function() {
		rTotal += parseInt($(this).val(), 10);										 
	});
	$('.day-total', row).val(rTotal);
	
	if($(field).hasClass('single')) {
		$('input.single', tbody).each(function() {
			sTotal += parseInt($(this).val(), 10);										 
		});
		$('#single_rooms_total',tbody).val(sTotal);
	}
	if($(field).hasClass('dbl')) {
		$('input.dbl', tbody).each(function() {
			dTotal += parseInt($(this).val(), 10);										 
		});
		$('#dbl_rooms_total',tbody).val(dTotal);
	}
	if($(field).hasClass('suite')) {
		$('input.suite', tbody).each(function() {
			dTotal += parseInt($(this).val(), 10);										 
		});
		$('#suite_rooms_total',tbody).val(dTotal);
	}
	
	$('.day-total', tbody).each(function() {
		total += parseInt($(this).val(), 10);								 
	});
	$('#total_rooms', tbody).val(total);
}
