var shown = -1;

function hidem() {
	$('#s'+shown+' span').remove();
	$('#s'+shown).hide();
	$('#n'+shown+' a').removeClass('on');
	$('#detailsPop').hide();
	$('#ovr').hide();
	shown = -1;
}

function drop(i) {
	if(shown > -1) { hidem(); }
	shown = i;
	$('#s'+i)
		.prepend('<span></span>')
		.show();
	$('#n'+i+' a').addClass('on');
	$('#ovr').show();
}

function spaJetsOn() {
	$(this).children('img').attr('src','../images/jets/on/'+$(this).children('img').attr('src').substr(15));
	$('#jets-feature .jets-info').html($(this).next().html());
	$('#jets-feature .jets-info').prepend('<h2>'+$(this).children('b').html()+'</h2>');
	$('#jets-feature .def-inside').hide();
	$('#jets-feature .jets-info').show();
}
function spaJetsOff() {
	$(this).children('img').attr('src','../images/jets/'+$(this).children('img').attr('src').substr(18));
	$('#jets-feature .jets-info').empty().hide();
	$('#jets-feature .def-inside').show();
}

function detailOver(link) {
	$('#detailsPop a').empty();
	var img = $(link).children("img");
	var pos = $(link).attr('href');
	var meta = $(link).next();
	//alert(pos);
	$("#det-s").html($(".s", meta).html());
	$("#det-d-en").html($(".d .en", meta).html());
	$("#det-d-m").html($(".d .m", meta).html());
	$("#det-j").html($(".j", meta).html());
	pos = pos.substr(0,pos.length -5);
	if(pos.indexOf('InGround') < 0) { 
		pos = 'images/' + pos.substr(0,3) + '-series' + pos.substr(9).toLowerCase() + '-small.jpg';
	} else { 
		pos = 'images/880-series/in-ground-' + img.attr("alt").toLowerCase() + '-small.jpg';
	}
	$('#detailsPop img').attr('src', pos);
	$('#detailsPop img').attr('alt',img.attr("alt"));
	$('#detailsPop a')
		.attr('href', $(link).attr('href'))
		.attr('class',img.attr("alt"))
		.append($(img).clone());
	pos = $(img).offset();
	$('#detailsPop')
		.css({left: (pos.left - 18), top: (pos.top -161)})
		.show();
	$('#ovr').show();
}
function newsPop() {
		$('body').append('<a href="" id="hider"></a><iframe id="signupframe" name="signupframe" width="375" height="160" frameborder="0"></iframe><a href="" id="closer">close</a>');
		$('#hider, #closer').click(removeLB);
		$('#hider').height($('body').height());
}

function colorPickerPop() {
		$('body').append('<a href="" id="hider"></a><iframe id="cpframe" src="../Features/ColorPicker.html" width="714" height="451" frameborder="0"></iframe><a href="" id="closer" class="closeCP">CLOSE X</a>');
		$('#hider, #closer').click(removeLB);
		$('#hider').height($('body').height());
		var hoff, woff, unfixpos = false;
		if($(window).height() > 725) {
			hoff = 134;	
		} else if($(window).height() > 450) {
			hoff = ($(window).height() - 451)/2;	
		} else {
			hoff = self.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
			unfixpos = true;
		}
		if($(window).width() > 854) {
			woff = 70;	
		} else if($(window).width() > 714) {
			woff = ($(window).width() - 714)/2;	
		} else {
			woff = self.pageXOffset || $.boxModel && document.documentElement.scrollLeft || document.body.scrollLeft;
			unfixpos = true;
		}
		$('#cpframe').css({ top:(hoff+"px"), left:(woff+"px"), position:unfixpos ? 'absolute' : 'fixed' });
		$('#closer.closeCP').css({ top:((hoff+2)+"px"), left:((woff+640)+"px"), position:unfixpos ? 'absolute' : 'fixed' });
		$(window).resize(function() { $('#hider').height($('body').height()); });
		
		return false;
}

function removeLB() {
	$('#hider, #signupframe, #closer, #cpframe').remove();
	return false;
}

var tmpclk;
var popX; var popY; var minX; var maxX; var minY; var maxY;
$(function(){
	$('body').append('<a href="" id="ovr"></a>');
	$('input').blur(function() {
		if($(this).val() == "") {
			$(this).val($(this).attr('title'));
			if($(this).attr('title')) {
				$(this).attr('title','');
			}
		}
	});
	$('input').focus(function() {
		tmpclk = $(this).val();
		$(this).attr('title',tmpclk).val("");
	});	
	$('#nav a').each(function(i){ $(this).mouseover(function() { drop(i); }); });
	$('#ovr').mouseover(hidem);
	var st = $('#n0 a').attr('href');
	var ps = st.indexOf('spas.html');
	st = st.substr(0,ps);
	$('#hdr form#dealer-finder').attr('method','post').attr('action',st + 'LocateDealer/LocateDealer.php');
	$('#compare a').mouseover(function() { detailOver(this); });
	$('#compare a[href$="Series.html"]').unbind('mouseover').css('margin','0').children().css('margin','0');	
	$('#detailsPop a').mouseout(hidem);
	//add submenus
	
	str = '<a href="' + st;
	$('#nav').after(
	    '<div id="s0">'+str+'spas.html">All Spas</a>'+str+'880Series/880Series.html">880 Series</a>'+str+'780Series/780Series.html">780 Series</a>'+str+'680Series/680Series.html">680 Series</a>'+str+'compare-spas.html">Compare</a></div>' +
		'<div id="s1">'+str+'Features/Jets.html">Jets</a>'+str+'Features/Colors.html">Colors</a>'+str+'Features/Lighting.html">Lighting</a>'+str+'Features/Seating.html">Seating</a>'+str+'Features/Stereos.html">Stereos</a>'+str+'Features/Covers.html">Covers</a>'+str+'Features/Controls.html">Controls</a>'+str+'Features/Purifiers.html">Purifiers</a></div>' +
		'<div id="s2">'+str+'Accessories/Synthetic-Accessories.html">Synthetic &amp; Wood</a>'+str+'Accessories/Complements.html">Complements</a>'+str+'Accessories/SunBrite-TV.html">SunBrite TV</a>'+str+'Accessories/SunScents.html">SunScents</a>'+str+'Accessories/Gazebos-Pergolas.html">Gazebos &amp; Pergolas</a>'+str+'Accessories/Cleaners-Filters.html"> Filters, Cleaners, &amp;<br />Ozone Systems</a>'+str+'Accessories/Cover-Lifters.html">Cover Lifters &amp;<br />Accessories</a>'+str+'Accessories/Accessories-Bazaar.html">Accessories Bazaar</a></div>' +
		'<div id="s3">'+str+'BackyardIdeas/Planning.html">Planning</a>'+str+'BackyardIdeas/Installations.html">Installations</a>'+str+'BackyardIdeas/How-To.html">How To</a>'+str+'BackyardIdeas/FAQs.html">FAQs</a></div>' +
		'<div id="s4">'+str+'Therapy/Hydrotherapy.html">Hydrotherapy</a>'+str+'Therapy/Aromatherapy.html">Aromatherapy</a>'+str+'Therapy/Alternative.html">Alternative Therapies</a>'+str+'Therapy/Stress.html">Stress</a>'+str+'Therapy/Arthritis.html">Arthritis</a>'+str+'Therapy/Sleep.html">Sleep</a>'+str+'Therapy/Workout.html">Workout</a></div>' +
		'<div id="s5">'+str+'SundanceDifference/WaterQuality.html">Water Quality</a>'+str+'SundanceDifference/Energy.html">Energy Efficiency</a>'+str+'SundanceDifference/Technology.html">Technology</a>'+str+'SundanceDifference/Manufacturing.html">Manufacturing</a>'+str+'SundanceDifference/ISO.html">ISO Certification</a>'+str+'SundanceDifference/Warranty.html">Warranty</a>'+str+'SundanceDifference/Honors.html">Honors</a>'+str+'SundanceDifference/FAQs.html">FAQs</a></div>'
	);
	$('#s0,#s1,#s2,#s3,#s4,#s5').addClass('sub');
	$('.sub').wrapInner('<ul></ul>');
	$('.sub a').wrap('<li></li>');
	if($.browser.msie && (jQuery.browser.version < 7.0)) {
		$('head').append('<link rel="stylesheet" href="includes/ie6.css" />');
	}
	
	// jets tab in spa pages
	$("#jets-links .jet a")
		.mouseover(spaJetsOn)
		.mouseout(spaJetsOff)
		.click(function() { return false; });
	if($('#spaDetails_colors')) {
		popX = $('#spaDetails_colors').offset();
		$('#spaDetails_colors').append('<span id="cpop"><span></span></span>');
		$('#cpop').hide();
		$('#spaDetails_colors div p ~ img')
			.mouseover(function() {
				popX = $(this).offset();
				minX = popX.left + 2; maxX = minX + $(this).width() - 2; minY = popX.top + 2; maxY = minY + $(this).height() - 3;
			}).mousemove(function(event) {
				popX = event.pageX -2;
				popY = event.pageY;
				moveCPop($(this).attr('alt'));
			});
		$('#cpop').mousemove(function(event) {
				popX = event.pageX -2;
				popY = event.pageY;
				moveCPop(false);
		});
	}
	
	// enlarge view on spa pages
	$('#spaDetails_spaPic a:first').click(function() {
		window.open($(this).attr('href'), 'SpaBig', 'width=600, height=620, location=no, menubar=no,resizeable=1,scrollbars=yes,toolbar=0,top=10,left=100');
		return false;
	});
	
	if($('#difference_sub')) {
		$('#difference_sub div:last').css('border-bottom','none');
	}
	$('form#emaillist').bind('submit',function() { newsPop(); });
	/*
	$('#request').attr('href','#').attr('title','Newsletter Sign Up').click(newsPop);
	if($('.newspop')) {
		$('.newspop').click(newsPop);
	}
	*/
	if(window.location.hash == '#colorPicker') {
		colorPickerPop();
	}
});

function moveCPop(imgALT) {
	if((popX > minX) && (popX < maxX) && (popY > minY) && (popY < maxY)) {
		$('#cpop').css({left: popX + 1, top: popY - 27});
		if(imgALT) {
			$('#cpop').show().children('span').html(imgALT);
		}
	} else {
		$('#cpop').hide();
	}
}
