$(function() {
	
	// Need to remove the width and height definitions for full-size images in posts so they scale proportionately
	/*	$('.size-full').removeAttr('width');
	$('.size-full').removeAttr('height');*/
	
	// SideBarFit ---------------------
	SideBarFit(); 
	
	// Setup the Share button ---------------------
	$('.share').hover(function(){
		var theID = $(this).attr("id"); theID = theID.substring(6); // this removes 'share_' and leaves id number
		$('#sharewindow_'+theID).css('display','block'); return false; 
	});
	
		$('.sharewindow').hover(
      function (){
				var theID = $(this).attr("id"); theID = theID.substring(12); // this removes 'sharewindow_' and leaves id number
				$('#share_'+theID).css('background-position','0px -38px'); 
			},
      function (){ 
				$(this).css('display','none');
				$('.share').css('background-position','0px 0px');
			}
    );
	
	$('.sharewindow').click(function(){ $(this).css('display','none'); });
	
	// Setup the Search button ---------------------
	// searchactive - means the user has hovered over the search icon and the previous input should be cleared only first time around
	// searchstay - means don't remove the search box on hover out - waits for user to click outside the box to remove it
	
	$('.nav_search').hover(function(){
		if( !$('#nav_searchbox').hasClass('searchactive') ){
			//$('#nav_searchbox').css('display','block');
			$('#nav_searchinput').val('');
			$('#nav_searchbox').addClass('searchactive');
			$('#nav_searchbox').stop(true,true).css('display','block').animate( {opacity: 1}, 100 );
			$('#nav_searchinput').focus();
		}
		return false;
	});
	
		$('#nav_searchbox').hover(
      function (){ },
      function (){ 
				if( ($('#nav_searchinput').val() =='') && (!$('#nav_searchbox').hasClass('searchstay')) ){
					$('#nav_searchbox').stop(true,true).animate( {opacity: 0}, 200, function(){$('#nav_searchbox').css('display','none');} );
					$('#nav_searchbox').removeClass('searchactive');
				}  
			}
		);
			
		$('#nav_searchinput').blur( function(e){
			$('#nav_searchbox').removeClass('searchactive');
			$('#nav_searchbox').removeClass('searchstay');
			// Create small delay in case search icon was clicked and therefore we need the click event to run first
			setTimeout(function(){
				if( !$('#nav_searchbox').hasClass('searchstay') ){
					$('#nav_searchbox').stop(true,true).animate( {opacity: 0}, 200, function(){$('#nav_searchbox').css('display','none');} );
				}
			}, 150);
		} );
		
		$('.nav_search').click( function(){
			//$('#nav_searchbox').css('display','block'); 
			$('#nav_searchbox').addClass('searchactive');
			$('#nav_searchbox').addClass('searchstay');
			$('#nav_searchbox').stop(true,true).css('display','block').css('opacity',1);
			$('#nav_searchinput').focus();
			if( $('#nav_searchinput').val() !='' ){
				// This triggers a submit of the form
				var event = jQuery.Event("submit");
				$("form:#searchform").trigger(event);
			}
			return false;
		} );
		
	$('#nav_searchinput').click( function(){ 
		$('#nav_searchbox').addClass('searchstay');
	} );
	
	// setup forms with jqueryFormField ---------------------
	$('.jqueryformfield').val(''); // clears any stored values if used previously
	$('.jqueryformfield').example(function() {
	 	return $(this).attr('title'); // uses the info in the title to display label inside the form field
	});
	
	// Setup the Signup button ---------------------
		$("#signup_button").click(function() {
		// validate and process form here
		var theName = $("input#signup_name").val();
		var theEmail = $("input#signup_email").val();
		//alert(theName+" "+theEmail);
		if (theName == "" || theName == $("input#signup_name").attr('title') || theEmail == "" || theEmail == $("input#signup_email").attr('title') ){return false;}
		
		return true;
		/*
		var theDataString = 'name='+ theName + '&email=' + theEmail;
		var theAction = $("#signup_form").attr('action'); //retrieves the value of the action attribute of the form
		
		$.ajax({
      type: "POST",
      url: theAction,  
      data: theDataString,
      success: function() {
        $('#signup').html("<div id='signup_message'></div>");
        $('#signup_message').html("<h2>Thanks for Signing Up!</h2>")
        .hide()
        .fadeIn(1500, function() {
          $('#signup_message');
        });
      }
    });
		
		return false;*/
	});
	
	ed_moreinfo_init();
	
});


// --------------------------------------------------------------------
// More Info

function ed_moreinfo_init(){
	$('.ed_moreinfo_text').hide();
	$('.ed_moreinfo').click(function() {
  	$('.ed_moreinfo_text',this).slideToggle(300);
		var theToggle = $('.ed_moreinfo_toggle span',this).text();
		if(theToggle == "More Info"){
			$('.ed_moreinfo_toggle span',this).text("Less Info")
			$('.ed_moreinfo_button',this).addClass('ed_moreinfo_button_less');
			$('.ed_moreinfo_button_less',this).removeClass('ed_moreinfo_button');
		}else{
			$('.ed_moreinfo_toggle span',this).text("More Info")
			$('.ed_moreinfo_button_less',this).addClass('ed_moreinfo_button');
			$('.ed_moreinfo_button',this).removeClass('ed_moreinfo_button_less');
		}
		return false;
	});
}

// --------------------------------------------------------------------
// SidebarFit
// Only adds enough <li> items in a <ul> to fit the space as determined by the main content's height
// Erik Gorton 2010

function SideBarFit() {

	var theRuler = $("#ruler");
	var theContainerHeight = $("#page_wrap").height();
	var theOutput = "";
	var theItems = $(".sidebar_item").get(); // Retrieves all inidividual list items in a <ul>
	
	// Add each of the list items and check if the height is bigger than the page_wrap - if so, don't add it on
	for (var i = 0; i < theItems.length; i++) {
			var theHtml = "";
			if(i>0){ theHtml = "<li class='sidebar_item'><div class='sidebar_separator'></div></li>"; }
			theHtml += "<li class='sidebar_item'>"+ $(theItems[i]).html() +"</li>";
			theRuler.html(theOutput+theHtml);
			if(theRuler.height() > theContainerHeight) { break; }
			theOutput += theHtml;
	}
	
	var theYear=(new Date()).getFullYear();
	theOutput += "<li class='sidebar_item'><div class='sidebar_separator'></div></li><li class='sidebar_item' id='footer'>";
	theOutput += "<a href='http://www.uproar.org.au'>uproar.org.au</a> &copy; "+theYear+" &nbsp; &nbsp; site by <a href='http://www.ethicaldesign.com.au'>EthicalDesign</a></li>";
	$("#sidebar").html(theOutput);
}

// ------------------------------------------------------------------------------

/*
 * jQuery Example Plugin 1.3.4
 * Populate form inputs with example text that disappears on focus.
 *
 * e.g.
 *  $('input#name').example('Bob Smith');
 *  $('input[@title]').example(function() {
 *    return $(this).attr('title');
 *  });
 *  $('textarea#message').example('Type your message here', {
 *    class_name: 'example_text',
 *    hide_label: true
 *  });
 *
 * Copyright (c) Paul Mucur (http://mucur.name), 2007-2008.
 * Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
 * licenses.
 *
 */
(function(A){A.fn.example=function(D,C){var B=A.extend({},A.fn.example.defaults,C,{example_text:D});var E=A.isFunction(B.example_text);if(!A.fn.example.bound_class_names[B.class_name]){A(window).unload(function(){A("."+B.class_name).val("")});A("form").submit(function(){A(this).find("."+B.class_name).val("")});A.fn.example.bound_class_names[B.class_name]=true}return this.each(function(){var G=A(this);var H=A.metadata?A.extend({},B,G.metadata()):B;if(A.browser.msie&&!G.attr("defaultValue")&&(E?G.val()!="":G.val()==H.example_text)){G.val("")}if(G.val()==""){G.addClass(B.class_name);G.val(E?H.example_text.call(this):H.example_text)}if(B.hide_label){var F=A("label[@for="+G.attr("id")+"]");F.next("br").hide();F.hide()}G.focus(function(){if(A(this).is("."+B.class_name)){A(this).val("");A(this).removeClass(B.class_name)}});G.blur(function(){if(A(this).val()==""){A(this).addClass(B.class_name);A(this).val(E?H.example_text.call(this):H.example_text)}})})};A.fn.example.defaults={example_text:"",class_name:"example",hide_label:false};A.fn.example.bound_class_names=[]})(jQuery);

/*
 * Metadata - jQuery plugin for parsing metadata from elements
 *
 * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.metadata.js 3620 2007-10-10 20:55:38Z pmclanahan $
 *
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(9($){$.r({3:{7:{8:\'l\',h:\'3\',q:/({.*})/,4:\'3\'},w:9(a,b){g.7.8=a;g.7.h=b},j:9(b,c){5 d=$.r({},g.7,c);2(!d.4.o)d.4=\'3\';5 a=$.n(b,d.4);2(a)6 a;a="{}";2(d.8=="l"){5 m=d.q.v(b.u);2(m)a=m[1]}k 2(d.8=="t"){2(!b.i)6;5 e=b.i(d.h);2(e.o)a=$.s(e[0].C)}k 2(b.p!=A){5 f=b.p(d.h);2(f)a=f}2(a.z(\'{\')<0)a="{"+a+"}";a=y("("+a+")");$.n(b,d.4,a);6 a}}});$.x.3=9(a){6 $.3.j(g[0],a)}})(B);',39,39,'||if|metadata|single|var|return|defaults|type|function|||||||this|name|getElementsByTagName|get|else|class||data|length|getAttribute|cre|extend|trim|elem|className|exec|setType|fn|eval|indexOf|undefined|jQuery|innerHTML'.split('|'),0,{}))


// --------------------------------------------------------------------
// Make a Comment

function MakeAComment(theCommand,theSiteUrl,thePostID){
	if(theCommand == "start"){
		var theHtml = '<div id="makecomment"><a href="#" onclick="MakeAComment(\'close\'); return false;"><div class="blackout"></div></a><div class="inner"><h2>Make a Comment</h2><form id="commentform" method="post" action="'+theSiteUrl+'/wp-comments-post.php"><input id="makecomment_name" name="author" class="jqueryformfield" type="text" title="Name" /><input id="makecomment_email" name="email" class="textfield_right jqueryformfield" type="text" title="Email (will be kept private)" /><div class="clear"></div><textarea id="makecomment_comment" name="comment" class="jqueryformfield" cols="" rows="" title="Comment">Comment..</textarea><input type="hidden" name="comment_post_ID" value="'+thePostID+'" id="comment_post_ID" /><a href="#" onclick="MakeAComment(\'submit\'); return false;"><div class="enter">Enter</div></a></form><a href="#" onclick="MakeAComment(\'close\'); return false;"><div class="close">close</div></a></div></div>';
		$("#wrapper").append(theHtml);
		// setup forms with jqueryFormField
		$('.jqueryformfield').val(''); // clears any stored values if used previously
		$('.jqueryformfield').example(function() {
			return $(this).attr('title'); // uses the info in the title to display label inside the form field
		});
	}else if(theCommand == "close"){
		$("#makecomment").remove();
	} else { // submit the comment
		// validate and process form here
		var theName = $("input#makecomment_name").val();
		var theEmail = $("input#makecomment_email").val();
		var theComment = $("textarea#makecomment_comment").val();
		if (theName == "" || theName == $("input#makecomment_name").attr('title') || theEmail == "" || theEmail == $("input#makecomment_email").attr('title') || theComment == "" || theComment == $("textarea#makecomment_comment").attr('title') ){ 
			alert("Please fill in all fields"); return false;
		}
		if(!ValidateEmail(theEmail)){
			alert("Please check your email address"); return false;
		}
		document.forms["commentform"].submit();
		return true;
	}
}

function ValidateEmail(str) {
		var at="@"; var dot="."; var lat=str.indexOf(at); var lstr=str.length; var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){return false;}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
		if (str.indexOf(at,(lat+1))!=-1){return false;}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
		if (str.indexOf(dot,(lat+2))==-1){return false;}
		if (str.indexOf(" ")!=-1){return false;}
		return true;
}

