// Comment module javascript file.

$j(document).ready(function() {		
	youtube_form = '<h2>Insert A YouTube Clip</h2><p>If you want your video to show up in your comment, paste the <strong>link</strong> from the YouTube video below.</p><form  onsubmit="insertYouTube(); return false;" method="post" name="addYouTube" id="addYouTube"><input type="text" name="youtubeLink" id="youtubeLink" onfocus="if (this.value == \'http://youtube.com/watch?v=EXAMPLE\') {this.value = \'\';}" value="http://youtube.com/watch?v=EXAMPLE" style="width: 300px" /><br /><br /><input type="submit" value=" Finished! " /></form>';
	
	$j('a[rel*=youtube]').click(function(e) {
		e.preventDefault();
		$j.facebox(youtube_form);
	});
	
	// Display the comment notice box
	//comment_status();
	
	// Set reply scrolling
	$j('a[rel*=reply]').click(function(e){
		e.preventDefault();
		// Scroll them down there.
		$j.scrollTo($j('#comment_form'), 1000);
	});
	
	$j('ul#comment_list span.report a').click(function(e){
		e.preventDefault();
		$j(this).parent().children('ul').slideToggle('fast');
	});
	
	$j('ul#comment_list span.report ul li.close').click(function(e){
		e.preventDefault();
		$j(this).parent().slideUp('fast');
	});	
	


	$j('form#'+form).submit(function(e){
		$j('#comment_submit').attr('disabled', 'disabled');
		$j('#comment_submit').attr('value', 'We are posting your comment now...');
		
		if(background_submit == 'on'){	
			e.preventDefault();
			$j.ajax({
				type: 'POST',
				url: '/public/comment/edit?background=1',
				dataType: 'json',
				data: $j(this).serialize(),
				timeout: 10000,
				success: function(msg){
					if(msg.status == 'success'){
						$j('#comment_list').append(msg.payload);
						$j('li#comment-'+msg.comment_id).slideDown();
						// Scroll them to the comment.
						$j.scrollTo($j('li#comment-'+msg.comment_id), 1000);
						// Blank out the parent and body values...
						replyToThis('','');
						$j('input#commentbody').val('');							
					} else if(msg.status == 'error'){
						var errors = '<h2>Commenting Error!</h2><ol>';
						for (x=0;x<msg.payload.length;x++){
							errors = errors+'<li>'+msg.payload[x]+'</li>';
						}
						errors = errors+'</ol>';
						$j.facebox(errors);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$j.facebox("There was an error.  Try again later.<br /><br />TextStatus: "+textStatus+"<br />Error: "+errorThrown);
				},
				complete: function(){
					// Reset the CAPTCHA
					Recaptcha.reload();	
					// Reset the button		
					$j('#comment_submit').removeAttr('disabled');
					$j('#comment_submit').attr('value', 'Submit Your Comment');	
				}
			});	
		}
	});	
	
	$j('form#email_form').submit(function(e){
		e.preventDefault();
		$j('#email_log').html('Sending...');
		$j.ajax({
			type: 'GET',
			url: $j(this).attr('action'),
			dataType: 'json',
			data: $j(this).serialize(),
			timeout: 2000,
			success: function(msg){
				$j('#email_log').html(msg['data']['response']);
			},
			error: function(){
				$j('#email_log').html('There was an error.  Try again later.');
			}
		});
	});	
});

function comment_status(){
	$j('input#commentname').val(memberData.screenname);
	$j('#comment_form #you img').attr('src', memberData.photo+'?filter=stl/trav_member&w=75');
	$j('#comment_form #you .pleaselogin').fadeOut();
}

function comment_login(){
	loginDoneExe = 'comment_status()';
	loginDoneTxt = 'You are ready to leave a verified comment!';
	login();
}

function comment_vote(id, reason){
	// Make the ajax call
	$j.ajax({
		type: "GET",
		url: base_url+"/public/comment/vote",
		dataType: "text",
		data: 'comment_id='+id+'&reason='+reason,
		timeout: 3000,
		error: function(){
			$j.facebox("Sorry, but we are having a few issues right now and the reporting feature is down.  Please try again later.");
		},
		success: function(msg){
			if(msg == 'already voted!'){
				$j('#reportLink'+id).html('<strong>You\'ve already voted!</strong>');
			}
			if(reason == 'spam' || reason == 'offensive'){
				$j('li#comment-'+id).addClass('greyed_out');
				$j('#reportLink'+id).html('<strong style="color: #990000;">OFFENSIVE!</strong>');
			} else if(reason == 'lame'){
				$j('#reportLink'+id).html('<strong style="color: #990000;">Voted Down!</strong>');
			} else if(reason == 'up'){
				$j('#reportLink'+id).html('<strong style="color: green;">Voted Up!</strong>');
			}
		}
	});
}

function replyToThis(id, name){
	// Set the hidden parent field to the id given.
	$j('#commentparent_comment_id').val(id);
	if(id != ''){
		// Set a little text to remind them they are replying...if id doesn't equal ''
		$j('#replyText').html('You are replying to a comment by <strong>'+name+'</strong>. (<a href="javascript:replyToThis(\'\', \'\')">Cancel.</a>)');
	} else {
		$j('#replyText').html('');
		// Send them back up to the comment
		$j.scrollTo($j('#comment-'+replied_id), 1000);	
	}
	replied_id = id;
}

function insertYouTube()
{
	var url = $j('#youtubeLink').val();
	re = /^http:\/\/w{0,3}\.?youtube.com\/watch\?v=([\w\-]+).*$/;
	if(re.test(url)){
		var reArray = re.exec(url);
		var ytID = reArray[1];
		
		// Insert the short code into the comment
		var theFieldId = 'commentbody';
		var theComment = $j('#'+theFieldId).val();
		$j('#'+theFieldId).val(theComment+'\n'+'[youtube_video='+ytID+']');
		$j('#facebox .content').html('Your YouTube video has been added as "shortcode". Don\'t remove it from your comment.');		
	} else {
		$j('#facebox .content').html('<p style="color: #990000;">The YouTube link you tried wasn\'t valid. Try again.</p>'+youtube_form);	
	}
}

function changeInteract(tab){
	$j('a[rel*=sub_tab]').removeClass('selected');
	$j('.sub_tab_div').css('display', 'none');
	
	$j('#'+tab+'_tab').addClass('selected');
	$j('#'+tab).css('display', 'block');
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Date: 2/19/2008
 * @author Ariel Flesler
 * @version 1.3.3
 */
;(function($){var o=$.scrollTo=function(a,b,c){o.window().scrollTo(a,b,c)};o.defaults={axis:'y',duration:1};o.window=function(){return $($.browser.safari?'body':'html')};$.fn.scrollTo=function(l,m,n){if(typeof m=='object'){n=m;m=0}n=$.extend({},o.defaults,n);m=m||n.speed||n.duration;n.queue=n.queue&&n.axis.length>1;if(n.queue)m/=2;n.offset=j(n.offset);n.over=j(n.over);return this.each(function(){var a=this,b=$(a),t=l,c,d={},w=b.is('html,body');switch(typeof t){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(t)){t=j(t);break}t=$(t,this);case'object':if(t.is||t.style)c=(t=$(t)).offset()}$.each(n.axis.split(''),function(i,f){var P=f=='x'?'Left':'Top',p=P.toLowerCase(),k='scroll'+P,e=a[k],D=f=='x'?'Width':'Height';if(c){d[k]=c[p]+(w?0:e-b.offset()[p]);if(n.margin){d[k]-=parseInt(t.css('margin'+P))||0;d[k]-=parseInt(t.css('border'+P+'Width'))||0}d[k]+=n.offset[p]||0;if(n.over[p])d[k]+=t[D.toLowerCase()]()*n.over[p]}else d[k]=t[p];if(/^\d+$/.test(d[k]))d[k]=d[k]<=0?0:Math.min(d[k],h(D));if(!i&&n.queue){if(e!=d[k])g(n.onAfterFirst);delete d[k]}});g(n.onAfter);function g(a){b.animate(d,m,n.easing,a&&function(){a.call(this,l)})};function h(D){var b=w?$.browser.opera?document.body:document.documentElement:a;return b['scroll'+D]-b['client'+D]}})};function j(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);