﻿$(function()
{    
    ////////////////////////////
    //limitChars('edit_description', 4000, 'charcounter');
    //limitChars('add_description', 4000, 'charcounter');
    //$('#edit_description').wordCount({size: '4000', counter:'charcounter' });
    ////////////////////////////
    $('#edit_description').keyup(function(){
        //$('#edit_description').wordCount({size: '4000', counter:'charcounter' });
        limitChars('edit_description', 4000, 'charcounter');
    });
    $('#add_description').keyup(function(){
        //$('#edit_description').wordCount({size: '4000', counter:'charcounter' });
        limitChars('add_description', 4000, 'charcounter');
    });
    ////////////////////////////
    var auth = $("#userId").val();
    //hide story details
    $('.storyDetails').css('display', 'none');
    $('.thankyourmentor').css('display', 'none');
    
    var qs = $.jqURL.qs();
    
    if (qs != undefined)
    {
    var sid = $.jqURL.get('sid');
    }
    if(sid != undefined || sid !="" )
    {
        $('#detailsStory' + sid).slideToggle(500);   
//        $.ajax(
//        {
//            type: "GET",
//            url: "/_extension/hmapi/api.aspx?section=stories&operation=hitcount&sid="+sid,
//            async: false,
//            success: function(msg)
//            {
//                //do nothing;
//            },
//            error: function(msg)
//            {
//                //do nothing;
//            }
//        });
    }
    var sid = $.jqURL.get('sid');
    if(sid != "" )
    {
        $('#details' + sid).slideToggle(500);   
    }
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  DISPLAY DETAILS OF STORY - MENTEE VIEW
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.storyTitle').click(function()
    {
       var id = $(this).attr('id');  
       $('#details' + id).slideToggle(500);
       return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  DISPLAY ADD TAG FORM
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.AddTags').click(function()
    {
       if(auth != "")
       {
            var id = $(this).attr('id');  
            var tagId = id.substr(4);
            $('#TagForm' + tagId).slideToggle('slow');
            return false;
       }
       else
       {
            alert('You have to be logged in to add tags');
       }
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD THE TAG ENTERED  - REPLY WITH SUCCESS/FAIL MESSAGE!
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('input.AddStoryTag').click(function()
    {   
        gid = $(this).attr("id");
        
        id = gid.substr(5);
        var tags = $('#tags' + id).val();
       
        //alert("tags!!!"+tags +"" +id);
        
        if(tags != "")
        {

            var data = $("#fmStoryTag"  + id).serialize();
            
            if (data == null || data =="")
            {
                var newdata = $("#frmStory").serialize();
                data = newdata; 
            }
            
              //alert(data);

            $.ajax
            ({
                type: "POST",
                url: "AddTags.publisha",
                data: data,
                async: false,
            
                success: function(msg)
                {  
                  alert('Your tag was added successfully ');
                  $('#TagForm' + id).slideToggle('slow');     
                },
                error: function(msg)
                {
                    alert('Oops - your tag was either blocked or a duplicate!');
                }
            });
        }
        else
        {
            alert('Please enter the tag you wish to add!');
        }        
        return false;
    });
    
    //TODO: DISPLAY THANK YOU BANK!!!
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD THANK YOU TO MENTOR THANK YOU BANK
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.lnkThankU').click(function() 
    {
        
        var isAuth = 'True';
        if(isAuth =='True' )
        {
            var fullId = $(this).attr("id"); 
            //alert(fullId);  
            var id = fullId.substr(6);
            //alert(id);
            var data = $("#fmThank"  + id).serialize(); 
            //alert(data);
            
            $.ajax
            ({
                type: "POST",
                url: "ThankMentor.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                    alert('Your thank you has been added to the mentor "thank you bank"');
                },
                error: function(msg)
                {
                   alert('Ooops! Your thank you was not recorded! Please try again!');
                }
            });
        }
        else
        {
            alert("You have to be logged in to thank a mentor.");
        }
      return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  EDIT STORY - DO VALIDATE FORM BEFORE SUBMITTING TEXT
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#EditStoryUpdateButton').click(function()
    {
        var title = $('#editTitle').val();
        var storyText = $('#edit_description').val();
        var lifeEdit = $('input[@name=lifeEdit]:checked').size()
        var workEdit = $('input[@name=workEdit]:checked').size()
        var learnEdit = $('input[@name=learnEdit]:checked').size()
        if(title != "" && storyText!="")// != "" && tags != "")
        {
           if(lifeEdit!=0 || workEdit!=0 || learnEdit!=0)
                $('#fmEditStory').submit();
            else
                alert('Please select from Life, Work or Learning');
        }
        else
        {
            alert('All field are mandatory!');
        }
    });
    
    $('#AddNewStoryUpdateButton').click(function()
    {
        var title = $('#newTitle').val();
        var storyText = $('#add_description').val();
        var lifeAdd = $('input[@name=lifeAdd]:checked').size()
        var workAdd = $('input[@name=workAdd]:checked').size()
        var learnAdd = $('input[@name=learnAdd]:checked').size()
       if(title != "" && storyText!="")// != "" && tags != "")
        {
           if(lifeAdd!=0 || workAdd!=0 || learnAdd!=0)
                $('#fmAddStory').submit();
            else
                alert('Please select from Life, Work or Learning');
        }
        else
        {
            alert('All field are mandatory!');
        }
    });
});


var bCancel = false;
function processForm(oForm) 
{
	if (bCancel) 
	{
		window.location = history.go(-1);
		return false;
	}
	return true;
}

function fmReportStory(id)
{
    var isLoggedIn = document.getElementById("isAuthenticated").value;
    var oForm = document.getElementById("fmReport" + id);
    if(isLoggedIn == 'True')
    {    
        oForm.action = "/publisha.content/report/confirm.publisha";
        oForm.submit();
    }
    else
    {
        alert('You have to be logged in to report the story!');
        return false;
    }
}

function limitChars(textid, limit, infodiv)
{
    var text = $('#'+textid).val();
    var textlength = text.length;
    if(textlength > limit)
    {
        $('#' + infodiv).html('all '+limit+' characters used');
        $('#'+textid).val(text.substr(0,limit));/// <reference path="spell.js" />
        return false;
    }
    else
    {
        $('#' + infodiv).html((limit - textlength) +' characters left.');
        return true;
    }
}



