//var $j = jQuery.noConflict();
// think this was breaking some things because the Jquery include already has noconflict turned on
var $j = jQuery;

function readmore(postid){
	$j('#readmore_link_'+postid).hide();
	$j('#readmore_'+postid).show();
	fixColumns();
}
function readless(postid){
	$j('#readmore_link_'+postid).show();
	$j('#readmore_'+postid).hide();
	fixColumns();
	$j(document).scrollTop($j('#readmore_link_'+postid).offset().top);
	
}
function fixColumns(){
	$j('#leftcol').add('#rightcol').css('height','auto');
	equalHeight($j('#leftcol').add('#rightcol'));
}

function displayInterviewPopup(message){
	$j('#interviewPopup').text(message);
	$j('#interviewPopup').css('display','block');
}
function hideInterviewPopup(){
	$j('#interviewPopup').css('display','none');
}

function scrollEvents(delta){
	if ($j('#feature_container').css('margin-left')!='0px') return false;

	if (delta > 0){
		$j('#feature_container').animate(
			{
				marginLeft: '-'+(300*delta)+'px'
			},
			500*delta,
			function(){
				$j('#feature_container').append($j('#feature_container > .feature:lt('+(delta)+')'));
				$j('#feature_container').css('margin-left','0px');
			}
		);
	}
	 else if (delta < 0){
		$j('#feature_container > .feature:gt('+ ( $j('#feature_container').children().size() + delta - 1) +')').insertBefore('#feature_container > .feature:first-child');
		$j('#feature_container').css('margin-left',(300*delta)+'px');
		$j('#feature_container').animate(
			{
				marginLeft: '0px'
			},
			500*delta*-1,
			function(){
			}
		);
	}
}
function sendSubscription(footer){
	var signupInput = $j('#signup_input');
	if (footer == true) signupInput = $j('#signup_input2');	
	
	var emailaddress = signupInput.attr('value');
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(emailaddress)) {
		alert('Sorry "'+emailaddress+'" is an invalid e-mail address');
		signupInput.attr('value','').focus();
	} else {
		signupInput.attr('value','Sending...').css('color','gray');
		$j.ajax({
			type: 'POST',
			data: 'email='+emailaddress+'&format=h&submit=Subscribe',
			url: "http://newsletter.placemint.com/ss/form.php?form=8",
			cache: false,
			success: function(data){
				signupInput.attr('value','Thanks!').css('color','green').animate({color:'#000000'},2000,function(){
					signupInput.attr('value','e-mail').css('color','black');
				});
			},
			error: function(req, status, error){
				alert('Sorry, an error occurred. Please Try Again.');
				signupInput.attr('value','ERROR! TRY AGAIN.').css('color','green').animate({color:'#000000'},2000,function(){
					signupInput.attr('value','e-mail').css('color','black');
				});
			}
		});
		/*
		var r = $j.post(
			'http://newsletter.placemint.com/ss/form.php?form=8',
			'email='+emailaddress+'&format=h&submit=Subscribe',
			function(data){
				alert(data);
				signupInput.attr('value','Thanks!').css('color','green').animate({color:'#000000'},1000,function(){
					signupInput.attr('value','e-mail').css('color','black');
				});
			}
		);
		*/
	}
}
function searchAction(){
	window.location="/search/"+escape($j('#search_input').attr('value'));
}


function equalHeight(group) {
	var tallest = 0;
	group.css('height','auto').css('min-height','');
	group.each(function() {
		var thisHeight = $j(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	//not exactly sure why this works, but it only works all the time if I set min-height
	group.css('min-height',tallest+'px');
	//group.css('height',tallest+'px');
}


function fixLeftPics(){
	//maxwidth is set in the css to 640 so they dont expand out of the left column on load
	//then we set to auto to find the true dimensions and scale down (or up) to fit 640px width
	$j('#leftcol img').each(function(){
		var i  = $j(this);
		if (i.width() >= 500){
			i.css('height', 'auto');
			i.css('width', 'auto');
			var ow = i.width();
			var oh = i.height();
			i.width(600);
			i.height(600*oh/ow);
		}
	});
}



$j(window).load(function() {
	
	equalHeight($j('#columnWrapper > div'));
	
	$j('#feature_container').css('display', 'block');
	// featured interview auto slideshow stuff
	$j('#feature_container').cycle({ 
	    fx:      'fade', 
	    speed:    300,
		timeout: 2000, 
		next:   '#feature_next', 
		prev:   '#feature_prev',
		pause:   1
	});
});

$j(document).ready(function(){
	//NEWSLETTER STUFF
	$j('#signup_input').focus(function(){
		if ($j(this).attr('value') == 'e-mail') $j(this).attr('value','');
	});
	$j('#signup_input2').focus(function(){
		if ($j(this).attr('value') == 'e-mail') $j(this).attr('value','');
	});
	$j('#signup_button').click(function(){
		sendSubscription();
	});
	$j('#signup_button2').click(function(){
		sendSubscription(true);
	});
	$j('#signup_input').keypress(function(e){ 
		if(e.keyCode == 13) $j('#signup_button').click();	
	});
	$j('#signup_input2').keypress(function(e){ 
		if(e.keyCode == 13) $j('#signup_button2').click();	
	});

	//SEARCH BOX
	$j('#search_input').focus(function(){
		if ($j(this).attr('value') == 'looking for something?') $j(this).attr('value','');
	});	
	$j('#search_button').click(function(){
		searchAction();
	});
	$j('#search_input').keypress(function(e){ 
		if(e.keyCode == 13) $j('#search_button').click();	
	});

	
	
	if (typeof(homepage)!='undefined'){
		// EVENTS STUFF
		//$j('#feature_container > .item:nth-child(1)').css('border-color','white');
		//$j('#feature_next').click(function(){scrollEvents(1);});
		//$j('#feature_prev').click(function(){scrollEvents(-1);});
		$j('#main_interview').hover(
			function(){
				$j('#feature_prev').fadeIn();
				$j('#feature_next').fadeIn();
			},
			function(){
				$j('#feature_prev').fadeOut();
				$j('#feature_next').fadeOut();			
			}
		);
		
		
	} else {
		$j('.single_post:last-child').css('border','none');
		fixLeftPics();
		fixColumns();
	}
		

	// twitter stuff
	$j(".tweet").tweet({
                username: "lookoutpresents",
                    join_text: "auto",
                    avatar_size: 0,
                    count: 4,
                    loading_text: "loading tweets...",
					template: "{text}",
					auto_join_text_default: "",
					        auto_join_text_ed: "",
					        auto_join_text_ing: "",
					        auto_join_text_reply: "",
					        auto_join_text_url: "",
					
                    refresh_interval: 60
                    });

	
//	$j('#feature_container').cycle({
//			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	//	});
	

});
