var justdropped=0;
var cyc;
var basepath = '';
var updateresults = false;
var imgss;
var sizeandexp = false;
var pvars = new Array();
var upgrades = {luxury:{o14:{s:0,p:0,w:0,ss:1},o15:{s:0,p:1,w:1,ss:1},o16:{s:0,p:0,w:1,ss:1},o17:{s:1,p:1,w:1,ss:1},o74:{s:0,p:0,w:1,ss:0},o75:{s:0,p:1,w:0,ss:0},o79:{s:1,p:0,w:0,ss:0}},comfort:{o14:{s:0,p:0,w:0,ss:1},o17:{s:0,p:0,w:1,ss:1},o77:{s:0,p:1,w:0,ss:0},o78:{s:0,p:0,w:1,ss:0},o80:{s:0,p:0,w:0,ss:0}}};

function hometick() {
	var on = $('#home .frames a.on');
	var nex = on.next().size()>0 ? on.next() : on.prev().prev();
	nex.click();
}

function superhero() {
	var img = $('#hero img:visible');
	var nex = img.next();
	if(nex.size() == 0) nex = $('#hero img:eq(0)');
	img.css('z-index',0);
	nex.css('z-index',1).fadeIn('slow',function() { img.hide(); });
	cyc = setTimeout(superhero,4000);
}

function inptrigger() {
	$(this).parent().addClass('on').siblings('.on').removeClass('on');
	var nam = $(this).attr('name');
	$('#'+nam+' span').html($(this).attr('title')).trigger('boom');
	
	if((nam=='experience') || (sizeandexp && (nam=='size'))) {
		if(sizeandexp) {
			nam = $("input[name='size']:checked").attr('title');
			nam = 'o' + nam.substr(0,2) + nam.substr(nam.indexOf('L x ')+4,2) + '_';
		} else {
			nam = '';
		}
		nam += $("input[name='experience']:checked").val();
		$('#currentimg').attr('src',imgss[nam]);
	}
	
	if(updateresults) updateresultsnow();
}

function updateresultsnow() {
	var res = new Array();
	$('#design input:checked').each(function(i) {
		res[i] = (i==2 ? $(this).attr('title') : $(this).val());
		switch(i) {
			case 0: // toggle Colors based on size
				$('#design label.color').each(function() {
					if((typeof pvars[res[i]][$(this).children('input').val()])=="object") { // enabled
						$(this).removeClass('off').children('input').removeAttr('disabled');
					} else { //disabled
						$(this).addClass('off').children('input').attr('disabled',true);
					}
				});
				break;
			case 1: // toggle EXP based on size+color
				$('#design label.exp').each(function() {
					var ex = $(this).children('input').attr('title');
					if((typeof pvars[res[0]][res[1]][ex])=='number') { // enabled
						$(this).removeClass('off').children('input').removeAttr('disabled');
					} else { //disabled
						$(this).addClass('off').children('input').attr('disabled',true);
						if($(this).hasClass('on')) {
							updateresults = false;
							$(this).siblings(':not(.off)').eq(0).children('input').trigger('click');
							updateresults = true;
						}
					}
				});
				break;
			case 2: // toggle Features (options) based on EXP.
				if($(this).parent().hasClass('off')) { //switch'd
					res[2] = $(this).parent().siblings('.on').children('input').attr('title');
				}
				var shex = {'Salon Spa':'ss','Whirlpool':'w','Pure Air':'p','Soaking Bath':'s'};
				var thecat = ($('#snav li.s1').hasClass('here') ? 'luxury' : 'comfort');
				$('#features input').each(function() {
					var op = 'o' + $(this).val();
					if(upgrades[thecat][op][shex[res[2]]]==1) {
						$(this).removeAttr('disabled').parent().removeClass('off');
					} else {
						$(this).attr('disabled',true).parent().addClass('off');
						if($(this).parent().hasClass('on')) {
							updateresults = false;
							$(this).parent().siblings('div.option:not(.off)').eq(0).children('input').trigger('click');
							updateresults = true;
						}
					}
				});
				break;
		}
	});
	
	var restring = '';
	for(i=0; i<res.length; i++) {
		restring += (i==0 ? '' : '-') + res[i];
	}
	//ok
	$('#results').val(restring);
}

function countfavs(count) {
	$('#myfavs span.count').html('Displaying '+ count + ' of ' + count);
}

function myfavorites() {
	var favs = jQuery.cookie("favorites");
	var favR = favs.split('|');
	favR.shift(); // remove the blank first...
	var loadurl = '';
	$('#right .pad').append('<form id="myfavs" method="post" action=""><div class="hdr"><input type="submit" value="Remove Selected" class="btn" /><span class="count" /></div></form>');
	countfavs(favR.length);
	$('#myfavs').submit(removefavorites);
	for(i=0; i<favR.length; i++) {
		loadurl = '' + favR[i];
		if((loadurl.length > 0) && (loadurl!='null')) {
			loadurl = basepath + 'options/favorites/' + loadurl;
			$.ajax({
				url: loadurl,
				cache: false,
				success: function(html){
					$("#myfavs").append(html);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert('ajax failed to load ' + loadurl);
				}
			});
		}
	}
}

function removefavorites() {
	var favs = jQuery.cookie("favorites");
	var favR = favs.split('|');
	favR.shift(); // remove the blank first...
	var newfavs = '';
	var count = favR.length;
	$('#myfavs .fav').each(function(i) {
		if($(this).find('input:checked').size() > 0) {
			$(this).fadeOut('fast',function() { $(this).remove(); });
			count--;
			countfavs(count);
		} else {
			newfavs += '|' + favR[i];
		}
	});
	
	jQuery.cookie("favorites",newfavs, { expires: 14, path: '/' });
	return false;
}

function floatleft() {
	var maxy = $('#design').offset();
	maxy = maxy.top + $('#design').height() - $('#designleft .summary').height();
	var miny = $('#designleft .summary').offset();
	miny = miny.top;
	var wheat = $(window).height();
	var updatemaxy = false;
	$('#design label').children('input').add($('#design div.option').children('input')).click(function() {
		updatemaxy = true;
	});
	
	$(window).scroll(function() {
		var sum = $('#designleft .summary');
		var sumo = sum.offset();
		sumo = sumo.top;
		if(updatemaxy) {
			maxy = $('#design').offset();
			maxy = maxy.top + $('#design').height() - sum.height();
		}
		updatemaxy = false;
		
		var sump = sum.position();
		var wt = $(window).scrollTop();
		if(((wt+wheat > sumo+sum.height()) && (wt < maxy)) || ((wt < sumo) && ((wt > miny) || (sump.top > 0)))) { // paste SUMMARY to top of screen
			sum.stop().animate({top: (wt-miny>0 ? (wt-miny) : 0)}, 400);
		} else if(wt > sumo) {
			sum.stop().animate({top: (maxy-miny)}, 400);
		}
	}).trigger('scroll');
}

$(function() {
	basepath = $('#page').nextAll('script').attr('src');
	basepath = basepath.substr(0,basepath.indexOf('includes'));
	$('#bdrop a, #ddrop a').bind({
		mouseover: function() {
			var tid = $(this).addClass('on').attr('id');
			if(justdropped != tid) {
				if(justdropped!=0) $('#'+justdropped).removeClass('on');
				var bd = tid.substr(0,1);
				var pli = $(this).parents('li[id^='+bd+']');
				$('#'+bd+'4').append('<div class="'+bd+'4i '+pli.attr('id')+'"><img src="'+ basepath +'images/nav/'+ tid + '.jpg" width="248" height="206" /><div><img src="'+ basepath +'images/nav/'+ tid + '.gif" />'+$(this).children('em').html()+'</div></div>').trigger('clear');
				justdropped = $(this).attr('id');
			}
			return false;
		},
		mouseout: function() {
			$(this).removeClass('on');
		}
	});
	$('#b4, #d4').bind('clear',function() {
		$(this).children(':visible').stop().fadeOut('fast',function() { $(this).remove(); });
		$(this).children(':hidden').stop().fadeIn('fast',function() { $(this).parent().removeClass('off'); });
	});
	$('#ddesign, #dbaths').bind('mouseenter',function() { $(this).siblings('ul').fadeIn('fast').children('li:last').addClass('off').trigger('clear').siblings().addClass('on'); $('#homefade').stop().fadeTo('fast',0.6); justdropped=0; })
	.parent().bind('mouseleave',function() { $(this).children('ul').fadeOut('fast').children('li:last').empty(); $('#homefade').stop().fadeOut('fast'); });
	$('#ddrop li:not(:last-child), #bdrop li:not(:last-child)').bind('mouseover', function() {
		if($(this).siblings('.on').size() > 0) {
			$(this).addClass('on').siblings('.on').removeClass('on');
		}
		$(this).children('a.first').trigger('mouseover');
	});
	$('#nav div.flop').each(function() {
		$(this).siblings('a').bind('mouseenter',function() {
			$(this).next().show();
			$(this).parent().parent().parent().css('z-index',20).siblings().css('z-index','-1');
		}).parent().bind({
			mouseover: function() { $(this).children('a:first-child').trigger('mouseover'); return false; },
			mouseleave: function() { $(this).children('.flop').hide();  $(this).parent().parent().css('z-index',1).siblings().css('z-index',1); }
		});
	});
	$('#sitemap').bind('mouseover',function() { $(this).next().fadeTo('fast',1.0); $('#homefade').stop().fadeTo('fast',0.6); })
		.parent().bind('mouseleave',function() { $('#homefade,#sdrop').stop().fadeOut('fast'); });
	$('#email').click(function() {
		$('body').append('<a href="#close" id="closer" /><div id="pop" />');
		$('#closer').height($(window).height()).click(function() {
			$('#closer,#pop').remove();
			return false;
		});
		$('#pop').load($(this).attr('href'));
		return false;
	});
	// homepage
	if($('#home').size() > 0) {
		$('#home .frames a').click(function() {
			if($(this).hasClass('on') == false) {
				var nam = $(this).html();
				var url = $(this).attr('href');
				var num = $(this).attr('class');
				num = num.substr(1);
				var eq = ':eq('+num+')';
				$(this).addClass('on').append('<span />').siblings('.on').removeClass('on').children('span').remove();
				$('#home .text').fadeOut(400); 
				$('#home .main img:visible').fadeOut(400,function() {
					$('#home .main img'+eq).fadeIn(800);
						$('#home .text').each(function() {
						$(this).find('h2.on, p.on').removeClass('on');
						$(this).find('h2'+eq+', p'+eq).addClass('on');
						$(this).find('img.name').attr('src','images/homepage/explore'+num+'.gif');
						$(this).find('a.exp').html('&gt; Experience ' + nam).attr('href',url);
						$(this).delay(400).fadeIn(300);
						});
				});
			}
			clearTimeout(cyc);
			cyc = setTimeout(hometick,6000);
			return false;
		});
		cyc = setTimeout(hometick,6000);
		
		$('#home .choose a.first').bind('mouseover',function() {
			$(this).addClass('on').next().slideDown('fast');
			$('#homefade').css('z-index',3).stop().fadeTo('fast',0.6);
		}).parent().bind('mouseleave',function() {
			$(this).children('a.first').removeClass('on').next().slideUp('fast');
			$('#homefade').stop().fadeOut('fast',function() { $(this).css('z-index',5); });
		});
	}
	
	$('input.txt, input.zip').attr('title',function() {
		return $(this).val();
	}).bind({
		focus: function() {
			$(this).val(function(index, value) {
				return (value==this.title ? '' : value);
			});
		},
		blur: function() {
			$(this).val(function(index, value) {
				return (value=='' ? this.title : value);
			});
		}
	});
	
	$('#snav li').bind({
		mouseenter: function() {
			$(this).addClass('on').removeClass('off');
			if($(this).parent().children('li.here').size() == 0) {
				$(this).siblings(':not(.on)').addClass('off');
			}
			$(this).children('div.block').children('img').fadeOut('fast');
		},
		mouseleave: function() {
			if($(this).hasClass('here')==false) {
				$(this).removeClass('on');
			}
			if($(this).parent().children('li.here').size() == 0) {
				$(this).siblings('.off').removeClass('off');
			} else {
				if($(this).hasClass('here')==false) $(this).addClass('off');
			}
			$(this).children('div.block').children('img').fadeIn('200');
		}
	});
	
	if($('#prods').size() > 0) {
		$('#tabs a').click(function() {
			$(this).parent().addClass('on').siblings('.on').removeClass('on');
			$(this).attr('href', function() {
				$('#prods .prod').show();
				if($(this).html()!='all') {
					$('#prods .prod:not(.'+$(this).html()+')').hide();
				}
				return this.href;
			});
			return false;
		});
		
		$('#prods .prod').bind({
			mouseenter: function() {
				$(this).children('div.i').fadeIn('fast');
			},
			mouseleave: function() {
				$(this).children('div.i').fadeOut('fast');
			}
		});
	}
	
	if($('#hero img').size() > 1) {
		cyc = setTimeout(superhero,4000);
	}
	
	if($('#faq').size() > 0) {
		$('#faq h2:gt(0)').click(function() {
			$(this).next('p').slideToggle('fast');
			return false;
		}).css('cursor','pointer').click();
	}
	
	if($('#tubtabs').size() > 0) {
		$('#tubtabs a').each(function() {
//			var txt = $(this).html();
			$(this).html(function(i,html) {
				return '<span class="on">'+ html +'</span><span class="off">'+ html +'</span>';
			});
			$(this).click(function() {
				if($(this).parent().hasClass('on')) return false;
				$(this).parent().addClass('on').siblings('.on').removeClass().children('a').children('span.off').show();
				$(this).children('span.off').hide();
				
				var onright = $(this).attr('href');
				var offright = (onright=='#details') ? '#design' : '#details';
				
				$(onright+', '+onright+'left').show();
				$(offright+', '+offright+'left').hide();
				
				if(onright=='#design') {
					floatleft();
					
					var savedresults = jQuery.cookie("savedresults");
					if(savedresults) {
						var saved = savedresults.split('-');
						$('#design input:checked').each(function(i) {
							if(i<saved.length) {
								var ident = '['+ (i==2 ? 'title' : 'value') +'='+ saved[i] +']';
								$(this).parent().parent().find('input:radio'+ident).click();
							}
						});
					}
				} else {
					$(window).unbind('scroll');
				}
				//return false;
			});
			var whash = '' + window.location.hash;
			if(whash == $(this).attr('href')) $(this).click();
		});
		
		if($('#design').size() > 0) {
			$('#design label').children('input').click(inptrigger);
			$('#design div.option').children('input').click(inptrigger);
			$('#design input:checked').trigger('click');
			updateresults = true;
			updateresultsnow();
			
			$('#design label.exp a.rm').click(function() {
				$(this).next().slideToggle('fast');
				$(this).parent().toggleClass('open');
				return false;
			});
			
			$('#designleft .summary div').bind('boom',function() {
				$(this).animate({ backgroundColor: "go" }, 300); //hackd
			});
			
			$('#saveresults').bind('submit',function() {
				var res = $(this).children('#results').val();
				var pth = '' + window.location;
				pth = pth.substr(pth.indexOf('/', 8));
				jQuery.cookie("savedresults",res, { expires: 14, path: pth });
				alert('Your results have been saved and will display the next time you visit this page.');
				return false;
			});
			
			$('#details a.designthis').click(function() {
				var markerd = $(this).attr('title');
//				alert('you want to design a ' + markerd + ' : ' + pvars[markerd][0] + '-' + pvars[markerd][1]);
				$('#design div.attribute:eq(0) input[value='+pvars[markerd][0]+']').click();
				$('#design div.attribute:eq(1) input[value='+pvars[markerd][1]+']').click();
				$('#design div.attribute:eq(2) input[title="'+markerd+'"]').click();
				updateresults = true;
				updateresultsnow();
				$('#tubtabs a:eq(1)').click();
			});
		}
	}
	
	if($('#moredesc').size() > 0) {
		$('#right').addClass('hasmore');
		$('#moredesc').click(function() {
			$(this).hide().parent().parent().addClass('on').stop().animate({
				top: '-=394',
				height: '+=394'
			}, 500, function() {
				$('#closedesc').show();
			}).children('div').stop().slideDown(500,function() {
				// Animation complete
			});

			return false;
		});
		$('#closedesc').click(function() {
			$(this).hide().parent().stop().animate({
				top: '+=394',
				height: '-=394'
			}, 500, function() {
				$(this).removeClass('on');
				$('#moredesc').show();
			}).children('div').stop().slideUp(500,function() {
				// Animation complete
			});
			return false;
		});
	}
	
	if(($('ul.collapse').size() > 0) && ($('ul.collapse').children('li').size() > 1)) {
		$('ul.collapse a.c').each(function(i) {
			if($(this).siblings('ul').size() > 0) {
				$(this).parent().addClass('ok');
				$(this).click(function() {
					$(this).next().slideToggle().parent().toggleClass('on');
					if($(this).parent().hasClass('on')) $(this).parent().siblings('li.ok.on').children('a.c').click();
					return false;
				});
				// at first, click = hide, so
				if($('#left').hasClass('landing')) {
					if(i>0) $(this).click();
				} else {
					if($(this).parent().hasClass('on')) {
						if($(this).next().find('input:checked').size() == 0) $(this).click();
					} else {
						$(this).parent().addClass('on');
					}
				}
			} else {
				$(this).parent().toggleClass('on');
			}
		});
		if($('#left').hasClass('landing')) {
			updateresults = true;
		}
		$('ul.collapse a.on').each(function() {
			if($(this).parent().parent().hasClass('collapse')) {
				$(this).attr('class','c').parent().addClass('on');
			}
			if($(this).parent().parent().prev().hasClass('c') == false) {
				$(this).parent().parent().prev().addClass('on');
			}
		});
		
		$('ul.collapse label.all').each(function() {
			$(this).click(function() {
				var chk = $(this).children('input').attr('checked');
				$(this).parent().siblings().find('input').attr('checked',chk);
			});
		});
		
		$('#results a').click(function() {
			var tar = $($(this).attr('href'));
			if(tar.parent().hasClass('on')) {
				tar.parent().siblings('.on').removeClass('on').children('ul').slideUp();
			} else {
				tar.click();
			}
			return false;
		});
	}
	
	$('#left div.download').each(function() {
		$(this).children('a.on').click(function() {
			$(this).toggleClass('on').next().slideToggle('fast');
			return false;
		}).click();
		$(this).children('div').children('p').children('a').click(function() {
			var val = $(this).prev().val();
			if(val!='#') {
				$(this).attr('href',val);
				return true;
			}
			return false;
		});
	});
	
	$('#favorites, #favorites2').click(function() {
		//alert('you want to add product id' + $(this).attr('href') + ' to your Favorites?');
		var favs = "" + jQuery.cookie("favorites");
		var href = $(this).attr('href');
		favs += "|" + href.substr(1);
		jQuery.cookie("favorites",favs, { expires: 14, path: '/' });
		alert('The product has been added to your Favorites.');
		return false;
	});
	
	$('#right .pad a.more').click(function() {
		if($(this).hasClass('less')) {
			if($(this).removeClass('less').html()=='READ LESS...') {
				$(this).html('READ MORE...').prev().slideUp().before($(this));
			} else {
				$(this).next().slideUp();
			}
		} else {
			if($(this).addClass('less').html()=='READ MORE...') {
				$(this).html('READ LESS...').next().slideDown().after($(this));
			} else {
				$(this).next().slideDown();
			}
		}
		return false;
	});
	
	if($('#land_left').size() > 0) {
		$.ajax({
			url: imgss,
			dataType: 'html',
			success: function(data) {
				$('#land_left').html(data).append('<span class="pager" />').children('div').each(function(i) {
					$('#land_left span.pager').append('<a href="#"'+(i==0 ? ' class="on"':'')+'>'+(i+1)+'</a>');
					if(i==0) {
						$(this).addClass('on');
					} else {
						$(this).hide();
					}
				});
				$('#land_left span.pager a').each(function(i) {
					$(this).click(function() {
						$(this).addClass('on').siblings('.on').removeClass();
						$('#land_left .slide:eq('+i+')').show().siblings('.slide.on').fadeOut('fast',function() {
							$(this).removeClass('on').siblings('.slide:visible').addClass('on');
						});
						return false;
					});
				});
			}
		});
	}
});

// http://plugins.jquery.com/project/color
(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = [223,223,223];
				fx.end = [255,255,255];
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});
})(jQuery);