﻿$(function () {
    $('.hover').bind('mouseover', function () {
        $(this).stop(true, false).fadeTo("slow", 0.6);
    }).bind('mouseout', function () {
        $(this).stop(true, false).fadeTo("slow", 1.0);
    });

    $('#scroll-pane').css('overflow', 'hidden');

    var difference = $('#scroll-content').height() - $('#scroll-pane').height();

    if (difference > 0) {
        var proportion = difference / $('#scroll-content').height();
        var handleHeight = Math.round((1 - proportion) * $('#scroll-pane').height());
        handleHeight -= handleHeight % 2;

        $("#scroll-pane").after('<\div id="slider-wrap"><\div id="slider-vertical"><\/div><\/div>');
        $("#slider-wrap").height($("#scroll-pane").height() - 22);

        $('#slider-vertical').slider({
            orientation: 'vertical',
            min: 0,
            max: 100,
            value: 100,
            slide: function (event, ui) {
                var topValue = -((100 - ui.value) * difference / 100);
                $('#scroll-content').css({ top: topValue });
            },
            change: function (event, ui) {
                var topValue = -((100 - ui.value) * difference / 100);
                $('#scroll-content').css({ top: topValue });
            }
        });

        $(".ui-slider-handle").css({ height: handleHeight, 'margin-bottom': -0.5 * handleHeight });

        var origSliderHeight = $("#slider-vertical").height();
        var sliderHeight = origSliderHeight - handleHeight;
        var sliderMargin = (origSliderHeight - sliderHeight) * 0.5;
        $(".ui-slider").css({ height: sliderHeight, 'margin-top': sliderMargin });

    }

    $(".ui-slider").click(function (event) {
        event.stopPropagation();
    });

    $("#slider-wrap").click(function (event) {
        var offsetTop = $(this).offset().top;
        var clickValue = (event.pageY - offsetTop) * 100 / $(this).height();
        $("#slider-vertical").slider("value", 100 - clickValue);
    });

    $("#scroll-pane,#slider-wrap").mousewheel(function (event, delta) {
        var speed = 5;
        var sliderVal = $("#slider-vertical").slider("value");

        sliderVal += (delta * speed);
        $("#slider-vertical").slider("value", sliderVal);

        event.preventDefault();
    });

    // Search

    $('#search_selector').click(function () {
        $('#search_options').animate({ height: 'toggle' })
    });

    //$('#search_options li').click(function () {
    //    $('#search_category').val($(this).attr('rel'));
    //    $('#search').attr('placeholder', 'Search ' + $(this).attr('rel'));
    //    $('#search_options').slideUp();
    //});

    $("#age-slider").slider({
        range: true,
        min: 0,
        max: 100,
        values: [20, 30],
        slide: function (event, ui) {
            $("#AgeFrom").val(ui.values[0]);
            $("#AgeTo").val(ui.values[1]);
            $(".AgeFromLabel").text(ui.values[0]);
            $(".AgeToLabel").text(ui.values[1]);
        }
    });
    $("#AgeFrom").val($("#age-slider").slider("values", 0));
    $("#AgeTo").val($("#age-slider").slider("values", 1));
    $(".AgeFromLabel").text($("#age-slider").slider("values", 0));
    $(".AgeToLabel").text($("#age-slider").slider("values", 1));

    $("#height-slider").slider({
        range: true,
        min: 50,
        max: 220,
        values: [160, 190],
        slide: function (event, ui) {
            $("#HeightFrom").val(ui.values[0]);
            $("#HeightTo").val(ui.values[1]);
            $(".HeightFromLabel").text(ui.values[0] + "cm");
            $(".HeightToLabel").text(ui.values[1] + "cm");
        }
    });
    $("#HeightFrom").val($("#height-slider").slider("values", 0));
    $("#HeightTo").val($("#height-slider").slider("values", 1));
    $(".HeightFromLabel").text($("#height-slider").slider("values", 0) + "cm");
    $(".HeightToLabel").text($("#height-slider").slider("values", 1) + "cm");
    placeholder();
});

/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
*
* Version: 3.0.2
* 
* Requires: 1.2.2+
*/
(function (c) { var a = ["DOMMouseScroll", "mousewheel"]; c.event.special.mousewheel = { setup: function () { if (this.addEventListener) { for (var d = a.length; d; ) { this.addEventListener(a[--d], b, false) } } else { this.onmousewheel = b } }, teardown: function () { if (this.removeEventListener) { for (var d = a.length; d; ) { this.removeEventListener(a[--d], b, false) } } else { this.onmousewheel = null } } }; c.fn.extend({ mousewheel: function (d) { return d ? this.bind("mousewheel", d) : this.trigger("mousewheel") }, unmousewheel: function (d) { return this.unbind("mousewheel", d) } }); function b(f) { var d = [].slice.call(arguments, 1), g = 0, e = true; f = c.event.fix(f || window.event); f.type = "mousewheel"; if (f.wheelDelta) { g = f.wheelDelta / 120 } if (f.detail) { g = -f.detail / 3 } d.unshift(f, g); return c.event.handle.apply(this, d) } })(jQuery);

function placeholder() {
    var detect = navigator.userAgent.toLowerCase();
    if (detect.indexOf("safari") > 0) return false;
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute("type") == "text") {
            if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
                inputs[i].value = inputs[i].getAttribute("placeholder");
                inputs[i].onclick = function () {
                    if (this.value == this.getAttribute("placeholder")) {
                        this.value = "";
                    }
                    return false;
                }
                inputs[i].onblur = function () {
                    if (this.value.length < 1) {
                        this.value = this.getAttribute("placeholder");
                    }
                }
            }
        }
    }
}
window.onload = function () {
}

function handleEnter(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        window.location = "http://eskimo.gardarth.com/Search/" + inField.value;
    }
}

$("#search").keyup(function (event) {
    if (event.keyCode == 13) {
        window.location = "http://eskimo.gardarth.com/Search/" + $(this).val();
    }
});
