﻿var __TIMER1 = null;

function EmailForm(i, isMobile) {
    $.ajax({
        url: "MailForm.ashx?a=build&i=" + i,
        dataType: "text",
        success: function(data) {
            $.modal(data, { 'closeHTML': '' });
        }
    });
}

function SendEmail() {
    var s = $("#frmEmail").serialize();
    $("#frmEmail button").hide();
    $("#spSaving").show();

    $.ajax({
        url: "MailForm.ashx?a=send",
        async: false,
        data: s,
        type: "POST",
        dataType: "json",
        success: function(data) {
            if (data.OK) {
                $.modal.close();
            }
            else {
                $("#frmEmail button").show();
                $("#spSaving").hide();
            }
            alert(data.Message);
        }
    });
}

function LoadMap(coordsA, coordsB) {
    var myLatlng = new google.maps.LatLng(coordsA, coordsB);
    var myOptions = {
        zoom: 10,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("mapDetail"), myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
}

function FindMap(address) {
    var geocoder = new google.maps.Geocoder();
    if (geocoder) {
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                LoadMap(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                // ulozit pozici do db
                var loc = results[0].geometry.location.lat() + "," + results[0].geometry.location.lng();
                $.get("SaveLocation.ashx?h10id=" + h10id + "&location=" + loc);
            }
            else {
                $(".dvDetail2 *").hide();
            }
        });
    }
}

function GlobalMobileInit() {
    // text pro vyhledavani
    TextboxProVyhledavani();

    // seznam hotelu
    SeznamHotelu();
}

function GlobalInit() {
    // vyhledavani zeme
    var z = $('#country');
    if (z.size() > 0) {
        // slozeni dropdownu zemi
        var selectedCountry = '';
        var selectedCountryName = '-- choose country --';
        var s = '<div id="country_combo2" class="dropDown">';
        z.find('option').each(function() {
            s += '<a rel="ddl" href="#' + this.value + '">' + this.innerHTML + '</a>';
            if (this.selected) {
                selectedCountry = this.value;
                selectedCountryName = this.innerHTML;
            }
        });
        s += '</div>';

        var s2 = '<div class="dvMiddle2a" id="country_combo">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + selectedCountryName + '</div>';
        s2 += '<input type="hidden" name="country" id="country" value="' + selectedCountry + '" />';

        // vlozit dropdown
        $("body").append(s);

        // nahrazeni dropdownlistu
        z.replaceWith(s2);

        $('#country_combo2, #country_combo2 a').mouseout(function(e) {
            e.stopPropagation();
            if (__TIMER1 == null && e.relatedTarget.rel != "ddl" && e.relatedTarget.id != "country_combo2")
                Country_SetTimer();
        })
        .mouseenter(function(e) {
            e.stopPropagation();
            if (__TIMER1 != null) {
                clearInterval(__TIMER1);
                __TIMER1 = null;
            }
        });
        $('#country_combo2 a').click(function(e) {
            e.preventDefault();
            e.stopPropagation();
            document.getElementById('country').value = this.href.substr(this.href.indexOf('#') + 1);
            $("#country_combo").html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + this.innerHTML);
            $('#country_combo2').slideUp('fast');
        });

        Country_MouseOver();
    }
    
    // vyhledavani textove
    TextboxProVyhledavani();

    // nahrazeni buttonu search
    var b = $('#btnSearch');
    if (b.size() > 0) {
        b.replaceWith('<img src="images/search.gif" id="btnSearch" alt="Search" />');
        $('#btnSearch').click(function() {
            if ($('#s').val() == __defaultSearchPhrase)
                $('#s').val("");
            document.getElementById('frmSearch').submit();
        });
    }

    // hotel list
    SeznamHotelu();
}

function SeznamHotelu() {
    var hl = $('#hotellist');
    if (hl.size() > 0) {
        hl.find('.hotelList2').hover(function() { $(this).addClass('hotelList2sel'); }, function() { $(this).removeClass('hotelList2sel'); })
        .click(function() {
            location.href = $(this).find(".hotelList3 a").attr("href");
        });
        hl.find('.hl_www, .hl_email').click(function(e) {
            e.stopPropagation();
        });
    }
}

function TextboxProVyhledavani() {
    var s = $('#s');
    if (s.size() > 0) {
        // text vyhledavani
        s.focus(function(e) {
            if (e.target.className == 'txtout') {
                e.target.value = '';
                e.target.className = 'txtin';
            }
            else {
                e.target.select();
            }
        })
        .blur(function(e) {
            if (e.target.value == '') {
                e.target.className = 'txtout';
                e.target.value = __defaultSearchPhrase;
            }
        });
    }
}

function Country_MouseOver() {
    $('#country_combo').one('mouseover', function(e) {
        var l = ($(e.target).offset().left + 1) + 'px';
        var t = ($(e.target).offset().top + 29) + 'px';
        $('#country_combo2').css({ left: l, top: t }).slideDown('fast');
    });
}

function Country_SetTimer() {
    __TIMER1 = setTimeout(function() {
        $('#country_combo2').slideUp('fast', Country_MouseOver);
    }, 400);
}

function RemoveLogo(text) {
    if (confirm(text)) {
        $.ajax({
            url: "RemoveLogo.ashx",
            success: function(data) {
                $('#logo2').hide();
                $('#logo1').show();
            }
        });
    }
}

function Logout() {
    $.ajax({
        url: "Logout.ashx",
        dataType: "text",
        success: function(data) {
            location.href = "index.aspx";
        }
    });
}

function Login() {
    $.ajax({
        url: "Login.ashx",
        type: "POST",
        data: "pass=" + document.getElementById("txtLogin").value,
        dataType: "json",
        error: function(XMLHttpRequest, textStatus, errorThrown) {alert(errorThrown);},
        success: function(data) {
            if (data.OK)
                location.href = "edit.aspx";
            else
                alert(data.Message);
        }
    });
}

function RemoveHotel(text) {
    if (confirm(text)) {
        $.ajax({
            url: "RemoveHotel.ashx",
            success: function(data) {
                location.href = "index.aspx";
            }
        });
    }
}
