<!--

$(document).ready(function() {
	
	// Used to delete and address via AJAX
	$('a.delete-address').click( function(e) {
		var id = $(this).attr('id');
		the_id = id.substr( id.lastIndexOf('-')+1, id.length-1 );
		$.post('/customer/delete_address', 
			{ id: the_id }, 
			function (data) {
				var success = processAjaxDelete(data, 'text');
				if( success ) {
					$('#' + id).parent().parent().fadeOut('fast', function() {
						$(this).remove();
					});
				}
			})
	});
	
	// Prep the header nav menu
	$('a#header-nav-regions, a#header-nav-stores, a#header-nav-search, a#header-nav-cart, a#header-nav-help').click( function(){
			switchTabOn($(this).attr('id'));
		}
	);
});

function switchTabOn( tab_id )
{
	var x = tab_id.lastIndexOf('-');
	var id = tab_id.substr(x+1, tab_id.length);
	var the_link = $('#' + tab_id);
	var the_menu = $('#header-tab-' + id);
	the_menu.siblings().removeClass().addClass('header-tab');
	the_menu.removeClass().addClass('header-tab-selected');
	the_link.parent().siblings('.selected').removeClass();
	the_link.parent().addClass('selected');
	var position = the_link.position();
	var bg_pos = position.left - 660;
	$('.header-tab-selected').css('background-position', '' + bg_pos + 'px bottom');
}

function processAjaxDelete( data, format )
{
	switch(format)
	{
		case 'text':
			if(data == "true") {
				return true;
			}
			break;
	}
}

/*
* Toggle all of the elements within a form on/off depending on the 
* checked value for the given checkbox_id element
*/
function formToggler(checkbox_id, form_id)
{
	var checked = $('#' + checkbox_id).attr('checked');
	if( checked == false )
	{
		disableFormElements(form_id);
	}
	else 
	{
		enableFormElements(form_id);
	}
	$('#' + checkbox_id).removeAttr('disabled');
}

function disableFormElements(form_id)
{
	$('#' + form_id + ' input, #' + form_id + ' textarea').attr('disabled', 'disabled');
}

function enableFormElements(form_id)
{
	$('#' + form_id + ' input, #' + form_id + ' textarea').removeAttr('disabled');
}

function toggleVisibility(checkbox_id, id)
{
	var checked = $('#' + checkbox_id).attr('checked');
	checked == true ? $('#' + id).show() : $('#' + id).hide();
}

// my happy happy zoom box
// uses 2 globals
// zoomed_index
// zoomed_list
//move the image div to center screen
function showDivDeadCentered(Xwidth,Yheight,divid) {
    // get scroll extents and calc where scroll is
    var scrolledX, scrolledY;
    if( self.pageYoffset ) { 
	    scrolledX = self.pageXoffset; 
	    scrolledY = self.pageYoffset; 
    } else if( document.documentElement && document.documentElement.scrollTop ) { 
	    scrolledX = document.documentElement.scrollLeft; 
	    scrolledY = document.documentElement.scrollTop; 
    } else if( document.body ) { 
	    scrolledX = document.body.scrollLeft; 
	    scrolledY = document.body.scrollTop; 
    } 
    // determine the coordinates of the center of browser's window 
    var centerX, centerY; 
    if( self.innerHeight ) { 
	    centerX = self.innerWidth; 
	    centerY = self.innerHeight; 
    } else if( document.documentElement && document.documentElement.clientHeight ) { 
	    centerX = document.documentElement.clientWidth; 
	    centerY = document.documentElement.clientHeight; 
    } else if( document.body ) { 
	    centerX = document.body.clientWidth; 
	    centerY = document.body.clientHeight; 
    }
    // calc the coordinate transform for the div
    var leftoffset = scrolledX + (centerX - Xwidth) / 2; 
    var topoffset = scrolledY + (centerY - Yheight) / 2;
    // position it and flip it on
    var obj=document.getElementById(divid);
    if (obj) {
	    var os=obj.style;
	    os.position='fixed';
	    os.top = topoffset + 'px';
	    os.left = leftoffset + 'px';
	    os.display = "block";
    }
}
// init the zoom flip vars then call the flip
function zoomEngine(preload_list)
{
    zoomed_index = 0;
    zoomed_list = document.getElementsByName(preload_list);
    if (zoomed_list.length == 1)
    	document.getElementById('zoom_nav_buttons').style.display = 'none';
    else
    	document.getElementById('zoom_nav_buttons').style.display = 'block';
    flipZoom(0);
}
// turn off display of zoom_box div
function closeZoom()
{
    var obj = document.getElementById('zoom_box');
    if (obj)
        obj.style.display = 'none';
}
//flip image and display
function flipZoom(offset)
{
	// extra roominess for buttons and whatnot
	var extra_pad = 80;
    // check offset for wrap around
    zoomed_index += offset;
    if (zoomed_index == zoomed_list.length)
        zoomed_index = 0;
    if (zoomed_index < 0)
        zoomed_index = zoomed_list.length - 1;
    // set the image source and get some initial measurements
    var img_obj = document.getElementById('zoom_image');
    if (img_obj) {
	    img_obj.src = zoomed_list[zoomed_index].src;
	    // set width for div
	    var zoom_box = document.getElementById('zoom_box');
	    if (zoom_box) {
		    zoom_box.style.width = Number(zoomed_list[zoomed_index].attributes.format_width.nodeValue) + extra_pad + 'px';
		    showDivDeadCentered(Number(zoomed_list[zoomed_index].attributes.format_width.nodeValue) + extra_pad, zoomed_list[zoomed_index].attributes.format_height.nodeValue, 'zoom_box');
	    }
    }
}

//gather product options to url friendly string
$(document).ready( function() {
	$('a#add_to_cart').click( function(e) {
		var opts_list = document.getElementsByName('product_option');
		var res = '';
		for (i=0; i < opts_list.length; i++)
			res += ((i > 0) ? '-' : '') + opts_list[i].value;
		this.href += (res) ? '/' + res : '';
	});
});

function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}

// calc the cart shipping and cart grand total
function calcCartShipping() {
	var shipper_selects = $("select[name^='shipper_select_']").get();
	if (shipper_selects.length > 0) {
		var ship_cost = 0;
		for (i = 0; i < shipper_selects.length; i++)
		{	// get the shipping cost from store selection
			ship_cost += Number(document.getElementById(shipper_selects[i].value + '_' + shipper_selects[i].id).value);
		}
		document.getElementById('ship_total_label').innerHTML = 'Shipping Total';
		document.getElementById('ship_total_field').innerHTML = ship_cost.toFixed(2);
		document.getElementById('grand_total_label').innerHTML = 'Grand Total';
		document.getElementById('grand_total_field').innerHTML = Number(ship_cost + Number(document.getElementById('cart_total').value)).toFixed(2);
	}
}

// play with cart shipping page
$(document).ready( function() {
	$("select[name^='shipper_select_']").change ( function() {
		calcCartShipping();
	});
	calcCartShipping();
});

-->