var defaultTxtColor = '#444';
var activeTxtColor = '#000';

function setImg(url,bigUrl){
	document.getElementById('mainProdPhoto').src = url;
	document.getElementById('mainProdPhotoLink').href = bigUrl;
}

function textFocusIn(textBox,defaultVal){
	if(textBox.value == defaultVal){
		textBox.value = '';
		textBox.style.color = activeTxtColor;
	}
}

function textFocusOut(textBox,defaultVal){
	if(textBox.value == ''){
		textBox.style.color = defaultTxtColor;
		textBox.value = defaultVal;
	}
}

// options
var distance = -10;
var time = 250;
var hideDelay = 500;

var hideDelayTimer = null;

// tracker
var beingShown = false;
var shown = false;

function addToCart(part,qtyHolder,btnID){
	qty = qtyHolder.value;
	$.post("/php/addtocart.php",
		   {Type: "addItem", PartNum: part, Qty: qty}, 
	       function(data){
		       traceData(data,btnID);
		   });
}

function traceData(data,btnID){
	itemInfo = eval('('+data+')');
	document.getElementById('cartCounter').innerHTML = "<strong><a href='/cart'>View Cart / Checkout (" + itemInfo.count + " items)</a></strong>";
	document.getElementById('cartPop').innerHTML = itemInfo.itemID + " Added to Cart";
	document.getElementById(btnID).value = "Added to Cart";
	fadeInOut();
}

function popout(){
	$('.bubbleInfo').each(function () {			    			
		// stops the hide event if we move from the trigger to the popup element
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		
		// don't trigger the animation again if we're being shown, or already visible
		if (beingShown || shown) {
			return;
		} 
		else {
			var trigger = $('.trigger', this);
			var popup = $('.popup', this).css('opacity', 0);
			
			beingShown = true;
		
			// reset position of popup box
		    popup.css({
		    	top: 10,
		       	left: 0,
		    	display: 'block' // brings the popup back in to view
		    })
		
		    // (we're using chaining on the popup) now animate it's opacity and position
		    .animate({
		    	top: '-=' + distance + 'px',
		        opacity: 1
		    }, 
		    time, 'swing', 
		    function() {
		    	// once the animation is complete, set the tracker variables
		        beingShown = false;
		        shown = true;
		    });
		}
	});
}

function pullIn(){
	$('.bubbleInfo').each(function(){
		var popup = $('.popup', this);	
    	popup.animate({
    		top: '+=' + distance + 'px',
    		opacity: 0      	
    	}, 
    	time, 'swing', function(){
    		shown = false;
    		popup.css('display', 'none');
    	});
	});
}

function fadeInOut(){
	if (beingShown || shown) {
		return;
	}
	else{ 
		popout();
		setTimeout('pullIn()',9000);
	}
}

function deleteAll(theForm){
	var delBoxes = new Array();
	
	ctrlBox = document.getElementById('checkAll');
		
	for(i=0; i < theForm.elements.length; i++){
		if(theForm.elements[i].className == "delMe"){
			delBoxes.push(theForm.elements[i]);
		}
	}
	
	for(i=0; i < delBoxes.length; i++){
		if(ctrlBox.checked){
			delBoxes[i].checked = true;
		}
		else{
			delBoxes[i].checked = false;
		}
	}
}	