
// Set the position of the element //
ac.$('speedBarMain').style.top = (ac.getWindowHeight()-26)+'px';
ac.$('speedBarMain').style.display = '';

// Keep the speedbar in place if the window is resized //
ac.addEvent(window, 'resize', function(){ ac.$('speedBarMain').style.top = (ac.getWindowHeight()-26)+'px'; });


// Show Modal View of the cart contents //
function displayCart(parent){

	clearModalContents();

	ac.createModal({div:'speedbarModal'});
	
	displaySpeedBarModalLink(parent);

	// Load the cart //
	ac.ajax({url:'cart.php?ajax_speedbar=load_cart', method:'POST', returned:'text', return_func:displaySpeedbarContents});

	}

function displayContact(parent){
	
	clearModalContents();
	
	ac.createModal({div:'speedbarModal'});
	
	displaySpeedBarModalLink(parent);

	// Load the Contact page //
	ac.ajax({url:'ajax_speed_bar.php?ajax_speedbar=load_contact', method:'POST', returned:'text', return_func:displaySpeedbarContents});

	}

function displaySupport(parent){

	clearModalContents();

	ac.createModal({div:'speedbarModal'});

	displaySpeedBarModalLink(parent);

	// Load the Contact page //
	ac.ajax({url:'ajax_speed_bar.php?ajax_speedbar=load_support', method:'POST', returned:'text', return_func:displaySpeedbarContents});

	}


// Display the link and highlight the selection //
function displaySpeedBarModalLink(parent){

	var pos = ac.calculateOffsets('speedbarModal');
	var parPos = ac.calculateOffsets(parent);

	unhighlightSpeedbar();

	ac.$(parent).className = 'selected';

	if (ac.IE){
	
		// Fade In //
		ac.changeOpacity(0, 'speedbarModalLink');
		ac.$('speedbarModalLink').style.display = '';
		ac.fade('speedbarModalLink', 0, 100, 550);
	
		// Adjust the speedbar modal window link //
		ac.$('speedbarModalLink').style.top = (ac.$('speedbarModal').offsetHeight+pos.y-2)+'px';
		ac.$('speedbarModalLink').style.left = (parPos.x+1)+'px';
		ac.$('speedbarModalLink').style.width = (ac.$(parent).offsetWidth-2)+'px'; // -2 for the borders //
		ac.$('speedbarModalLink').style.height = (pos.y-20)+'px';

	} else {

		// Adjust the speedbar modal window link //
		ac.$('speedbarModalLink').style.display = '';
		ac.$('speedbarModalLink').style.top = (ac.$('speedbarModal').offsetHeight+pos.y-2)+'px'; // -2 for the border //
		ac.$('speedbarModalLink').style.left = parPos.x+'px';
		ac.$('speedbarModalLink').style.width = (ac.$(parent).offsetWidth-2)+'px'; // -2 for the borders //
		ac.$('speedbarModalLink').style.height = (pos.y-20)+'px';

		}
	}

// Hide the previously displayed data, and set the loading image //
function clearModalContents(){
	
	// Make sure the add to cart isn't displaying //
	if (ac.$('addToCartDisplay') && ac.$('addToCartDisplay').innerHTML != ''){
		ac.$('addToCartDisplay').style.display = 'none';
		ac.$('addToCartDisplay').innerHTML = '';
	}
	
	// Set the loading image //
	ac.$('speedbarModalContents').innerHTML = '<div class="speedBarLoading"><img src="'+ac.ImagesDir+'/loading_a.gif" /><p>Loading</p></div>';
	}
	
// Remove the highlight Class from the speedbar items //
function unhighlightSpeedbar(){

	// Fade Out //
	if (ac.IE)
		ac.fade('speedbarModalLink', 100, 0, 550);

 	// Make sure only the selected link is highlighted //
	var divs = ac.$('speedBarLinks').getElementsByTagName('div');
	for (i = 0; i < divs.length; i++){

		if (divs[i].className == 'selected')
			divs[i].className = 'selection';
		}
	}


// Function to copy the data returned from the AJAX function to the modal window //
function displaySpeedbarContents(html){
	ac.$('speedbarModalContents').innerHTML = html;
	}

// Error for cart update //
var not_a_valid_number = 'This is not a valid amount.';
var editInProgress = false;

// Edit the cart //
function updateOptions(id){

	// Loop through the elements //
	var poarr = document.forms['cartform'].elements;
	var params = 'ajax_speedbar=edit_options&cartid='+id+'&';
		
	for (x=0; x < poarr.length; x++){
	
		if (poarr[x].type == 'button') // Don't send button data //
			continue;

		// Check if this is an amount field //
		if (poarr[x].name.substr(0, 14) == 'productindexes'){
			
			if (!ac.isint(poarr[x].value)){
				alert(not_a_valid_number);
					return;
				
				}
			}
	
		params += poarr[x].name+'='+encodeURI(poarr[x].value)+'&';
		
		}

	// Preserve the height //
	ac.$('cartid_'+id).style.height = ac.$('cartid_'+id).offsetHeight+'px';

	// Hide the display //
	ac.$('tr_'+id).style.display = 'none';
	ac.$('wait_'+id).style.display = '';	

	params = params.substr(0, params.length-1);
	
	ac.ajax({url:'cart.php?ajax_speedbar=edit_options', method:'POST', parameters:params, returned:'xml', return_func:updateCartDisplay});
		
	}


function unmaskEditForm(id){

	ac.$(id).className = 'hover';
		
	// Position //
	var pos = ac.calculateOffsets(id);

	// Hide text and show the form option & text tags //
	var divs = ac.$(id).getElementsByTagName('div');
	for (i = 0; i < divs.length; i++){

		if (divs[i].id.search(/_edit/) > 0)
			divs[i].style.display = '';
				else
			divs[i].style.display = 'none';
		}
	}
	
	
function maskEditForm(id){

	if (editInProgress)
		return;

	ac.$(id).className = '';
	
	// Hide form options and display the text //
	var divs = ac.$(id).getElementsByTagName('div');
	for (i = 0; i < divs.length; i++){
	
		if (divs[i].id.search(/_edit/) > 0)
			divs[i].style.display = 'none';
				else
			divs[i].style.display = '';
		}

	}


function deleteCartItem(cartid){

	ac.$('amount_'+cartid).value = 0;
	updateOptions(cartid);

	}

// Update the cart display //
function updateCartDisplay(xml){

	// Check for errors //
	try{
		var error = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;

		if(error){
			alert(error);
		}

	}catch(e){}
	
	// Check for empty cart //
	try{
		var cart_empty = xml.getElementsByTagName('cart_empty')[0].childNodes[0].nodeValue;

		if(cart_empty){
			location.reload(true);
			return;		
		}

	}catch(e){}
	
	try{

	var cartid = xml.getElementsByTagName('cartid')[0].childNodes[0].nodeValue;
	var cartitem = xml.getElementsByTagName('cartitem')[0].childNodes[0].nodeValue;
	var minicart = xml.getElementsByTagName('minicart')[0].childNodes[0].nodeValue;
	var cart_totals = xml.getElementsByTagName('cart_totals')[0].childNodes[0].nodeValue;
	
	}catch(e){ // Cart is likely expired //
		location.reload(true);
			return;}
	
	if (cartid == 0) // Product deleted //
		return ;

	// Update Cart display //
	ac.$('cartItems').innerHTML = cartitem;
	
	// Update Minicart //
	ac.$('minicartText').innerHTML = minicart;
	
	// Update Cart totals //
	ac.$('cart_totals').innerHTML = cart_totals;
	
	}




// Display the right slideup for the links on the right of the speedbar //
function displaySlideR(parent, title){

	// Reset (not for a re-loded license agreement) //
	ac.$('speedBarSlideRCtrl').style.display = 'none';
	ac.$('speedBarSlideR').style.display = 'none';

	if (ac.IE){
		ac.$('speedBarSlideR').style.position = 'absolute';
		ac.$('speedBarSlideRCtrl').style.position = 'absolute';
		}

	// Copy the AJAX return to the display //	
	ac.displaySlideRContents = function(html){ ac.$('speedBarSlideR').innerHTML = html;	};

	// Load the cart //
	ac.ajax({url:'ajax_speed_bar.php?ajax_speedbar='+parent, method:'POST', returned:'text', return_func:ac.displaySlideRContents});
	
	if (parent.substr(0, 12) == 'load_license') // Sometimes this is a full query string //
		parent = 'load_license';

	// Set the loading image //
	ac.$('speedBarSlideR').innerHTML = '<div class="speedBarLoading"><img src="'+ac.ImagesDir+'/loading_b.gif" /><p>Loading</p></div>';

	ac.changeOpacity(0, 'speedBarSlideR');
	ac.$('speedBarSlideR').style.display = '';
	var parOffset = ac.calculateOffsets(parent);
	
	var posTop = (ac.getWindowHeight()-ac.$('speedBarSlideR').offsetHeight-ac.$('speedBarContainer').offsetHeight); 
	var posRight = ac.getWindowWidth()-parOffset.x-ac.$(parent).offsetWidth;
	
	// Add the scroll pos for IE Absolute positioning //
	if (ac.IE)
		posTop += document.documentElement.scrollTop;
	
	ac.$('speedBarSlideR').style.top = posTop+'px';
	ac.$('speedBarSlideR').style.right = posRight+'px';

	ac.fade('speedBarSlideR', 0, 90, 300);
	
	ac.$('speedBarSlideRTitle').innerHTML = title;

	setTimeout('showCTRL('+posTop+', '+posRight+')', 2000);	
		
	}


// Show the CTRL Bar //
function showCTRL(posTop, posRight){

	// Display the Control bar //
	ac.changeOpacity(0, 'speedBarSlideRCtrl');
	ac.$('speedBarSlideRCtrl').style.display = '';
	ac.$('speedBarSlideRCtrl').style.top = (posTop-21)+'px';
	ac.$('speedBarSlideRCtrl').style.right = posRight+'px';
	ac.fade('speedBarSlideRCtrl', 0, 80, 500);
	
	}
		
// Hide the right popup //
function closeSlideR(){

	// Make sure the cursor doesn't disappear //
	document.body.focus();

	ac.fade('speedBarSlideRCtrl', 80, 0, 200);
	ac.fade('speedBarSlideR', 80, 0, 200);
	
	setTimeout("ac.$('speedBarSlideRCtrl').style.display = 'none'", 200);
	setTimeout("ac.$('speedBarSlideR').style.display = 'none'", 200);
	
	}


	
	
	
	
	
	
// Tool Tips //

function toolTip(elem, message){

	var pos = ac.calculateOffsets(elem);

	// Create tooltip if needed //
	if (!ac.$('speedBarTip'))
		ac.createElem({node:'div', id:'speedBarTip'}, 'body');
		
	// Position tip //
	ac.$('speedBarTip').style.top = document.documentElement.scrollTop+(pos.y-30)+'px';
	ac.$('speedBarTip').style.right = (getWindowWidth()-pos.x-30)+'px';

	// Fill  messages & fade in display //
	ac.$('speedBarTip').innerHTML = message;
	ac.changeOpacity(0, 'speedBarTip');
	ac.$('speedBarTip').style.display = '';
	ac.fade('speedBarTip', 0, 80, 200);

	}


function clearToolTip(){

	if (ac.$('speedBarTip')){
		ac.$('speedBarTip').style.display = 'none';
		ac.$('speedBarTip').innerHTML = '';	
		}
	}

// Update the minicart //
function updateMinicart(){

	ac.updateMinicartText = function(html){ ac.$('minicartText').innerHTML = html; };
	ac.ajax({url:'ajax_speed_bar.php?ajax_speedbar=load_minicart', method:'POST', returned:'text', return_func:ac.updateMinicartText});

	}
	
	
function asbLogin(){

		ac.$('asbLoginWait').style.display = '';
		ac.$('asbLoginButton').style.display = 'none';

	// Post Variables // 
	var params = 'username=' + encodeURI(document.secureform.username.value) +
	'&password=' + encodeURI(document.secureform.password.value) +
	'&mode=login' +
	'&usertype=C&checkoutOne=Y';


	ac.asbLoginAJAX = function(xmlResponse){

		// Check for antibot error //
		try{
	
			// Update the fields from the XML document //
			var antibot = xmlResponse.getElementsByTagName('antibot')[0].childNodes[0].nodeValue;
			
			if (antibot){
				window.location = 'error_message.php?antibot_error';
				return;
			}
	
			} catch(e) {}
				
		// Check for redirect //
		try{
		
			var redirect = xmlResponse.getElementsByTagName('redirect')[0].childNodes[0].nodeValue;
			
			if (redirect){
				window.location = redirect;
				return;
			}
	
			} catch(e) {}	
	
		// Check for incorrect logins //
		try{
			var invalid = xmlResponse.getElementsByTagName('invalid')[0].childNodes[0].nodeValue;
			
			if (invalid){
			alert('Invalid username and/or password');
			ac.$('asbLoginWait').style.display = 'none';
			ac.$('asbLoginButton').style.display = '';
			return;
			}
					
			} catch(e) {}
		
		//*** Login Successfull ***//
		location.reload(true);		
		};

	ac.ajax({url:'include/login.php', method:'POST', returned:'xml', parameters:params, return_func:ac.asbLoginAJAX});

		}
