﻿$(document).ready(function() {
    var divclick = false;

    // STAR RATE //
    $('.auto-submit-star').rating({
        required: true,
        callback: function(value, link) {
            // 'this' is the hidden form element holding the current value
            // 'value' is the value selected
            // 'element' points to the link element that received the click.
            $.ajax({
                type: "POST",
                url: "/ServerSet.aspx",
                data: { service: "activity_rating", id: $('#activityId').val(), score: value },
                dataType: "html",
                success: function(result) {
                    $('#rating_text').html(result);
                }
            });
        }
    });

    $('#search_submit').click(function() {
        var search_text = $('#search_text').val();
        search_text = search_text.replace(/ /g, "-").replace(/\+/g, "-").replace(/\%/g, "-").replace(/\|/g, "-").replace(/\?/g, "").replace(/\//g, "-");
        var search_category = "";
        var search_string = "";

        $("input[name='search_category[]']:checked:enabled", '#aspnetForm').each(function() {
            if (search_category != "") {
                search_category += "-" + this.value;
            } else {
                search_category = this.value;
            }
        });
        var search_season = $("input[@name='search_season']:checked").val();
        var search_price = $("input[@name='search_price']:checked").val();
        var search_age = "";
        var search_ageFrom = $('#search_ageFrom').val();
        var search_ageTo = $('#search_ageTo').val();
        var search_location = $('#search_location').val();

        if (search_season != "") {
            search_string = search_season;
        }
        if (search_category != "") {
            if (search_string != "") {
                search_string += "-" + search_category;
            } else {
                search_string += search_category;
            }
        }
        if (search_location != "") {
            search_string += search_location;
        }
        if (search_ageFrom != "00" || search_ageTo != "00") {
            search_age = "alder" + search_ageFrom + search_ageTo;
            if (search_string != "") {
                search_string += "-" + search_age;
            } else {
                search_string += search_age;
            }
        }
        if (search_price != "") {
            if (search_string != "") {
                search_string += "-" + search_price;
            } else {
                search_string += search_price;
            }
        }
        if (search_text != "") {
            if (search_string != "") {
                search_string += "-" + search_text;
            } else {
                search_string += search_text;
            }
        }
        //"/søk/" + search_price + "-" + search_season + "-" + search_category + "-" + search_text;
        //alert("/søk/" + search_string);
        window.location = "/søk/" + search_string;
    });

    // COMMENT //
    // Switch to comment form
    /*$('#comment_form_link').click(function() {
    //Hide comment_link
    $(this).fadeOut('slow');
    //Show  comment form
    $('#comment_form').show('fast');
    //Attach - Remove caption when box is selected
    $('.comment_input').focus(function() {
    this.style.color = 'black';
    if (this.value == this.defaultValue) {
    this.value = '';
    }
    if (this.value != this.defaultValue) {
    this.select();
    }
    });
    //Attach - Insert caption if input text is blank
    $('.comment_input').blur(function() {
    if ($.trim(this.value == '')) {
    this.value = this.defaultValue;
    this.style.color = 'Silver';
    }
    });
    //Attach - Submit form
    $('#comment_submit').click(function() {
    var comment_name = $('#comment_name').val().replace(/</g, '&lt;').replace(/>/g, '&gt;');
    var comment_text = $('#comment_text').val().replace(/</g, '&lt;').replace(/>/g, '&gt;');
    $.ajax({
    type: "POST",
    url: "/ServerSet.aspx",
    data: { service: "activity_comment", id: $('#activityId').val(), name: comment_name, comment: comment_text },
    dataType: "html",
    success: function(result) {
    if ($('#comment_none_yet').length == 0) {
    $('#comment_list').before(result);
    } else {
    $('#comment_list').html(result);
    }
    $('#comment_form').fadeOut('slow');
    }
    });
    });
    });*/

    // SEARCH //

    //Assign submit button onclick event
    $('#inp_search_button').click(function() {
        var text = $('#site_search').val();
        window.location = "/søk/" + text.replace(/ /g, "-").replace(/\+/g, "-").replace(/\%/g, "-").replace(/\|/g, "-").replace(/\?/g, "").replace(/\//g, "-");
        return false;
    });

    //Call submit button onclick event on enter in search box
    $('#site_search').keydown(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            if ($('#site_search').val().length > 0) {
                $('#inp_search_button').click();
            }
            return false;
        }
    });

    if (document.getElementById("expLocationList") != null) {
        $.ajax({
            type: "POST",
            url: "/ServerGet.aspx",
            data: { service: "location_dropdown", value: "0" },
            dataType: "html",
            success: function(result) {
                $('#expLocationList').html(result);
                //Expandable ul
                $('ul.expandable li img').click(function() {
                    var src = $(this).attr('src');
                    if (src == '/images/toggle.gif') {
                        $(this).attr('src', '/images/toggle_off.gif');
                    } else {
                        $(this).attr('src', '/images/toggle.gif');
                    }
                    $(this).parent().children('ul').toggle("fast");
                });
            }
        });
    }
    $('#search_location_lookup').keyup(function() {
        $('#search_location_result').html("<img src='/images/pending.gif' alt='Søker' title='' />");
        $('#search_location_result').show();
        $.ajax({
            type: "POST",
            url: "/ServerGet.aspx",
            data: { service: "location", value: $(this).val() },
            dataType: "html",
            success: function(result) {
                $('#search_location_result').html(result);
            }
        });
    });
    $('#search_location_lookup').blur(function() {
        if (divclick) return (divclick = false);
        $('#search_location_result').hide();
    });

    $('#search_location_result').mousedown(function() {
        divclick = true;
    });

    //End of page.load
});

function selectLocationFromSearch(link, name) {
    selectLocation(link, name);
    $('#search_location_result').hide();
    $('#search_location_lookup').val("").focus();
}

function selectLocation(link, name) {
    $('#selected_locations').append("<a href='#' class='location' title='Klikk for å fjerne'>" + name + "</a>");
    $('#search_location').val($('#search_location').val() + "-" + link);
}

// IMAGE POPUP //
TopUp.images_path = "/Addons/top_up/";
TopUp.players_path = "/Addons/top_up/players/";

TopUp.addPresets({
    "#activity_image_list a": {
        title: "{alt} ({current} av {total})",
        group: "activity_image_list",
        readAltText: 1,
        shaded: 1,
        effect: 'transform',
        resizable: 0
    }
});

function CalendarSubmit(answer) {
    if ($('#calendar_id').val() != "" && $('#calendar_name').val() != "" && $('#calendar_email').val() != "") {
        $.ajax({
            type: "POST",
            url: "/ServerSet.aspx",
            data: {
                service: "calendar",
                id: $('#calendar_id').val(),
                name: $('#calendar_name').val(),
                email: $('#calendar_email').val(),
                newsletter: $('#calendar_newsletter').is(':checked'),
                answer: answer
            },
            success: function(result) {
                $('#calendar_form').html(result);
            }
        });
    } else {
        alert("Alle felter må fylles ut");
    }
}

// SCROLLING MENU //
/*var maxHeight = 580;
$(function() {
            $(".dropdown > li").hover(function() {

                var $container = $(this),
                $list = $container.find("ul"),
                $anchor = $container.find("a"),
                height = $list.height() * 1.1,       // make sure there is enough room at the bottom
                multiplier = height / maxHeight;     // needs to move faster if list is taller

                // need to save height here so it can revert on mouseout            
                $container.data("origHeight", $container.height());

                // so it can retain it's rollover color all the while the dropdown is open
                $anchor.addClass("hover");

                // make sure dropdown appears directly below parent list item    
                $list
                .show()
                .css({
                    paddingTop: $container.data("origHeight")
                });

                // don't do any animation if list shorter than max
                if (multiplier > 1) {
                    $container.css({
                        height: maxHeight,
                        overflow: "hidden"
                    })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
                }

            }, function() {
                var $el = $(this);

                // put things back to normal
                $el
                    .height($(this).data("origHeight"))
                    .find("ul")
                    .css({ top: 0 })
                    .hide()
                    .end()
                    .find("a")
                    .removeClass("hover");
            });
});


/*$(function() {
    $.ajax({
        type: "POST",
        url: "/ServerGet.aspx",
        data: { service: "location_menu" },
        dataType: "html",
        success: function(result) {
            $('#locationDropdownMenu').html(result);
            $(".dropdown > li").hover(function() {

                var $container = $(this),
                $list = $container.find("ul"),
                $anchor = $container.find("a"),
                height = $list.height() * 1.1,       // make sure there is enough room at the bottom
                multiplier = height / maxHeight;     // needs to move faster if list is taller

                // need to save height here so it can revert on mouseout            
                $container.data("origHeight", $container.height());

                // so it can retain it's rollover color all the while the dropdown is open
                $anchor.addClass("hover");

                // make sure dropdown appears directly below parent list item    
                $list
                .show()
                .css({
                    paddingTop: $container.data("origHeight")
                });

                // don't do any animation if list shorter than max
                if (multiplier > 1) {
                    $container.css({
                        height: maxHeight,
                        overflow: "hidden"
                    })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
                }

            }, function() {
                var $el = $(this);

                // put things back to normal
                $el
                    .height($(this).data("origHeight"))
                    .find("ul")
                    .css({ top: 0 })
                    .hide()
                    .end()
                    .find("a")
                    .removeClass("hover");
            });
        }
        
    });
});*/
