
jQuery(document).ready(function(){
 	
 	
 	////
	// hide on load
	//
	//$('#accesspanel').css('display','none');
	//console.log($('#accesspanel p.error').length);
	if($('#accesspanel p.error').length > 0)
	{
	var window_h = window.innerHeight;
	var c_margin = ((window_h/2)-(200/2))*2/3;
	
	$('#accesspanel').css('margin-top', c_margin);
	
	$('#blanket').css({'height':$(document).height()}).show();
	}
	
	////
	// News box tabs
	//
	$('#newsbox .newsItem1').click(function(){
	$(this).addClass('selected');
	$('#newsbox .newsItem2').removeClass('selected');
	$('#newsbox .newsItem3').removeClass('selected');
	});
	
	
	$('#newsbox .newsItem2').click(function(){
	$(this).addClass('selected');
	$('#newsbox .newsItem1').removeClass('selected');
	$('#newsbox .newsItem3').removeClass('selected');
	});
	
	$('#newsbox .newsItem3').click(function(){
	$(this).addClass('selected');
	$('#newsbox .newsItem2').removeClass('selected');
	$('#newsbox .newsItem1').removeClass('selected');
	});
	
 
	
	
	
	////
	// show login form on click
	//
	$('.loginbtn:not(.isloggedin) a').click(function(event) {
		event.preventDefault();
		
		
		var window_h = $(window).height();
		var c_margin = ((window_h/2)-(200/2))*2/3;
		
		$('#accesspanel').css('margin-top', c_margin);
		
		$('#blanket').css({'height':$(document).height()}).show();
		
		
		
		
	});
	
	
	////
	// close login window
	//
	$('.closebtn a').click(function(event) {
		 event.preventDefault();
		 
		$('#blanket').hide();
	});
	
	
	
	////
	// check height for crazy menu and correct
	//
	if($('#maincontent').height()<$('#leftside').height()+24)
	{
		//$('#outerwrap').append('[ch'+$('#maincontent').height()+'lh'+$('#leftside').height()+']');
		$('#maincontent').height($('#leftside').height()+24);
		
	}
	
	
	
	
	
	////
	// nav actions
	//
	$('#leftside .nav > ul > li > a').click(function(event){
		event.preventDefault();
		//console.log($(this).parent('li').children('ul').css('display'));
//		if ($(this).hasClass('foo'))
//		{
			
			////close previous 
			$('.zing').parent('li').children('ul').stop().slideUp('fast');
			$('.zing').removeClass('zing');
			$(this).addClass('zing');
			///open clicked
			$(this).parent('li').children('ul').stop().slideDown('fast', function() {
				
			    //console.log('#content:'+$('#content').height()+';');
			    //console.log('#leftside:'+$('#leftside').height()+';');
			    
			    if($('#maincontent').height()<$('#leftside').height()+24)
			    {
			    	$('#maincontent').height($('#leftside').height()+24);
			    }
			   // console.log('new-#content:'+$('#content').height()+';');
			   // console.log('new-#leftside:'+$('#leftside').height()+';');
			  });
			
			
//		}
	});
	
	
	/////
	// submenu role to expose
	//
//	$('#leftside .nav > ul > li > ul > li').mouseenter(function(event){
		//event.preventDefault();
		//{
//			$(this).children('ul').stop(false, true).slideDown('100');
		
//	});
	////// close
//	$('#leftside .nav > ul > li > ul > li').mouseleave(function(event){
		//event.preventDefault();
//			$(this).children('ul').stop(false, true).slideUp('100');
//	});
	
	
	
	////
	// add '_blank' to external links
	//
//	$('a[href^="http"]:not([href*="'+location.hostname+'"])').each(function() {
//		var orgTitle = $(this).attr("title");
//		
//		$(this).attr({'target': "_blank", 'title': orgTitle+" [new window]"});
//		$(this).append('<span class="out">^</span>');
//	});
	
	////
	// add 'target="_blank"' to links to PDFs
	//
	$('a[href$=".pdf"]').each(function() {
		var orgTitle = $(this).attr("title");
		
		$(this).attr({'target': "_blank", 'title': orgTitle+" [.PDF]"});
		$(this).addClass('pdf');
	});
	
	
	
	////
	// change mm image via ajax after a period of time
	
	if($('body.philanthropy').length)
	{
		var auto_refresh = setInterval(function(){
			
			$('#mm').load('http://researchaustraliaphilanthropy.org/medmember.html', function() {
				//alert('who0hoo load');
			});
			
		}, 400000);
	}
	else
	{
		var auto_refresh = setInterval(function(){
			
			$('#mm').load('http://researchaustralia.org/dev/medmember.html', function() {
				//alert('who0hoo load');
			});
			
		}, 400000);
	}
	
	
	
	
	
	////
	// RAP Banner flash
	//
	if($('#RAPbanner').length)
	{
		$('#RAPbanner').flash({
	            'src':'http://researchaustralia.org/philanthropy_assets/media/RAP-Banner.swf',
	            'width':'424',
	            'height':'54',
	            'allowfullscreen':'false',
	            'allowscriptaccess':'always',
	            'wmode':'transparent',
	            'flashvars': {
	            'clickTAG':'http://researchaustraliaphilanthropy.org/linkage-program-and-services/services/online-brochure.html'
	            }
	        });
	}
	
	
	
	
	/////
	// ssb slide show box
	//
	if($('#ssb').length)
		{
		var sswidth = 429;
		
		var ssn = ($('#ssb .strip').children().length)-1; //number of slides -1
		var strip = $('#ssb .strip');
		
		$('#ssb').click(function(event) {
			if(!$('#ssb').hasClass('clicked'))
			{
				$('#ssb').addClass('clicked');
				$('#ssb .clickshield').hide();
			}
		});
		
		
		
		////
		// auto slide
		//
		var auto_slide_refresh = setInterval(function(){
			if(!$('#ssb').hasClass('clicked'))
			{
				if(strip.position().left <= -((ssn*sswidth)-ssn))
				{
					strip.animate({
					    left: '0'
					    }, 'fast');
				}
				else
				{
					strip.animate({
					    left: '-=429'
					    });
				 }
			}
		}, 5000);
		
		////
		// Click shield
		//
		$('#ssb .clickshield').click(function(event) {
			$(this).hide();
		});
		
		////
		// next arrow ssb
		//
		$('#ssb .next').click(function(event) {
			event.preventDefault();
			
			//alert('current position'+strip.position().left);
			
			if(strip.position().left <= -((ssn*sswidth)-ssn))
			{
				strip.animate({
				    left: '0'
				    }, 'fast');
			}
			else
			{
				strip.animate({
				    left: '-=429'
				    });
			 }
		});
		
		////
		// previous arrow ssb
		//
		$('#ssb .prev').click(function(event) {
			event.preventDefault();
			
			//alert('current position'+strip.position().left);
			
			if(strip.position().left >= 0)
			{
				strip.animate({
				    left: -(ssn*sswidth)
				    }, 'fast');
			}
			else
			{
				strip.animate({
				    left: '+=429'
				    });
			 }
		});
	}
	
	////
	// meltwater feed
	//
	if($('#mw').length)
	{
	var MWoptions = {
			'showThumbs':true
			};
			
		//click
		$('#mw').click(function(event) {
			if(!$('#mw').hasClass('clicked'))
			{
				$('#mw').addClass('clicked');
				
				$('#mwslide .dash').css('top','0');
				  
				$('#mwslide').overscroll(MWoptions);
			}
		});
	
	var mwh = $('#mwslide .dash').height() - $('#mwslide').height();
	
	var mwItem = $('#mwslide .item').outerHeight();
		
		var mw_refresh = setInterval(function(){
			if(!$('#mw').hasClass('clicked'))
			{
				if($('#mwslide .dash').position().top <= -mwh)
				{
					$('#mwslide .dash').animate({
						top: '0'
					});
				}
				else
				{
					//alert(-mwItem);
					$('#mwslide .dash').animate({
						top: '-='+mwItem
					});
				}
			}		  
		}, 4000);
		
		
		
	}
	
	
	
	
	
	
	if($('#raplive').length)
	{
		var TAoptions = {
					'maxCharacterSize': 140,
					'originalStyle': 'originalDisplayInfo',
					'warningStyle': 'warningDisplayInfo',
					'warningNumber': 30,
					'displayFormat': '#left Characters Left'
			};
			$('#raplive textarea').textareaCount(TAoptions);
			
			
			var OSoptions = {
				'showThumbs':false
				};
			$('#raplive .quip-comment-list').overscroll(OSoptions);
		
		if(window.location.hash == "")
		{
		$('#addform').hide();
		}
		else
		{
		$('#addpost').hide();
		}
		
		$('#addpost').click(function(event){
			event.preventDefault();
			$(this).hide();
			$('#addform').slideDown();
			
		});
		
		$('#addform .close').click(function(event){
			event.preventDefault();
			$('#addform').slideUp();
			
			$('#addpost').show();
		});
	}
	
	if($('body#home').not('.philanthropy').length)
	{
		$('#mw .item:last').addClass('last');
		
		$('#nnb').hide();
		
		$('#mw .nnb').click(function(event){
			event.preventDefault();
			$('#mw').hide();
			$('#nnb').show();
		});
		
		$('#nnb .mw').click(function(event){
			event.preventDefault();
			$('#nnb').hide();
			$('#mw').show();
		});
	}
	
	if($('#eventreg').length)
	{
		$('#eventreg form').validate({
		  rules: {
		    regfirstname: {
		      required: true,
		      minlength: 2
		    },
		    reglastname: {
		      required: true,
		      minlength: 2
		    }
		  }
		});
		
	}
	
	
	//
	// validate the donations form
	//
	if($('#donationform').length)
	{
		//use validation
		$('#donationform').validate({
			rules: {
			  cardHolderName: {
			    required: true,
			    minlength: 2,
			    maxlength: 50
			  },
			  email: {
			    required: true,
			    email: true,
			    minlength: 2,
			    maxlength: 50
			  },
			  amount: {
			    required: true,
			    number: true,
			    maxlength: 11,
			    range: [1, 99999999.99]
			  }
			}
		});
		
		//
		// decimal must be present
		// adds '.00' if no decimal is present
		//
		$('#amount').blur(function(){
			dollar =$('#amount').val();
			if(dollar.length >= 1 && dollar%1 == 0)
			{
				if(dollar.lastIndexOf('.')<0)
				{
					$('#amount').val(dollar+'.00'); 
				}
			}
		});
		
		
	}
	
	
	///
	// Rich text editor - WYSIWYG
	//
	if($('.rtearea').length)
	{
	    jQuery('.rtearea-').cleditor({
	              width:        500, // width not including margins, borders or padding
	              height:       250, // height not including margins, borders or padding
	              controls:     // controls to add to the toolbar
	                            "bold italic underline strikethrough subscript superscript | " +
	                            "style | color removeformat | bullets  " +
	                            " | undo redo | " +
	                            "link unlink | cut copy pastetext | source",
	              colors:       // colors in the color popup
	                            "FFF FCC FC9 FF9 FFC 9F9 9FF CFF CCF FCF " +
	                            "CCC F66 F96 FF6 FF3 6F9 3FF 6FF 99F F9F " +
	                            "BBB F00 F90 FC6 FF0 3F3 6CC 3CF 66C C6C " +
	                            "999 C00 F60 FC3 FC0 3C0 0CC 36F 63F C3C " +
	                            "666 900 C60 C93 990 090 399 33F 60C 939 " +
	                            "333 600 930 963 660 060 366 009 339 636 " +
	                            "000 300 630 633 330 030 033 006 309 303",    
	              fonts:        // font names in the font popup
	                            "",
	              sizes:        // sizes in the font size popup
	                            "",
	              styles:       // styles in the style popup
	                            [["Paragraph", "<p>"], 
	                            ["Header 3", "<h3>"],  ["Header 4","<h4>"]],
	              useCSS:       false, // use CSS to style HTML when possible (not supported in ie)
	              docType:      // Document type contained within the editor
	                            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
	              docCSSFile:   // CSS file used to style the document contained within the editor
	                            "", 
	              bodyStyle:    // style to assign to document body contained within the editor
	                            "margin:4px; font:10pt Arial,Verdana; cursor:text"
	            });
	}
	
	
	///
	// multiselect for related questions
	//
	if($('.related_questions_select').length)
	{
		$('.related_questions_select input').change(function(){
			var csvstr = ''
			$('.related_questions_select input:checked').each(function(){
				csvstr += $(this).val()+','
			});
	
			csvstr = csvstr.slice(0,-1);
			$('#related_questions').val(csvstr);
		});
	
	}
	
	///
	// areYouSure
	//
	// dialog confirmation
	//
	if($('.areYouSure').length)
	{
		$('.areYouSure').click(function(event){
			event.preventDefault();
			if(confirm('Are You Sure? '))
			{
				window.location.href = $(this).attr("href")+'&amp;refadd='+escape(window.location.href);
			}
		});
	}
	
	
	
	///
	// popup box for re:search flash thing
	//
	if($('#ReSearch').length)
	{
		$('#ReSearch').hide();
		
		$('#lresearch, #lsearchimg').click(function(event){
			event.preventDefault();
			$('#ReSearch').fadeIn();
		});
		
		
		$('#ReSearch .flashbox').flash({
	            'src':'http://researchaustralia.org/reSearch-ra.swf',
	            'width':'960',
	            'height':' 600',
	            'allowfullscreen':'false',
	            'allowscriptaccess':'always',
	            'wmode':'transparent',
	            'flashvars': {
	            	}
	        });
	        
	        
	        
	       $('#ReSearch .theclosebtn').click(function(event){
	       		event.preventDefault();
	       		$('#ReSearch').hide();
	       	}); 
	        
	}
	
	
	
	
	////
	// msplash modal splash for showing quotes
	//
	if($('#msplash').length)
	{
		var quotesar = new Array('<h1>Research &hellip; not an option extra <span class="by">Adam (Student)</span></h1>',
				'<h1>Medical research is the soundest investment any nation can make. <span class="by">Geraldine (Manager)</span></h1>', 
				'<h1>A budget cut now would result in years of hard work going to waste. <span class="by">Mary (HR Manager)</span></h1>',  
				'<h1>Cutting the budget is a bandaid fix, not an efficient long term strategy to save money. <span class="by">Tamara (Scientist)</span></h1>',
				'<h1>no meaningful improvements are made without research <span class="by">Janne (Nurse)</span></h1>',
				'<h1>Cutting funding would be akin to murder! <span class="by">Melinda (Researcher)</span></h1>',
				'<h1>To cut &hellip; would be  a short-sighted, retrogressive and detrimental step.</h1>', 
				'<h1>Australia cannot afford to cut funding to medical research.</h1>',
				'<h1>Cutting medical research funding = cutting life belt of the future!</h1>',
				'<h1>Cuts to medical research are short sighted and damaging to the country.</h1>',
				'<h1>Please dont take away funding. It will affect everyone.</h1>',
				'<h1>Surely Medical Research is more important than an NBN!!</h1>',
				'<h1>Funding cuts will threaten the future health every Australian.</h1>');
				var randno = Math.floor ( Math.random() * quotesar.length );
				
				var q = quotesar[randno];
				
				$('#msplash h1').replaceWith(q);
		
		$('#msplash').css({'height':$(document).height()}).fadeIn('fast');
		$('#msplash').click(function(event){
			event.preventDefault();
			$(this).hide();
			});
		$('#msplash').delay(5000).fadeOut('slow');
	}
	
	
	
	
	////
	// update price if member selected for event form
	//
	if($('#regproducts').length)
	{

	var text;
	var rel;
	var rad;
	var relems = new Array();
	var etext = new Array();
	
	$('input[name=regmember]:radio').change(function(){
		rad = $(this);
		
		
		$('#regproducts option').each(function(i){
			if(i!=0)
			{
				
				text = $(this).text();
				
				etext = text.split(') ');
				
				rel = $(this).attr('rel');
				
				relems = rel.split('|');
				
				if(rad.val() == 'yes')
				{
					$(this).text(etext[0]+') '+relems[0]);
				}
				else
				{
					$(this).text(etext[0]+') '+relems[1]);
				}
				
			}
		});
	});
	
	
	
	
//	var text;
//	var rel;
//	var etext = new Array();
//		$('input[name=regmember]:radio').change(function(){
//			
//				$('#regproducts option').each(function(i){
//				
//					if(i!=0)
//					{
//						
//						text = $(this).text();
//						rel = $(this).attr('rel');
//						etext = text.split(') ');
//						
//						$(this).text(etext[0]+') '+rel);
//						$(this).attr('rel', etext[1])
//						
//					}
//
//
//				});
//		});
	}
	
});
