 $(document).ready(function() {
   
   // adding search type toogler and detecting which area to switch to
   $('.dgs_search_toggler').each(function() {
   
        var $me = $(this);
        var $search_box = $me.parent().parent().parent().parent();
        
        $me.click(function() {
            $me.blur();            
            $search_box.slideUp('slow', function(){
                $search_box.siblings().slideDown('slow');
            });        
        });   
   });
   
   // showing results item links on hover
   $('.dgs_results_item').each(function() {
        
        var $item = $(this);
   
        $item.hover(function() {
            $('.dgs_results_item_links', $item).css('visibility', 'visible');     
        }, function() {
            $('.dgs_results_item_links', $item).css('visibility', 'hidden');        
        });
   
   
   });
   
    // cleaning up forms from help texts
    $('#dgs_content form').submit(function() {
        $('input', $(this)).not('[title=""]').each(function() {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });
        
        // getting values and form name for last state cookie
        var form_data = ""
        
        // serializing the form elements
        $('input:visible, select:visible',this).each(function() {
            if ($(this).val() != '') {
                form_data += '&' + $(this).attr('name') + '=' + $(this).val();
            }
        });
        
        var form_name = $(this).parent().attr('id');
        $.cookie('dgs_118_last', 'form_name='+ form_name + form_data, {path: '/'});        
    });

   // creating YP advanced search form data collection
   $('#dgs_search_yp_advanced form').submit(function() {

        var who_what = $('#id_who_what', this).val();
        var zip = $('#id_zip', this).val();
        var city = $('#id_city', this).val();
        var street = $('#id_street', this).val();
        var area = $('#id_area', this).val();
        var phone = $('#id_phone', this).val();        

        $('#id_keyword').val($.trim(who_what + ' ' + phone));
        $('#id_location').val($.trim(zip + ' ' + city + ' ' + area));           
   });

   // creating WP advanced search form data collection
   $('#dgs_search_wp_advanced form').submit(function() {

        var who_what = $('#id_who_what', this).val();
        var zip = $('#id_zip', this).val();
        var city = $('#id_city', this).val();
        var street = $('#id_street', this).val();
        var area = $('#id_area', this).val();
        var phone = $('#id_phone', this).val();        

        $('#id_firstname').val($.trim(who_what + ' ' + phone));
        $('#id_location').val($.trim(zip + ' ' + city + ' ' + area));
                   
   });

   // attaching search reset link
   $('.dgs_search_reset a').click(function() {   
        $(this).parents('form').find('input').val('');
        $(this).parents('form').find('select').val('');
   });
   
    // attaching map toggler
    $('#dgs_search_map_and_routes_tab_map').click(function() {
        $(this).addClass('active').blur();
        $('#dgs_search_map_and_routes_tab_route').removeClass('active');
        $('#dgs_search_map_and_routes_tabs').attr('class', 'map');
        $('#dgs_search_route').fadeOut('normal',function() {
            $('#dgs_search_map').fadeIn('normal');
        });        
    });

    $('#dgs_search_map_and_routes_tab_route').click(function() {
        $(this).addClass('active').blur();
        $('#dgs_search_map_and_routes_tab_map').removeClass('active');
        $('#dgs_search_map_and_routes_tabs').attr('class', 'route');
        $('#dgs_search_map').fadeOut('normal',function() {
            $('#dgs_search_route').fadeIn('normal');
        });        
    });
    
    // attaching map submit
    $('#dgs_search_map_form').submit(function() {        
        var address = $('#street', this).val();
        var zip = $('#zip', this).val();
        var city = $('#city', this).val();        
        window.open('http://www.dgs.dk/vbw/kort/resultat.do?areaCountry=dk&ypAddress='+ address +'&addressPostalID='+ zip +'&addressPlaceName='+ city +'', "dgs_map");
        return false
    });

    // attaching route submit
    $('#dgs_search_route_form').submit(function() {        
        
        var from_address = $('#from_address', this).val();
        var from_zip = $('#from_zip', this).val();
        var from_city = $('#from_city', this).val();        
        
        var to_address = $('#to_address', this).val();
        var to_zip = $('#to_zip', this).val();
        var to_city = $('#to_city', this).val();        
        
        window.open('http://www.dgs.dk/vbw/kort/rute-resultat.do?from.g='+ from_address +'&from.b='+ from_city +'&from.z='+ from_zip +'&to.g='+ to_address +'&to.b'+ to_city +'=&to.z='+ to_zip +'', "dgs_map");
        return false
    });

    // form hints
    $('#dgs_content input').not('[title=""]').each(function() {
    
        var help_text = $(this).attr('title');
    
        if($(this).val('')) {
            $(this).addClass('help_text').val(help_text);
        }
        
        $(this).focus(function() {
            if($(this).val() == help_text) {
                $(this).removeClass('help_text').val('');
            }            
        });

        $(this).blur(function() {
            if($(this).val() == '') {
                $(this).addClass('help_text').val(help_text);
            }
        });
    });

    
    // activating form if cookie is placed with a previous search
    var cookie_data = $.cookie('dgs_118_last');

    if (cookie_data != '') {
        var cookie_object = dgs_strToObj(cookie_data);
        // if form is detected on page matching cookie data then proceed.
        if ( $('#'+cookie_object.form_name).size() > 0) {
            // setting active map/route tab or form depending on form name            
            if (cookie_object.form_name == 'dgs_search_map') {
                $('#dgs_search_map_and_routes_tab_map').click();
            } else if (cookie_object.form_name == 'dgs_search_route') {
                $('#dgs_search_map_and_routes_tab_route').click();            
            } else {
                $('#'+cookie_object.form_name).show().siblings().hide();            
            }
        }
        
            // adding data from cookie to field
            $('#dgs_content form:visible').each(function() {
                $.each(cookie_object, function(i, val) {
                    if (val != '') {
                        $('[name="' + i + '"]').val(val).removeClass('help_text');
                    }                    
                });                
            });
        
    }    

 });
 

function dgs_strToObj(strInput) {

    var arrTmp = '';
    var objReturn = {};

    if(strInput != null) {
        arrTmp = strInput.split("&");
   
       $.each(arrTmp, function() {
            var arrTmp2 = this.split('=');
            objReturn[arrTmp2[0]] = arrTmp2[1];
       });
    }
      
   return objReturn;
}
 