﻿$(function () {


    $('#sidebar').scrollFollow({
        container: 'main',
        offset: 10
    });

    $('#header input')
    .autocomplete('/autoComplete.aspx')
    .result(function (event, item) {
        location.href = item[1];
    });

    //      

    //    $('#request').click(function() {
    //        location.href = '/foresporsel';
    //        return false;
    //    });

    Date.format = 'dd.mm.yyyy';

    $('.date-pick').datePicker({ showYearNavigation: false });

    $('.date-pick').dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);


    $('.add').click(function () {
        $.post(
            '/Picklist.ashx?action=AddToPicklist&nodeId=' + $(this).attr('rel'),
            function (data) {
                CreatePicklist($('item', data))
            },
            'xml'
        );
        //$(this).removeClass('add').addClass('added');
        return false;
    });

    AttachDelete('.delete');

    //    $.get(
    //        '/Base/KB/GetPicklistXml', 
    //        function(data) {
    //            CreatePicklist($('item', data))
    //            
    //            $('.add').each(function() {
    //                var add = $(this);
    //                $('item', data).each(function() {
    //                    if($(add).attr('rel') == $(this).attr('nodeId')) {
    //                        $(add).attr('style', 'color:#ccc');
    //                    }
    //                });
    //                $(this).show('fast');
    //            });
    //        },
    //        'xml'
    //    ); 

    // Search

    $("#header input").keydown(function (e) {
        //        if (e.keyCode == 13) 
        //        {
        //	        $("#header button").click();
        //	        return false;
        //        }
    });

    $("#header button").click(function () {
        location.href = "/hoteller.aspx?s=" + encodeURIComponent($("#header input").val());
        return false;
    });

});

function CreatePicklist(item) {
    $('#picklist').html('').append('<ul></ul>');

    $(item).each(function() {
        $('#picklist ul').append('<li><a href="' + $(this).attr('url') + '">' + $(this).text() + '</a><br /><a href="#" class="delete" rel="' + $(this).attr('nodeId') + '">Fjern</a>');
        AttachDelete('#picklist li:last .delete');
    });
    
//    $('#request').click(function() {
//        location.href = '/foresporsel';
//        return false;
//    });
    
}

function AttachDelete(selector) {
    $(selector).click(function() {            
        $.post(
            '/Picklist.ashx?action=RemoveFromPicklist&nodeId=' + $(this).attr('rel'), 
            function(data) {
                CreatePicklist($('item', data))
            },
            'xml'
        );    
        return false;
    });
}