// jQuery stuff
$(document).ready(function() {
	// Modal overlay window
	jQuery('a[rel*=facebox]').facebox()
	
	
	$("#navigation  li").hover(
		function(){
			if($(this).attr('id') == 'nav_about') {
				$('ul', this).fadeIn('fast').css('right', $(this).width() - 10);
			} else {
				$(this).find('ul:eq(0)').fadeIn();
				//$('ul', this).fadeIn('fast');
			}
		},
		function(){
			$('ul', this).fadeOut('fast');
		}
	);
	
/*
	if(document.all) {
		$("#navigation li").hoverClass ("sfHover");
	}
*/
	
	// Tooltips
	$('a.tips').cluetip({
		positionBy: 'fixed',
		dropShadow: false,
		showTitle: false,
		local:true,
		width: '270px',
		leftOffset: -137,
		fx: {			 
			open: 'show',
			openSpeed: ''
		}
	});
	
	// Show / hide
	$('a.show-hide').click(function(){
		$(this).toggleClass('open').next().slideToggle('slow');
		return false;
	});
	
	// Folded Stuff
	$(function() {
		var foldedContent = $('ul.foldedContainer li > div');
		$(foldedContent).hide();
		$('ul.foldedContainer li > a').click(function () {
		$(foldedContent).hide().filter(this.hash).slideToggle();
		$('ul.foldedContainer li > a').removeClass('open');
		$(this).addClass('open').blur();
		return false;
		});		
	});
	
		
	// Skin preview
	$('#skin-preview-select a').click(function(){
		$('#skin-preview img').replaceWith('<img src="' + $(this).attr('title') + '">');
		return false;
	});
	
	// Tab groups
	$(function() {
		var tabContainers = $('div.tabs > div');
		$('div.tabs ul.tabNavigation a').click(function () {
			tabContainers.hide().filter(this.hash).fadeIn();
			$('div.tabs ul.tabNavigation > li').removeClass('selected');
			$(this).parent().addClass('selected').blur();
			return false;
		}).filter(':first').click();
	});
	
	// link inside tab container is clicked, wanting to switch container being displayed
	$('#tab_getting_started p a').click(function(){
		var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(this.hash).fadeIn();
		$('div.tabs ul.tabNavigation > li').removeClass('selected');
		$('div.tabs ul.tabNavigation > li').not(':first').addClass('selected');
//		$(this).parent().addClass('selected').blur();
		return false;
	});


	
	
});

function ajaxFileUpload() {
	$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
	});
	$.ajaxFileUpload ({
		url: 'js/ajaxfileupload/doajaxfileupload.php',
		secureuri: false,
		fileElementId: 'fileToUpload',
		dataType: 'json',
		success: function(data, status){
			if(typeof(data.error) != 'undefined') {
				if(data.error != '') {
					alert(data.error);
				} else {
					//alert(data.msg);
					$('#user_picture').attr('src', data.url);
				}
			}
		},
		error: function(data, status, e){
			alert(e);
		}
	})
	return false;
}






// ChoiceBot create/edit
make_vocab = function() {
	type = document.getElementById('productType').value;
	singular = document.getElementById('createSingular');
	plural = document.getElementById('createPlural');
	var pluralized = '';
	
	var replace = new Array(
			new Array(/us$/, 'i'),
			new Array(/ey$/, 'ies'),
			new Array(/oy$/, 'oys'),
			new Array(/y$/, 'ies'),
			new Array(/o$/, 'oes'),
			new Array(/ss$/, 'sses'),
			new Array(/sh$/, 'shes'),
			new Array(/ch$/, 'ches'),
			new Array(/lf$/, 'lves'),
			new Array(/x$/, 'xes')
		);
	if(type.length > 0) {
		for(var i = 0; i < replace.length; i++) {
			if(type.match(replace[i][0])) {
				pluralized = type.replace(replace[i][0], replace[i][1]);
				break;
			}
		}
		if(pluralized == '') {
			pluralized = type+'s';
		}
		
		var ipod = /^[a-zA-Z]{1}[A-Z]{1}/;
		if(type.match(ipod)) {
			plural.value = pluralized;
			singular.value = type;
		} else {
			plural.value = pluralized.toLowerCase();
			singular.value = type.toLowerCase();
		}
	} else {
		plural.value = singular.value = '';
	}
}

validateCreate = function() {
	var private = document.getElementById('privacy').checked;
	var sharing = document.getElementById('pershareShare').checked;
	if(private == true && sharing == true) {
		alert('You cannot make a -shared- ChoiceBot private.\nPlease check your selections.');
	} else {
		document.form_create_cbot.submit();
	}
}

// Hover fix for IE6
$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};	
/* 
Code Highlighting 
Courtesy of Dean Edwards star-light 
http://dean.edwards.name/my/behaviors/#star-light.htc
	- with jQuery methods added, of course
*/
