$(function() {
	// slide form fields
	$("#slides textarea, #slides input[type=text]").wrap('<div class="field_bg"></div>');
	
	// login hover for IE
	if ($.browser.msie) {
		//loginTimeout = null;
		$("#nav #nav_actions li:has(#login)").hover(
			function(){
				//clearTimeout(loginTimeout);
				$("#login").addClass("stick");
			},
			function(){
				//loginTimeout = setTimeout(function(){$("#login").removeClass("stick")}, 3000);
				$("#login").removeClass("stick");
			}
		);
	}
	
	// login form handler
	$("form", "#login").submit(function(){
		$.post(this.action, $(this).serialize(),
			function(response){
				if (response.success == true && response.redirect) {
					$(".feedback", "#login").html('');
					window.location = response.redirect;
				} else {
					$(".feedback", "#login").html(response.feedback);
				}
			}, "json"
		);
		return false;
	});
	
	// footer login reveals login box
	$("#footer_login").click(function(){
		$("#login").addClass("stick");
		$("#login").one("mouseleave", function(){
			$(this).removeClass("stick");
		});
	});
	
	// home page slideshow
	$('#featured').cycle({ 
		timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         1000,  // speed of the transition (any valid fx speed value) 
		next:          null,  // id of element to use as click trigger for next slide 
		prev:          null,  // id of element to use as click trigger for previous slide 
		before:        null,  // transition callback (scope set to element to be shown) 
		after:         null,  // transition callback (scope set to element that was shown) 
		height:       'auto', // container height 
		sync:          1,     // true if in/out transitions should occur simultaneously 
		fit:           0,     // force slides to fit container 
		pause:         0,     // true to enable "pause on hover" 
		delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
		slideExpr:     null  // expression for selecting slides (if something other than all children is required) 
	});
	
	// "more options" click
	//$("#more_options").attr("href", "javascript:void(0);");
	$("#more_options, #locations_crumb").click(function(){
		showSlide("#slide_categories");
	});
	
	// "email this page" click
	//$("#email_page").attr("href", "javascript:void(0);");
	$("#email_page").click(function(){
		showSlide("#slide_email");
	});
	
	// "email this page" field actions
	$("#slide_email textarea, #slide_email input[type='text']").each(function(){
		var field = $(this);
		field.data("default", this.value);
		field.focus(function(){
			if (this.value == field.data("default") || field.hasClass("error")) {
				this.value = "";
				field.removeClass("error");
			}
		});
		$(this).blur(function(){
			if (this.value == "") {
				this.value = field.data("default");
			}
		});
	});
	
	// "email this page" form handler
	$("#slide_email form").submit(function(){
		var form = $(this);
		$.post(form[0].action, form.serialize(),
			function(response){
				if (response.success == true) {
					$(".feedback", "#slide_email").remove();
					$("#slide_email fieldset:last div:last").append('<span class="feedback">' + response.feedback + '</span>');
					setTimeout(function(){resetSlides()}, 3000); 
				} else {
					$("textarea, input[type='text']", "#slide_email").each(function(){
						if (response.feedback[this.name]) {
							this.value = response.feedback[this.name];
							$(this).addClass("error");
						}
					});
				}
			}, "json"
		);
		return false;
	});
	
	// "contact" click
	$("#contact_site").attr("href", "javascript:void(0);");
	$("#contact_site, #footer_contact_site").click(function(){
		showSlide("#slide_contact");
	});
	
	// "contact" field actions
	$("#slide_contact textarea, #slide_contact input[type='text']").each(function(){
		var field = $(this);
		field.data("default", this.value);
		field.focus(function(){
			if (this.value == field.data("default") || field.hasClass("error")) {
				this.value = "";
				field.removeClass("error");
			}
		});
		$(this).blur(function(){
			if (this.value == "") {
				this.value = field.data("default");
			}
		});
	});
	
	// "contact" form handler
	$("#slide_contact form").submit(function(){
		var form = $(this);
		$.post(form[0].action, form.serialize(),
			function(response){
				if (response.success == true) {
					$(".feedback", "#slide_contact").remove();
					$("#slide_contact fieldset:last div:last").append('<div class="feedback">' + response.feedback + '</div>');
					setTimeout(function(){resetSlides()}, 3000); 
				} else {
					$("textarea, input[type='text']", "#slide_contact").each(function(){
						if (response.feedback[this.name]) {
							this.value = response.feedback[this.name];
							$(this).addClass("error");
						}
					});
				}
			}, "json"
		);
		return false;
	});
	
	// new window links
	$(".new_win").click(function() {
		window.open(this.href, 'newWin');
		return false;
	});
	
	// pricing boxes (on apply page)
	$("input[name='skill_level']").click(function(){
		if (this.value == "Professional") {
			//console.log(this.value);
			$("#pricing_freshtalent").hide();
			$("#pricing_professional").fadeIn();
		} else {
			//console.log(this.value);
			$("#pricing_professional").hide();
			$("#pricing_freshtalent").fadeIn();
		}
	});
});

function resetSlides()
{
	$("#slides > div").each(function(){
		if ($(this).css("display") == "block") {
			$(this).toggle("blind", {direction: "vertical"}, 500);
		}
	});
}

function showSlide(slideSelector)
{
	var timeout = false;
	$("#slides > div:not(" + slideSelector + ")").each(function(){
		if ($(this).css("display") == "block") {
			$(this).toggle("blind", {direction: "vertical"}, 500);
			timeout = true;
		}
	});
	if (timeout) {
		setTimeout('slideFunc("' + slideSelector + '")', 500);
	} else {
		slideFunc(slideSelector);
	}
}

function slideFunc(slideSelector) {
	$(slideSelector).toggle("blind", {direction: "vertical"}, 500);
}

jQuery.fn.fadeToggle = function(speed, callback){
	return (this.is(":visible")) ? this.fadeOut(speed, callback) : this.fadeIn(speed, callback);
};

function generateFlashVideo(flvPath) {
	document.write( ' \
					<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" \
						codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" \
						width="640" height="402" id="flv_player" align="middle"> \
						<param name="allowScriptAccess" value="sameDomain" /> \
						<param name="movie" value="/flv_player.swf?flvPath=/' + flvPath + '" /> \
						<param name="quality" value="high" /> \
						<param name="bgcolor" value="#ffffff" /> \
						<embed src="/flv_player.swf?flvPath=/' + flvPath + '" quality="high" bgcolor="#ffffff" width="640" height="402" name="flv_player" \
							align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" \
							pluginspage="http://www.macromedia.com/go/getflashplayer" /> \
					</object> '
	);
}

