/******************CK Editor***********************/
function placeCKEditor(trip_url) 
{
    var editor = CKEDITOR.replace( 'editor1',
    {
        customConfig : '/media/third_party/ckeditor/custom/ckeditor_config.js'
    });
    trip_name = trip_url;
    CKEDITOR.on( 'dialogDefinition', function( ev )
        {
            // Take the dialog name and its definition from the event
            // data.
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
    
            // Check if the definition is from the dialog we're
            // interested on (the "Link" dialog).
            if ( dialogName == 'link' )
            {
                // Get a reference to the "Link Info" tab.
                var infoTab = dialogDefinition.getContents( 'info' );
                // Remove the "Link Type" and "protocol" from the "info" tab.
                infoTab.remove( 'linkType' );
                infoTab.remove('protocol');
                
				// Set the default value for the URL field.
                var urlField = infoTab.get( 'url' );
                urlField['default'] = 'http://';
				
                // Remove the "Target" tab from the "Link" dialog.
                dialogDefinition.removeContents( 'advanced' );
                dialogDefinition.removeContents( 'target' );
            }
            if ( dialogName == 'image' )
            {
                // Get a reference to the "Image Properties" tab.
                var imageProperties = dialogDefinition.getContents( 'info' );
                //look in image.js for the names of these things we're removing
                imageProperties.remove('txtHSpace');
                imageProperties.remove('txtVSpace');
                imageProperties.remove('cmbAlign');
                // Get a reference to the "Link Info" tab.
                var linkTab = dialogDefinition.getContents( 'Link' );
                linkTab.remove( 'cmbTarget' );
                // Set the default value for the URL field.
                var urlField = linkTab.get( 'txtUrl' );
                urlField['default'] = 'http://';
                // Remove the "Advanced" tab
                dialogDefinition.removeContents( 'advanced' );
            }
        });
    
}
/*****************Toggle is_draft**************************/
function toggleIsDraft(value)
{   
    $('#id_is_draft').val(value);
}
/******************Delete functions***********************/
function OnDeleteEntryFromEntry(entry_url, trip_url)
{
    var answer = confirm('You are about to permanently delete this journal entry. Press "Cancel" to stop or "OK" to delete.');
    if (answer) {
        $.post("/trips/"+trip_url+"/journal/"+entry_url+"/delete/",{}, function(data) {
            window.location.replace("/trips/"+trip_url+"/journal/");
        });
    }
}

function OnDeleteEntryFromEntryList(entry_id, entry_url, trip_url)
{
    var answer = confirm('You are about to permanently delete this journal entry. Press "Cancel" to stop or "OK" to delete.');
    if (answer) {
        var temp = "#entryId" + entry_id;
        $(temp).hide("slow");
        $.post("/trips/"+trip_url+"/journal/"+entry_url+"/delete/",{});
   }
}
        
function OnDeleteTrip(trip_id, trip_url)
{
    var answer = confirm('You are about to permanently delete this trip. Press "Cancel" to stop or "OK" to delete.');
    if (answer) {
        var temp = "#tripId" + trip_id;
        $(temp).hide("slow");
        $.post("/trips/"+trip_url+"/delete/",{});
    }
}
function OnRemoveAssociatedTrip(trip_id, trip_url)
{
    var temp = "#tripId" + trip_id;
    $(temp).hide("slow");
    $.post("/trips/"+trip_url+"/remove_association/",{});
    
}
function OnDeleteImage(image_id, trip_url)
{
    var answer = confirm('You are about to permanently delete this image. Press "Cancel" to stop or "OK" to delete.');
        if (answer) {
            var temp = "#imageId" + image_id;
            $(temp).hide("slow");
            $.post("/trips/"+trip_url+"/photos/delete/",{'image_id': image_id});
        }
}
/*******************Date Picker functions*********************/
function singleDatePicker()
{
    Date.firstDayOfWeek = 0;
    Date.format = 'mm/dd/yyyy';
    $('#id_date').datePicker({startDate:'01/01/1996'});
}

function dateRangePicker()
{
    Date.firstDayOfWeek = 0;
    Date.format = 'mm/dd/yyyy';
	$('#id_start_date, #id_end_date').datePicker({startDate:'01/01/1996'});

    $('#id_start_date').bind(
        'dpClosed',
        function(e, selectedDates)
        {
            var d = selectedDates[0];
            if (d) {
                d = new Date(d);
                $('#id_end_date').dpSetStartDate(d.addDays(0).asString());
            }
        }
    );
    $('#id_end_date').bind(
        'dpClosed',
        function(e, selectedDates)
        {
            var d = selectedDates[0];
            if (d) {
                d = new Date(d);
                $('#id_start_date').dpSetEndDate(d.asString());
            }
        }
    );
}
/********************************Update My Account Function********************************/
function OnUpdateAccount(username)
{
    var email = $('#id_email').val();
    var currentPassword = $('#id_current_password').val();
    var newPassword = $('#id_new_password').val();
    var verifyNewPassword = $('#id_verify_new_password').val();
    $.post('/people/'+username+'/account/edit_via_post_only/',
        {'email':email, 'current_password':currentPassword, 'new_password':newPassword, 'verify_new_password':verifyNewPassword},
        function(data){
            $('#message, .formError').text('');
            if (data == '') {
                $('#message').addClass('showMessageStyles');
                $('#message').html('<p>Your account has been updated.</p>');
            }
            else {
                $('#message').removeClass('showMessageStyles');
                var errors_dictionary = JSON.parse(data);
                for (e in errors_dictionary) {
                   $('#'+e+'Error').text(errors_dictionary[e]);
                }
            }
        }
    );
};
/*******************************Twitter Functions*****************************************/
function SubmitTwitterUsername(trip_name_slug) {
    var twitter_username = $('#id_twitter_username').val();
    var spinner = $('<img src="/media/images/loader-small.gif" style="margin-left: 10px;" />').insertAfter('#submitTwitterUsername');
    $.post('/trips/'+trip_name_slug+'/twitter/submit/',
        {'twitter_username':twitter_username, 'json_requested': 1},
        function(data){
            var info = JSON.parse(data);
            if (info['error']) {
                $('#twitterError').text(info['error']).show();
                spinner.remove();
            }
            if (info['tweets']) {
                $('#addTwitterForm').hide();
                $('#twitterFeed').show();
                tweets_dictionary = info['tweets'];
                for (i in tweets_dictionary) {
                    $('.tweets').append('<p>' + tweets_dictionary[i]['text'] + '</p><span>' + tweets_dictionary[i]['month'] + '/' + tweets_dictionary[i]['day'] + '/' + tweets_dictionary[i]['year']  + '</span>');
                }
            }
        }
        );
}
function OnRemoveTwitterFeed(trip_url)
{
    $('#twitterFeed h3').text('Twitter Disabled').css('font-size','1.3em');
    $('#twitterFeed p, #twitterFeed span, #twitterFeed .tweets').hide();
    $('#message').html('<p>We&rsquo;ve stop displaying your Tweets for this trip.</p>');
    $.post("/trips/"+trip_url+"/twitter/remove/",{});
}
/*******************************Hide Section Functions**************************************/
function ToggleSection() {
    $('.mapHelpLink').click(function() {
        $('.mapHelp').toggle();
        return false;
    });
};
/*******************************Share Link*******************************************/

