
_byo_type = 'default';
_byo_lasttype = '';

_byo_country = '';
_byo_lastcountry = '';

_byo_location = '';
_byo_lastlocation = '';

_byo_mainform_visible = false;


function add_load_event(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		if (typeof func == 'function') {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
}


_byo_title_logo_effect = null;
function byo_title_logo_over () {
	if (BrowserDetect.browser == 'Explorer') {
		$('byo_title_logo').style.display = 'block';
		return;
	}
	if (_byo_title_logo_effect != null) {
		_byo_title_logo_effect.cancel();
	}
	_byo_title_logo_effect = new Effect.Appear('byo_title_logo', {
		duration: 0.4,
		afterFinish: function () {
			_byo_title_logo_effect = new Effect.Pulsate('byo_title_logo', { pulses: 100, duration: 80, from: 0.5, afterFinish: function () { _byo_title_logo_effect=null; } });
		}
	});	
}
function byo_title_logo_out () {
	if (BrowserDetect.browser == 'Explorer') {
		$('byo_title_logo').style.display = 'none';
		return;
	}
	if (_byo_title_logo_effect != null) {
		_byo_title_logo_effect.cancel();
	}
	_byo_title_logo_effect = new Effect.Fade('byo_title_logo', {
		duration: 0.4, afterFinish: function () {
			_byo_title_logo_effect=null;
		}
	});
}


function byo_onload () {
	var d = '';
	d += '<select style="width: 135px;" onchange="byo_type_select(this);" id="id_country">';
	d += '<option value="default">Please select...</option>';
	d += '<option value="1"';
	if (_byo_first_country == '1') { d += ' selected="selected"'; }
	d += '>Stag UK</option>';
	d += '<option value="2"';
	if (_byo_first_country == '2') { d += ' selected="selected"'; }
	d += '>Stag Europe</option>';
	d += '<option value="3"';
	if (_byo_first_country == '3') { d += ' selected="selected"'; }
	d += '>Hen UK</option>';
	d += '<option value="4"';
	if (_byo_first_country == '4') { d += ' selected="selected"'; }
	d += '>Hen Europe</option>';
	d += '</select>';
	$('byo_country_list').innerHTML = d;
	new Effect.Appear('byo_country_list', {	duration: 0.3	});
	if (_byo_first_country != '') {
		_byo_first_country = '';
		byo_type_select();
	}
	_byo_step = 'step1'; // set inital step
	try {
		pageTracker._trackPageview("/funnel/start.html"); 
	} catch (err) { }
}

add_load_event(byo_onload);


function byo_type_select () {
	document.body.focus();

	var obj = $('id_country');
	var v = obj.options[obj.selectedIndex].value;

	_byo_lastcountry = _byo_country;
	_byo_country = v;

	if (_byo_lastcountry == _byo_country) {
		// nothing has changed
		return;
	}

	var type = 'default';
	if (v == '1' || v == '2') {
		type = 'stag';
	} else if (v == '3' || v == '4') {
		type = 'hen';
	}
	
	// set a timeout for class change
	setTimeout('byo_change_class();', 700);

	// hide mainform if it was visible
	if (_byo_mainform_visible) {
		byo_hide_mainform();
	}

	if (type != _byo_type) {
		// type has changed - therefore change the colour

		_byo_lasttype = _byo_type;
		_byo_type = type;

		// change title image
		$('byo_title_'+_byo_lasttype).style.zIndex = 1;
		$('byo_title_'+_byo_type).style.zIndex = 2;
		new Effect.Appear('byo_title_'+_byo_type, {duration: 0.4, afterFinish: function(){ $('byo_title_'+_byo_lasttype).style.display='none'; } });

		// change title text
		$('byo_heading_title_'+_byo_lasttype).style.zIndex = 1;
		$('byo_heading_title_'+_byo_type).style.zIndex = 2;
		new Effect.Appear('byo_heading_title_'+_byo_type, {duration: 0.4, afterFinish: function(){ $('byo_heading_title_'+_byo_lasttype).style.display='none'; } });

		// change control background
		$('byo_controlchange_'+_byo_lasttype).style.zIndex = 1;
		$('byo_controlchange_'+_byo_type).style.zIndex = 2;
		new Effect.Appear('byo_controlchange_'+_byo_type, {duration: 0.4, afterFinish: function(){ $('byo_controlchange_'+_byo_lasttype).style.display='none'; } });

		// change photo
		$('byo_photo_'+_byo_lasttype).style.zIndex = 1;
		$('byo_photo_'+_byo_type).style.zIndex = 2;
		new Effect.Appear('byo_photo_'+_byo_type, {duration: 0.4, afterFinish: function(){ $('byo_photo_'+_byo_lasttype).style.display='none'; } });

	}

	// reset location vars
	_byo_location = '';
	_byo_lastlocation = '';

	if (type == 'stag' || type == 'hen') {
		// get the location list for this id_country
		
		var fade_speed = 0.2;
		if (_byo_lasttype == 'default') {
			fade_speed = 0;
		}

		new Effect.Fade('byo_locations_list', {
			queue: {position: 'end', scope: 'location'},
			duration: fade_speed,
			afterFinish: function () {
				new Ajax.Updater('byo_locations_list', 'scripts/locations_list.php', {
					evalScripts: true,
					parameters: {
						id_country: v
					},
					onComplete: function(transport) {
						new Effect.Appear('byo_locations_list', {
							queue: { position: 'end', scope: 'location'},
							duration: 0.3
						});
						byo_check_first_location();
					}
				});
			}
		});

	} else {
		// no id_country selected, hide the location list

		new Effect.Fade('byo_locations_list', { duration: 0.4 });

	}

}


function byo_change_class () {
	$('byo_class').className = 'byo-'+_byo_type;
}


function byo_hide_mainform () {
	new Effect.ScrollTo('byo_top', { duration: 0.5, offset: -20 });
	new Effect.BlindUp('byo_mainform', { duration: 0.7 });
	_byo_mainform_visible = false;
}


_byo_number_loc_changes = 0;

function byo_loc_change () {
	document.body.focus();

	if (_byo_number_loc_changes > 0 && _byo_first_location != '') {
		_byo_first_location = '';
	}
	_byo_number_loc_changes++;

	_byo_lastlocation = _byo_location;
	_byo_location = $('id_location').options[$('id_location').selectedIndex].value;

	if (_byo_location == _byo_lastlocation) {
		// nothing changed
		return;
	}

	if (_byo_location == '') {
		// something other than a location is selected
		
		if (_byo_mainform_visible) {
			byo_hide_mainform();
		}

	} else {
		// a location has been selected

		if (!_byo_mainform_visible) {

			new Effect.Appear('byo_mainform', { duration: 1 });
			_byo_mainform_visible = true;
			new Effect.BlindUp('byo_day_activities', { duration: 0 });
			new Effect.BlindUp('byo_night_activities', { duration: 0 });
			setTimeout('byo_get_activities();', 500);

			if (_byo_first_location == '') {
				new Effect.ScrollTo('byo_controlchange_position', { duration: 0.5 });
			}

		} else {

			new Effect.ScrollTo('byo_controlchange_position', {
				duration: 0.7,
				afterFinish: byo_get_activities
			});
			new Effect.BlindUp('byo_day_activities', {
				duration: 0.5,
				delay: 0.2
			});
			new Effect.BlindUp('byo_night_activities', {
				duration: 0.5,
				delay: 0.2
			});

		}

	}

}


function byo_check_first_location () {
	if (_byo_first_location != '') {
		var s = $('id_location');
		var o = s.options;
		var found = false;
		for (var i = 0; i < o.length; i++) {
			if (o[i].value == _byo_first_location) {
				s.selectedIndex = i;
				found = true;
			}
		}
		if (found) {
			byo_loc_change();
		} else {
			_byo_first_location = '';
		}
	}
}



function byo_get_activities () {

	new Ajax.Request('scripts/activities.php', {
		parameters: {
			id_location: _byo_location,
			id_country: _byo_country
		},
		onSuccess: function(transport) {
			eval(transport.responseText);
			$('byo_day_activities').innerHTML = day_content;
			$('byo_night_activities').innerHTML = night_content;
			if (_byo_first_location == '') {
				new Effect.ScrollTo('byo_controlchange_position', { duration: 0.8 });
			}
			new Effect.BlindDown('byo_day_activities',{
				duration: 0.8 
			});
			new Effect.BlindDown('byo_night_activities',{
				duration: 0.8 
			});
		}
	});

}


_byo_moreinfo_toggling = false;
function byo_activity_moreinfo_toggle ( id ) {
	if (_byo_moreinfo_toggling) { return; }
	_byo_moreinfo_toggling = true;
	var l = $('byo_activity_moreinfo_link_'+id);
	if (l.innerHTML == 'show info') {
		l.innerHTML = 'hide info';
		new Effect.BlindDown('byo_activity_moreinfo_div_'+id, { duration: 0.4, afterFinish: function () { _byo_moreinfo_toggling=false; } });
	} else {
		l.innerHTML = 'show info';
		new Effect.BlindUp('byo_activity_moreinfo_div_'+id, { duration: 0.4, afterFinish: function () { _byo_moreinfo_toggling=false; } });
	}
}


function byo_reset_activities () {
	if (confirm('Are you sure you want to reset the selected activities?')) {
		_byo_location = '';
		byo_loc_change();
	}
}


_byo_pulsing = new Array();

function byo_pulse_over ( obj ) {
	if (!_byo_pulsing[obj.id]) {
		_byo_pulsing[obj.id] = null;
	}
	if (_byo_pulsing[obj.id] == null) {
		_byo_pulsing[obj.id] = new Effect.Pulsate(obj.id, { pulses: 100, duration: 80, from: 0.7 });
		// alert(obj.id);
	}
}
function byo_pulse_out ( obj ) {
	if (!_byo_pulsing[obj.id]) {
		_byo_pulsing[obj.id] = null;
	}
	if (_byo_pulsing[obj.id] != null) {
		_byo_pulsing[obj.id].cancel();
		_byo_pulsing[obj.id] = null;
		new Effect.Appear(obj.id, { duration: 0.2 });
	}
}




function byo_draw_calendar() {
  var div = $('byo_calendar_content');
  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, byo_calendar_select);
  // hide week numbers
  cal.weekNumbers = false;
  // We want some dates to be disabled; see function isDisabled above
  //cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%a %e%x %b %Y");
  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(div);
  // ... we can show it here.
  cal.show();
}

add_load_event(byo_draw_calendar);

_byo_calendar_visible = false;

function byo_calendar_show () {
	_byo_calendar_cancel_click = true;
	byo_pulse_out($('byo_calendar_button'));
	new Effect.Appear('byo_calendar', { duration: 0.3 });
	_byo_calendar_visible = true;
}

function byo_calendar_select ( calendar, datestring ) {
	if (calendar.dateClicked) {
      // OK, a date was clicked
      var y = calendar.date.getFullYear();
      var m = calendar.date.getMonth() + 1;
      var d = calendar.date.getDate();
			byo_calendar_hide();
			$('byo_date_printed').innerHTML = datestring;
			$('byo_date_input').value = y+'-'+m+'-'+d;
    }
}

function byo_calendar_hide () {
	new Effect.Fade('byo_calendar', { duration: 0.3 });
	_byo_calendar_visible = false;
}

_byo_calendar_cancel_click = false;

function byo_calendar_click_onpage () {
	if (!_byo_calendar_cancel_click && _byo_calendar_visible) {
		byo_calendar_hide();
 }
	_byo_calendar_cancel_click = false;
}


function byo_continue_click () {
	// check mainform for validity etc
  var errors = new Array();
  
  var checks = false;
  var c = document.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++) {
		if (c[i].type.toLowerCase() == 'checkbox' && c[i].name.substr(2,11) == 'id_activity' && c[i].checked) {

			checks = true;
		}
  }
  if (!checks) {
		errors[errors.length] = 'Please select at least 1 activity from the available activity lists';
  }

  var p = byo_string_only_numbers($('byo_people_input').value);
	$('byo_people_input').value = p;
	if (p == '') {
		errors[errors.length] = 'Please enter an approx number of guests';
	} else {
		p *= 1;
		if (p < 1) {
			errors[errors.length] = 'Please enter an approx number of guests';
		}
	}

	if ($('byo_date_input').value == '') {
		errors[errors.length] = 'Please select an approx date for your event';
	}

	if (errors.length > 1) {
		alert('There are '+errors.length+' errors that must be fixed before you can continue.\n\n* '+errors.join('\n* '));
	} else if (errors.length > 0) {
		alert('There is an error that must be fixed before you can continue.\n\n* '+errors.join('\n* '));
	} else {
		goto_page2();
	}

}

function byo_string_only_numbers ( str ) {
	var o = '';
	for (var i = 0; i < str.length; i++) {
		var c = str.substr(i,1);
		if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
			o += c;
		}
	}
	return o;
}



_byo_step = '';
_byo_iframe_request = '';

function byo_iframe_load () {
	if (_byo_step == '') {
		// page hasn't fully loaded yet
		return;
	}
	if (_byo_iframe_request == _byo_step) {
		// on the same step already
		return;
	}
	if (_byo_iframe_request == 'step1') {
		byo_back_to_page1();
		return;
	}
	if (_byo_iframe_request == 'step2') {
		if ($('byo-page2').innerHTML != '') {
			byo_back_to_page2();
			return;
		}
	}
}


function byo_back_to_page1 () {
	_byo_step = 'step1';
	new Effect.ScrollTo('byo_top', { duration: 0.8, offset: -20 });
	new Effect.BlindUp('byo-page2', {
				delay: 0.2,
				duration: 0.8,
				afterFinish: function () {  
					var cc_speed_default = 0;
					var cc_speed_stag = 0;
					var cc_speed_hen = 0;
					if (_byo_type == 'stag') { cc_speed_stag = 0.8; }
					if (_byo_type == 'hen') { cc_speed_hen = 0.8; }
					if (_byo_type == 'default') { cc_speed_default = 0.8; }
					new Effect.BlindDown('byo-maincontainer', {duration: 1} );	
					new Effect.Appear('byo_controlchange_default', { delay: 1-cc_speed_default, duration: cc_speed_default });
					new Effect.Appear('byo_controlchange_stag', { delay: 1-cc_speed_stag, duration: cc_speed_stag });
					new Effect.Appear('byo_controlchange_hen', { delay: 1-cc_speed_hen, duration: cc_speed_hen });
					new Effect.Appear('byo-controlchange-content', { delay: 0.2, duration: 0.8 });
					new Effect.Appear('byo_photo_default', { delay: 1-cc_speed_default, duration: cc_speed_default });
					new Effect.Appear('byo_photo_stag', { delay: 1-cc_speed_stag, duration: cc_speed_stag });
					new Effect.Appear('byo_photo_hen', { delay: 1-cc_speed_hen, duration: cc_speed_hen });
					if (_byo_iframe_request == 'step2') {
						history.go(-1);
					}
				}
			});
}


function byo_back_to_page2 () {
	// this is a copy of goto_page2() just without the ajax updater
	// (shouldn't really repeat code but being lazy here - jezz)
	_byo_step = 'step2';
	var cc_speed_default = 0;
	var cc_speed_stag = 0;
	var cc_speed_hen = 0;
	if (_byo_type == 'stag') { cc_speed_stag = 0.8; }
	if (_byo_type == 'hen') { cc_speed_hen = 0.8; }
	if (_byo_type == 'default') { cc_speed_default = 0.8; }
	new Effect.Fade('byo_controlchange_default', { duration: cc_speed_default });
	new Effect.Fade('byo_controlchange_stag', { duration: cc_speed_stag });
	new Effect.Fade('byo_controlchange_hen', { duration: cc_speed_hen });
	new Effect.Fade('byo-controlchange-content', { duration: 0.8 });
	new Effect.Fade('byo_photo_default', { duration: cc_speed_default });
	new Effect.Fade('byo_photo_stag', { duration: cc_speed_stag });
	new Effect.Fade('byo_photo_hen', { duration: cc_speed_hen });
	new Effect.ScrollTo('byo_top', { duration: 0.8, offset: -20 });
	new Effect.BlindUp('byo-maincontainer', {
		duration: 0.8, delay: 0.2, afterFinish: function () {
			new Effect.BlindDown('byo-page2', {duration: 1} );	
		}
	});
}


function goto_page2() {
	_byo_step = 'step2';
	var cc_speed_default = 0;
	var cc_speed_stag = 0;
	var cc_speed_hen = 0;
	if (_byo_type == 'stag') { cc_speed_stag = 0.8; }
	if (_byo_type == 'hen') { cc_speed_hen = 0.8; }
	if (_byo_type == 'default') { cc_speed_default = 0.8; }
	new Effect.Fade('byo_controlchange_default', { duration: cc_speed_default });
	new Effect.Fade('byo_controlchange_stag', { duration: cc_speed_stag });
	new Effect.Fade('byo_controlchange_hen', { duration: cc_speed_hen });
	new Effect.Fade('byo-controlchange-content', { duration: 0.8 });
	new Effect.Fade('byo_photo_default', { duration: cc_speed_default });
	new Effect.Fade('byo_photo_stag', { duration: cc_speed_stag });
	new Effect.Fade('byo_photo_hen', { duration: cc_speed_hen });
	new Effect.ScrollTo('byo_top', { duration: 0.8, offset: -20 });
	new Effect.BlindUp('byo-maincontainer', { duration: 0.8, delay: 0.2,
			afterFinish: function () { 
				var params = new Array();
				var c=document.getElementsByTagName('input');
				var t=0;
				var dcount=0,ncount=0;
				for (var i=0; i<c.length; i++) {
					if (c[i].type.toLowerCase() == 'checkbox' && c[i].name.substr(2,11) == 'id_activity' && c[i].checked) {
						if (c[i].name.substr(0,1)=='d') {
							params[c[i].name.substr(0,1)+(dcount++)] = c[i].value;	
						} else {
							params[c[i].name.substr(0,1)+(ncount++)] = c[i].value;	
						}
					}
				}
				params['day_count']=dcount;
				params['night_count']=ncount;
				params['id_location']=$('id_location').value;
				params['nights']=$('byo_nights').options[$('byo_nights').selectedIndex].value;
				params['byo_date_input']=$('byo_date_input').value;
				params['byo_people_input']=$('byo_people_input').value;
				params['byo_type']=_byo_type;
				$('byo_iframe').contentWindow.document.location = 'iframe_step2.html';
				new Ajax.Updater('byo-page2','step2.php', { parameters: params, onComplete: function() {
      
					new Effect.BlindDown('byo-page2', {duration: 1} );	
					try {
						pageTracker._trackPageview("/funnel/step1.html"); 
					} catch (err) { }
							
			}});


		}});
}







/* browser detect */

BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



