

$(function(){

	if ($('#landing-video').length || $('#promo-video').length || $('#instructional-video').length) {
		$("a[rel^='prettyPhoto']").prettyPhoto();
	}
	
	// Main page animation . . .
	if ($('#main-image').length) {
		mntimeout = setInterval( "slideSwitch('ffwd')", 5000);
	}
	if ($('#rewinder').length) {
		$('#rewinder a').click(function() {
			slideSwitch(this.id); // Careful, ffwd and rewind
			return false;
		});
	}
	
	// Main signup
	if ($('#main-bottom #wide form').length) {
		$('#main-bottom #wide .signup-input').focus(function() { popSub(this.id,0); });
		$('#main-bottom #wide .signup-input').blur(function() { popSub(this.id,1); });
		$('#main-bottom #wide form').submit(function() { return optinValidate(this); });
	}
	
	// For both products entry page and direct products pages, both having different class requirements.
	if ($('#products-nav').length || $('#prod-nav').length) {
		var ul = ($('#products-nav').length)?'products-nav':'prod-nav';
		var cls = ($('#products-nav').length)?'product-tabs':'prod-tabs';
		$('#'+ul+' li a').click(function() {
		$('#'+ul+' li a').removeClass('current');
		$(this).addClass('current');
			var $targ=$(this).attr('rel');
			if ($('.'+cls).length) {
				$('.'+cls).each(function() {
					if ($targ == this.id) { $(this).css('display','block'); }
					else { $(this).css('display','none'); }
				});
			}
			return false;
		});
	}

	// For the product page scroll list . . .
	timeout=null;
	linesize=14; // px;
	$('#product-codes #down').mousedown(function() { 
		scroll_list('down');
		return false;
	});
	$('#product-codes #down').mouseup(function() { stop_scroll(); });
	
	$('#product-codes #up').mousedown(function() { 
		scroll_list('up');
		return false;
	});
	$('#product-codes #up').mouseup(function() { stop_scroll(); });
	
	// product specials . . .
	if ($('#specials a').length) {
		$('#specials a').click(function() {
			var day=new Date();
			var id=day.getTime();
			var $targ=$(this).attr('rel');
			var dims=$targ.split(',');
			var params='width='+dims[0]+',height='+dims[1]+',scrollbars,resizable';
			var win = open(this.href,id,params);
			return false;
		});
	}
	
	// FAQ page . . .
	if ($('#faq-page').length) {
		$('#faq-page h2 a').click(function() {
			//$('#faq-page .closed-faq').css('display','none');
			var targid = 'div_'+this.id;
			var set = ($('#faq-page #'+targid).css('display')=='block')?'none':'block';
			$('#faq-page #'+targid).css('display',set);
			return false;
		});
	}
	
	// Order page ....
	if ($('#complete-form #distributor').length) {
		$('#product-codes #list-window ul li a').click(function() {
			var targid = $(this).attr('href');
			$('#complete-form #distributor').val(targid).attr('selected', true); 
			return false;
		});
		$('#complete-form #same_address').click(function() { pop_shipping(this); });
	}
	if ($('#category').length) {
		$('#category').change(function() { this.form.submit(); });
	}
	if ($('#product_id').length) {
		$('#product_id').change(function() { this.form.submit(); });
	}
	if ($('#next-button-row').length) {
		$('#next-button-row').addClass('no-display'); // Visible by default for accessibility
	}
	
	// Resources (video)
	
	if ($('#resources-video').length) {
		$('#resources-video a').click(function() {
			return newWin($(this).attr('href'),480,255);
		});
	}
	
	// Clipped images in resources section url,title,width,height
	if ($('.clipped-img').length) {
		$('.clipped-img').click(function() {
			return writeWin($(this).attr('href'),$(this).attr('title'),1500,800);
		});
	}
	
	// "sexy curls" on product pages
	if ($('#curl-target').length) {
		$( '#curl-target').fold();
		$('#turn_object').click(function() {window.open('http://www.sofreliner.com'); });
	}
});

// Main page label-less form

function popSub(objid,st) {
	vals = {
		// Non salesforce version.
		'fname':'First Name',
		'lname':'Last Name',
		'email':'Enter your email address',
		// salesforce version.
		'first_name':'First Name',
		'last_name':'Last Name',
		//'email':'Enter your email address'
	};
	for (key in vals) {
		if ((objid==key) && document.getElementById(key)) {
			if ((st==0) && (document.getElementById(key).value==vals[key])) {
				document.getElementById(key).value='';
			}
			if ((st==1) && (document.getElementById(key).value=='')) {
				document.getElementById(key).value=vals[key];
			}
			break;
		}
	}
		
}

// Main opt-in validation 
function optinValidate(form) {
	var em = document.getElementById('email');
	var fn = document.getElementById('fname');
	var ln = document.getElementById('lname');
	var nl = document.getElementById('nl');
	var prom = document.getElementById('promos');

	var regex = /.*\@.*[.].*/;
	var msg='';
	if (! regex.test(em.value)==true) {
		msg += 'Please enter a valid email address.\n';
	}
	if ((fn.value=='') || (fn.value=='First Name') || (ln.value=='') || (ln.value=='Last Name')) {
		msg += 'Please enter your first and last name.\n';
	}
	if (! (nl.checked) && ! (prom.checked)) {
		msg += 'Please select the emails you would like to receive.\n';
	}	
	if (msg !='') { alert(msg); }
	else { form.submit(); }
	return false;

}

// Order form: populate shipping fields if box checked.

function pop_shipping(chk) {

	var addr = ['address','address2','city','state','zip','country'];
	var st;
	for (i=0;i<addr.length;i++) {
		var shp = 'ship_'+addr[i];
		if (document.getElementById(addr[i]) && document.getElementById(shp)) {
			if (addr[i]=='state') {
				st = (chk.checked)?document.getElementById(addr[i]).selectedIndex:0;
				document.getElementById(shp).selectedIndex=st;
			}
			else {
				st = (chk.checked)?document.getElementById(addr[i]).value:'';
				document.getElementById(shp).value=st;
			}
		}
	}
}

// Products pages and order page: a scrolling li in a window.

function scroll_list(direction) {
	if ($('#product-codes #list-window ul').length) {
		var currtop		= parseInt($('#product-codes #list-window ul').css('top'));
		var wintop		= parseInt($('#product-codes #list-window').css('top'));
		var ht			= parseInt($('#product-codes #list-window ul').css('height'));
		var	winht		= parseInt($('#product-codes #list-window').css('height'));
		var currbot		= parseInt(currtop+ht);
		var winbot		= parseInt(wintop+winht);

		if ((direction=='up') && ((currtop+linesize) >= linesize)) {
			$('#product-codes #list-window ul').stop(true,true);
			return;
		}

		if ((direction=='down') && ((currbot) <= (winbot - linesize))) {
			$('#product-codes #list-window ul').stop(true,true);
			return;
		}
		if (direction=='down') { 
			$('#product-codes #list-window ul').animate({'top':'-='+linesize+'px'},30); 
		}
		else { 
			$('#product-codes #list-window ul').animate({'top':'+='+linesize+'px'},30); 
		}
		if (! timeout) { timeout = setInterval(function() { scroll_list(direction)}, 30); }
	}
}

function stop_scroll () {
	clearInterval(timeout);
	timeout=null;
	$('#product-codes #list-window ul').stop(true,true);
}

function slideSwitch(direction) {
	var $active = $('#main-image div.active');
	if (direction=='ffwd') {
		if ( $active.length == 0 ) { $active = $('#main-image div:last'); }
		var $next =  $active.next().length ? $active.next(): $('#main-image div:first');
	}
	else {
		if ( $active.length == 0 ) { $active = $('#main-image div:first'); }
		var $next =  $active.prev().length ? $active.prev(): $('#main-image div:last');
	}
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
	if (! mntimeout) {
		mntimeout = setInterval( "slideSwitch('ffwd')", 5000);
	}
}

// Resources video
function newWin(url,width,height) {
	var day = new Date();
	var id = day.getTime();
	var params = 'width='+width+',height='+height+',resizable';
	var win = window.open(url,id,params);
	return false;
}

// various
function writeWin(url,title,width,height) {
	var day = new Date();
	var id = day.getTime();
	var params = 'width='+width+',height='+height+',resizable,scrollbars';
	var msg = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'+
		'<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">'+
		'<title>'+title+'</title><style type="text/css"> body { text-align: center; font-family:arial,helvetica,sans-serif; }'+
		'</style></head><body><h3>'+title+'</h3><p><img src="'+url+'" alt="'+title+'"></p><form action="" onsubmit="return false;">'+
		'<hr><input type="button" onclick="window.close();" value="Close Window"><hr></form></body></html>';
	var win = window.open('',id,params);
	win.document.write(msg);
	win.document.close();
	return false;
}


// Used by events page.
function more_info(id) {
	if (document.getElementById(id)) {
		var day=new Date();
		var wid=day.getTime();
		var params='width=500,height=500,scrollbars,resizable';
		var msg='<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">'+
		'<head><title>More Information</title>'+
		'<style type=\"text/css\"> body { text-align: center; font-family: arial, helvetica, sans-serif; }</style>'+
		'</head><body><h3>More Information</h3><p>' + document.getElementById(id).innerHTML + '</p><form action=\"\"><hr>'+
		'<input type=\"button\" onclick=\"window.close();\" value=\"Close Window\"><hr></form></body></html>';
		var win = open('',wid,params);
		win.document.write(msg);
		win.document.close();
	}
	return false;
}
			
