$(document).ready( function() {
	
	//$('.front .views-nodequeue_8-block').html('');
	
	$('.manifesto-details .view-content, .manifesto-details .view-header, .manifesto-details .view-footer').addClass('manifesto-scripted');
	
	$('.manifesto-details .view-content li:first').addClass('active');
	
	var manifestoBody = $('.manifesto-details .view-content li:first .views-field-body').html();
	
	$('.manifesto-body-holder').html(manifestoBody);
	
	$('.manifesto-details .view-content li').click(function() {
		var oldbody = $('.manifesto-body-holder').html();
		var newbody = $('.views-field-body',this).html();
		if (oldbody != newbody) {
			$('.manifesto-body-holder').fadeOut('slow',function() {
				$('.manifesto-body-holder').html(newbody);
				$('.manifesto-body-holder').fadeIn('slow');
				});
			$('.manifesto-details li.active').removeClass('active');
			$(this).addClass('active');
			}
		});
	
	$('.view-toolkit').hide();
	
	$('#block-panels_mini-charities_toolkit .center-wrapper .panel-col-first').click(function() {
		$('.view-toolkit').slideToggle('slow');
		});

	$('.view-nodequeue-15 div.views-field-title a,  .view-homepage-partners div.views-field-title a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250
	});
	
	  // Add Scroller Object
	  $jScroller.add("#scroller_container","#scroller","up",2);

	  // Start Autoscroller
	  $jScroller.start();
	  
  	$('#block-views-peoples_taskforce-block_1 li.jcarousel-item a, #block-views-Partners-block_1 li.jcarousel-item a').click(function() {	  
	var link = $(this).attr('href') + '#content-area .view-display-id-page_1';
	$('#content-area .view-display-id-page_1').fadeOut("slow",function() {
		$('#content-area .view-display-id-page_1').load(link, function () {
			$('#content-area .view-display-id-page_1').fadeIn("slow");
			});
		});
	return false;
	});
	
	$('form#contact-mail-page input.form-submit').val('Submit');
		
	$('.slideshow').cycle({speed: 1000, timeout:  6000});

	$('#slideshow-wrapper').cycle({speed: 1000, timeout: 5000});
	
	$('.read-more-link').each(function() {
		$(this).html('<a href="javascript:void(0);">READ MORE</a>');
		});
	$('.read-more-text').each(function() {
		$(this).hide();
		});

        $('.read-more-link').click(function() {
                $(this).next().slideToggle(1000);
                if ($(this).html().search(/hide/i) > 0) {
                        $(this).html('<a href="javascript:void(0);" class="read-more">READ MORE</a>');
                        }
                else {
                        $(this).html('<a href="javascript:void(0);" class="read-more">Hide text</a>');
                        }
                });
                
        $('ul.nice-menu li ul li a.active').each( function() {
        	$(this).parent().parent().parent().children('a').addClass('active');
	});
	
	$("ul.nice-menu li#menu-2678 a").each(function() {
	  if (window.location.pathname.search(/stories/i) > 0) {
	    	$(this).addClass("active");
	  }	
	});

	/////////Read More Links

	/*
        $("a").each(function() {
	        if ($(this).html().search(/more/i) > 0) {
        	        $(this).addClass('read-more');
                	}
        	});
	*/
		
	/////////FAQ Page script

	$('#block-views-FAQ_Digital-block_1 .views-field-body, #block-views-FAQ_Partner-block_1 .views-field-body, #block-views-FAQ_gettingonline-block_1 .views-field-body, #block-views-FAQ_Commercial-block_1 .views-field-body, #block-views-FAQ_Volunteer-block_1 .views-field-body, #block-views-FAQ_Rural-block_1 .views-field-body, #block-views-FAQ_VResearch-block_1 .views-field-body').each(function() {
        	$(this).hide();
        	});

	$('#block-views-FAQ_Digital-block_1 .views-field-title, #block-views-FAQ_Partner-block_1 .views-field-title, #block-views-FAQ_gettingonline-block_1 .views-field-title, #block-views-FAQ_Commercial-block_1 .views-field-title, #block-views-FAQ_Volunteer-block_1 .views-field-title, #block-views-FAQ_Rural-block_1 .views-field-title, #block-views-FAQ_Research-block_1 .views-field-title').click(function() {
                $(this).next().slideToggle(500);
		});
		
	$('#block-views-FAQ_Digital-block_1 .views-field-title a, #block-views-FAQ_Partner-block_1 .views-field-title a, #block-views-FAQ_gettingonline-block_1 .views-field-title a, #block-views-FAQ_Commercial-block_1 .views-field-title a, #block-views-FAQ_Volunteer-block_1 .views-field-title a, #block-views-FAQ_Rural-block_1 .views-field-title a, #block-views-FAQ_Research-block_1 .views-field-title a').click(function(event) {
		event.preventDefault();
		});
	
	$("ul#main-menu li a").each(function() {
	  	var url_array = window.location.pathname.split("/");
	  	var href_array = $(this).attr("href").split("/");
  		if (url_array[1] == "stories" && href_array[1] == "success-stories") {
	    		$(this).addClass("active");
	    		}
	});
	
	// Toggling random bit of About Us section
	
	$('#block-panels_mini-about_us_2').before('<div id="about-us-more"><p><a class="read-more" href="#">Read More</a></p></div>');
	$('#block-panels_mini-about_us_2').hide();
	$('#about-us-more').toggle(function() {
		$('#block-panels_mini-about_us_2').slideDown();
		$('#about-us-more a').html('Click to hide');
		},
		function () {
			$('#block-panels_mini-about_us_2').slideUp();
			$('#about-us-more a').html('Read more');
			});
	
	/** new window **/
	
	$(function(){
	    	$('a.new-window, li#menu-2214 a').click(function(){
			window.open(this.href);
			return false;
    	});
    
        $(".podcast-player").hide();
        
	function sendEvent(swf, typ,prm) { 
	  thisMovie(swf).sendEvent(typ,prm); 
	}

	function thisMovie(swf) {
	  if(navigator.appName.indexOf("Microsoft") != -1) {
	    return window[swf];
	  } else {
	    return document[swf];
	  }
	}
	
	
	$(".row-resize .col").each(function() {
	    	var newheight = $(this).parent().innerHeight() - 12;
	    	$(this).height(newheight);
    	});
    	
    	$("#block-panels_mini-get_online_intro .panel-panel, #block-panels_mini-about_us_bottom_copy .panel-panel").each(function() {
    		var newheight = $(this).parent().innerHeight();
    		$(this).height(newheight);
    	});
    	
    	 $("#block-panels_mini-homepage_2_bottom_3 .panel-pane").each(function() {
    		var newheight = $(this).parent().parent().parent().innerHeight() - 2;
    		$(this).height(newheight);
    	});
    	
    	$(".row-resize a.read-more").each(function() {
    		$(this).parent().parent().css({'position' : 'absolute', 'bottom' : '10px'});
    	});
	
	$(".view-case-studies tr").each(function () {
		var height1 = $(".col-1 .views-field-teaser",this).height();
		var height2 = $(".col-2 .views-field-teaser",this).height();
		if (height1 > height2) {
			$(".col-2 .views-field-teaser",this).height(height1);
			}
		if (height2 > height1) {
			$(".col-1 .views-field-teaser",this).height(height2);
			}
		});
});

	$('.page-get-involved #webform-client-form-326 #webform-component-section_2, .page-get-involved #webform-client-form-326 input#edit-submit,.page-get-involved fieldset#webform-component-pledges div,.page-get-involved #webform-component-create_my_own_task,.page-get-involved .compulsory-fields, .page-get-involved #webform-component-subsection_1').hide();
	
	
	$('#webform-component-postal_address').before('<h4>Care to tell us more? (Don\'t worry if you don\'t want to - just click submit)</h4>');
	
	$('#webform-component-subsection_1').prepend('<p>Please provide us with some basic information so that we can send you a thankyou note and partner information</p>');
	
	var pledgeheight = 0;
	$('.pledge-taxonomy-inner').each(function() {
		if ($(this).height() > pledgeheight) {
			pledgeheight = $(this).height();
			}
		});
	$('.pledge-taxonomy-inner').each(function() {
		$(this).height(pledgeheight);
		});
	
	
	function validateform() {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = $('input#edit-submitted-subsection-1-email-address').val();
		if (	$('input#edit-submitted-subsection-1-first-name').val().length > 0 &&
			$('input#edit-submitted-subsection-1-surname').val().length > 0 &&
			$('input#edit-submitted-subsection-1-organisation-name').val().length > 0 &&
			$('input#edit-submitted-subsection-1-job-title').val().length > 0 &&
			$('input#edit-submitted-subsection-1-terms-and-conditions-I-have-read-the-terms-and-conditions').attr('checked') &&
			reg.test(address) ) {
				return true;
			}
		else {
			return false;
			}
		}
		

		
	function fireEvent(obj,evt){
		var fireOnThis = obj;
		if( document.createEvent ) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( evt, true, false );
		fireOnThis.dispatchEvent(evObj);
		} else if( document.createEventObject ) {
		fireOnThis.fireEvent('on'+evt);
		}
	}
		
		
	function populateTextarea() {	
		$('#node-326 #webform-component-pledges textarea').val('Want to tell us more about how you will do this?');
		}
		
	populateTextarea();
	
	$('.form-step-1').click(function() {
		populateTextarea();
		$('#webform-component-section_2,input#edit-submit,.submit-2,#webform-component-subsection_1').slideUp(function() {
			$('#webform-component-section_1,#mini-panel-form_section_1_text,.pledges-holder,.get-online-message').slideDown();
			$('.form-step-2,.form-step-3').removeClass('form-step-underline');
			$('.form-step-1').addClass('form-step-underline');
			});
		scrollFormTop();
		});
		
	function cleanTextarea() {
		$('#node-326 #webform-component-pledges textarea').each(function() {
			if ($(this).val() == 'Want to tell us more about how you will do this?') {
				$(this).val('');
				}
			});
		}
		
	function scrollFormTop() {
		var scrollto = $(".form-steps").offset().top;
		$('html, body').animate({scrollTop: scrollto}, 1000);
		}
			
	function step_2() {
		$('.get-online-message,#webform-component-section_2,input#edit-submit,#mini-panel-form_section_1_text,#webform-component-section_1,.pledges-holder').slideUp(function() {
			$('#webform-component-subsection_1,.submit-2').slideDown();
			$('.form-step-1,.form-step-3').removeClass('form-step-underline');
			$('.form-step-2').addClass('form-step-underline');
			});
			cleanTextarea();
			scrollFormTop();
		}
		
	$('.form-step-2').click(function() {
			step_2();
		});
		
	function step_3() {
		if (validateform()) {
			$('.get-online-message,#webform-component-subsection_1,.submit-2,#webform-component-section_1,#mini-panel-form_section_1_text,.pledges-holder').slideUp(function() {
				$('#webform-component-section_2,input#edit-submit').slideDown();
				$('.form-step-1,.form-step-2').removeClass('form-step-underline');
				$('.form-step-3').addClass('form-step-underline');
				});
			}
		else {
			fireEvent(document.getElementById("form_error_link"),'click');
			}
		cleanTextarea();
		scrollFormTop();
		}
		
	/* take this out later!
	$('.form-step-3').click(function() {
			step_3();
		}); */	
		
	function updatepromise() {
		var promisetitle = $('.view-digital-promises .item-list ul li:first .views-field-title span').html();
		$('.promise-title').html(promisetitle);
		var promisetext = $('.view-digital-promises .item-list ul li:first .views-field-body div.field-content').html();
		$('.promise-text').html(promisetext);

        	$('.view-digital-promises .item-list ul li').each(function() {
        		if ($('.views-field-title',this).hasClass('tax-color-1')) { var newcolor = '#930059'; }
        		else if ($('.views-field-title',this).hasClass('tax-color-2')) { var newcolor = '#9bc73c'; }
        		else if ($('.views-field-title',this).hasClass('tax-color-3')) { var newcolor = '#dfa53a'; }
        		else if ($('.views-field-title',this).hasClass('tax-color-6')) { var newcolor = '#000000'; }
        		else { var newcolor = '#1a84be'; }
        		if ($('.view-digital-promises .item-list ul li').index(this) == 0) {
				$('#promise-details').animate({ backgroundColor: newcolor },300);
				$(this).animate({ backgroundColor: newcolor },300);
                		$('span',this).animate({ color: "white" });
                		}
                	else {
                		$('span',this).css('color',newcolor);
                		$(this).css('background','transparent none');
                		}
                	});
		}

	updatepromise();

	$('.promises-next').click(function() {
		if ($('.view-digital-promises .item-list ul li').length > 1) {
			var transit = $('.view-digital-promises .item-list ul li:first');
			$('.view-digital-promises .item-list ul').append(transit);
			updatepromise();
		}
	});

	$('.promises-prev').click(function() {
		if ($('.view-digital-promises .item-list ul li').length > 1) {
			var transit = $('.view-digital-promises .item-list ul li:last');
			$('.view-digital-promises .item-list ul').prepend(transit);
			updatepromise();
		}
	});
		
	$('.view-digital-promises li.views-row').click(function() {
		if ($('.view-digital-promises .item-list ul li').length > 1) {
			var transit = $(this);
			$('.view-digital-promises .item-list ul').prepend(transit);
			updatepromise();
		}
	});	
	
	
	var compulsorytext = '<div class="compulsory-fields"><p>Fields marked <span class="form-required">*</span> are compulsory</div><div class="submit-2"><p>Next</p></div>';
	$('#node-326 #webform-component-job_title').after(compulsorytext);
	$('#webform-component-section_1').append('<div class="submit-1"><p>Next</p></div>');
	$('.all-pledge-taxonomies').after($('#mini-panel-form_section_1_text'));
	$('.submit-1').click(function() {
		step_2();
		});
	$('.submit-2').click(function() {
		step_3();
		});
	$('.promises-categories li').click(function() {
		var promise_category = $.trim($(this).html());
		if (promise_category == 'All') {
			$('ul#hidden-promise-list li').each(function() {
				$('.view-digital-promises .item-list ul').append($(this));
				$('ul#hidden-promise-list',this).remove();
				});
			}
		else {
		$('.view-digital-promises li.views-row').each(function() {
			var li_category = $.trim($('.views-field-name span',this).html());
			if (li_category != promise_category) {
				$('ul#hidden-promise-list').append($(this));
				$('.views-digital-promises',this).remove();
				}
			});
		$('ul#hidden-promise-list li').each(function() {
			var li_category = $.trim($('.views-field-name span',this).html());
			if (li_category == promise_category) {
				$('.view-digital-promises .item-list ul').append($(this));
				$('ul#hidden-promise-list',this).remove();
				}
			});
		}
		updatepromise();
		});

	$('#webform-components-pledget input').each(function() {
		$(this)[0].checked = false;
		});

	$('.go-do-2').click(function() {
		$('#webform-component-section_1,.form-steps,#mini-panel-form_section_1_text').slideDown();
		
		if ($('.pledges-holder').length == 0) {
	       $('div.form-steps').after('<div class="pledges-holder"><h3>I will...</h3><div class="pledges-holder-2"><div class="pledge-holder views-field-title" id="add-more">Choose another by selecting from the list above</div></div>');
		}
		
		var formkey = 'node_' + $('.view-digital-promises .item-list ul li:first .views-field-nid span').html() + '_select';
		
		var text_formkey = formkey.replace("_select", "_textarea");
		update_textarea(text_formkey);
			
		if (!$('.pledges-holder-2 div.views-field-title').hasClass(formkey)) {
			var newstyle = $('.view-digital-promises .promise-left-block .item-list ul li:first div.views-field-title').attr('class');
			var newtext = $('.view-digital-promises .item-list ul li:first .views-field-title span').html();
			var formtext = '<div class="pledge-holder ' + newstyle + ' ' + formkey + '"><span class="pledge-text" id="' + text_formkey + '">' + newtext + '</span><span class="closebox" id="' + formkey + '">X</span></div>';
			$('div#add-more').before(formtext);
			
			$('span.closebox').click(function() {
				var removeid = this.id;
				$('#webform-component-pledges input').each(function() {
					if($(this).attr('name').search(removeid) != -1) {
						$(this)[0].checked = false;
						}
					});
				var textarea_id = 'webform-component-' + $(this).prev().attr('id');
				nullify_textarea(textarea_id);
				$(this).parent().remove();
				});	
						
			$('span.pledge-text').click(function() {
				var newformkey = $(this).attr('id');
				update_textarea(newformkey);
				});
				
			$('#webform-component-pledges input').each(function() {
				if($(this).attr('name').search(formkey) != -1) {
					$(this)[0].checked = true;
				}
			});
		}

				
	});
		
	function update_textarea(text_formkey) {
		$('fieldset#webform-component-pledges .webform-component-textarea').each(function() {
			if($(this).attr('id').search(text_formkey) != -1) {
				$(this).show();
				$(this).children().show();
				}
			else {
				$(this).hide();
				}
			});
		}
	
	function nullify_textarea(webform_id) {
		$('.webform-component-textarea').each(function() {
			if ($(this).attr('id') == webform_id) {
				$(this).hide();
				$('textarea',this).val('');
				}
			});
		}
		
	$('.hidden-form-intro').eq(0).before('<p class="promise-more">Click here to find out more about the sign-up process</p>');
	$('.hidden-form-intro').hide();
	$('p.promise-more').toggle(function() {
			$('.hidden-form-intro').slideDown();
			$('p.promise-more').html('Click to hide');
		},
		function() {
			$('.hidden-form-intro').slideUp();
			$('p.promise-more').html('Click here to find out more about the sign-up process');
			}
		);
		
	$('#node-326 #webform-component-pledges textarea').focus(function() {
		if ($(this).val() == 'Want to tell us more about how you will do this?') {
			$(this).val('');
			}
		});
		
		   $('.pledge-taxonomies input.form-checkbox').each(function() {
		  	 if ($(this).is(':checked')) {
			   	$(this).after('<div class="input-ring ring-on"></div>');
			   	$(this).hide();
			   	}
			 else  {
			   	$(this).after('<div class="input-ring ring-off"></div>');
			   	$(this).hide();
			   	}
	   	});
	   	
	   $('.pledge-taxonomy-inner .form-checkboxes').toggle(function() {
	   		$('input',this).attr('checked','checked');
	   		$('.input-ring',this).addClass('ring-on');
	   		$('.input-ring',this).removeClass('ring-off');
	   	},
	   	function() {
	   		$('input',this).removeAttr('checked');
	   		$('.input-ring',this).addClass('ring-off');
	   		$('.input-ring',this).removeClass('ring-on');
	   		});
	   		
	   	//$('#webform-component-section_1 input.form-checkbox, #webform-component-section_1 .input-ring').tooltip({ 
	$('#webform-component-section_1 .all-pledge-taxonomies .webform-component-select .form-item').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250,
	    bodyHandler: function() { 
	    	var description = '<h4>' + $(this).children().eq(0).html() + '</h4><p>' + $(this).children().eq(2).html() + '</p>';
	    	//var description = $(this).parent().parent().parent().next().html();
        	return description;
    		} 
	});

	function mycarousel_initCallback(carousel) {
		    jQuery('.jcarousel-control a').bind('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        		carousel.startAuto(0);
			return false;
		    });
		 
		    jQuery('.jcarousel-scroll select').bind('change', function() {
			carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
			return false;
		    });
		    
		    
		    jQuery('.rotator-video').bind('click', function() {
        		carousel.stopAuto();
		    });    
		    
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(function() {
			carousel.stopAuto();
		    }, function() {
			carousel.startAuto();
		    });
	};
	
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		$('.jcarousel-control a').each(function() {
			if ($(this).html() == idx) {
				$(this).addClass('active');
				}
			else {
				$(this).removeClass('active');
				}
			});
		}
		
 
	// Ride the carousel...
		jQuery("#mycarousel").jcarousel({
		auto: 4,
		wrap: 'last',
		scroll: 1,
		initCallback: mycarousel_initCallback,
        	itemFirstInCallback:  mycarousel_itemFirstInCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	    });
	    
	   $('.rotator-video').click(function() {
	   	var unicornid = $(this).attr('id').replace('unicorn-','');
		var videohtml = '<div class="rotator-video-holder"><object id="flashobject" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" allowNetworking="all" height="240" width="300" data="http://r.unicornmedia.com/content.aspx?uid=944323b8-db67-4623-b9bf-63069fd5dc37&at=ee1c36c9-c4e2-4ba8-9039-10f4692059fa"><param name="autoPlay" value="true"><param name="quality" value="high" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://r.unicornmedia.com/content.aspx?uid=944323b8-db67-4623-b9bf-63069fd5dc37&at=ee1c36c9-c4e2-4ba8-9039-10f4692059fa" /><param name="flashvars" value="config=http://r.unicornmedia.com/embed/ee1c36c9-c4e2-4ba8-9039-10f4692059fa?view=item%26view_id=' + unicornid + '" /></object></div>';
		$(this).after(videohtml);
		$(this).hide();
	   	});
	   	
	   $('.jcarousel-control a').click(function() {
	   	$('.rotator-video').each(function() {
	   		$(this).show();
	   		});
	   	$('.rotator-video-holder').each(function() {
	   		$(this).html('');
	   		});
	   	});
	   	

	
	   		
    	
});


