$(document).ready(function(){

	// HERO CYCLER
	var cycleTime = 15000
	var heroTimer = setInterval("nextHero()", cycleTime);

	// PR HIDER
	var prButton = $('#pr_box_button');
	var prText = $('#pr_text');
	prButton.click(function(){
		if(prText.is(':visible'))
			{
			prText.fadeTo('fast',0,function(){
				prText.slideUp('fast');
				prButton.css('background-image','url(http://www.blogsmithmedia.com/www.engadget.com/media/show_full_pr_button.jpg)');
			});
			}
		else
			{
			prText.fadeTo('fast',0).slideDown('fast',function(){
				prText.fadeTo('fast',1);
				prButton.css('background-image','url(http://www.blogsmithmedia.com/www.engadget.com/media/hide_full_pr_button.jpg)');
			});
			}
	});

	// FTS NAV
	var $backBtn = $('#fts-back');
	var $nextBtn = $('#fts-next');
	var $thePort = $('#chart_port');
	var leftMargin = 0;
	var portWidth = 490;
	var barWidth = 35;
	var barAmountMove = 14;
	var movePortOnBar = 7;
	var amountMove = barWidth*barAmountMove;
	var maxMargin = (($('.follow .bar').length-8)*(barWidth*-1));
	var newMargin = 0;
	$nextBtn.click(function(){
		if(leftMargin >= maxMargin){
			$('.follow .bar .date-divider').html('<br/>');
			var newMargin = leftMargin-amountMove;
			$thePort.animate({ marginLeft: newMargin+'px' }, "normal");
			$('.follow .bar').removeClass('openBar');
			leftMargin = newMargin;
		}
		return false;
	});
	$backBtn.click(function(){
		if(leftMargin<0){
			$('.follow .bar .date-divider').html('<br/>');
			var newMargin = leftMargin+amountMove;
			$thePort.animate({ marginLeft: newMargin+'px' }, "normal");
			$('.follow .bar').removeClass('openBar');
			leftMargin = newMargin;
		}
		return false;
	});
	$('.follow .bar').click(function(){
		var portMoved = 0;
		var thisIndex = $('.follow .bar').index(this);
		var hittingThisBar = ((thisIndex+1)-(barAmountMove*((leftMargin*-1)/portWidth)));
		var classes = $('.follow .bar').eq(thisIndex).attr('class');
		if (classes.indexOf('openBar') > -1) {
		    	$('.follow .bar').eq(thisIndex).removeClass('openBar');
		    	$('.follow .bar .date-divider').html('<br/>');
		    	$('.fts-bar-close').hide();
		} else {
			if(hittingThisBar > movePortOnBar)
				{
				var moveFactor = hittingThisBar-movePortOnBar;
				var moveThisMuch = barWidth*movePortOnBar;
				newMargin = leftMargin-moveThisMuch;
				$thePort.animate({ marginLeft: newMargin+'px' }, "normal");
				leftMargin = newMargin;
				}
			$('.follow .bar').removeClass('openBar');
			$('.fts-bar-close').hide();
			$('.follow .bar .date-divider').html('<br/>');
			$('.follow .bar').eq(thisIndex).addClass('openBar').find('.fts-bar-close').show();
			$('.follow .bar .date-divider').eq(thisIndex).html(' - ')
		}
	});

	// VIDEO PAGING IN HUBS
	var startingID = 0;
	$('#video-hub-next').click(function(){
		changeVideoHubPlayer(1);
		return false;
	});
	$('#video-hub-prev').click(function(){
		changeVideoHubPlayer(0);
		return false;
	});
	
	function changeVideoHubPlayer(way)
		{
		var videoCount = $('.video-info').length;
		var videoOn = startingID+1;
		if(way && videoOn < videoCount) startingID++;			
		else if(way && videoOn == videoCount) startingID = 0;			
		if(!way && videoOn > 1) startingID--;
		else if(!way && videoOn == 1) startingID = videoCount-1;
		var videoNowOn = startingID+1;
		var videoID = $('.video-info:eq('+startingID+')').attr('id');
		var videoTitle = $('.video-info:eq('+startingID+') .video-info-title').text();
		var videoLength = $('.video-info:eq('+startingID+') .video-info-length').text();
		var videoDate = $('.video-info:eq('+startingID+') .video-info-date').text();
		var videoHeight = $('.video-info:eq('+startingID+') .video-info-height').text();
		
		var $player = $('#hub-video-player-holder');
		var videoPlayer = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="313" height="[[[HEIGHT]]]" id="viddler_[[[ID]]]"><param name="movie" value="http://www.viddler.com/simple/[[[ID]]]/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/simple/[[[ID]]]/" width="313" height="[[[HEIGHT]]]" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_[[[ID]]]"></embed></object>';
		var newPlayer = videoPlayer.replace(/\[\[\[HEIGHT\]\]\]/gi, videoHeight).replace(/\[\[\[ID\]\]\]/gi, videoID);
		$player.empty().html(newPlayer);
		
		//$player.find('object').attr('id','viddler_'+videoID).attr('height',videoHeight);
		//$player.find('param[name=movie]').attr('value','http://www.viddler.com/simple/'+videoID+'/');
		//$player.find('embed').attr('src','http://www.viddler.com/simple/'+videoID+'/').attr('name','viddler_'+videoID).attr('height',videoHeight);
		$('#numerator').text(videoNowOn);
		$('#hub-video-player-date').text(videoDate);
		$('#hub-video-player-title').text(videoTitle);
		$('#hub-video-player-length').text('('+videoLength+')');
		}

	// HERO STUFF
	$('.my-little-hero .hero_menu a').live('click',function(){
		clearInterval(heroTimer);
		heroTimer = setInterval("nextHero()", cycleTime);
		var heroIndexNow = $('.my-little-hero .hero_menu').index($(this).parent().parent());
		var heroIndexNew = $('.my-little-hero .hero_menu:eq('+heroIndexNow+') a').index(this);
		if($('.my-little-hero:eq('+heroIndexNow+') .hero_bigheadline').hasClass('hero_bigheadline_open'))
			{
			$('.my-little-hero').eq(heroIndexNow).fadeOut('normal',function(){
				$('.my-little-hero').eq(heroIndexNew).fadeIn(150,function(){
					closeHeroBigHeadline(heroIndexNow);
				});
			});
			return false;
			}
		$('.my-little-hero').eq(heroIndexNow).fadeOut(150,function(){
			$('.my-little-hero').eq(heroIndexNew).fadeIn(150);
		});
		return false;
	});

	// HIDE THE LAST COMMA ON FILED UNDER
	var filedUnderOriginal = jQuery.trim($('.filed_under').html());
	var filedUnderNew = filedUnderOriginal.slice(0,-1);
	$('.filed_under').html(filedUnderNew)
	
	// COMMENT STUFF
	var $expandLink = $('.expand_children');
	$('.child').parent().prev().find('.parent .expand_children').show();
	var commentIndex = new Array();
	var counter = 0;
	$('.expand_children a:visible').each(function(){
		commentIndex[counter] = $('.parent .expand_children a').index(this);
		counter++
	});	
	for(i=0; i<commentIndex.length; i++){
		var commentCount = 0
		$('.parent').eq(commentIndex[i]).parents('.commentlinks').nextAll().each(function() {
    			if($(this).find('div:first').is('.child')) commentCount++;
			else return false;
		});
		$('.parent').eq(commentIndex[i]).find('.expand_children a').html(commentCount+' replies');
	}
	var $badComments = $('.level0 .container_contents_holder, .level1 .container_contents_holder');
	$('.level0, .level1').prepend('<div class="badComment_msg">This comment has been down-ranked into oblivion. <a href="">View comment</a></div>');
	$('.badComment_msg a').live('click',function(){
		if($(this).html()=='View comment') $(this).html('Hide comment').parent().next().show();
		else $(this).html('View comment').parent().next().hide();
		return false;
	});
	$expandLink.click(function(){
		if($(this).parents('.commentlinks').next().find('div:first').is(':visible')) $(this).css('background-image','url(http://www.blogsmithmedia.com/www.engadget.com/media/reply_arrow.png)');
		else $(this).css('background-image','url(http://www.blogsmithmedia.com/www.engadget.com/media/reply_arrow_down.png)');
		$(this).parents('.commentlinks').nextAll().each(function() {
    			if($(this).find('div:first').is('.child')) $(this).find('div:first').toggle();
			else return false;
		});
		return false;
	});

	// ARCHIVE CODE	
	var cal = $('.col2_archives_main');
	$("#archive_prev").live("click", function(){		
		rotateMonth($(this).find('a').attr('id'));		
		return false;
	});
	$("#archive_next").live("click", function(){
		rotateMonth($(this).find('a').attr('id'));		
		return false;
	});
	function rotateMonth(month){
		$.ajax({
			type : 'POST',
			url : '/index.php?a=archive-grab&svar_archive='+month+'&lPivots=svar_archive',
			type: 'html',
			success : function (data) {
				 var goodPart = data.split('<script type="text/javascript">');
				 cal.fadeTo('fast',0,function(){
				 	cal.html(goodPart[0]).fadeTo('fast',1);
				 });
			}   
		});
	}
	// END ARCHIVE CODE

	// DAN'S TIP US CODE
	$('#tipus_popup_holder .submit').click(function(){
		var dataStrTip = $('#tipUsForm').serialize();
		$.post('/?a=send-tip&id=tips', dataStrTip, function(data) {
			alert('Thanks for the tip!');
			location.reload(true);
			// hideTipUs();
		});
		return false;
	});
	// END DAN'S TIP US CODE
	
	centerPagination();
	
	$('.faq_arrow a').click(showAnswerBody);
	$('.faq_question a').click(showAnswerBody);
	$('#podcasts .podcast_readmore').live('click',function(){
		var l_xEntry = $(this).parent().parent().parent();
		$('#podcasts .podcast_entry_selected').each(function(){
			$(this).find('.podcast_content').hide('fast');
			$(this).removeClass('podcast_entry_selected');
		});
		$(l_xEntry).find('.podcast_content').show('fast');
		$(l_xEntry).addClass('podcast_entry_selected');
		return false;
	});
	
	$('.sprite_readmore a').live('click',function(){
		var heroIndexNow = $('.my-little-hero').index($(this).parents('.my-little-hero'));
		openHeroBigHeadline(heroIndexNow);
		return false;
		});
	$('.sprite_close a').live('click',function(){
		var heroIndexNow = $('.my-little-hero').index($(this).parents('.my-little-hero'));
		closeHeroBigHeadline(heroIndexNow);
		return false;
		});
	
	$('#topstories .a_block').hover(function(){
		$(this).addClass('a_block_hover');
	},function(){
		$(this).removeClass('a_block_hover');
	});
	
	$('#anon_selector div').click(anonSelector);
	
	$('#tipus_tab, .nav_tipus, .footer_tipus').click(showTipUs);
	
	$('#tipus_cancel').click(hideTipUs);
	$('#tipus_overlay').click(hideTipUs);
	
	$('#nav_dropdown .current').click(openSearchNavDropdown);
	$('#nav_dropdown .nav_popup a').click(selectSearchNavDropdown);
	
	$('#headlink').hover(
		function() { $('a.engadget_us_hdr',this).css({'background-position':'bottom', 'border-bottom':'none'}); $('ul', this).css('display', 'block'); },
		function() { $('a.engadget_us_hdr',this).css({'background-position':'top', 'border-bottom':'none'}); $('ul', this).css('display', 'none'); });

	$('#footerlink').hover(
		function() { $('a.engadget_us_hdr',this).css({'background-position':'bottom', 'border-bottom':'none'}); $('ul', this).css('display', 'block'); },
		function() { $('a.engadget_us_hdr',this).css({'background-position':'top', 'border-bottom':'none'});  $('ul', this).css('display', 'none'); });
    
	setShareButtons();
	
	$('.calendar_icon').click(showCalendarPopup);
	
	setSearchForm();
	
	$('#engadget_hdr_login').click(clickHeaderLogin);
	$('#engadget_hdr_register').click(clickRegisterLogin);
	
	
	
	$('#login_popup .cancel, #register_popup .cancel, #register_finish').click(function(){
			hideHeaderPopups();
			return false;
	});

	$('.gallery_img a').hover(function(){ $(this).children('.border').show(); }, function(){ $(this).children('.border').hide(); });
	
	$('#follow_topic .bar').hover(function(){$(this).addClass('more_zindex').children('.tooltip, .tooltip_flipped').show();}, function(){$(this).removeClass('more_zindex').children('.tooltip, .tooltip_flipped').hide();});
	
	$('#comments .replies').click(function(){
		l_xRepliesClicked = $(this);								   
		if(l_xRepliesClicked.hasClass('replies_open')){
			l_xRepliesClicked.removeClass('replies_open');
			closeReplies(l_xRepliesClicked);	
		}
		else{
			l_xRepliesClicked.addClass('replies_open');
			openReplies(l_xRepliesClicked);
		}
	});
	
	$('#comments .comment_report').click(function(){
		$(this).siblings('.comment_report_popup').toggle().end().parent('.comment_report_holder').toggleClass('popup_open');
		return false;
	});
	$('#comments .comment_show').click(function(){
		$(this).parent().parent().toggleClass('low_ranking');
		if($(this).children('a').html() == 'SHOW')
			$(this).children('a').html('HIDE');
		else
			$(this).children('a').html('SHOW');
		return false;
	});
	$('#comments .comments_newest').click(function(){
		$(this).addClass('sprite_selected').siblings('.comments_oldest').removeClass('sprite_selected').end().siblings('.selector_arrow').animate({right: '29px'}, 200);
		return false;
	});
	$('#comments .comments_oldest').click(function(){
		$(this).addClass('sprite_selected').siblings('.comments_newest').removeClass('sprite_selected').end().siblings('.selector_arrow').animate({right: '126px'}, 200);
		return false;
	});
	$('#comments .comment_report_popup .cancel').click(function(){
		$(this).parent().parent().hide().siblings('.comment_report').removeClass('sprite_selected').parent('.comment_report_holder').removeClass('popup_open');																
	});
	
	
        $('#tbox_gallery .thumb').hover(
		function(){ 
			if(!$(this).parent('.thumb').hasClass('thumb_selected'))
				$(this).children('.hover_border').show(); 
		},
		function(){ 
			$(this).children('.hover_border').hide(); 
		}
	);
	
	// $('.gallery_open').click(openGallery);
	// $('#tbox_overlay').click(closeGallery);

	
	
	
	$('#podcasts .podcast_sort_dropdown .current, #recap .recap_sort_holder .current').click(function(){
		$(this).siblings('.popup').toggle();
		return false;
	});
	$('#podcasts .podcast_sort_dropdown .popup a').click(function(){
		l_sSelectedHtml = $(this).html();
		$(this).addClass('selected').siblings().removeClass('selected').end().parent('.popup').hide();
		$('#podcasts .podcast_sort_dropdown .current').html(l_sSelectedHtml);
		return false;
	});
	
	$('#recap .recap_sort_holder .popup a').click(function(){
		l_sSelectedHtml = $(this).html();
		l_sSelectedWidth = $(this).width();
		$(this).addClass('selected').siblings().removeClass('selected').end().parent('.popup').hide().siblings('.current').width(l_sSelectedWidth).html(l_sSelectedHtml);
		return false;
	});

	$('#recap .orb_center').hover(function(){
		$(this).children('.orb_tooltip').show();
		$(this).next('div').show();
	}, function(){
		$(this).children('.orb_tooltip').hide();
		$(this).next('div').hide();
	});
	$('#recap .orb_tooltip').click(function(){
		window.location = $(this).children('.article-url-jar').html();
	});
	// $('#recap .orb').hover(function(){$(this).addClass('more_zindex');}, function(){$(this).removeClass('more_zindex');});
	
	$('.col2_poll .col2_poll_submit a').click(function(){
		$('.col2_poll').toggle();
		$('.col2_poll2').toggle();		
		return false;
    });
	
	$('.search_filter_top .showmore a').click(function(){
		$('.search_filter_section .search_filter_more_section').show();
		$('.search_filter_top .showmore').hide();
		return false;
	});
	
	$('.search_filter_top .showless a').click(function(){
		$('.search_filter_section .search_filter_more_section').hide();
		$('.search_filter_top .showmore').show();
		return false;
	});
	
	$('.col2 .archives_bottom .date_cell a').live('mouseover',function(){	
		$(this).siblings('.post_count').show("fast", showFix);	
	 });
	$('.col2 .archives_bottom .date_cell a').live('mouseout',function(){
		$(this).siblings('.post_count').hide("fast", hideFix);
	 });	
	
	$('.blogroll .post_content .post_body .blogroll_poll .poll_submit a').click(function(){
		$(this).parent().siblings(".blogroll_poll_selections_holder").children("li").children("input").hide();
		$(this).parent().siblings(".blogroll_poll_selections_holder").children("li").children("label").addClass("results_label");		
		$(this).parent().parent().siblings(".results").show();
		$(this).parent().hide();
		
		return false;
    });
    
    // THIS IS WHAT KILLS STUFF WHEN YOU CLICK OFF
	
	$('#login_popup, #register_popup, #register_finish, .share_popup, #nav_dropdown .nav_popup').click(function(e) {
		e.stopPropagation();
	});
    
	$(document).click(function() {
		hideHeaderPopups();
		$('#nav_dropdown .nav_popup').hide();
		$('.share_popup:visible').hide();
		$('.post_share').removeClass('sprite_selected');
	});
    
    // DAN'S EMAIL SHARE STUFF
    
    $('.post_share span').click(function(){
		var postID = $(this).attr('id');
		var index = $('.post_share span').index(this);
		var theGoods = $.ajax({
			type : 'POST',
			url : '/',
			data : 'a=email-ajax&postid='+postID,
			success : function (data) {
				 zipItUp(data,postID);
			}   
		});
	});
	
	$('.post_share .submit').click(function(){
		var postID = breakIt('submit_','_DBC',$(this).attr('id'));
		var dataStr = $('#form_'+postID).serialize();	                
		//alert(dataStr);
		$.ajax({
			url: "/?a=email-submit",
			type: "POST",
			data: dataStr,
			success: function(msg){
				//alert(msg);
			}
		});
		return false;
	});  

	/** Functions for Join The Discussion comment section**/
	$('.article .aim a').click(function(){
		$('#join_discussion_aim').show();
		$('#join_discussion_new').hide();
		$('#join_discussion_returning').hide();
		return false;
   	});
	
	$('.article .returning a').click(function(){
		$('#join_discussion_returning').show();
		$('#join_discussion_aim').hide();
		$('#join_discussion_new').hide();
		return false;
   	});

	$('.article .newreader a').click(function(){
		$('#join_discussion_new').show();
		$('#join_discussion_aim').hide();
		$('#join_discussion_returning').hide();
		return false;
   	});
	
	$('.article .join_discussion_replyto').click(function(){
		$('.article .join_discussion_replyto').remove();
		return false;
    	});
	
});  

// hero cycler
function nextHero(){
	if($('body').hasClass('hero-is-open') == false)
		{
		var heroLength = $('.hero_menu').eq(0).find('.hero_link').length;
		var heroActive = $('.hero_link_active:visible').prevAll('.hero_link').length;
		if((heroActive+1) < heroLength) $('.hero_link_active:visible').next('.hero_link').find('a').trigger('click');
		else $('.hero_menu:visible').find('.hero_link').eq(0).find('a').trigger('click');
		}
	}

//this function makes sure that several archives aren't opened at once.
function showFix(){
	$('.col2 .archives_bottom .date_cell .post_count').hide();
	$(this).show();	
}

function hideFix(){
	$(this).hide();	
}

function centerPagination(){
	                                                 
   $('.pagination_center').width( $('.pagination_center').width() ).css('display','block').css('float','none')

}

function bpScrubSigninForm() {
   // scrub form data to make sure valid
   return true;
}

function bpScrubRegisterForm() {
   // scrub register date to make sure valid
   return true;
}

// function bpCreateAccount() {
 //       var email = document.getElementById('emailaddr').value;
//        var key = document.getElementById('newkey').value;
 //       var un = document.getElementById('newusername').value;
 //       alert('/bp_access/create/?email='+email+'&key='+key+'&un='+un);
 //       $.ajax({
//		url: '/bp_access/create/',
////		type: 'html',
//		data: '&email='+email+'&key='+key+'&un='+un,
 //               success: function(msg){
// ("REGISTER!");
//		}
//	});
//}



function showRegistrationFinish(){
        if(bpScrubRegisterForm()) {
            bpCreateAccount();	
            $('#register_popup .register_proper').hide();
	    $('#register_popup .register_finish').show();
        } else {
            //if user enters garbage, what next
        }    
	return false;
}


function clickHeaderLogin(){
	hideHeaderPopups();
	
	$('#login_popup').show();
	$('#engadget_hdr_login').addClass('sprite_selected engadget_hdr_register_active');
	return false;
}
function clickRegisterLogin(){
	hideHeaderPopups();
	
	$('#register_popup .register_proper').show();
	$('#register_popup .register_finish').hide();
	
	$('#register_popup').show();
	$('#engadget_hdr_register').addClass('sprite_selected engadget_hdr_register_active');
	return false;
}

function hideHeaderPopups(){
	
	$('#login_popup').hide();
	$('#register_popup').hide();
	$('#engadget_hdr_register').removeClass('engadget_hdr_register_active sprite_selected');
	$('#engadget_hdr_login').removeClass('engadget_hdr_register_active sprite_selected');
}

function openSearchNavDropdown(){
	$('#nav_dropdown .nav_popup').show();
	return false;
	
}

function selectSearchNavDropdown(){
	$('#nav_dropdown .nav_popup').hide();
	$('#nav_dropdown .current').html(  $(this).html() ); 
	return false;
}


function setSearchForm(){
	$('#searchform').val($('#searchform').attr('rel'));
	$('#searchform').focus(function(){
		if($(this).val() == $(this).attr('rel')){
			$(this).val('');
		}
	});
	$('#searchform').blur(function(){
		if(!$(this).val())
			$(this).val($(this).attr('rel'));
	});
}


function showTipUs(){
	
	window.scrollTo(0,130);
	
	$('#tipus_overlay').height( $('body').height()+200 );
	
	
	$('#tipus_tab').hide();
	$("#tipus_popup_holder").show();
	
	return false;
}
function hideTipUs(){
	$('#tipus_tab').show();
	$("#tipus_popup_holder").hide();
	
	return false;
}

function anonSelector(){
	
	$('#anon_selector .selected').removeClass('selected');
	$(this).addClass('selected');
	
}



function closeHeroBigHeadline(eqAlf){
	$('body').removeClass('hero-is-open');
	var l_nHeight = $('.my-little-hero:eq('+eqAlf+') .hero_bigheadline .top_content').css('height');
	if(!parseInt(l_nHeight)) l_nHeight = $('.my-little-hero:eq('+eqAlf+') .hero_bigheadline .top_content').height();
	$('.my-little-hero:eq('+eqAlf+') .hero_bigheadline').animate({'height':l_nHeight},100);
	$('.my-little-hero:eq('+eqAlf+') .hero_bigheadline').removeClass('hero_bigheadline_open');
	return false;
}
function openHeroBigHeadline(eqAlf){
	$('body').addClass('hero-is-open');
	var l_nHeight = $('.my-little-hero:eq('+eqAlf+') .hero_bigheadline .bottom_content').css('height');
	if(!parseInt(l_nHeight)) l_nHeight = $('.my-little-hero:eq('+eqAlf+') .hero_bigheadline .bottom_content').height();
	$('.my-little-hero:eq('+eqAlf+') .hero_bigheadline').animate({'height':l_nHeight},100, function(){
		$('.my-little-hero:eq('+eqAlf+') .hero_bigheadline').addClass('hero_bigheadline_open');
	});
	return false;
}      


    
function setShareButtons(){
	   /*
	$('.blogroll .share_popup').each(function(){
		var l_nRight = $(this).css('right');
		$(this).data('right',parseInt(l_nRight));
		alert($(this).css('right'));
    });
	 */
	$('#podcasts .podcast_share a').click(clickSharePopup);
	$('#podcasts .share_form .cancel').click(clickShareCancel);
	
	$('.blogroll .post_share a').click(clickSharePopup);
	$('.blogroll .share_form .cancel').click(clickShareCancel);
	
	$('.article .post_share a').click(clickSharePopup);
	$('.article .share_form .cancel').click(clickShareCancel);

	$('.col2_featured_contributors .share a').click(clickSharePopupCol2);
	$('.col2_featured_contributors .share_form .cancel').click(clickShareCancelCol2);	 

	$('.col2_most_commented .share a').click(clickSharePopupCol2);
	$('.col2_most_commented .share_form .cancel').click(clickShareCancelCol2);	
	
	$('.col2_game_changer .share a').click(clickSharePopupCol2);
	$('.col2_game_changer .share_form .cancel').click(clickShareCancelCol2);
	
	$('.col2_kirf .share a').click(clickSharePopupCol2);
	$('.col2_kirf .share_form .cancel').click(clickShareCancelCol2);
	
	$('.col2_poll .share a').click(clickSharePopupCol2);
	$('.col2_poll .share_form .cancel').click(clickShareCancelCol2);
	
	$('.col2_poll2 .share a').click(clickSharePopupCol2);
	$('.col2_poll2 .share_form .cancel').click(clickShareCancelCol2);
	
	$('.col2_ask_engadget .share a').click(clickSharePopupCol2);
	$('.col2_ask_engadget .share_form .cancel').click(clickShareCancelCol2);		
	
	$('.col2_stats .share a').click(clickSharePopupCol2);
	$('.col2_stats .share_form .cancel').click(clickShareCancelCol2);		
	
}    

function clickSharePopupCol2(){
	var l_xPopup = $(this).parent().siblings('.share_popup'); 
	var l_xShareBtn = $(this).parent();	
	
	l_xPopup.toggle();  
	$(l_xShareBtn).toggleClass("sprite_selected");
	
	return false;
}                            
function clickShareCancelCol2(){
	$(this).parent().parent().parent().hide(); 
	$(this).parent().parent().parent().parent().find(".share").removeClass('sprite_selected');	
	return false;  
}

function clickSharePopup(){
	                                                                     
	 var l_xPopup = $(this).parent().parent().siblings('.share_popup'); 
	 var l_xShareBtn = $(this).parent(); 
	
	$(l_xPopup).toggle();  
	$(l_xShareBtn).toggleClass("sprite_selected");       
	
	//align share popup                                                                                   
	var l_nBaseWidth = 14;
    var l_nCommentWidth =  $(l_xShareBtn).siblings('.post_comment_icon').width(); // alert(l_nCommentWidth);   
    var l_nDiscussWidth =  $(l_xShareBtn).siblings('.post_comment').width();     
   
	//var l_nOffset = l_nCommentWidth - l_nBaseWidth; // alert(l_nOffset); 
	//var l_nPopupPosition = parseInt( $(l_xPopup).data('right'));
	
	var l_nNewPopupPosition = l_nCommentWidth + l_nBaseWidth + l_nDiscussWidth; // alert(l_nNewPopupPosition);
	$(l_xPopup).css('right', l_nNewPopupPosition); 
	                      	
	return false;
} 

function alignSharePopup(){
	//rah!
}  

function clickShareCancel(){                                      
	$(this).parent().parent().parent().hide(); 
	$(this).parent().parent().parent().parent().find(".post_share, .podcast_share").removeClass('sprite_selected');
	return false;  	
}




function showCalendarPopup(){
	$('.calendar_popup').toggle();
	return false;
}


function openReplies(p_xRepliesClicked){
	l_xComment = p_xRepliesClicked.parent().parent().parent().parent();
	if(l_xComment.hasClass('original_comment'))
		l_xComment.children('.reply_level_1').slideDown(200);
	else if(l_xComment.hasClass('reply_level_1'))
		l_xComment.children('.reply_level_2').slideDown(200);
	else if(l_xComment.hasClass('reply_level_2'))
		l_xComment.children('.reply_level_3').slideDown(200);
}

function closeReplies(p_xRepliesClicked){
	l_xComment = p_xRepliesClicked.parent().parent().parent().parent();
	l_xComment.children('.reply_wrapper').slideUp(200);
}

function openGallery(){
	
	//window.scrollTo(0,0);
	
	$('#tbox_overlay').height( $('body').height()+200 );
	$('#tbox_pic_loader').empty();
	$('#tbox_nav_loader').empty();
	$("#tbox_gallery").show();
	
	return false;
}
function closeGallery(){
        // close the gallery ad div and clear it out
        adsClrAd(galleryDiv)          
	$('#tbox_gallery').hide();
	return false;
}

function showAnswerBody() {
	l_xFaqArrowSprite = $(this).parent().parent().find('.faq_arrow');
	l_xFaqArrowSprite.toggleClass("sprite_selected");
	
	l_xAnswerBody = $(this).parent().parent().siblings('.faq_answer');
	l_xAnswerBody.slideToggle("fast");
	return false;
}

// MORE CUSTOM JS

function zipItUp(d,pID){
	$('#sID_'+pID).val(breakIt("sID.value = '","';var uID = ",d));
	$('#uID_'+pID).val(breakIt("uID.value = '","';var tStamp = ",d));
	$('#tStamp_'+pID).val(breakIt("tStamp.value = '","';var cacheId = ",d));
	$('#cacheId_'+pID).val(breakIt("cacheId.value = '","';anchor.parentNode",d));
}

function breakIt(b1,b2,d){
	var break1 = d.split(b1);
	var break2 = break1[1].split(b2);
	return break2[0];
}

// BREAKING and FEATURED MODULE

	$("a.swap").click(function () {
		$(".featured_posts").hide();
		$(".breaking_posts").fadeIn();
		$(".featured_on_header").hide();
		$(".breaking_on_header").show();
  		return false;
  	});
  	$("a.swapBack").click(function () {
		$(".breaking_posts").hide();
		$(".featured_posts").fadeIn();
		$(".breaking_on_header").hide();
		$(".featured_on_header").show();
  		return false;
  	});