var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var $ = function(id) {
      return document.getElementById(id);
} 

//if (!console) console = {log:function(m){alert(m)}};

YAHOO.namespace("boatingbay");

YAHOO.boatingbay.sync = {
	messages : [
		["1) Select Boat Type", "1) Select Boat Type (VALUE)"],
		["2) Select Manufacturer", "2) Select Manufacturer (VALUE)"],
		["3) Set Length Limits", "3) Set Length Limits (over MIN ft)", "3) Set Length Limits (up to MAX ft)", "3) Set Length Limits (between MIN ft and MAX ft)"],
		["4) Set Year Limits", "4) Set Year Limits (after MIN)", "4) Set Year Limits (before MAX)", "4) Set Year Limits (between MIN and MAX)"],
		["5) Set Price Range", "5) Set Price Range (more than $MIN)", "5) Set Price Range (less than $MAX)", "5) Set Price Range (between $MIN and $MAX)"],
		["6) Select Location", "6) Select Location (VALUE)"],
		["Results"]
	],
	message_next : "skip&nbsp;&raquo;",
	buttons_dir : 'search_img',
	buttons : [
		{ // Step 1
		powerboat: {type: 16},
		sailboat: {type: 17},
		houseboat: {type: 18},
		personalwatercraft: {type: 19},
		pontoondeckboat: {type: 20},
		commercialvessel: {type: 21},
		classicboat: {type: 22}
		},
		{ // Step 2
		seeall: {opt1: ''}
		},
		{ // Step 3
		'20limit': {optmin4: '1', optmax4: '20'},
		'40limit': {optmin4: '20', optmax4: '40'},
		'60limit': {optmin4: '40', optmax4: '60'},
		over60: {optmin4: '60', optmax4: '200'},
		seeall: {optmin4: '', optmax4: ''}
		},
		{ // Step 4
		'5yearlimit': {optmin3: (new Date()).getFullYear()-5,  optmax3: (new Date()).getFullYear()+1},
		'10yearlimit': {optmin3: (new Date()).getFullYear()-10, optmax3: (new Date()).getFullYear()+1},
		'20yearlimit': {optmin3: (new Date()).getFullYear()-20, optmax3: (new Date()).getFullYear()+1},
		seeall: {optmin3: '', optmax3: ''}
		},
		{ // Step 5
		'upto20': {minprice: 1, maxprice: 20000},
		'20to40': {minprice: 20000, maxprice: 40000},
		'40to80': {minprice: 40000, maxprice: 80000},
		'over80': {minprice: 80000, maxprice: 10000000},
		'seeall': {minprice: '', maxprice: ''}
		},
		{ // Step 6
		seeall: {state: ''}
		},
		{ // Finish
		}
	],

	init : function (video) {
		this.video = this.getFlashMovie("bsa");
		//alert(this.video);
		this.accordion = $("search_accordion");
		this.step = 1;
		this.steps = [];

		this.attachEvents(this.accordion.getElementsByTagName("dt"));
		this.addUpdateListeners(this.accordion.getElementsByTagName("select"));
		this.addUpdateListeners(this.accordion.getElementsByTagName("input"));
		this.addNavigation(this.accordion.getElementsByTagName("div"));

		// Play the first one when page loads
		this.clickHeader(null, this.step);
		this.updateHeaders(true);
	},
	attachEvents : function(headers) {
		this.steps = headers;
		for(var i=0; i<headers.length; i++) {
			Event.addListener(headers[i], "mouseup", this.clickHeader, i+1, this);
		}
	},
	addNavigation : function(groups) {
		for(var i=0; i<groups.length; i++) {
			//console.log(groups[i]);
			var imgs = groups[i].getElementsByTagName("img");
			for (var j=0; j<imgs.length; j++) {
				//console.log("Key: " + this.getKeyFromImg(imgs[j]));
				var actions = this.buttons[i][this.getKeyFromImg(imgs[j])];
				if ( actions ) {
					this.attachButtonEvents(imgs[j], actions, i+1);
				}
			}
			if (i < groups.length-1) groups[i].appendChild(this.makeNext(i+1));
		}
	},
	getKeyFromImg : function (img) {
		var match = img.src.match(/([^\/]*?)\.png$/);
		return match? match[1] : 'null';
	},
	attachButtonEvents : function(button, actions, currentStep) {
		//var el = document.createElement('img');
		var link = button.parentNode;
		link.href = "javascript:void(0);";
		//el.src = this.buttons_dir + '/' + button.src;
		Dom.addClass(button, "button");
		var form = $("search_form");
		var press = function () {
			//console.log("clicked!");
			for (var key in actions) {
				$(key).value = actions[key];
			}
			this.goto(currentStep+1);
		};
		Event.addListener(link, "mouseup", press, null, this);
	},
	makeNext : function (currentStep) {
		//var next_text = document.createTextNode(this.message_next);
		var next_link = document.createElement('a');
		Dom.addClass(next_link, "next");
		next_link.setAttribute('href', "javascript:void(0)");
		Event.addListener(next_link, "click", function(){this.goto(currentStep+1)}, null, this);
		//next_link.appendChild(next_text);
		next_link.innerHTML = this.message_next;
		return next_link;
	},

	addUpdateListeners : function(inputs) {
		//console.log("Adding update listeners.");
		for (input in inputs) {
			if (!inputs[input].nodeName) continue;
			var tagName = inputs[input].nodeName.toUpperCase();
			if (tagName != "SELECT" && tagName != "INPUT") continue;
			//console.log("Adding update listener for %o", inputs[input]);
			Event.addListener(inputs[input], "change", function(){this.updateHeaders();this.next();}, null, this);
		}
	},
	updateHeaders : function (hold) {
		//console.log("Updating headers...");
		var h = this.accordion.getElementsByTagName("dt");
		var divs = this.accordion.getElementsByTagName("div");
		for (var i=0; i<h.length; i++) {
			var inputs = divs[i].getElementsByTagName("select");
			if (!inputs.length) inputs = divs[i].getElementsByTagName("input");
			this.setMessaging(h[i], this.messages[i], inputs);
		}
	},
	setMessaging : function(header, message, inputs) {
		var text = message[0];
		if (inputs.length == 1 && inputs[0].value != '' && inputs[0].type.toLowerCase().indexOf('select') >= 0) {
				text = message[1].replace("VALUE", inputs[0].options[inputs[0].selectedIndex].text);
		} else if (inputs.length == 2) {
			if (inputs[0].value != '' && inputs[1].value == '') {
				text = message[1].replace("MIN", inputs[0].value);
			} else if (inputs[0].value == '' && inputs[1].value != '') {
				text = message[2].replace("MAX", inputs[1].value);
			} else if (inputs[0].value != '' && inputs[1].value != '') {
				text = message[3].replace("MIN", inputs[0].value);
				text = text.replace("MAX", inputs[1].value);
			}
		}
		//if (header.innerHTML != text) console.log("Changing messaging from '"+header.innerHTML+"' to '"+text+"'");
		header.innerHTML = text;
	},		
	getFlashMovie : function(movieName) {
		return document.getElementById(movieName);
		//var isIE = navigator.appName.indexOf("Microsoft") != -1;
		//return isIE? window[movieName] : document[movieName];
	},
	clickHeader : function(e, step) {
		this.step = step;
		this.video.setStep(step);
		this.updateHeaders();
		return true;
	},
	goto : function (step) {
		step--;
		// Forward click to appropriate header
		if( document.createEvent ) {
			var evObj = document.createEvent('MouseEvents');
			evObj.initEvent("mouseup", true, true );
			this.steps[step].dispatchEvent(evObj);
		} else if( document.createEventObject ) {
			this.steps[step].fireEvent("onmouseup");
		}
	},
	next : function () {
		if (this.step >= this.steps.length) return;
		//console.log("next...");
		this.goto(this.step+1);
	}
};

init = function() {
	YAHOO.boatingbay.sync.init();
};

Event.on(window,"load",init);

// Embed flash movie
var flashvars = {};
var params = {menu:'false',allowScriptAccess:'always',wmode:'transparent'};
var attributes = {id:'bsa', name:'bsa', "class":"video"};

swfobject.embedSWF("http://www.boatingbay.com/features/searchassistant/bsa.swf", "video", "210", "350", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
