var url = 'http://www.savetheplanet.ca/';

/** NEW AJAX JQUERY **/
function dig(cn, id)
{
	$(cn).removeClass('vote');
	$(cn).html('<img src="images/loading.gif" style="border:0px;left:15px;position:relative;top:4px;">');

	$.get(url+'dig.php?i='+id,function(data) {

			$(cn).html('&nbsp;');
			returned = data.split('|');

			if(data.indexOf('|' != -1)) 
			{
				var id=returned[1];
				var numdigs=returned[2];
				var status=returned[0];
				
				if(status == '1')
				{
					$('#numdigs'+id).html(numdigs);
					$(cn).addClass('novote');
					$(cn).attr('onclick', 'return false;');
				}
				else if(status == '2')
				{
					$(cn).addClass('novote');
					$(cn).attr('onclick', 'return false;');
				}
				else if(status == '0')
				{
					$(cn).html('Login');
					$(cn).attr('onclick', 'return false;');
				}
			}
	});
	return false;
}



function openWindow(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function vote(storyId, divId)
{
	document.getElementById('digbut'+divId+storyId).innerHTML = '<img src="/images/loading.gif">';
	jQuery.get(url+'dig.php?i='+storyId,'',function(data){
		voteResponse(storyId, divId, data);
	});

	return false;
}


function voteResponse(storyId, divId, data)
{

        var response = data;
		var id = storyId;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[1];
			var numdigs=returned[2];
			var status=returned[0];
			
			if(status == '1')
			{
				document.getElementById('digbut'+divId+id).innerHTML = '';
				document.getElementById('numdigs'+divId+id).innerHTML = numdigs + '<br><span style="font-size:10px;">votes</span>';
  			}
			else if(status == '2')
			{
				document.getElementById('digbut'+divId+id).innerHTML = '';
			}
			else if(status == '0')
			{
				document.getElementById('digbut'+divId+id).innerHTML = '<a href="'+url+'login.php">Login<\/a>';
			}
		}
    
}

function flag_story(id, reason, cmd)
{
	if(reason == '')
	{
		$('#reason').append('<span style="color:red;">Please select a reason.</span>');
	}
	else
	{
		$('#flagnav').html('<img src="/images/loading.gif">');
		jQuery.get(url+'bury.php?i='+id+'&reason='+reason,'',function(data){
			flagResponse(id, data, cmd);
		});
	}	
	return false;

}

function flagResponse(id, data, divId)
{
	var response = data;
	var cmd = divId;
	
	returned = response.split('|');

	if(response.indexOf('|' != -1)) 
	{
		var id=returned[1];
		var status=returned[0];
		
		if(status == '0')
		{
			document.getElementById('digbut'+id).innerHTML = '<a href="'+url+'login.php">Login<\/a>';
		}
		else
		{
			document.getElementById('storyb'+cmd+id).style.filter = "Alpha(Opacity=80)";
			document.getElementById('storyb'+cmd+id).style.opacity = ".2";
			//document.getElementById('digbut'+cmd+id).innerHTML = 'Flagged';
			document.getElementById('storyb'+cmd+id).disabled = true;
			tb_remove();
		}
	}
}


// Post comment
function postComment(id)
{

	var fields =  $("#add_comment").serialize();
	jQuery.post( 'http://www.savetheplanet.ca/story_comments.php?id='+id, fields, function(data) {
		$('#storyMenu ul').tabs();
	});
}

// Email story
function emailStory(id)
{
	var data = '';
	var fields = $("form").serialize();
	
	jQuery.post( 'http://www.savetheplanet.ca/story_email.php?id='+id+'&cmd=email', fields, function(){
		//$('#storyMenu ul').tabs();
		$('#res').html('Your story was successfully sent!</br>');
	});

}

// Captcha
function new_freecap(){

		if(document.getElementById)
		{
			thesrc = document.getElementById("impng").src;
			thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
			document.getElementById("impng").src = thesrc+"?"+Math.round(Math.random()*100000);
		} else {
			alert("Sorry, cannot autoreload image\n");
		}
}

// Show forgot pass form
function forgotPass()
{
	$('#loginDiv').hide();
	$('#retrievePass').show();
	return false;
}

function retrievePass()
{
	var fields =  $("#fpassword").serialize();
	$('#retrievePassLoading').ajaxStart(function() { 
		$("#fPassResponse").hide();
		$(this).show(); 
	}).ajaxStop(function() {
		 $(this).hide();
 	});

	jQuery.post( '/login.php', fields, function(data) {
		
		var tp = data.split('|');
		
		$("#fPassResponse").show();
		$("#fPassResponse").html(tp[1]); 
	
		if(tp[0])
		{
			$("#fButton").html('<input type="button" name="Submit" value="Close" style="font-size:10px;" onclick="return tb_remove();">');
		}
		
	} );

	return false;
}

