// isCommTab

jqN(document).ready(function() {
	// Default State of the Comments

	setCommentView();


	// The changing state of the comments.
	jqN('#user_comm, .post_comm').css('cursor', 'pointer');
	jqN('#user_comm').click(function() {
		setCommentView();
	});
	jqN('#post_comm').click(function() {
		setCommentPost();		
	});
	
	jqN('.shareThisPost').click(function() {
		jqN(this).toggleShare();
	});
	
	jqN('.linkValueTXT input').click(function() {
		this.select()
	})
	
	jqN("#you_news_recent").click(function(){
		jqN(this).addClass("ugcon");
		jqN("#you_news_popular").removeClass("ugcon");
		jqN(".most_popular_div").hide();
		jqN(".most_recent_div").show();
	});
	
	jqN("#you_news_popular").click(function(){
		jqN(this).addClass("ugcon");
		jqN("#you_news_recent").removeClass("ugcon");
		jqN(".most_recent_div").hide();
		jqN(".most_popular_div").show();		
	});

jqN(".additional_story").click(function(){
jqN(this).remove();
jqN("#text form").show();
});
});

function myNBCLogin_blog()
{
	if(jqN.cookie('sn_nbc_b')){
		var t = jqN.cookie('sn_nbc_b').split('|');
		// Log out old uuid users
		if (t[1].indexOf(",") >= 0 && document.domain == "www.nbc.com")	{
			jqN.cookie('sn_nbc_a', '', { expires: -1, domain: ".nbc.com" });
			jqN.cookie('sn_nbc_b', '', { expires: -1, domain: ".nbc.com" });
			jqN.cookie('sn_nbc_c', '', { expires: -1, domain: ".nbc.com" });
			jqN.cookie('snas', '', { expires: -1, domain: ".nbc.com" });			
		} else {
			jqN('.sn_login_form_div').hide();
			jqN('#ugc_submit').show();
		}
	} else {
		jqN('.sn_login_form_div').show();
	}
	
	jqN('#sn_login_submit_btn').click(function(){
	
		jqN('#sn_login_submit_btn').val('Logging In...');
		var loginForm = jqN('.sn_login_form');
		var emailSub = jqN('#snf_email').val();
		var passSub = jqN('#sn_password').val();

		var theQuery = "method=login&email="+emailSub+"&password="+passSub+"&remember=0";
		var logState = 1;
			jqN.ajax({
				type:"POST",
				url:"/app/api/sn/account/",
				data: theQuery,
				dataType:"xml",
				error: function(){
					//jqN('#mynbcLoginForm').append('<div id=\"response-text\">Sorry can\'t login from here.</div>');
					//alert('fail');
					var currentLocation = window.location;
					window.location = 'http://my.nbc.com/accounts/login?redirectUrl='+currentLocation+'';
				},
				success: function(msg){
					//alert(jqN('msg',msg).text());
					if(jqN('status', msg).text() == '0'){
						var response = jqN('msg', msg).text();;
					jqN('#mynbcLoginForm').append('<div id=\"response-text\">'+response+'</div>');
					jqN('#response-text').fadeIn('slow').animate({opacity:1.0},3000).fadeOut('slow');
					
					}
					
					if(jqN('status', msg).text() == '1'){
					
						jqN.cookie('sn_nbc_username', jqN('userName',msg).text(), { domain: ".nbc.com" });
						
						jqN('.sn_login_form_div').hide();
						jqN('#ugc_submit').show();
						if(logState==1){
							jqN('#mynbcLoginForm').hide('slow');
					hideVisible('#A-searchform',0);
					hideVisible('#nbc_MastLogo',0);
							var userName = jqN('userName', msg).text();
							jqN('#myNBC_log_status').html('<a href=\"http://my.nbc.com/'+userName+'\" title=\"'+userName+'\">Hello, '+userName+'</a>');
							jqN('#mynbc_login_out').addClass('logout');
							logState = 1;
						}else{return false;}
					}
				}
			})
	})
	
}

function send2FriendWebEx(obj)
{
    var arr_parts   = obj.id.split('_');
    var elem_num    = arr_parts[1];
    var prefix      = arr_parts[0];
    if (prefix.indexOf('MostPopular') != -1) {
        var appendThis = 'MostPopular';
    } else {
        var appendThis = '';
    }
    var email       = encodeURI(document.getElementById('email'+appendThis+'_'+elem_num).value); // get the senders email
    var friendemail = encodeURI(document.getElementById('friendemail'+appendThis+'_'+elem_num).value); // get the friends email
    var subject     = encodeURI(document.getElementById('subject'+appendThis+'_'+elem_num).value); // get the subject
    var message     = "Your friend knew you'd be dying to check out this latest info from NBC.com...\n";
    message         += document.getElementById('message'+appendThis+'_'+elem_num).value; // get the message
    var message     = escape(message); // get the message
    var url         = escape(document.getElementById('url'+appendThis+'_'+elem_num).value); // get the url
    var mediaName   = encodeURI(document.getElementById('mediaName'+appendThis+'_'+elem_num).value); // get the media name
    var mediaDesc   = encodeURI(document.getElementById('mediaDesc'+appendThis+'_'+elem_num).value); // get the media desc 
    var bool_email       = validateEmailLocal(email);
    var bool_friendemail = validateEmailLocal(friendemail);
    if (!bool_email) {
        // alert('Please enter a valid e-mail address for yourself.');
jqN('.socialNetworkLinks').after('<p class="email_alert">Please enter a valid e-mail address for yourself.</p>')
        document.getElementById('email'+appendThis+'_'+elem_num).focus();
setTimeout('jqN(".email_alert").fadeOut("slow",function() {jqN(this).remove()})', 3000);
        return false;
    }
    if (!bool_friendemail) {
        // alert("Please enter a valid friend's e-mail address.");
		jqN('.socialNetworkLinks').after("<p class=\"email_alert\">Please enter a valid friend's e-mail address.</p>")
		setTimeout('jqN(".email_alert").fadeOut("slow",function() {jqN(this).remove()})', 3000);

        document.getElementById('friendemail'+appendThis+'_'+elem_num).focus();
        return false;
    }

    // the 'mediaName' and 'mediaDesc' form the HTML form have the same values already
    // assigned to the 'message' variable, so go ahead and set 'mediaDesc' and 'mediaName'
    // to empty string as a workaround:
    mediaName = '';
    mediaDesc = '';
    jqN.get("/app/form_mail/send2friend.scet.php?email="+email+"&friendemail="+friendemail+"&subject="+subject+"&message="+message+"&url="+url+"&mediaName="+mediaName+"&mediaDesc="+mediaDesc, function(data) {
            confirmItLocal(elem_num,appendThis);
                  });
}

function confirmItLocal(iteration,appendThis)
{

    //
    document.getElementById('friendemail'+appendThis+'_'+iteration).value = '';
    jqN('.scet_send_confirmation'+appendThis+'_'+iteration).html("<h5>Your email was sent. Enter another email address to send again.</h5>");
setTimeout("jqN('.shareBox h6').remove().fadeOut('slow');", 3000);
}

function validateEmailLocal(value)
{
    var good = /^([\w\d\.\-_]+)@([\w\d\.\-]+)$/i;
    var evil =  /[^a-z0-9\@_\-\.]+/i;
        
    if (good.test(value) && !evil.test(value)) {
        return true;
    } else {
        return false;
    }
}

// jqN('#A-searchform input').hint();

function setCommentView (){
	jqN('.sn_comment_form_div').hide();
	jqN('.sn_comment_div').show();
	jqN('#post_comm').removeClass('isCommTab');
	jqN('#user_comm').addClass('isCommTab');
	jqN('.sn_comment_pager_div').show();
}
function setCommentPost (){
	jqN('.sn_comment_div').hide();
	jqN('.sn_comment_form_div').show();
	jqN('#user_comm').removeClass('isCommTab');
	jqN('#post_comm').addClass('isCommTab');
	jqN('.sn_comment_pager_div').hide();
}
function displayRating(id)
		{			
			var result = nbcu.sn.rating.getRatingByContent(null, 'BLOG_POST-'+id, null, null, 'SS_BLOG');
			/*alert("Avg Rating value: " + result["avgRatingValue"] + " | Rating count: " + result["ratingCount"]);*/
			
			var avg = result['avgRatingValue'];
			
			var rnd = Math.round(avg);
			var per = rnd*20;
			
			jqN("#"+id+" .current-rating").css({"width":per+"%"});
			jqN("#"+id+" .current-rating").html("Currently "+rnd+" out of 5 stars.");
			
		}


jqN.fn.clearForm = function(){
	return this.each(function(){
		var type = this.type, tag = this.tagName.toLowerCase();
		if(tag == 'form')
			return jqN(':input',this).clearForm();
		if(type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if(type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if(tag == 'select')
			this.selectedIndex = -1;		
	});
}

jqN.fn.toggleShare = function(){
	return this.each(function(){
		if (jqN(this).parent().next('.jf_sharebx').css("display") == "none") {
			jqN(this).parent().next('.jf_sharebx').slideDown('fast');
		} else{
			jqN(this).parent().next('.jf_sharebx').slideToggle('fast');
		}
	jqN(this).toggleClass("makeOpc");												
	});
};

function valForm(){
		var msg='';
		if(jqN('#url').val()== '' || jqN('#url').val() == 'URL'){
			msg += 'Please enter the url of the site\n';
		}
		if(jqN('#contentTitle').val()== '' || jqN('#contentTitle').val() == 'Title'){
			msg += 'Please enter the title of post\n';
		}
		if(jqN('#postText').val()== ''){
			msg += 'Please enter the description of post\n';
		}
		if(jqN('#show').val()== '' || jqN('#show').val() == '-- SELECT --'){
			msg += 'Please select the show\n';
		}
		if(jqN('#tags').val()== '' || jqN('#tags').val() == 'Tags'){
			msg += 'Please enter a couple tags for your post\n';
		}
		if(msg != ''){
			alert(msg);
			return false;
		}else{
			return true;
		}
	return false;
}

function pageScroll(){
	window.scrollBy(0,0);
	scrolldelay = setTimeout('pageScroll()',50);
}
function stopScroll(){
	clearTimeout(scrolldelay);
}

