$(document).ready(function(){
	var hCol2 = $('.col2').height();
	var hCol3 = $('.col3').height();
	var hCol2Ul = $('.col2 ul').height();
	var hCol3Ul = $('.col3 ul').height();
	
	if(!document.URL.match(/mietgeraete/)) {
		if (hCol2 > hCol3) {
			$('.col3').css('height', hCol2+'px');
			$('.col3 ul').css('height', hCol2Ul+'px');
		}
		else {
			$('.col2').css('height', hCol3+'px');
			$('.col2 ul').css('height', hCol3Ul+'px');
		}
	}
	$('#map_canvas.locations_index').css('height',hCol2+'px');
	
	if($('#tool_rq').hasClass('show-form')) {
		$('#tool_rq').show();
	} else {
		$('#tool_rq').hide();
	}
	
	$('#rq_info').hide();
	$('.show').click(function() {
		$(this).next().slideDown();
		document.location.href='#mietanfrage-stellen';
	});
	$('#show_tool_rq').click(function() {
		$('#tool_rq').slideToggle();
		setTimeout("document.location.href='#mietanfrage-stellen'", 400);
	});
	
	$('.close').click(function() {
		$(this).parent().slideUp();
	});
	
	$('.category_link').click(function() {
		var browserName = navigator.appName; 
		var browserVer = parseInt(navigator.appVersion); 
		if (browserName == "Microsoft Internet Explorer" && browserVer <= 4) {		
			return true;
		} else {
			var element = $(this);
			$.get("/program/getsubcategories/id/" + element.attr('rel'), function(data){
				$('#load_tools_frame').empty();
				
				var patt1=/<li/gi;
				var amount_of = data.match(patt1).length;
				var multiplied = parseInt(amount_of)*40;
	
				$('.col3').css('height', (multiplied+50) + 'px !important');
				$('.col3 ul').css('height', multiplied + 'px !important');
				
				$('#load_tools_frame').html(data);
			});
			return false;
		}
	});
	
	/*
	// Start Standorte overlay
	$('#navigateformshow').click(function (){
	location.reload(true);
	});
	
	$('li.location').mouseover(function(e) {
		$('.locations li.hover').removeClass('hover');
		var element = $(this);
		var type_of = element.attr('id');
		if(type_of == 'left-list') {
			
		}
		$(this).css('position','relative');
		$(this).addClass('hover');
		$('#pop-info').remove();
		$(this).append('<p id="pop-info"></p>');
		$('#pop-info').html('<a href="google-routenplanung/zu/rentas/in/' + element.attr('rel') + '">Google-Routenplanung</a><br><a href="bing-routenplanung/zu/rentas/in/' + element.attr('rel') + '">Bing-Routenplanung</a>');
		return false;
	});
	*/
	// ende Standorte Overlay
	
	

	$('#layout_search_input').focus(function() {
		val = $(this).val();
		if(val == 'Suchen') {
			$(this).val('');
		} else {}
	});
	$('#layout_search_input').blur(function() {
		val = $(this).val();
		if(val == '') {
			$(this).val('Suchen');
		} else {}
	});

	$('#detail_plz').focus(function() {
		val = $(this).val();
		if(val == 'Postleitzahl') {
			$(this).val('');
		} else {}
		$('#detail_city').val('Stadt');
	});
	$('#detail_plz').blur(function() {
		val = $(this).val();
		if(val == '') {
			$(this).val('Postleitzahl');
		} else {}
	});

	$('#detail_city').focus(function() {
		val = $(this).val();
		if(val == 'Stadt') {
			$(this).val('');
		} else {}
		$('#detail_plz').val('Postleitzahl');
	});
	$('#detail_city').blur(function() {
		val = $(this).val();
		if(val == '') {
			$(this).val('Stadt');
		} else {}
	});
	
	$('input[type="submit"]').addClass('submit');
	
	//$("#layout_search_input").keyup(function() {
	//	var element = $(this);
	//	var query = element.val();
	//	
	//	element.addClass('loading-live');
	//	
	//	if(query.length >= 3) {
	//	    $.get('/search/live/?q=' + query, function(text){
	//	    	if(text.length > 0) {
	//				$('#live_results').show();
	//				element.removeClass('loading-live');
	//				$('#live_results').empty();
	//				$('#live_results').html(text);
	//				alert('auf');
	//	    	} else if(text.length == 0) {
	//				$('#live_results').hide();
	//		    	element.removeClass('loading');
	//				alert('zu');
	//	    	}
	//	    });
	//	}
	//});
	
	//$("#layout_search_input").blur(function() {
	//	$(this).removeClass('loading-live');
	//});
	

	jQuery('#tool_search_value').liveSearch({url: '/search/live-tools/?q='});
	jQuery('#layout_search_input').liveSearch({url: '/search/live/?q='});
	/*
	$("#tool_search_value").keyup(function() {
		var element = $(this);
		var query = element.val();
		
		element.addClass('loading-live');
		
		if(query.length > 3) {
		    $.get('/search/live-tools/?q=' + query, function(text){
		    	if(text.length > 0) {
					$('#tool_search_live_results').show();
					element.removeClass('loading-live');
					$('#tool_search_live_results').empty();
					$('#tool_search_live_results').html(text);
		    	} else if(text.length == 0) {
					$('#tool_search_live_results').hide();
			    	element.removeClass('loading');
		    	}
		    });
		}
	});
	
	$("#tool_search_value").blur(function() {
		$(this).removeClass('loading-live');
	});
	*/
	
	

	$('.security_transport img').mouseover(function() {
		$('#icon_mo_bubble').remove();
		
		var position = $(this).position();
		var title = $(this).attr('title');					
		
		var pos_top = position.top;
			pos_top = (pos_top-26);
		var pos_left = position.left;
			pos_left = (pos_left+24);
		
		var element = '<div id="icon_mo_bubble" style="position:absolute;top:'+pos_top+'px;left:'+pos_left+'px;"><img src="/images/bubble_left_part.png" /><div>'+title+'</div></div>';
		
		$('#wrapper').append(element);
	});
	$('.security_transport img').mouseout(function() {
		$('#icon_mo_bubble').remove();
	});
	
	/* Ihr Standort */
	//if($.cookie('your_service_center'))  {
	//	$('#your-center').empty();
	//	$('#your-center').html($.cookie('your_service_center'));
	//} else {
	    $.get('/index/find-your-location', function(data){
	    	//$.cookie('your_service_center', data);
			$('#your-center').html(data);
	    });
	//}
});

function rmclass(element) {
	element.removeClass('freed');
}

function adclass(element) {
	element.addClass('freed');
}

function closeLiveSearch() {
	$('#live_results').empty();
	$('#live_results').hide();
	$("#layout_search_input").removeClass('loading-live');
}

function closeLiveToolSearch() {
	$('#tool_search_live_results').empty();
	$('#tool_search_live_results').hide();
	$("#tool_search_value").removeClass('loading-live');
}

jQuery.fn.liveSearch = function (conf) {
    var config = jQuery.extend({
        url:            '/search-results.php?q=', 
        id:                'jquery-live-search', 
        duration:        400, 
        typeDelay:        200,
        loadingClass:    'loading', 
        onSlideUp:        function () {}, 
        uptadePosition:    false
    }, conf);

    var liveSearch    = jQuery('#' + config.id);

    // Create live-search if it doesn't exist
    if (!liveSearch.length) {
        liveSearch = jQuery('<div id="' + config.id + '"></div>')
                        .appendTo(document.body)
                        .hide()
                        .slideUp(0);

        // Close live-search when clicking outside it
        jQuery(document.body).click(function(event) {
            var clicked = jQuery(event.target);

            if (!(clicked.is('#' + config.id) || clicked.parents('#' + config.id).length || clicked.is('input'))) {
                liveSearch.slideUp(config.duration, function () {
                    config.onSlideUp();
                });
            }
        });
    }

    return this.each(function () {
        var input                            = jQuery(this).attr('autocomplete', 'off');
        var liveSearchPaddingBorderHoriz    = parseInt(liveSearch.css('paddingLeft'), 10) + parseInt(liveSearch.css('paddingRight'), 10) + parseInt(liveSearch.css('borderLeftWidth'), 10) + parseInt(liveSearch.css('borderRightWidth'), 10);

        // Re calculates live search's position
        var repositionLiveSearch = function () {
            var tmpOffset    = input.offset();
            var inputDim    = {
                left:        tmpOffset.left, 
                top:        tmpOffset.top, 
                width:        input.outerWidth(), 
                height:        input.outerHeight()
            };

            inputDim.topPos        = inputDim.top + inputDim.height;
            inputDim.totalWidth    = inputDim.width - liveSearchPaddingBorderHoriz;

            liveSearch.css({
                position:    'absolute', 
                left:        inputDim.left + 'px', 
                top:        inputDim.topPos + 'px',
                width:        inputDim.totalWidth + 'px'
            });
        };

        // Shows live-search for this input
        var showLiveSearch = function () {
            // Always reposition the live-search every time it is shown
            // in case user has resized browser-window or zoomed in or whatever
            repositionLiveSearch();

            // We need to bind a resize-event every time live search is shown
            // so it resizes based on the correct input element
            $(window).unbind('resize', repositionLiveSearch);
            $(window).bind('resize', repositionLiveSearch);

            liveSearch.slideDown(config.duration);
        };

        // Hides live-search for this input
        var hideLiveSearch = function () {
            liveSearch.slideUp(config.duration, function () {
                config.onSlideUp();
            });
        };

        input
            // On focus, if the live-search is empty, perform an new search
            // If not, just slide it down. Only do this if there's something in the input
            .focus(function () {
                if (this.value !== '') {
                    // Perform a new search if there are no search results
                    if (liveSearch.html() == '') {
                        this.lastValue = '';
                        input.keyup();
                    }
                    // If there are search results show live search
                    else {
                        // HACK: In case search field changes width onfocus
                        setTimeout(showLiveSearch, 1);
                    }
                }
            })
            // Auto update live-search onkeyup
            .keyup(function () {
                // Don't update live-search if it's got the same value as last time
                if (this.value != this.lastValue) {
                    input.addClass(config.loadingClass);

                    var q = this.value;

                    // Stop previous ajax-request
                    if (this.timer) {
                        clearTimeout(this.timer);
                    }

                    // Start a new ajax-request in X ms
                    this.timer = setTimeout(function () {
                        jQuery.get(config.url + q, function (data) {
                            input.removeClass(config.loadingClass);

                            // Show live-search if results and search-term aren't empty
                            if (data.length && q.length) {
                                liveSearch.html(data);
                                showLiveSearch();
                            }
                            else {
                                hideLiveSearch();
                            }
                        });
                    }, config.typeDelay);

                    this.lastValue = this.value;
                }
            });
    });
};
