
/* JQUERY */

	$(function(){
	
		/* LANDING PAGE */
			var isLanding = $('#is-landing-page');
			var isSafetyLanding = $('#landing-hero-safety');
			if((isLanding).length) {
				$('body').addClass('landing-page');
				if((isSafetyLanding).length) { $('body').addClass('landing-page-safety'); }
			}
		
		/* TOGGLE INPUT LABELS */
			var getLabel = $('form#searchform label');
			$(getLabel).each(function(){
				var getInputVal = $(this).next().val();
				if(getInputVal !== ""){ $(this).hide(); } else {  }
			});
			
			var getInput = $('form#searchform input');
			$(getInput).each(function(){
				$(this).focus(function(){  
					$(this).prev('label').hide(); 
					});
				$(this).blur(function(){ 
					if (($(this).attr('value')) === ""){ 
						$(this).prev('label').show(); } 
					});
			});
			
		
		/* APPEND COLON TO BREADCRUMB ANCHORS */
			//$('#breadcrumb li:not(:last)').append(':'); 
			
		
		/* ADD HEROS TO HISTORY AND DEMO HOUSE PAGES */
			if ($('#history-panel-2008').length) { 
				$('#page-left').css('background','url(/wp-content/themes/CharliesHouse/images/heros/hero-history.jpg) -5px 10px no-repeat'); 
				$('#history-panel-2008 p:first').css('padding-right','240px');
			}
			if ($('#demohouse-gallery').length) { 
				$('#page').css('background','url(/wp-content/themes/CharliesHouse/images/backgrounds/bkg-demohouse.jpg) 85px 160px no-repeat');
				$('#page-left h2').css('padding-bottom','275px');
			}
			
			
		/* PARSE ALERT FEED */
			
			$.ajax({
				type: "GET",
				url: "/_assets/resources/alertfeedproxy.php?proxy_url=http://www.cpsc.gov/cpscpub/prerel/prerelchild.xml",
				dataType: "xml",
				success: parseXml
			}); 	
			
			function parseXml(xml) {
			  
			  $(xml).find('item').each(function(){
			  	var title = $(this).find('title').text();
			  	var description = $(this).find('description').text();
			  	var link = $(this).find('link').text();
			  	var date = $(this).find('pubDate').text();
			    $("#alert-list ul").append('<li><a href="'+link+'" target="_blank">'+title+'</a><span class="rssdate">'+date+'</span><span class="rssdesc">'+description+'</span></li>');
			  });
			  
			}
			
			
			
			
			
		/* REMOVE <BR> FROM FEED PULL */
			if ($('#alert-list').length) { $('#alert-list br').each(function(){ $(this).remove(); }); }
			
			
		/* RETURN FALSE ON EMPTY ANCHORS, OPEN OUTSIDE LINKS IN NEW WINDOW 
			$('a[href=""]').bind('click',function(){ return false; });
			var extLink = $('a[href^="http://"]');
			$(extLink).each(function(){
				$(this).attr({ target: "_blank" });
			});*/
			
			
			
		/* CHANGE H2 PADDING IF HERO IS ON PAGE */
			$('img.page-hero').each(function(){ $('h2').css('margin-top','0px'); });
			
			
			
		/* REMOVE BORDERS, VALUES, ETC... */
			$('#pagination-search a:last, #nav-utility a:first, .list-room:last').css('border','none');
			$('#submit-volunteer, #submit-contactus').val('');
			$('.list-room').each(function(){
				var listRoomPs = $('p',this).size();
				var listRoomPsBy2 = ((listRoomPs/2)%1);
				if(listRoomPsBy2 == 0 ){
					$('p:last',this).css('border','none').prev().css('border','none');
				} else { $('p:last',this).css('border','none'); }
			});
			$('.eventdetail-pod:odd').css('margin-right','0px');
			
			
			
		/* REQUIRED FORM FIELDS */
			$('form fieldset.required label').append('<span class="req"> *</span>'); 
			var getReqInputs = $('fieldset.required input');
			var getReqSelects = $('fieldset.required select');
			var getReqTextareas = $('fieldset.required textarea');
		
		
		
		/* VOLUNTEER, SIGN-IN, REGISTER, CONTACT US: VALIDATE AND SUBMIT */
			$('#submit-volunteer,#submit-signin, #submit-register, #submit-contactus').live('click', function(){
				$('.req-message').prev('input').css('background','#fff');
				$('.req-message').remove();
				$(getReqInputs).each(function(){ 
					var getInputValue = $(this).val();
					var getInputName = $(this).attr('rel');
					if( getInputValue === '' ) { 
						var getFormSumitValue = $('#form-submit').val();
						$(this).css('background','#ffc');
						$(this).parent().append('<span class="req-message">Please enter '+getInputName+'</span>');
					} else { }
				});	
				$(getReqSelects).each(function(){ 
					var getSelectValue = $(':selected',this).val();
					var getSelectName = $(this).attr('rel'); 
					if( getSelectValue === '' ) { $(this).parent().append('<span class="req-message">'+getSelectName+'</span>'); }
						 else {  } 
				});	
				$(getReqTextareas).each(function(){ 
					var getTextareaValue = $(this).val();
					var getTextareaName = $(this).attr('rel');
					if( getTextareaValue === '' ) { 
						var getFormSumitValue = $('#form-submit').val();
						$(this).css('background','#ffc');
						$(this).parent().append('<span class="req-message">Please enter '+getTextareaName+'</span>');
					} else { }
				});	
				var getReqMessageCount = $('.req-message').length;
				if (getReqMessageCount === 0){ return true; }
					else { return false; }
			});
		
		var mT = 0;	
			
		/* VIRTUAL HOUSE */
			$('.page-template-virtual-house-php .icon-vhouse').bind('click', function(){
				var getHref = $(this).attr('href');
				if(($(this).hasClass('vhouse-icon-on'))) { 
					$(this).removeClass('vhouse-icon-on');
					$('.vhouse-sect').css('display','none');
					$('#landing').fadeIn(300);
					return false; 
					}
					else {
						$('.vhouse-icon-on').removeClass('vhouse-icon-on');
						$(this).addClass('vhouse-icon-on');
						$('.vhouse-sect').css('display','none');
						$(getHref).fadeIn(300);
						$('.vhouse-list ul.on').removeClass('on');
						$(getHref+' .vhouse-list ul').addClass('on').css('margin-top','0px');
						
						mT = 0;
						
						$('.page-template-virtual-house-php .vhouse-list-up').live('mousedown',function(){
							var getListHeight = $('.vhouse-list ul.on').height();
							var mouseupEvent = setInterval(function() {
									if(mT < 0) {
	   							$('.vhouse-list ul.on').css('margin-top',(mT+5)+'px');
									mT = (mT+5);
									return false 
								} else { return false }
							}, 50);
							$('.page-template-virtual-house-php .vhouse-list-up').live('mouseup',function(){ clearInterval(mouseupEvent); });
						});
						
						
						$('.page-template-virtual-house-php .vhouse-list-down').live('mousedown',function(){
							var getListHeight = $('.vhouse-list ul.on').height();
							var mousedownEvent = setInterval(function() {
								if((mT*-1) < (getListHeight-250)) {
	   							$('.vhouse-list ul.on').css('margin-top',(mT-5)+'px');
									mT = (mT-5);
									return false 
								} else { return false }
							}, 50);
							$('.page-template-virtual-house-php .vhouse-list-down').live('mouseup',function(){ clearInterval(mousedownEvent); });
						
						});
						return false;
					}
				return false;
			});
			
			
		/* DEMO HOUSE GALLERY */
			var isDemoHouse = '#demohouse-gallery';
			if($(isDemoHouse).length){
				var dgh = '#demohouse-gallery';
				var getTitle = $(dgh+' a span:first').html();
				var getImage = $(dgh+' a:first').attr('href');
				$(dgh+' a:first').addClass('on');
				$(dgh).prepend('<img src="'+getImage+'"><div class="clear"></div>');
				$(dgh).prepend('<h4>'+getTitle+'</h4');
				$(dgh+' a').each(function(){
					var getThumb = $(this).attr('href');
					$(this).append('<img src="'+getThumb+'" width="32" height="32" />');
				});
				$(dgh+' a:not(.on)').live('click', function(){
					var getTitle = $('span', this).html();
					var getImage = $(this).attr('href');
					$(dgh+' h4').html(getTitle);
					$(dgh+' img:first').attr('src',getImage);
					$(dgh+' a.on').removeClass('on');
					$(this).addClass('on');
					return false;
				});
				$(dgh+' a.on').live('click', function(){
					return false;
				});
			}
			
			
	});
	
	
	
	
/* JS FUNCTIONS */

	function confirmLogout(){
		var answer = confirm("Are you sure you'd like to log out?");
		if (answer){ 
			eraseCookie('ck-username');
			window.location = "/"; 
		} else{ alert("You have not been logged out"); }
	}
	
