//
// cdbaby.js
// helper functions and effects functions for cdbaby.com
// uses scriptaculous.js (see http://script.aculo.us)
//
// author: Martin
// second revision: 11/2008
//


// Initializes the slideylist by doubling all of the list's contents and appending it to the front of the list.
// This allows the list to be scrolled in either direction without the possibility of the user getting a glimpse
// of the mechanism that is moving items from one end of the list to the other to give the illusion of wraparound.
function slideylistinit(listid, viewportwidth, host)
	{
	var aurl = host+"/home-"+listid;

	new Ajax.Updater($(listid), aurl, { method: 'get' });
	$(listid).style.left = (0 - viewportwidth) + "px";

	$(listid+'-right').style.display = "block";
	$(listid+'-left').style.display = "block";
	}

// infinitely looping sliding list
function slideylist(listid, direction, xslide)
	{
	// how many <li> elements are visible in the viewport at once?
	var lisvisible = 1;

	var xpos = parseInt($(listid).style.left);
	var albumslist = $(listid).innerHTML;

	if(direction == 'right')
		{
		xslide = 0 - xslide; // sliding right is negative

		var firstcover = "";
		for(var i = 0; i < lisvisible; i++)
			{
			// Note the toLowerCase() call is critical for this to work in IE7.
			// For some reason IE's js/DOM engine represents XHTML internally in all uppercase regardless of the source,
			// so parsing the albumslist string for "<li" fails unless you TELL IE7 to make it lowercase first.
			firstcover = albumslist.slice(0, albumslist.toLowerCase().indexOf('<li', 4)); // start a few chars in to avoid the first "<li"
			albumslist = albumslist.slice(albumslist.toLowerCase().indexOf('<li', 4)).concat(firstcover);
			}
		}
	if(direction == 'left')
		{
		var lastcover = "";
		for(var i = 0; i < lisvisible; i++)
			{
			// See note above re: toLowerCase() & IE7
			lastcover = albumslist.slice(albumslist.toLowerCase().lastIndexOf('<li'));
			albumslist = lastcover.concat(albumslist.slice(0, albumslist.toLowerCase().lastIndexOf('<li')));
			}
		}

	$(listid).innerHTML = albumslist;
	xpos = xpos - xslide;
	$(listid).style.left = xpos + "px";
	
	new Effect.Move(listid, { beforeStart: disablecontrols(listid), x: xslide, duration: 1, transition: Effect.Transitions.sinoidal, afterFinish: enablecontrols(listid) });
	}

function disablecontrols(listid) {
	$(listid+'-right').style.display = "none";
	$(listid+'-left').style.display = "none";
}
function enablecontrols(listid) {
	rightone = listid+'-right';
	leftone = listid+'-left';
	setTimeout("$(rightone).style.display = 'block'", 1000);
	setTimeout("$(leftone).style.display = 'block'", 1000);
}


//
// Yer basic copy one address to another address with the same data fields script.
//
function copyaddress(destprefix, sourceprefix) {
	if(destprefix == undefined) destprefix = 'bill_';
	if(sourceprefix == undefined) sourceprefix = '';

	// See if "state field" has changed in type.

	if (document.getElementById(sourceprefix+"state") != undefined && document.getElementById(destprefix+"state") != undefined) 
		{
		if (document.getElementById(destprefix+"state").type != document.getElementById(sourceprefix+"state").type && document.getElementById(sourceprefix+"state").type == "text") 
			{
			document.getElementById("cell_" + destprefix + "state").innerHTML = '<input type="text" id="__ID__" name="__ID__" size="15" value="" />'.replace(/__ID__/, destprefix + "state");
			}
		else if (document.getElementById(destprefix+"state").type != document.getElementById(sourceprefix+"state").type && document.getElementById(sourceprefix+"state").type.substr(0, 6)  == "select") 
			{
			document.getElementById("cell_" + destprefix + "state").innerHTML = '<select id="__ID__" name="__ID__"></select>'.replace(/__ID__/, destprefix + "state");
			}
		}

	var i = 0;
	var provinces = address_data["provinces"];
	var srcState = document.getElementById(sourceprefix + "state");
	var dstState = document.getElementById(destprefix + "state");
	if (dstState && dstState.type.substr(0, 6) == "select") {
		dstState.length = 0;
		var country_selection = document.getElementById(sourceprefix + "country");
		for (var k in provinces[country_selection.value]) {
			// Copied this loop from address.js
			if (typeof provinces[country_selection.value][k] != "function") {
				dstState.options[i] = new Option(provinces[country_selection.value][k], k);
				i++;
			}
		}
	}

	var fields = ["name", "addr1", "addr2", "city", "state", "postalcode", "country"];
	for(var i in fields)
		{
		var srcEl = document.getElementById(sourceprefix+fields[i]);
		var dstEl = document.getElementById(destprefix+fields[i]);
		if(fields[i] == "postalcode" && (srcEl != undefined) && (dstEl != undefined))
			{
			dstEl.value = srcEl.value;
			dstEl.style.display = srcEl.style.display;
			}
		}
}

//
// Alias for special use of copyaddress
//
function revertaddress(destprefix) {
	if(destprefix == undefined) destprefix = 'bill_';
	var sourceprefix = 'original_'+destprefix;

	copyaddress(destprefix, sourceprefix);
}

//
// Fancy delete-all-fields-at-once address script.
//
function emptyaddress(destprefix) {
	if(destprefix == undefined) destprefix = "bill_";

	var fields = ["name", "addr1", "addr2", "city", "state", "postalcode", "country"];
	for(var i in fields)
		{
		if(document.getElementById(destprefix+fields[i]) != undefined) { document.getElementById(destprefix+fields[i]).value = ""; }
		}
	}
