function setupForms() {
	// Fake :focus state for IE
	$('input[type=text]', 'select', 'textarea').each(function(input) {
		input.focus(function() {
			this.addClass('focus');
		});
		input.blur(function() {
			this.removeClass('focus');
		});
	});
}

// MOVED TO global-shared.js
// Set up search input
//function setupSearchBox() {
//	var searchBox = $('#hd-search')
//	if (searchBox.length > 0) {
//		var input = searchBox.children('.input-text');
//		var defaultValue = 'Find a Pipe...';
//	
//		if(input.val() != defaultValue) searchBox.addClass('focused');
//	
//		input.focus(function() {
//			searchBox.addClass('focused');
//			if(input.val() == defaultValue) 
//				input.val('');
//		});
//		
//		input.blur(function() {
//			if(input.val() == defaultValue || input.val() == '') 
//				searchBox.removeClass('focused');
//			if(input.val() == '') 
//				input.val(defaultValue);
//		});
//	}
//};
//

// set base options for modal windows
var sbModalOptions = {
	animate:		false,
	displayNav:		false,
   	modal: 			true,
	overlayOpacity:	0.2,
	resizeDuration:	0.1
};

// open modal window -- inline version for speed (save for later)
/*
function openModal(url,width,height) {
	var modalContents = $(url).html();
    Shadowbox.open({
        content:    modalContents,
        height:     height,
        player:     "html",
        width:      width,
        options: 	sbModalOptions
    });
}
*/

// MOVED TO global-shared.js
// open modal window
/*
function openModalFrame(url,width,height) {
    Shadowbox.open({
        content:    url,
        height:     height,
        player:     "iframe",
        width:      width,
        options: 	sbModalOptions
    });
}
*/
// set up modal window
/*
function setUpModalFrame() {
	var modalForm = $('.modal form');
	if(modalForm.length){
		modalForm.attr('target','_parent');
	}
	var cancelButton = $('.modal #cancel');
	if(cancelButton.length){
		cancelButton.click(function(){
			window.parent.Shadowbox.close();
	    });
	}
} 
*/
// set up navigation
//function setupNav() {
	/*
	var createPipe = $('a.create');
	if(createPipe.length){
		createPipe.click(function(){
			openModalFrame($(this).attr('href'),600,410);
	     	return false;
	    });
	}
	
	var copyPipe = $('#copy-pipe');
	if(copyPipe.length){
		copyPipe.click(function(){
			if(!($(this).parent().hasClass('off'))) {
				openModalFrame($(this).attr('href'),600,430);	
				return false;
			}
	    });
	}
	*/
/*	
	var deletePipe = $('#delete-pipe');
	if(deletePipe.length){
		deletePipe.click(function(){
			openModalFrame($(this).attr('href'),600,220);
	     	return false;
	    });
	}
*/	
//	var updatePipe = $('#pipe-header h1 .edit');
//	if(updatePipe.length){
//		updatePipe.click(function(){
//			openModalFrame($(this).attr('href'),600,400);
//	     	return false;
//	    });
//	}
//	
//	var updateDesc = $('#pipe-desc .edit');
//	if(updateDesc.length){
//		updateDesc.click(function(){
//			openModalFrame($(this).attr('href'),600,400);
//	     	return false;
//	    });
//	}
//}
// END moved to global-shared.js


// set up pipe settings navigation
function setupSettingsNav() {
	if($('body.settings').length > 1){
		var inputToggler = $('.inputs-toggler > input[type=checkbox]');
		inputToggler
			.each(function(){
				$(this).parent().next('div').hide();
			})
			.click(function(){
				$(this).parent().next('div').toggle();
			});
		$(inputToggler+'[checked]').each(function(){
			$(this).parent().next('div').show();
		});
	}
}

function focusFirst() {
	if($('.focus-first').length > 0){
		$('#main .form-inputs:first input').focus();
	}
}

// set up ratings widget
function setupRatings() {
	var rating = $('#rating');
	if(rating.length > 0) {
		var rater = $('.rater a');
		var currentRating = $('#rating-yours .rating-value').attr('class');
		var ratingValue = $('#rating-yours .rating-value');
		rater.hover(function(){
			ratingValue.attr('class','rating-value ' + $(this).attr('class'));
		});
		rater.mouseout(function(){
			ratingValue.attr('class',currentRating);
		});
	}
}

// set up results list settings togglers 
function setupResultsListTogglers() {
	var rsTogglers = $('.rs-toggler');
	if (rsTogglers.length > 0) {
		rsTogglers.click (function(){
			var rsTarget = $(this).attr('href');
			$(rsTarget).toggle();
			return false;
		});
	}
}

// set up results togglers
function setupResultsTogglers() {
	var togglerButtons = $('.ft > .toggler-wrap > .togglers li');
	var togglerTargets = $('.ft > .panel');
	var embedSwitchers = $('.ft > .panel .togglers li');
	var textarea = $('.togglees textarea');
	
	textarea.click(function(){
		this.select();
	});
	
	var currentTabs = $('.togglees li');
	//alert(currentTabs.children('textarea').length);
	
	togglerButtons.each(function(){
		$(this).click(function(){	
			togglerButtons.removeClass('current');
			var targetClass = '.' + $(this).attr('title');
			$(this).parent().parent().siblings('.panel').hide();			
			$(this).parent().parent().siblings(targetClass).show();
			$(this).addClass('current');
			textarea.select();
		});
		
	});
	
	embedSwitchers.each(function(){	
		var parentResultId = $(this).parents('.result').attr('id');
		var embedTargets = $('#' + parentResultId + ' .ft > div.panel div.options ul.togglees li');
		$(this).click(function(){	
			var evalClasses = $(this).attr('class').split(' ');		
			$(embedTargets).hide();							
			switch(evalClasses[0]){
				case 'normalink' :					
					$('#' + parentResultId + ' ul.togglees li.normalink').show();
					$('#' + parentResultId + ' ul.togglees li.normalink textarea').focus();
					$('div.panel ul.togglers li').removeClass('current');
					$('div.panel ul.togglers li.normalink').addClass('current');
					return false;
					break;
				case 'permalink' :				
					$('#' + parentResultId + ' .togglees .permalink').show();
					$('#' + parentResultId + ' .togglees .permalink textarea').focus();
					$('div.panel ul.togglers li').removeClass('current');
					$('div.panel ul.togglers li.permalink').addClass('current');
					return false;
					break;
				case 'workflowlink' :
					$('#' + parentResultId + ' .togglees .workflowlink').show();
					$('#' + parentResultId + ' .togglees .workflowlink textarea').focus();
					$('div.panel ul.togglers li').removeClass('current');
					$('div.panel ul.togglers li.workflowlink').addClass('current');
					return false;
					break;
				default:
					return false;
					break;
			}
		});
	});
}	

// set up collapse buttons
function setupCollapseButtons() {
	$('#main .hd .toggler').click(function(){
		var toggler = $(this);
		if(toggler.hasClass('open')) {
			toggler.html('Hide');
			toggler.addClass('open');		
		} else {
			toggler.html('Show');
			toggler.removeClass('open');		
		}
		toggler.parent().siblings().animate({height: 'toggle'}, 100);
	});
}

// set up close button
function setupCloseButton() {
	var togglerButtons = $('.ft > .toggler-wrap .togglers li');
	$('.close a').click(function(){
		var parentPanel = $(this).parents('.panel');
		parentPanel.hide();
		togglerButtons.removeClass('current');
		return false;
	});
}

// set up view togglers 
function setupViewTogglers() {
	// loop through original images
	var resultImages = $('.result-image img');
	
	// create an array with the original image widths
	var imageWidth = new Array();
	
	if (resultImages.length > 0) {
		resultImages.each(function(ri){
			var parentResultId = $(this).parents('.result').attr('id');
			var targetImg = $(this);
			var targetImgId = targetImg.attr('id');			
			var viewTogglers = $('#' + parentResultId + ' .views li a.tbutton');
			
			// get original image size and save it -- not sure why this isn't working
			image(this,{
				success : function() {	
					imageWidth[resultImages.index(this)] = this.width;
				}
			});				

			// set up click function on buttons while we know the original width
			viewTogglers.each(function(){
				$(this).click(function(){
					var resultIndex = $(this).attr('rel');
					viewTogglers.removeClass('current');
					$(this).addClass('current');
					switch($(this).parent().attr('class')){
						case 'view-fit' :
							$('#result-image' + resultIndex).animate({width: '100%'}, 100);
							return false;
							break;
						case 'view-full' :				
							$('#result-image' + resultIndex).attr('style',''); //hold for when we get loadimage working >> .animate({width: imageWidth[ri] + 'px'}, 100)
							return false;
							break;
						default:
					}
				});
			});
		});
	}	
}

function setupResultImages() {
	var resultImages = $('.result-image img');
	if (resultImages.length > 0) {
		resultImages.each(function(){
			var targetImg = $(this);
			var targetImgId = targetImg.attr('id');
			image(targetImgId,{
				success : function () {	
					$(this).parent().attr('style','background-image:none');		
					//targetImg.attr('style','width:100%;max-width:' + this.width + 'px;max-height:' + this.height + 'px');
				}
			});		
		});
	}
}

// set up togglers for results settings output
function setupSettingsTogglers() {
	var togglers = $('.toggler');
	togglers.click(function(){
		var currentTarget = $(this).attr('href');
		$(currentTarget).animate({height:'toggle'},100);
		$(this).toggleClass('open');
		return false;
	});
}

// set up togglers for main pipes settings screen for allowing user inputs
function setupPipeSettings() {
	var togglers = $('.pipe-settings-grid .inputs-toggler > input');	
	togglers.each(function(){
		var target = $(this).parent().prev().children('.user-inputs');
		var reqTarget = $(this).nextAll('.required-toggler');
		if($(this).attr('checked')){
			$(this).parents('.setting-entry').addClass('user-enabled');	
			reqTarget.show();			
		}
	}).click(function(){
		var target = $(this).parent().prev().children('.user-inputs');
		var reqTarget = $(this).nextAll('.required-toggler');
		if($(this).attr('checked')){
			reqTarget.animate({height:'show'},100);	
			target.animate({height:'show'}, 100).prev().find('.form-label').animate({height:'show'}, 100);	
		    target.parent().parent().addClass('user-enabled');
		} else {
			target.prev().find('.form-label').animate({height:'hide'}, 100);	
			target.animate({height:'hide'}, 100);
			target.parent().parent().removeClass('user-enabled');
			reqTarget.animate({height:'hide'}, 100).children().attr('checked','');			
		}
		/*
		var targetLabel = $(this).parent().toggleClass('');
		
		$(targetLabel + ' .form-label').animate({height:'toggle',opacity:'toggle'},100).attr('style','');
		var target = $(this).parent().next('.user-inputs');
		target.animate({height:'toggle',opacity:'toggle'},100);
		$(this).parent().parent().toggleClass('selected');
		*/
	});
}

function setupMsgClosers() {
	var msgClosers = $('#msg-container a');
	if (msgClosers.length > 0) {
		msgClosers.click(function(){
			$('#msg-container').animate({opacity:'hide'},100);
			return false;
		});
	}
}

function setupExpertTogglers() {
	var expertParams = $('#settings-form .param-expert');
	var togglers = $('#settings-component .expert-toggler');
	if(expertParams.length > 0){
		togglers.click( function(){
			$('.param-expert').animate({opacity:'toggle'},500);	
			if($(this).hasClass('closed')) {
				$(this).html('Hide Advanced Settings');
			} else {
				$(this).html('Show Advanced Settings');
			}
			$(this).toggleClass('closed');
			return false;
		}); 		
	} else {
		togglers.hide();
	}
}

var IEconfirmMessage = "It looks like you\'re using Internet Explorer.\r\n \r\nRight now, the pipe editor does not work in Internet Explorer.\r\n \r\nYou will need to use Firefox 3.x, Safari 4.x, or Google Chrome.\r\n \r\nAre you sure you want to continue?";
function blockEditor() {
	
	var editButton = $('#pipe-nav .tool-edit a');
	var createButtonNav = $('#sn-create a');
	var createButtonSidebar = $('#pipe-nav .tool-create a');
	var importButtonSidebar = $('#pipe-nav .tool-import a');

	function stopIE() {
		return confirm(IEconfirmMessage);
	}
	
	if (editButton.length > 0) editButton.click(stopIE);
	if (createButtonNav.length > 0) createButtonNav.click(stopIE);
	if (createButtonSidebar.length > 0) createButtonSidebar.click(stopIE);
	if (importButtonSidebar.length > 0) importButtonSidebar.click(stopIE);
}	

$(document).ready(function(){
	setupSearchBox();
	setupNav();
	setupSettingsNav();
	setUpModalFrame();
	setupRatings();
	setupResultsTogglers();
	setupCloseButton();
	setupCollapseButtons();
	setupViewTogglers();
	setupResultImages();
	setupSettingsTogglers();
	setupPipeSettings();
	setupExpertTogglers();
	setupMsgClosers();
	focusFirst();
	setupResultsListTogglers();
	// if($.browser.msie) blockEditor();
		
	// cycle images on home page
	var featureCycle = $('#samples-content');
	if (featureCycle.length > 0) {
		$('#samples-content').cycle({ 
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0, 
		    next:   '#next'
		});
    }
	
});
