//add a horizontal line after every form field, but sometimes there are rows that don't have fields or some rows have multiple fields.
//so, grab all fiels, and then add a css class to their parent
function setupFormBorders() {
	$('form > table > tr, form > table > tbody > tr').each(function() {
		var row = $(this);
		if(row.find('input, select, textarea').length > 0) {
			row.addClass('bottomBorderRow');
		}
	});
}

//tabs are crazy now
//to create tabs, create a ul, the li with .activeTab will be the active tab, unless the url matches one of the tabs
//a tab url can be a full url, or it can be an anchor, like #tabName.  in that case, selecting it will hide all tabs and show the element with id #tabNameColumn
//also, any links in the document that go to that anchor will set this tab as the active one
function setupTabs() {
	function setActiveTab(tab) {
		var ul = tab.parents('ul');
		var newTab = tab.children().attr('href')+'Column';
		if(newTab.indexOf('/') < 0) {
			var $newTab = $(newTab);
			
			if($newTab && $newTab.length > 0) {
				ul.find('.activeTab').removeClass('activeTab');
				tab.addClass('activeTab');
				
				$('div.tabContainer, div.paddedTabContainer').hide();
				$newTab.show();
				
				var callback = tab.children().attr('callback');
				if(callback && callback.length > 0) {
					eval(callback);
				}
			}
		}
	}
	
	$('ul.tabs li').each(function() {
		var li = $(this);
		li.click(function() {
			setActiveTab(li);
		});
		setTimeout(function() {
			li.find('a').each(function() {
				// get all links in the document that have this same class, but are not this link (identified by the dummy class)
				var link = $(this);
				var href = link.attr('href');
				if(href.indexOf('/') >= 0) {
					return;
				}
				var clas = 'tabLinkDummyClass';
				link.addClass(clas);
				$('a').each(function() {
					var docLink = $(this);
					if(!docLink.hasClass(clas) && docLink.attr('href').indexOf(href) >= 0) {
						docLink.click(function() {
							setActiveTab(li);
						});
					}
				});
				
				if(!li.hasClass('activeTab')) {
					$(link.attr('href')+'Column').hide();
				}
			});
		}, 200);
	});
	
	if(!($.browser.msie && $.browser.version < 7)) {
		var bits = (window.location+'').split('#');
		if(bits.length > 1) {
			var href = '#'+bits[bits.length-1];
			var aTag = $('a[href="'+href+'"]');
			setTimeout(function() {
				setActiveTab(aTag.parent());
			}, 200);
		}
	}
		
}
function setupValidation() {
	$('form[validate=true]').liveValidate();
}
function setupJqtransform() {
	$('form.styleize').jqTransform({imgPath: '/images/jqtransform'});
}
function setupAutocomplete() {
	$('input.autocompleteTags').autocomplete(tag_array);
	$('input.autocompleteCompanies').autocomplete(company_array);
}
function setupInFieldInstructions() {
	$('input[instructions], textarea[instructions], select[instructions]').each(function() {
		var $this = $(this);
		if($this.data('instructions-done'))
			return;
		if($this.hasClass('infieldInstructions')) {
			var originalValue = $this.val(), instructions = $this.attr('instructions');
			if(originalValue.length == 0)
				$this.val(instructions);
			$this.focus(function() {
				if($this.val() == instructions)
					$this.val('');
			}).blur(function() {
				if($this.val() == '')
					$this.val(instructions);
			});
		} else {
			var instructions = $this.attr('instructions');
			$this.after('<div class="fieldInstructions">'+instructions+'</div>');
			var instructionsDiv = $($this.find('~ div.fieldInstructions').get(0));
			$this.focus(function() {
				instructionsDiv.addClass('focused');
			}).blur(function() {
				instructionsDiv.removeClass('focused');
			});
		}
		$this.data('instructions-done', true);
	});
}
//select tags are different cuz jqutransform replaces them with a ul, so u no longer get the focus event on the select
function setupSelectInFieldInstructions() {
	$('div.selectFieldInstructions').each(function() {
		var $this = $(this);
		if($this.data('instructions-done'))
			return;
		$this.find('~ .jqTransformSelectWrapper').each(function() {
			var select = $(this);
			select.focus(function() {
				$this.addClass('focused');
			}).blur(function() {
				$this.removeClass('focused');
			});
		});
		$this.data('instructions-done', true);
	});
}
function addBubble(target, bubble) {
	bubble.data('showing', false);
	bubble.data('bubbleTimeout', null);
	
	var hideBubbleFunction = function(e) {
		bubble.data('bubbleTimeout', setTimeout(function() {
			//$.browser.msie ? bubble.hide() : bubble.fadeOut("fast");
			if($.browser.msie) {
				bubble.animate({top: parseInt(bubble.css('top'))-10}, 200, function() {bubble.hide();});
			} else {
				bubble.animate({opacity: 0, top: parseInt(bubble.css('top'))-10}, 200);
			}
			bubble.data('showing', false);
		}, 250));
	};
		
	target.mouseenter(function(e) {
		// mouseenter may be fired multiple times, but we only want it to do something if the bubble is already hidden
		if(!bubble.data('showing')) {
			bubble.data('showing', true);
			
			// first calculate the difference between the target element and the mouse, then add the distance from the target element to the next relative element, then center the bubble at that point
			//var x = (e.pageX - target.offset().left) + target.position().left - (bubble.width() / 2);
			//var y = (e.pageY - target.offset().top) + target.position().top - (bubble.height());
			
			// just center the thing right above the target element
			var x = target.position().left + (target.width() / 2) - (bubble.width() / 2);
			var y = target.position().top - bubble.height() - parseInt(bubble.css('padding-top')) - parseInt(bubble.css('padding-bottom'));
			
			//$.browser.msie ? bubble.show() : bubble.fadeIn("fast");
			bubble.css('top', y-10).css('left', x).show();
			if($.browser.msie) {
				bubble.animate({top: y}, 150);
			} else {
				bubble.css({opacity: 0});
				bubble.animate({opacity: 1, top: y}, 150);
			}
		}
		if(bubble.data('bubbleTimeout') != null) {
			clearTimeout(bubble.data('bubbleTimeout'));
			bubble.data('bubbleTimeout', null);
		}
	}).mouseleave(hideBubbleFunction);
	
	
	bubble.mouseenter(function(e) {
		if(bubble.data('bubbleTimeout') != null) {
			clearTimeout(bubble.data('bubbleTimeout'));
			bubble.data('bubbleTimeout', null);
		}
	}).mouseleave(hideBubbleFunction);
}
function configureImageflow() {
    /* Hide loading bar, show content and initialize mouse event listening after loading */
    try {
        if(document.getElementById(conf_imageflow)) {
            hide(conf_loading);
            refresh(true);
            show(conf_images);
            show(conf_scrollbar);
            initMouseWheel();
            initMouseDrag();
            moveTo(5000);
        }
    } catch (e) { }
}

function configureHighslide() {
	hs.numberOfImagesToPreload = 20;
	hs.outlineType = 'rounded-white';
	//hs.captionEval = 'this.thumb.alt';
	hs.captionSlideSpeed = null;
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.fadeInOut = true;
	hs.registerOverlay({
		html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>', position: 'top right', fade: 2
	});
	hs.registerOverlay( {
		thumbnailId: null, overlayId: 'highslideControls', position: 'bottom center', fade: 2, hideOnMouseOut: true
	});
	/*hs.addSlideshow( {
		thumbstrip: {
			position: 'above',
			useControls: true,
			fixedControls: true,
			mode: 'horizontal',
			relativeTo: 'expander',
			repeat: true
		}
	});*/
}
function HideKampyleButton() { document.getElementById("kampylink").style.display="none";document.getElementById("close_button").style.display="none";}	if((screen.width<=800) && (screen.height<=600)) { document.getElementById("close_button").style.display="block";}