/*
*p|r jQuery Goodies
*Add all common document.ready calls here
*/

var sortClass = '';

$(document).ready(function(){
	
	$(".zoom-me").fancybox();
	
	// Homepage Carousel
	$("#project-slider-wrap").jCarouselLite({
		btnNext: "#next",
		btnPrev: "#back",
		circular: true,
		easing: "easeInOutQuad",
		speed: 900
	});

	// eNews Signup
	$('h2#enews a').click(function(){
		$.scrollTo('#footer', 800, {easing:'easeInOutQuad'});
		$('#name').focus();
		$('#foot-enews').attr("checked", "checked");
		return false;
	});

	// Site Wide Tooltips
	$('#related-work a, #peeps-list a, #blog-pagination a, #view-more a').tooltip({
		track: true, 
		delay: 0, 
		fade: 400,
		showURL: false,
		top: 25,
   		left: 0
	});
	
	// TABS
	$("#blog-tabs").idTabs(); 
	
	// IE fixes
	if (jQuery.browser.msie){
		//Remove ALT in IE
		$('#peeps-list a img').removeAttr("alt");
		
		// IE 6 specific fixes
		if(jQuery.browser.version < 7){
			// reset the background for the first item in home page LIs
			$('#project-slider li').css('background-image',$('#project-slider li').eq(1).css('background-image'));
			
			// hide the buttons because the scroll doesn't work right
			$('#next, #back').hide();
		}
	}

	// US List Page - Hijack links and send to ajax page (checking for js basically)
/*
	var peepURL = "http://paramoreredd.com/us/peep-a/";
	$('#peeps-list a').each(function() {
		$(this).attr("href",peepURL + '#' + this.id);
	});
*/
	
	//Set width and height w/jquery
	$('#work-list-slider, #work-list-slider li.page, #work-list-wrapper').width(960).height(475);

	// reset for FireFox gallery resize (cycle?)
	$('#case-study-gallery img').width(498).height(315).css('display','block');
	$('#project-image img').width(620).height(393).css('display','block');
	
	//Work Project Page Images Cycle	
	$('#project-image').cycle({
		fx: 'scrollHorz',
    	timeout: 0,
	    speed:   500,
		easing: 'easeInOutQuad',
		pager: '#project-cycle-links'
	});
	
	$('#case-study-gallery').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		speed: 500,
		easing: 'easeInOutQuad',
		pager: '#project-cycle-links'
	});
	
	
	// Define the active class, and start rebuilding the HTML for the work items
	$('.sort-list a, #view-case-studies a, #view-all-work a').click(function() {
		if ($(this).hasClass("act") == false){
			// get the selected hash / class
			pageHash = this.hash;
			sortClass = pageHash.replace('#','');

			// set the active tab
			$('ul.column a').removeClass('act');
			$(this).addClass('act');

			if(this.id == 'case-study' || this.id == 'btn-view-all') {
				$('div.sort-list.act').slideUp('fast');
				$('#work-tabs a').removeClass('act');
				if(this.id == 'case-study') $('#view-all-work a').removeClass('act');
				if(this.id == 'btn-view-all') $('#view-case-studies a').removeClass('act');
			} else {
				$('#view-more a').removeClass('act');
			}
			// fade out and callback the function to rebuild the HTML
			$('#work-list-nav').fadeOut('fast');
			$('#work-list-slider').fadeOut('fast',ajaxLoadWork(this.href));
		
		}
		return false;
	});
	
	// Work Ajax...
	var allWorkURL = "http://paramoreredd.com/work/project-ajax/";
	var workURL = allWorkURL + "c/";
	$('.sort-list a, #view-case-studies a, #view-all-work a').each(function() {
		var url = (this.id == 'btn-view-all') ? allWorkURL : workURL + this.id;
		$(this).attr("href",url);
	});
	
	function ajaxLoadWork(url) {
		$('#work-list-wrapper').load(url,fadeInNewWorkList);
	}
	
	function fadeInNewWorkList() {
		$('#work-list-slider').fadeIn('fast');
	}
	
	//Work List Page Tabs
	$('div.sort-list').hide();
	$('div.sort-list:first').addClass('act');

	$("ul#work-tabs a").click(function(){
		var divID = this.hash;
		if ($(this).hasClass("act") == false){
			$('ul#work-tabs a, #view-case-studies a, #view-all-work a').removeClass('act');
			$(this).addClass('act');
			$('div.sort-list.act').slideUp('fast', function() {
				$(this).removeClass('act');
				$(divID).slideDown('slow').addClass('act');
			});
		}
		return false; 
	});
	
	//Blog Comments Live Preivew
	$('#comment-form').one('focus',function() {
		$('#comment').parent().after('<div id="live-preview"></div>');
	});
	var comment = ''; 
	var name = ''; 
	var after = ''; 
	$('#message').keyup(function() {
		comment = $(this).val();
		name = $('#comment-name').val();
		if(name != '')
		{
			after = '<h3>' + name + ' <span>said</span></h3>';
		}
		comment = comment.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />').replace(/(<\/?)script/g,"$1noscript");
		finalVar = '<p>Comment Preview</p><div id="preview">' + after + comment + '</div>';
		$('#live-preview').html( finalVar );
	});

	// panels expand/contract
	$("#panel, .panel").toggle(function(){
		if( this.id == "panel" ){ var height_is = "200px"; }
		else{ var height_is = "160px"; }
		$(this).animate({ height: height_is	}, 1000, "easeInQuint");
		$(this).find('h2 a').addClass('act');
		$(this).css('cursor','pointer');
	},function(){
		if( this.id == "panel" ){ var height_is = "36px"; }
		else{ var height_is = "26px"; }
		$(this).animate({ height: height_is }, 1000, "easeInQuint");
		$(this).find('h2 a').removeClass('act');
	});
	
	//Prevent the stupid link thing for buttons
	$("a#next, a#back").attr({ onClick: "return false;" });


	// eNews Update Pref Form Validation
	$('#enews-update').RSV({
		//errorTextIntro: "",
		displayType: "display-html",
		errorFieldClass: "required",
		errorTargetElementId: "form-error",
		rules: [
		"required,val_50089,Please provide your <strong>name</strong>.",
		"required,email,Please provide your <strong>e-mail address</strong>.",
		"valid_email,email,Please enter a valid <strong>e-mail address</strong>."
		]
	});
	
	// Connect Form Validation
	$('#rfp-form').RSV({
		//errorTextIntro: "",
		displayType: "display-html",
		errorFieldClass: "required",
		errorTargetElementId: "form-error",
		rules: [
		"required,name,Please provide your <strong>name</strong>.",
		"required,email,Please provide your <strong>e-mail address</strong>.",
		"valid_email,email,Please enter a valid <strong>e-mail address</strong>.",
		"required,message,Please provide your <strong>message</strong>."
		]
	});
	
	// NC Gov Conference Form
	$('#form-nc-gov').RSV({
		//errorTextIntro: "",
		displayType: "display-html",
		errorFieldClass: "required",
		errorTargetElementId: "form-error",
		rules: [
		"required,name,Please provide your <strong>name</strong>.",
		"required,email,Please provide your <strong>e-mail address</strong>.",
		"valid_email,email,Please enter a valid <strong>e-mail address</strong>.",
		"required,company,Please provide your <strong>company name</strong>."
		]
	});
	
	// Email Marketing Co-Op Program Form
	$('#email-marketing-form').RSV({
		//errorTextIntro: "",
		displayType: "display-html",
		errorFieldClass: "required",
		errorTargetElementId: "form-error",
		rules: [
		"required,fname,Please provide your <strong>first name</strong>.",
		"required,lname,Please provide your <strong>last name</strong>.",
		"required,company,Please provide your <strong>company name</strong>.",
		"required,email,Please provide your <strong>e-mail address</strong>.",
		"valid_email,email,Please enter a valid <strong>e-mail address</strong>.",
		"required,desired_month,Please select your <strong>desired month</strong>.",
		"required,desired_email,Please select your <strong>desired email</strong>."
		]
	});
	
	// Footer Form Validation
	$('#footer-contact').RSV({
		displayType: "display-html",
		onCompleteHandler: submitFooterForm,
		errorFieldClass: "required",
		rules: [
		"equals_original,name,Please provide your name.",
		"valid_email,email,Please enter a valid e-mail address."
		]
	});
	
	//Footer Form Submit...custom error message
	function submitFooterError(f, errorInfo) {
		$('#footer-contact input').removeClass('required');
		for (var i=0; i<errorInfo.length; i++)
		{
			var fieldName;

			// radio button
			if (errorInfo[i][0].type == undefined)
			fieldName = errorInfo[i][0][0].name;
			else
			fieldName = errorInfo[i][0].name;

			$("input[name='"+fieldName+"']").addClass('required');
		}
		
		if (errorInfo.length == 0) {
			submitFooterForm();
		}
		else {
			if ($('#foot-form h2').hasClass("required") == false){
				$('#foot-form h2').fadeOut('slow',function(){
					$(this).text("Whoops...looks like you need to fix something.").fadeIn('slow').addClass('required');
				});
			}
		}
		return false;
	}
	
	//Footer Form Submit...runs after validation
	function submitFooterForm() {
		var form_vals = escape($('#footer-contact').serialize());
		$.ajax({ url: "/other/enews-ab/footer/submit/", type: 'POST', data: 'value='+form_vals, async: false });
		
		$('#footer-contact').fadeOut('fast', function(){
			$('#footer-contact-sending').fadeIn('fast',function(){
				var act  = $('#footer-contact').attr('action');
				var vars = $('#footer-contact').serialize();
				$.post(
					act,
					vars,
					function(){
						$('#footer-contact-sending, #foot-form h2').fadeOut('fast',function(){
							$('#footer-contact-thanks').fadeIn('fast');
							$('#foot-form h2').text("Thanks!").fadeIn('slow');
						});
					}
				);
			});
		});
		return false;
	}
	
	//Show hide input copy
	$('#footer-contact input:text, #footer-contact textarea').focus(function() {
		if ($(this).hasClass("required") == false){
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		}
	})
	.blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}

	});
	//Awards Page Show Links
	$('.awards-list li ul').hide();
	$('.awards-more').click(function() { 
		$(this).parent().next('ul').slideToggle(400);
		return false;
	});
	
	//Slide Toggle Awards
	$('#awards-btn').click(function() { 
		$('#foot-awards').slideToggle(400);
		return false;
	});
	
	//Valid way to open links in new window
	$("a[rel='external']").click(function(){window.open(this.href); return false;});
	
	// easter cat
	$("#awards-cat-img").hide();
	
	$("#awards-cat").click(function(){
		$("#awards-cat-img").fadeIn(500);
		pageTracker._trackPageview('Awards-Easter-Egg');
		setTimeout("$('#awards-cat-img').fadeOut(500);", 1500);
		return false;
	});
	

});//End document.ready