$(document).ready(function($) {
	//Set select-boxes with bottom & shadow div's
	$('select#days-field').selectmenu({wrapperElement:"<div class = select-div>"});
	$('select#distance-field').selectmenu({wrapperElement:"<div class = select-div>"});
	$('select#cat-field').selectmenu(); 
	//$('select#cat-field-sec').selectmenu();
	//$('select#cat-field-third').selectmenu();
});



function initSpecialSearch()
{
    $(".range li").click(function(x, e) {
        clickRange(this);
    });

    $(".range li").mouseover(function(x, e) {
        overRange(this);
    });

    $(".range li").mouseout(function(x, e) {
        outRange(this);
    });

    $("#cat-list li").click(function(x, e) {
        chooseCat(this);
    });

    $("#subcat-list li").click(function(x, e) {
        chooseSubCat(this);
    });

}

var current_search_bloc = '';
var moving_bloc = false;
function activeSpecialSearch(e)
{
	$('#query-suggest').hide().html('');
    if ( ! moving_bloc)
    {
        moving_bloc = true;
        if ($(e).hasClass('tab-active'))
        {
            $('#'+$(e).attr('id')+'-bloc').slideUp(300, function() {
                $(e).removeClass('tab-active');
                $('.search-bloc-element').css('display', 'none');
                moving_bloc = false;
                $("#main-content").css("z-index", 1);
            });
            current_search_bloc = '';
            
        } else {

            //$('#special-search li').removeClass('tab-active');
            $("#main-content").css("z-index", 2);

            var new_search_bloc = $(e).attr('id');
            if (current_search_bloc != '')
            {
                $('#'+current_search_bloc+'-bloc').slideUp(300, function() {
                    $('#'+current_search_bloc).removeClass('tab-active');
                    $(e).addClass('tab-active');
                    $('#'+new_search_bloc+'-bloc').slideDown(300, function() {
                        moving_bloc = false;
                    });
                    current_search_bloc = new_search_bloc;
                });
            } else {
                $(e).addClass('tab-active');
                $('.search-bloc-element').css('display', 'block');
                $('#'+new_search_bloc+'-bloc').slideDown(300, function() {
                    moving_bloc = false;
                });
                current_search_bloc = new_search_bloc;
            }

        }
    }

}


function clickRange(e) {
    //alert(this.value);
    //alert($(e).attr('value'));
    var range = getRangeName(e);
    var state = getRangeStatus(range);

    if (state == 0) {
        $(e).addClass('hl');
        $('#'+range+'-fst-field').val(e.value);
        //$(e).addClass('first');
        state = 1;
    } else if (state == 1) {

        //$(e).addClass('second');
        if ($('#'+range+'-fst-field').val() == e.value) {
            $(e).parent().children().removeClass('hl');
            $('#'+range+'-fst-field').val('');
            state = 0;
        } else {
            $(e).addClass('hl');
            if (e.value <  $('#'+range+'-fst-field').val())
            {
                $('#'+range+'-scd-field').val($('#'+range+'-fst-field').val());
                $('#'+range+'-fst-field').val(e.value)
            }
            else
                $('#'+range+'-scd-field').val(e.value);
            state = 2;
        }
    } else if (state == 2) {
         $(e).parent().children().removeClass('hl');
         $('#'+range+'-fst-field').val(e.value);
         $('#'+range+'-scd-field').val('');
         $(e).addClass('hl');
         state = 1;
    }
    setRange(range, $('#'+range+'-fst-field').val(), $('#'+range+'-scd-field').val())
	
	if ($('#distance2-fst-field').val() != '' || $('#distance2-scd-field').val() != '')
		$('#distance-filter-value').css('display', 'block');
	else 
		$('#distance-filter-value').css('display', 'none');
		
	if ($('#price2-fst-field').val() != '' || $('#price2-scd-field').val() != '')
		$('#price-filter-value').css('display', 'block');
	else 
		$('#price-filter-value').css('display', 'none');
}

function overRange(e)
{
    var range = getRangeName(e);
    var state = getRangeStatus(range);
    var v1 = $('#'+range+'-fst-field').val();
    var v2 = e.value;

    if (v1 > v2) {var tmp = v1;v1 = v2;v2 = tmp;}

    if (state == 1 && v1 < v2) {
        $(e).parent().children().each(function (i) {
            if (this.value >= v1 && this.value <= v2)
                $(this).addClass('hl');
            else
                $(this).removeClass('hl');
        });

    }

    if ($('#'+range+'-scd-field').val() == '') setRange(range, v2, v1)

}

function outRange(e)
{
    var range = getRangeName(e);
    var state = getRangeStatus(range);
    if (state == 0) $('#'+range+'-range-from').text('');
}

function getRangeName(e) {
    var range = 'price';
    if ($(e).parent().attr('id') == 'price2-range') range = 'price2';
    if ($(e).parent().attr('id') == 'distance-range') range = 'distance';
    if ($(e).parent().attr('id') == 'distance2-range') range = 'distance2';
    if ($(e).parent().attr('id') == 'hours-range') range = 'hours';
    if ($(e).parent().attr('id') == 'fans-range') range = 'fans';
    return range;
}

function getRangeStatus(range) {
    var status = 0;
    if ($('#'+range+'-fst-field').val() != '') status = 1;
    if ($('#'+range+'-scd-field').val() != '') status = 2;
    return status;
}

function setRange(range, x, y) {
    if ((y == '' || x == y) && parseInt(y) != 0) {
        x = filterRangeValue(range, x);
        $('#'+range+'-range-from').text(x);
        $('#'+range+'-range-to').text('');
    } else if (parseInt(x) < parseInt(y)) {
        x = filterRangeValue(range, x);
        y = filterRangeValue(range, y);
        $('#'+range+'-range-from').text(x);
        $('#'+range+'-range-to').text(y);
    } else if (parseInt(x) > parseInt(y)) {
        x = filterRangeValue(range, x);
        y = filterRangeValue(range, y);
        $('#'+range+'-range-from').text(y);
        $('#'+range+'-range-to').text(x);
    }
}

function filterRangeValue(range, v) {
    if (range == 'distance' || range == 'distance2') if (v >= 1000) v = String(v/1000)+'km'; else v = String(v)+'m';
    if (range == 'price' || range == 'price2') v = String(v)+'€';
    return v;
}



function selectCat(e) {

    if ($('#'+e.name+'-list').css('display') == 'block') $('#'+e.name+'-list').css('display', 'none');
    else $('#'+e.name+'-list').css('display', 'block');

}

function chooseCat(e) {
    /*$('#cat-field').val($(e).text());
    $('#cat-list').css('display', 'none');
    */


    $('#subcat-chosen').html('');
    $('#cat-value').val($(e).attr('value'));
    $('#cat-name').val($(e).text());

    $.get('/category/get-sub/id/'+$(e).attr('value'), function(data) {
        $('#subcat-list').html(data);
        if ($(e).attr('value') != 0)
        {
            $('#subcat-list').parent().css('display', 'block');
            $('#cat-list').parent().css('display', 'none');
            $('#category-chosen').css('display', 'block');
            $('#category-chosen .name').text($(e).text());
        }
    else $('#cat-list').css('display', 'none');
        if ($(e).attr('value') == 0) $('#ext-subcat').css('display', 'none');
    });
}

function chooseSubCat(e) {
    var exists = false;
    $('#subcat-chosen input').each(function (i) {
        if (this.value == e.value) exists = true;
    });
    if ( ! exists) {
        $('#subcat-list').css('display', 'none');
        var html = $('#subcat-chosen').html();
        $('#subcat-chosen').html(html + '<span><input type="hidden" name="subcats[]"  value="'+$(e).attr('value')+'" /><input type="hidden" name="subcats-name[]"  value="'+$(e).text()+'" />'+$(e).text()+' <sup onclick="removeCat(this)">x</sup></span>');
    }
}

function removeCat(e) {
    $(e).parent().remove();
}


function resetCat() {
    $('#category-chosen').css('display', 'none');
    $('#category-chosen .name').text('');
    $('#subcat-chosen').html('');
    $('#subcat-list').parent().css('display', 'none');
    $('#cat-list').parent().css('display', 'block');
}

function starOver(e, x) {
    var n = 1;
    $(e).parent().children().each( function() {
        if (n <= x) $(this).addClass("star-green");
        else $(this).removeClass("star-green");
        n++;
        });
}

function starOut(e) {
    $(e).parent().children().removeClass("star-green");
}

function fixStarsValue(e, i) {
    var width = i * 24;
    $(e).parent().css('width', width+'px');
    $('#stars-value').val(i);
    $('.field .stars-form').val(i);
}


function checkExtSearch() {
    if ($('#what-field').val() == '')
    {
        launchNote($('#extended-search-100').html(), '#cf3018');
        $('#what-field').focus();
        return false;
    }
    return true;
}


function toggleLocations(sfx)
{
    $('#where-field-suggest'+sfx).slideToggle();
    if (sfx == '')
    {
        var e = $('#ext-where').find('.suggest');
        if (e.hasClass('locations-open')) e.removeClass('locations-open');
        else e.addClass('locations-open');
    }
}

function selectLocation(e, id, name, sfx)
{
    $('#where-field'+sfx).val(name);
    $('#location-field'+sfx).val(id);
    toggleLocations(sfx);
}

function selectOldCity(name, sfx)
{
    $('#location-field-result').val('');
    $('#where-field'+sfx).val(name);
    toggleLocations(sfx);
}

function scrollList(direction) {
	this.scrollDirection = direction;		
	$slider = $('.slider-vertical');
	
	var currentValue = $slider.slider('value');
	var singleValue = Math.round(100 / ($('.scroll-content-item').length)+ 4);
	var newValue = null;
	if(direction == 1) {
		newValue = currentValue + singleValue;
	}
	else {
		newValue = currentValue - singleValue;
	}
	
	
	$slider.slider('value',newValue);

};

function checkTime(e) 
{ 
	var atime;
	re = /^(\d{1,2}):(\d{2})(:00)?$/; 
	if(e.value != '') 
	{ 
		if(regs = e.value.match(re)) 
		{ 
			if(regs[1] > 23) 
			{ 
				launchNote($('#extended-search-101').html(), '#cf3018');
				e.value = "";
				window.setTimeout(function ()
				{
		        e.focus();
				}, 0);
			}  
			if(regs[2] > 59) 
			{ 
				launchNote($('#extended-search-101').html(), '#cf3018');
				e.value = "";
				window.setTimeout(function ()
				{
		        e.focus();
				}, 0);					
			} 
			else
			{	
			}
		} 
		else 
		{ 
			launchNote($('#extended-search-101').html(), '#cf3018');
			e.value = "";
			window.setTimeout(function ()
			{
	        e.focus();
			}, 0);		
		}
	}
	else
	{
	}
}

function checkPrice(e) 
{ 
	var aprice;
	re = /^[0-9]+(\,{0,1}||\.{0,1})+(\d{2})*$/; 
	if(e.value != '') 
	{ 
		if(regs = !e.value.match(re))
		{
			launchNote($('#extended-search-102').html(), '#cf3018');
			e.value = "";
			window.setTimeout(function ()
			{
	        e.focus();
			}, 0);
	        return false;
		} 
		else 
		{ 
		}
	}
	else
	{
		 return true;
	}
}
function bottom(){
	  var div=document.getElementById("bottom-days");
	  if(div.style.display=="none"){
	    div.style.display="block";
	  }
	  else div.style.display="none";
}

function selectCategorySpecial(e)
{
    if ($(e).hasClass('main'))
    {
        if ($(e).find('option:selected').val() > 0)
        {
            $(e).next().next().next().next().next().fadeOut('fast');
            loadSubCategoriesSpecial($(e).next().next(), $(e).find('option:selected').val());
        }
        else
        {
            $(e).next().next().next().fadeOut('fast');
            $(e).next().next().next().next().next().fadeOut('fast');
        }
    }
    else if ($(e).hasClass('sub'))
    {
        if ($(e).find('option:selected').val() > 0)
        {
            loadSubCategoriesSpecial($(e).next().next(), $(e).find('option:selected').val());
            $('#cat-field-third-button').css('display', 'block');
        }
        else
        {
            $(e).next().next().next().fadeOut('fast');
        }
    }
    else
    {
        if ($(e).find('option:selected').val() > 0)
        {
        	var value = $(e).find('option:selected').val();
        	$(e).parent().find('.category-id').val(value);
        	if($(e).parent().parent().parent().parent().attr("id") == "shop-details")
        	{
	        	if($(e).find('option:selected').html() == "sonstige")
	        	{
	        		showCatUserSuggest(e);
	        	}
	        	else 
	        	{
	        		showCatSuggestions(e, value);
	        	}
        	}
        }
    }
}


function loadSubCategoriesSpecial (e, id)
{
    $.post('/category/load', {
        iParentCategoryId: id,
        sType: 'options'},
        function(data){
            e.html(data);
            $(e).parent().find('.category-id').val(id);
            $('#cat-field-sec').selectmenu();
            $(e).next().fadeIn('fast');
            $('#cat-field-third').selectmenu(); 
        }
    );
}
