/* Author: 
	Peter Braden
*/
$(function(){

$(".info, .media, #addToProfile, .tabs").tabs()

$('#showAdd').click(function(){
	$('#addToProfile').dialog({modal:true, resizable:false, width:600, height:400});
});

$('#showLogin').click(function(){
	$('<a id = "fblogin" href = "https://graph.facebook.com/oauth/authorize?client_id=178799842134099&redirect_uri=http://rightpedal.com/account/login"> <img src = "http://developers.facebook.com/images/devsite/login-button.png" alt="Login with Facebook"  /></a>').dialog({title:"You need to login to post", modal:true, resizable:false, width:300, height:100});
});




$('#addVideoSubmit').click(function(){
	var url = $('#addVideoUrl').val();
	var title = $('#addVideoTitle').val();
	var car = $("#carId").val();
	
	if (url.match(/http:\/\/(?:www\.)?youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)/)){
		url = url.match(/http:\/\/(?:www\.)?youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)/)[1];
		
		$("#addVideo").html("<p class='empty'>Loading&hellip;</p>");
		$.post('/upload/video', {youtube_id:url, title:title, car_id:car}, function(){
			window.location.reload()})
		} else {
		// Bad youtube link
		alert("We didn't recognise that youtube link");
	} 
	
	
});

$("#addNewsSubmit").click(function(){
	var url = $('#addNewsUrl').val();
	var title = $('#addNewsTitle').val();
	var car = $("#carId").val();
	
	$.post('/upload/news', {url:url, title:title, car_id:car}, function(){
			window.location.reload()
	})
});





$(".pic-h a").hover(function(){
	$(this).find('span').fadeIn();
	
}, function(){
	$(this).find('span').fadeOut();
});



$(".replyComment").click(function(){
	$("#id_parent").val($(this).attr('pid'));
	$("#replyTitle").text($(this).attr('name'));
	$("#id_comment").focus();
	return false;
});


$("a.lightbox").live('click', function(e){
	e.stopPropagation()
	$(".ui-dialog").children().dialog("close");
	var div = $("<div />")
	
	var curr = $(this).attr('id').slice(4)
	
	if($("#pic-" + (parseInt(curr)-1)).is("*")){
		div.append("<a class= 'lightbox prev' id='pic-" + (parseInt(curr)-1) + "' href = '#' big='" + $("#pic-" + (parseInt(curr)-1)).attr('big') + "'>&lt;</a>");	
	} else {
		div.append("<span class='spacer'>&nbsp;</span>");
	}
	
	div.append("<img class='lightbox' src = '" + $(this).attr('big') + "' />")

	if($("#pic-" + (parseInt(curr)+1)).is("*")){
		div.append("<a class= 'lightbox next' id='pic-" + (parseInt(curr)+1) + "' href = '#' big='" + $("#pic-" + (parseInt(curr)+1)).attr('big') + "'>&gt;</a>");	
	} else {
		div.append("<span class='spacer'>&nbsp;</span>");
	}
	
	div.dialog({modal:true, resizable:false, width:690, height:500});
	$('a').blur();
	return false;
});

$('.ui-widget-overlay').live("click", function() {
	$(".ui-dialog").children().dialog("close");
});   


$('#photos .pag a').click(function(){
	$('#photos .pag a.selected').removeClass('selected');	
	$(this).addClass('selected');
	var x = parseInt($(this).text()) - 1;
	$('#photos .pages').animate({left : -x*600});
	return false;
})

$('#videos .pag a').click(function(){
	$('#videos .pag a.selected').removeClass('selected');	
	$(this).addClass('selected');
	var x = parseInt($(this).text()) - 1;
	$('#videos .pages').animate({left : -x*600});
	return false;
})	



});
