$(document).ready(function(){
				   
	/**************************************************
			APPLY OVERLAYS TO IMAGES
	****************************************************/
	//PORTAL OVERLAYS
	function applyPortalThumbOverlays(){
		$('.portal_browse_image_item .portal_thumb_area a').prepend('<img src="/images/portal-thumb-image.png" class="tab_thumb_item_overlay" style="margin:0 0 0 0;display:block;position:absolute" />')
		$('.portal_browse_video_item .portal_thumb_area a').prepend('<img src="/images/portal-thumb-video.png" class="tab_thumb_item_overlay" style="margin:0 0 0 0;display:block;position:absolute" />')
	}
	
	function applyTabThumbOverlays(){
		$('.tab_thumb_items').each(function(){
			if($(this).attr('type') == 'images'){
				$(this).prepend('<img src="/images/activity-thumb-image.png" class="tab_thumb_item_overlay" style="margin:8px 0 0 5px;display:block;position:absolute" />');	
			}else if($(this).attr('type') == 'video'){
				$(this).prepend('<img src="/images/activity-thumb-video.png" class="tab_thumb_item_overlay" style="margin:8px 0 0 5px;display:block;position:absolute" />');	
			}
		})
	}
	applyPortalThumbOverlays();
	applyTabThumbOverlays();
	
	/************************************
			FILTER GRID ANIMATIONS
	************************************/
	//LARGE FILTER BUTTON HOVER FUNCTION
	$(".big_browse_btns").hover(function(){
			$(this).css({'backgroundPosition':'bottom'});
		},function(){
			if($(this).hasClass('active_big_browse'))
			{}
			else{$(this).css({'backgroundPosition':'top'});}
		}
	)

	//SMALLER FILTER BUTTON HOVER FUNCTION
	$(".small_browse_btns").hover(function(){
			$(this).css({'backgroundPosition':'bottom'});}
		,function(){
			if($(this).hasClass('active_small_browse'))
			{}
			else{$(this).css({'backgroundPosition':'top'});}
		}
	)

	/*************************************************
			MAIN NAVIGATION DROPDOWN
	***************************************************/
	$('.main_nav_dropdown_body').css({'opacity':'.97'});
	
	
	
	
	/******************************
     	Newsletter	Signup Form
	******************************/
	//EMAIL VALIDATOR FUNCTION
	function isValidEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);}
	
	//NEWSLETTER ON SUBMIT FUNCTION
	$("#newsletter_signup_form").submit(function(){
		var temail = $("#newsletter_email").val();
		var zipcode = $("#newsletter_zip").val();
		var tgender = $('input[name=gender]:checked').val();
		var birthYear = $('#age_input').val();
		var errorLog = new Array;		
		var myDate = new Date();
		//alert(myDate.getMonth());
		if(temail == ''){
			errorLog.push('Email Address');
			$('#email_tooltip').fadeIn();
		}else{
			if(isValidEmail(temail)){
				$('#email_tooltip').fadeOut();
				$('#invalidemail_tooltip').fadeOut();
			}else{
				$('#invalidemail_tooltip').fadeIn();
				errorLog.push('a valid Email');
			;}
		};
		if(zipcode == '' || zipcode == 'Zip Code'){
			errorLog.push('Zip Code');
			$('#zip_tooltip').fadeIn();
		}else{
			$('#zip_tooltip').fadeOut();
		};
		if(tgender == undefined){
			errorLog.push('Gender');
			$('#gender_tooltip').fadeIn();
		}else{
			$('#gender_tooltip').fadeOut();
		};
		if(birthYear == ''){
			errorLog.push('Year of Birth');
			$('#year_tooltip').fadeIn();
		}else{
			$('#year_tooltip').fadeOut();			
			if(myDate.getFullYear() - birthYear <= 18 && ($("#under_18_month").val() == '' || $("#under_18_day").val() == '')){	
				if($("#under_18_month").val() == ''){errorLog.push('Birth Month');	$('#month_tooltip').fadeIn();}else{$('#month_tooltip').fadeOut();};
				if($("#under_18_day").val() == ''){errorLog.push('Birth Day');$('#day_tooltip').fadeIn();}else{$('#day_tooltip').fadeOut();};
				
			}		
		};

		if(errorLog.length != 0){
			return false;
			//$('.newsletter_submit_message').replaceWith('<div class="newsletter_submit_message" style="display:none;padding:10px;font-variant:small-caps">sorry you forgot to fill in '+errorLog.join(', ')+'</div>');
			//$('.newsletter_submit_message').fadeIn(350);
		}else{
			$('.newsletter_tooltip').fadeOut();			
			$.post("/dynamic/sendSignup.php",{ email : temail, zip : zipcode, gender : tgender, age : birthYear },
			function(data){
				$('#newsletter_signup_form').replaceWith('<div id="newsletter_signup_form" style="display:none;padding:10px;font-variant:small-caps">'+data+'</div>');
				$('#newsletter_signup_form').fadeIn(350);
				}
			);			
		}	
		return false;
	 });
	
	//USED FOR AGE SELECTOR MENU
	var ageSelected = false;
	var newletterOpen = false;
	$('.main_nav_item').hover(
		function(){
			if($(this).attr('id') != 'subscribe_link'){
				$(this).children('.main_nav_dropdown_children').fadeIn(100);
			}
		},
		function(){
			if($(this).attr('id') == 'subscribe_link'){
				return false;
			}else{
				//This is to check whether or not the usere is using the age dropdown selector
				if(ageSelected == false){
					$(this).children('.main_nav_dropdown_children').fadeOut(100);
				}else{
					ageSelected = false;
				}
			}			
		}	
	)
	//SHOW SIGNUP FORM ON MOUSE RELEASE
	$("#subscribe_link").mouseup(function(){
		$(this).children('.main_nav_dropdown_children').fadeIn(100);
		
		//HIDE SIGNUP FORM ON CLICK OUTSIDE OF FORM
		$(document).click(function(){
			$("#subscribe_link .main_nav_dropdown_children").slideUp(150);
		});
		$('#subscribe_link .main_nav_dropdown_children').click(function(e){
			e.stopPropagation();
		});
		$('#subscribe_link').click(function(e){
			e.stopPropagation();
		});
	})

	//When user clicks on a year in the age selector it records year to variable 'selectedAge'	
	$("#age_selector li").click(function(){

		selectedAge = parseInt($(this).text());
		var d = new Date();
		var ageDifference = d.getFullYear() - selectedAge;
		$('#birth_year_selector').text(selectedAge);
		if(ageDifference <= 18){
			$('#under_18_box').show();
			
		}else{
			$('#under_18_box').hide();
		}
		$('#age_input').val(selectedAge);
		
		$(this).parent().parent().parent().fadeOut(200);
		ageSelected = true;
	})
	$("#birth_year_selector").click(function(){
		$(".age_selector_dropdown_children").fadeIn(200);
	})
	
	
	$("#month_selector li").click(function(){
		selectedMonth = $(this).text();
		var d = new Date();
		$('#birth_month_selector').text(selectedMonth);		
		$('#under_18_month').val(selectedMonth);		
		$(this).parent().parent().parent().fadeOut(200);
		ageSelected = true;
	})
	$("#birth_month_selector").click(function(){
		$(".month_selector_dropdown_children").fadeIn(200);
	})
	
	$("#date_selector li").click(function(){
		selectedDate = $(this).text();
		var d = new Date();
		$('#birth_date_selector').text(selectedDate);		
		$('#under_18_day').val(selectedDate);		
		$(this).parent().parent().parent().fadeOut(200);
		ageSelected = true;
	})
	$("#birth_date_selector").click(function(){
		$(".date_selector_dropdown_children").fadeIn(200);
	})
	
	
	/*************************************************
				SGC QA FORM
	**************************************************/
	$('#QAinputName').focus(function(){checkQAName();});
	$('#QAinputName').blur(function(){checkQANameBlur();});
	$('#QAinputEmail').focus(function(){checkQAEmail();});
	$('#QAinputEmail').blur(function(){checkQAEmailBlur();});
	$('#QAinputLoc').focus(function(){checkQALocal();});
	$('#QAinputLoc').blur(function(){checkQALocalBlur();});
	$('#QAinputQuest').focus(function(){checkQAQuest();});
	$('#QAinputQuest').blur(function(){checkQAQuestBlur();});
	function checkQAName(){if($('#QAinputName').attr('value') == 'Your Name'){$('#QAinputName').attr('value','');}}
	function checkQANameBlur(){if($('#QAinputName').attr('value') == ''){$('#QAinputName').attr('value','Your Name');}}
	function checkQAEmail(){if($('#QAinputEmail').attr('value') == 'Your Email Address'){$('#QAinputEmail').attr('value','');}}
	function checkQAEmailBlur(){if($('#QAinputEmail').attr('value') == ''){$('#QAinputEmail').attr('value','Your Email Address');}}
	function checkQALocal(){if($('#QAinputLoc').attr('value') == 'Your State and/or City'){$('#QAinputLoc').attr('value','');}}
	function checkQALocalBlur(){if($('#QAinputLoc').attr('value') == ''){$('#QAinputLoc').attr('value','Your State and/or City');}}
	function checkQAQuest(){if($('#QAinputQuest').attr('value') == 'Submit a question or YouTube link to your video question'){$('#QAinputQuest').attr('value','');}}
	function checkQAQuestBlur(){if($('#QAinputQuest').attr('value') == ''){$('#QAinputQuest').attr('value','Submit a question or YouTube link to your video question');}}
	
	function submitResults(){
		if($('#qaCheckbox:checked').val() != null){
			theName = $('#QAinputName').attr('value');
			theEmail = $('#QAinputEmail').attr('value');
			theLocal = $('#QAinputLoc').attr('value');
			theQuest = $('#QAinputQuest').attr('value');
			
			var errorLog = new Array();
			
			if((theName == '') || (theName == 'Your Name')){errorLog.push(" name")}						
			if((theEmail == '') || (theEmail == 'Your Email Address')){errorLog.push(" email")}
			if((theLocal == '') || (theLocal == 'Your State and/or City')){errorLog.push(" location")}
			
			if(errorLog.length > 0){
				$('#QAError').replaceWith('<p class="hi_lite" id="QAError">Sorry you forgot to fill in your '+errorLog+'!</p>');					
			}else{
				$.getJSON('/dynamic/submitTextQuestion.php',{name:theName,email:theEmail,location:theLocal,question:theQuest},function(json){
					if(json.status == 'error'){							
						$('#QAError').replaceWith('<p class="hi_lite" id="QAError">Sorry there were errors in your submission, please try again</p>');
					}
					else{							
						$('#QAError').replaceWith('<p class="hi_lite" id="QAError">Thank you, your submission was successful!</p>');
					}
				});
			}
		}
		else{
			$('#QAError').replaceWith('<p class="hi_lite" id="QAError">Sorry You need to check the checkbox!</p>');
		}
		return false;										
	};
	$('#submitQA').live('click',function(){
		submitResults();								 
	});
	
	/********************************************
			FEEDBACK FORM
	*********************************************/
	$('#feedback_link_btn').live('click',function(){$('#feedback_footer_form').fadeIn(150);});
	$('#feedback_close_btn').live('click',function(){$('#feedback_footer_form').fadeOut(150);})
	$('#feedback_email').focus(function(){
		if($(this).attr('value') == 'Email'){$(this).attr('value','');}
	})
	$('#feedback_email').blur(function(){
		if($(this).attr('value') == ''){$(this).attr('value','Email');}			   
	});
	$('#feedback_text').focus(function(){
		if($(this).attr('value') == 'Please type your feedback here'){$(this).attr('value','');}
	})
	$('#feedback_text').blur(function(){
		if($(this).attr('value') == ''){$(this).attr('value','Please type your feedback here');}
	})
	$("#feedback_submit").click(function(){
		submitFeedback();									
	})
		  
	function checkFeedbackEmail(){if($('#feedback_email').attr('value') == 'Your Email Address'){$('#feedback_email').attr('value','');}}
	function checkFeedback(){if($('#feedback_text').attr('value') == 'Please type your feedback here'){$('#feedback_text').attr('value','');}}
	function submitFeedback(){
		if($('#feedbackCheckbox:checked').val() != null){
			theEmail = $('#feedback_email').attr('value');
			theFeedback = $('#feedback_text').attr('value');
			var errorLog = new Array();
			if((theEmail == '') || (theEmail == 'Your Email Address')){errorLog.push(" email")}
				if(errorLog.length > 0){
					$('#FError').replaceWith('<p class="hi_lite" id="FError">Sorry you forgot to fill in your '+errorLog+'!</p>');
				}else{
					$.getJSON('/dynamic/sendFeedback.php',{email:theEmail,feedback:theFeedback},function(json){
						if(json.status == 'error'){
							$('#FError').replaceWith('<p class="hi_lite" id="FError">Sorry there were errors in your feedback submission, please try again</p>');
						}
						else{
							$('#FError').replaceWith('<p class="hi_lite" id="FError">Thank you, your feedback submission was successful!</p>');
						}
					});
				}
			}else{
				$('#FError').replaceWith('<p class="hi_lite" id="FError">Sorry You need to check the checkbox!</p>');
			}
			return false;
		};
	
	
	
	/*****************************************
			NARROW RESULTS DROPDOWN
	*******************************************/
	$("#narrow_results_top").click(function(){
		$('#narrow_results_bottom').slideDown(150,function(){
			$(this).mouseleave(function(){
				$('#narrow_results_bottom').slideUp(150);
			})
		});
	})

	
	/*****************************************
			EPISODE BUTTONS ON CLICK
	*******************************************/
	$(".episode_season_btn").click(function(){
		var theTarget = $(this).attr('href');
		//alert(theTarget);
		$('.episode_season_item').css({'backgroundPosition':'top'});
		$(this).children('.episode_season_item').css({
			'backgroundPosition': 'bottom'
		});
		$('.episode_listing').after('<div class="loading_screen" style="width:100pxpx;height;100px;padding:50px 0 50px 430px;"><img src="/images/loade_img.gif" alt="loading..." /></div>')
		$('.episode_listing').slideUp(750,function(){
			$(".episode_listing").replaceWith('<div class="episode_listing" style="display:none"><br /> <div class="episodes_col_1"></div><div class="episodes_col_2"></div></div>');
			evenOdd = true;
			$.getJSON(theTarget,function(data){
				$.each(data.items, function(i,item){
					if(evenOdd == true){
						$(".episodes_col_1").append('<div class="episode_list_item"><div class="episode_list_img"><a href="'+item.url+'"><img width="136" height="86" src="'+item.activityThumb+'" alt="'+item.title+'" /></a></div><div class="episode_list_description"><a href="'+item.url+'" class="hi_lite"><b>Episode '+ parseInt(i+1) +' - '+item.title+'</b></a><p>Original air date: '+item.airDate+'</p><br /><p>'+item.intro+'</p><a href="#" class="hi_lite episode_list_summary_btn"  onclick="return false;">FULL SUMMARY</a><p class="episode_list_summary">'+item.summary+'</p></div><div class="clear"></div><hr class="tab_hr" /></div>');
						evenOdd = false;
					}else{
						$(".episodes_col_2").append('<div class="episode_list_item"><div class="episode_list_img"><a href="'+item.url+'"><img width="136" height="86" src="'+item.activityThumb+'" alt="'+item.title+'" /></a></div><div class="episode_list_description"><a href="'+item.url+'" class="hi_lite"><b>Episode '+ parseInt(i+1) +' - '+item.title+'</b></a><p>Original air date: '+item.airDate+'</p><br /><p>'+item.intro+'</p><a href="#" class="hi_lite episode_list_summary_btn"  onclick="return false;">FULL SUMMARY</a><p class="episode_list_summary">'+item.summary+'</p></div><div class="clear"></div><hr class="tab_hr" /></div>');
						evenOdd = true;
					}
					
					//$(".episode_list_item:odd").css({"float":"right",'clear':'right'});
					//$(".episode_list_item:even").css({"float":"left",'clear':'left'});
					$(".episodes_col_1").css({'float':'left','width':'450px'})
					$(".episodes_col_2").css({'float':'right','width':'450px'})
				})
				$('.loading_screen').fadeOut(150,function(){
				$('.episode_listing').slideDown(700);
			});
			})
			
			
		});
	})
	
	$(".episode_list_summary_btn").live('click', function(){
		thisBtn = $(this);
		$(".episode_list_summary_btn").parent().parent().css({'background':'none'})
		$('.episode_list_summary').not($(thisBtn).parent().children('.episode_list_summary')).slideUp(250,function(){
			$(thisBtn).parent().children('.episode_list_summary').slideDown(350);
			$(thisBtn).parent().parent().css({'background':'#e5e5eb'})
		});
	})

	
	/*************************************************
			EPISODE LISITING DESCRIPTION TOGGLER
	*************************************************/
	$(".episode_selection_description").hide();
	$(".episode_toggle_down").hide();
	
	$('.toggle_episode_link').click(function(){
		$(this).children('.episode_toggle_up').toggle();
		$(this).children('.episode_toggle_down').toggle();
		$(this).parent().parent().next(".episode_selection_description").slideToggle();
		return false;
	})
	$(".episode_show_all").click(function(){
		$(".episode_selection_description").slideDown();
		$('.episode_toggle_up').hide();
		$('.episode_toggle_down').show();
		return false;
	})
	$(".episode_hide_all").click(function(){
		$(".episode_selection_description").slideUp();
		$('.episode_toggle_up').show();
		$('.episode_toggle_down').hide();
		return false;
	})
		
		
	/*****************************************
		      SUGGEST A TAG POP UP
	******************************************/
	$('.suggest_tag_link').click(function(){
		$('.suggest_tag_popup').fadeIn();
	})
	
	$('.tag_cat_select_parent').click(function(){
		$('.tag_cat_select_children').fadeIn();
		return false;
	})
	$('.tag_cat_select_children').mouseleave(function(){
		$(this).fadeOut();
	})
	$(".popup_close_btn").click(function(){
		$('.suggest_tag_popup').fadeOut();
		return false;
	})
	
	
	/*****************************************
			TAB SYSTEM
	*******************************************/
	var current_tab;
	function initializeTabs(){
		current_tab = $('body').find('.active_tab').children('.tab_link').attr('id');
		$('.tab_contents[rel!='+current_tab+']').hide();
		$('#'+current_tab+' .tab_title_inactive').hide();
		$('#'+current_tab+' .tab_title_active').show();
	}
	initializeTabs();
	
	$('.tab_link').live('click',function(){
		$("#" + current_tab).parent().removeClass('active_tab');
		$(this).parent().addClass('active_tab');
		$('.tab_title_inactive').show();
		$('.tab_title_active').hide();
		$(this).children('.tab_title_inactive').hide();
		$(this).children('.tab_title_active').show();
		current_tab = $(this).attr('id');
		$('.tab_contents[rel!='+current_tab+']').fadeOut('fast',function(){$('.tab_contents[rel='+current_tab+']').fadeIn('slow');});
		return false;
	})
	
	/******************************************************
			PREDICTIVE SEARCH
	*******************************************************/
	var predictiveOpen = false;
	var keyPressDelay = false;
	var firstKeyDown = true;
	var liTracking = 0;
	var liArray = new Array();	
	var liLength = 0;
	
	$('#main_nav_search').focus(function(){if($(this).val() == 'SEARCH'){$(this).val('');}})
	$('#main_nav_search').blur(function(){if($(this).val() == ''){$(this).val('SEARCH');}})
	$('#newsletter_email').focus(function(){if($(this).val() == 'Email Address'){$(this).val('');}})
	$('#newsletter_email').blur(function(){if($(this).val() == ''){$(this).val('Email Address');}})
	$('#newsletter_zip').focus(function(){if($(this).val() == 'Zip Code'){$(this).val('');}})
	$('#newsletter_zip').blur(function(){if($(this).val() == ''){$(this).val('Zip Code');}})
	
	$('#search_bar_go').click(function(){
		$("#main_nav_search_bar").submit();								 
	})
	$('#see_results_btn').click(function(){
		$("#main_nav_search_bar").submit();								 
	})
	
	$("#main_nav_search").keyup(function(event){	
		if(event.keyCode == 38)
		{
			if(liTracking >= 2){
				liTracking--;
				theLi = "#predictive_search_body ul li:nth-child("+liTracking+")";
				$('#predictive_search_body ul li').css({'background':'#d8d8d8'});
				$(theLi).css({'background':'url(/images/predictive-hover-bg.gif) no-repeat'});
			}
		}else if(event.keyCode == 40){
			if(liTracking <= liLength-1){
				liTracking++;

				theLi = "#predictive_search_body ul li:nth-child("+liTracking+")";
				$('#predictive_search_body ul li').css({'background':'#d8d8d8'});
				$(theLi).css({'background':'url(/images/predictive-hover-bg.gif) no-repeat'});
			}
		}else if(event.keyCode == 13){
			if(liTracking != 0)
			{
				//$('#main_nav_search').val($(theLi + ' a').text());
				//$("#main_nav_search_bar").submit();
				window.location= $(theLi + ' a').attr('href');
			}else{
				$("#main_nav_search_bar").submit();
			}
			
		}else{
			if(keyPressDelay == false){
				liTracking = 0;
				liLength = 0;
				firstKeyDown = true;
				predictiveType = $("#main_nav_search").val();
				//alert(predictiveType);
				$("#predictive_search_box").slideDown(200);
				predictiveOpen = true;
				$("#predictive_search_body ul").replaceWith("<ul> </ul>")
				$.getJSON("/dynamic/tagSearch.php?value="+predictiveType,function(data){
					var resultCount = 25;
					if (data.length < 25){resultCount = data.length;}
					for(i = 0;i<resultCount;i++)
					{
						$('#predictive_search_body ul').append('<li class="predictive_links"><a href="'+data[i].url+'" style="display:block;width:100%;height:100%;line-height:18px">'+data[i].value+'</a></li>');
						liLength++;
					}	
					
				})
					


				keyPressDelay = true;
				timer = setTimeout(function(){
					keyPressDelay = false;	
				},550)
			}
		}
	})
	
	$('body').mousedown(function(e){
		if(predictiveOpen == true)
		{
			var mouseXpos = e.pageX;
			var mouseYpos = e.pageY;
			var predictiveXInit = $("#predictive_search_box").offset().left;
			var predictiveXEnd = predictiveXInit + $("#predictive_search_box").width();
			var predictiveYInit = $("#predictive_search_box").offset().top;
			var predictiveYEnd = predictiveYInit + $("#predictive_search_box").height();
			
			if(mouseXpos > predictiveXInit && mouseXpos < predictiveXEnd && mouseYpos > predictiveYInit && mouseYpos < predictiveYEnd){
				return false;
			}else{
				$("#predictive_search_box").slideUp(200);
			}
		}
		
	});
	
	/*EXTRAS PAGE BUDDY ICON TABS*/
		$(document).ready(function(){
				var openTab = $('.active_icon_tab').attr('href');
				$(openTab).fadeIn();
				
				$(document).find('.active_icon_tab').css({'backgroundPosition':'bottom'});
				
				$('.icon_tabs').click(function(){
					$(openTab).hide();
					openTab = $(this).attr('href');
					$(openTab).show();
					$(document).find('.active_icon_tab').removeClass('active_icon_tab');
					$(this).addClass('active_icon_tab');
					$('.icon_tabs').css({'backgroundPosition':'top'});
					$(this).css({'backgroundPosition':'bottom'});
					return false;
				})
				
			})
})
	

