var img = new Array();
var _ia;

$(document).ready(
	function() {
//		init_intro();
		init_gallery();
//		init_order();
//		init_contact();
//		init_video();
		rewrite_secure();
	}
);

/*$(window).load(
	function() {
		init_home();
	}
);
*/

/* function init_home() {
*	var m, i, l, t;
*
*	_ia = $('#montage_img');
*
*	m = _ia;
*	if (m.length) {
*		img.push(m.attr('src'));
*
*		l = m.next();
*		do {
*			img.push(l.attr('src'));
*			l = l.next()
*		} while (l.length);
*
*		cycle_out(1);
*	}
*}
*/
function _cycle_out(idx) { mi.fadeOut('slow', function() { cycle_src(idx); }); }

function cycle_out(idx) {
	_ia.fadeOut(
		'slow',
		function () {
			cycle_src(idx);
		}
	);
}

function cycle_src(idx) {
	var f;
	idx = ++idx % img.length;
	_ia.attr('src', img[idx]);
	cycle_in(idx);
}

function cycle_in(idx) {
	_ia.fadeIn('slow');

	var f = 'cycle_out(' + idx + ')';
	window.setTimeout(f, 8000);
}



function init_intro() {
//	window.setTimeout(intro_fade, 10000);

	$('#experiences_main').append(
		'<p><a name="continue" href="#continue" id="experiences_continue">Continue</a></p>'
	);

	var h, i;

	if (h = $('#experiences_extra')) {
//		$('#content_experiences').attr('height', h.attr('height'));

//		h.fadeOut(
//			100,
//			function() {
//				this.style.margin = '0 0 0 25%';
//			}
//		);
	}

	$('#experiences_continue').click(
		function() {
			intro_fade();
			return false;
		}
	);
}

function intro_fade() {
	$('#experiences_main').fadeOut(
		'slow',
		function() {
			$('#experiences_extra').attr('style', 'margin-left:25%');
		}
	);
}


function init_gallery() {
	$(function() {
	    $("#carousel").jCarouselLite({
	        btnNext: "#thumb_next",
	        btnPrev: "#thumb_prev",
	        vertical: false,
	        circular: false,
	        visible:  10,
	        scroll: 5
	    });
	});

	$('#carousel a').click(
		function() {
			return swapout(this);
		}
	);
}

function swapout(a) {
	var img;
	img = a.href.replace(/^(.+)t\.jpg$/, '$1f.jpg');
	$('#fullsize').fadeOut(3000, function () { fadein(img); });

	return false;
}

function fadein(img) {
	var fs = $('#fullsize');
	fs.attr('src', img);
	fs.fadeIn('slow');

	return false;
}


function init_order() {
	var cls = 'color';
	var s = 'a.' + cls;
	$(s).click(function() { return show_preview(this.href, 'color'); });

	s = '#' + cls + '_img';
	$(s).click(function() { return hide_preview('color') });

	cls = 'shape';
	s = 'a.' + cls;
	$(s).click(function() { return show_preview(this.href, 'shape'); });

	s = '#' + cls + '_img';
	$(s).click(function() { return hide_preview('shape') });

	s = 'select.inlay';
	$(s).change(function() { return inlay_change(this.id) });

	$('#psth010').submit (function() { return complex_cart('psth010',  'shape', 'inlay') } );
	$('#psth0100').submit(function() { return complex_cart('psth0100', 'shape', 'inlay', 'leg') });
}


function ucfirst(str) {
	var uf = new String();
	uf  = str.substr(0, 1).toUpperCase();
	uf += str.substr(1);

	return uf;
}

function combine(keys, values) {
	var i = new Number();
	var imp = new String(), kl = new Number();

	kl = keys.length;
	if (kl && (kl == values.length)) {
		for (i = 0; i < kl; i++) {
			imp += ', ' + keys[i] + ': ' + values[i];
		}

		imp = imp.substr(2);
	}

	return imp;
}

function show_preview(img, cls) {
	var s = '#' + cls + '_img';

	ip = $(s);
	ip.attr('src', img);

	dp = ip.parent();
	dp.removeClass('hidden');
	dp.removeClass('none');

	dp.show('slow');

	return false;
}

function hide_preview(cls) {
	var dp = $('#' + cls + '_preview');
	dp.hide('slow');

	return false;
}

function inlay_change(id) {
	var si, r, ot, ao, nt;

	id = '#' + id;
	var si = $(id);

	r = false;
	ot = false;

	if (si.attr('value').toLowerCase() == 'other') {
		r = true;
		ao = si.children();
		nt = 'none';

		ot = prompt('What kind of design would you like?')

		if (ot) {
			var me,tt, m;

			for (i = 0; i < ao.length; i++) {
				tt = ao[i].firstChild;
				tt = tt.data;

				me = new RegExp(ot, 'ig');

				if (m = tt.match(me)) {
					nt = tt;
					break;
				}
			}

			if (!m) {
				nt = ot;
				nt = ucfirst(ot);
				h = '<option value="%s">%s</option>';
				h = h.replace(/%s/g, nt);

				si.append(h);
			}
		}

		si.val(nt);
	}

	return r;
}

function complex_cart() {
	var l, i, v;

	v = new String();
	for (i = 1; i < arguments.length; i++) {
		l = '#' + arguments[0] + ' :input[@name=' + arguments[i] + ']';

		v += ', ';
		v += ucfirst(arguments[i]);
		v += ': ';
		v += $(l).fieldValue()[0];
	}

	v = v.substr(2);

	l = '#' + arguments[0] + 'os1';
	$(l).val(v);

	return true;
}



function init_contact() {

}


function init_video() {
	mi = $('#video_schara');
	if (mi.length) {
		mi.html('');

		mi.flash(
			{	src: '/media/swf/flvplayer.swf?file=/media/swf/PSTH320.flv&amp;autostart=false',
				width: 320,
				height: 240
			}
		);
	}
}

$(document).ready(function(){
	$("form").submit(function() {
		if ($("input[name='username']").val()) {
			$.cookie('secret', $("input[type='password']").val());
			return true;
		} else {
			return false;
		}
        });
});

function rewrite_secure() {
	var secret = $.cookie('secret');
	$("a[href*='private-']").attr("href", function() {
			return this.href.slice(0,-5) + secret + ".html";
		});
}

function menu_goto( menuform ) {
	var baseurl = "http://www.msfsmpb.org";
	selecteditem = menuform.newurl.selectedIndex ;
	newurl = menuform.newurl.options[ selecteditem ].value;
	if (newurl.length != 0) {
		location.href = baseurl + newurl;
	}
}
