jQuery.noConflict();

var contextPath = '';
var handlePrefix = '123456789';

jQuery(document).ready(function(){

        if(window.location.hostname != 'sharegeo.edina.ac.uk'){
            colourBanner(window.location.hostname);
        }
        else{
            handlePrefix = '10672';
        }

        if(jQuery('#aspect_submission_StepTransformer_list_submit-describe').length > 0 ){
            populateTooltips();
        }

        if(jQuery('#file_news_div_news').length > 0 ){
            createLatestItems();
        }

        if(jQuery('#aspect_submission_StepTransformer_field_dc_coverage_spatial').length > 0){
            rearrangeSpatialField();
        }

        if(jQuery('#aspect_artifactbrowser_ItemViewer_div_item-view').length > 0){
            rearrangeItemView();
        }

        fix_links();
    });

// ensure labels have hints in deposit page
function populateTooltips() {
    var form = jQuery('#aspect_submission_StepTransformer_list_submit-describe');
    form.find('li').each(function() {        
            var item = jQuery(this);
            var label = item.children().first();
            
            var div = label.next();
            var help = div.children('.field-help');
            
            if(help.length == 0){
                help = label.next().children('.composite-help');
            }
            
            label.attr('title', help.text());
        });
}

// colour dev and beta banner
function colourBanner(hostname) {
    if(hostname == 'dlib-oxgangs.ucs.ed.ac.uk'){
        jQuery('#headermenu').css('background-color', 'red');
    }
    else if(hostname == 'devel.edina.ac.uk'){
        jQuery('#headermenu').css('background-color', 'brown');
    }
}

// create latest items on home page
function createLatestItems() {
    var div = jQuery('#file_news_div_news');
    var url = div.find('a')[0];
    var count = 0;

    jQuery.get(url, function(data) {
            
            div.append('<h2>Latest Items</h2>');
            var feed = jQuery(data);
            var list = '<ul>';
            feed.find('item').each(function() {
                    var item = jQuery(this);
                    var title = item.find('title').text();
                    var link = item.find('link').text();
                    var date = item.find('pubDate').text().substr(5, 11);
                    list = list + '<li><a href=' + link + '>' + title + ' (' + date + ')</a></li>';
                    if(count == 4){
                        return;
                    }
                    else {
                        ++count;
                    }
                });

            div.append(list);
        });
}

// modify links invarious ways
function fix_links() {

    // don't display submissions navigation link
    var submissionsURL = contextPath + "/submissions";
    var contributeLink = jQuery('#aspect_artifactbrowser_Navigation_list_account').find('a[href=' + submissionsURL + ']');

    if(contributeLink.length > 0){
        contributeLink.parent().css('display', 'none');
    }

    // change submit link to open sharegeo community submit
    var submissionPage = jQuery('#aspect_submission_Submissions_div_submissions').find('a[href="/dspace/submit"]');
    if(submissionPage.length > 0){
        submissionPage.attr('href', contextPath +
                            '/handle/' + handlePrefix +
                            '/2/submit');
    }

    // don't display community navigation link
    
    var communityURL = contextPath + "/community-list";
    var collectionLink = jQuery('#aspect_artifactbrowser_Navigation_list_browse').find('a[href=' + communityURL + ']');
    if(collectionLink.length > 0){
        collectionLink.parent().css('display', 'none');
    }

    // remove This Collection navigation links
    var browseLists = jQuery('#aspect_artifactbrowser_Navigation_list_browse').find('ul[class=ds-simple-list]');
    if(browseLists.length > 1)
    {
        browseLists.last().parent().css('display', 'none');
    }
}

// rearrange spatial control in ingest describe form
function rearrangeSpatialField(){

    // from text field get parent div
    var textField = jQuery('#aspect_submission_StepTransformer_field_dc_coverage_spatial');
    var parent = textField.parent();
    
    // currently inserted values
    var previous = parent.find('.ds-previous-values');

    // move current value above text field
    previous.insertBefore(textField);
    //previous.append('<a href="javascript:editMap()">Edit Extents</a>');

    addButton = parent.find('.ds-add-button');
    deleteButton = parent.find('.ds-delete-button');
    help = parent.find('.field-help');

    //  move delete button after help
    deleteButton.insertAfter(help);
    
    // enable help and move it before text field
    help.css('display', 'block');
    help.insertBefore(textField);

    //
    var extents = jQuery('input[name=dc_coverage_spatial_selected]:first-child');
    jQuery('<p>Your data has been validated and the following bounding box coordinates extracted.  To visualise this extent on a map and have the option to change them click <a href="javascript:editMap()">Edit Extents</a>.</p>').insertBefore(extents);
    extents.attr('disabled', 'disabled');
}

// do to edit map
function editMap() {
    var form = jQuery('#aspect_submission_StepTransformer_div_submit-describe');
    form.append('<input name="adjust-map" type="hidden" value="1"></input>');     
    jQuery('#aspect_submission_StepTransformer_field_submit_next').click();
}

// change view item page
function rearrangeItemView(){
    var item = jQuery('#aspect_artifactbrowser_ItemViewer_div_item-view');
    var icon = jQuery('#org_edina_sharegeo_app_xmlui_aspect_sharegeo_OpenViewItem_div_download-all');

    //console.info(item.find('.license-info'));

    // hide list os files
    item.find('h2').hide();
    item.find('.file-list').hide();

    // move download icon above license info
    var licenseInfo = item.find('.license-info');
    icon.insertBefore(licenseInfo);
}
