var entryCount = 0;
var LocationLat = 0;
var LocationLng = 0;
var ZoomLevel = 0;
var LocationWebID = '';

var updateEntryRatings = function(eid) {
    $.ajax({
        type: "POST",
        url: "ajax-service",
        data: { ajaxDataServiceType: "GetEntryRatings", Entry: eid },
        success: function(rating) {
            $("[eid=" + eid + "][action=RateEntryPositive]").html("<span>% " + rating.positiveRate + "</span>");
            $("[eid=" + eid + "][action=RateEntryNegative]").html("<span>% " + rating.negativeRate + "</    >");
        }
    });
}

$(document).ready(function() {
    //BEGIN: General
    $(".btn").bind('click', function() {
        var entry_id = this.getAttribute("eid");
        var action_type = this.getAttribute("action");
        var locationWebID = this.getAttribute("lid");
        switch (action_type) {
            case 'OpenShareMe':
                $("#share-me").slideToggle("slow");
                break;
            case 'MoreEntry':
                $('#' + entry_id + '-more').removeClass('nodisplay');
                $('#more-button-' + entry_id).remove();
                //setTimeout('this.remove()', 100);
                break;
            case 'RateEntryPositive':
            case 'RateEntryNegative':
            case 'ComplaintEntry':
                //$('#rate-notify-' + entry_id).html('...');
                $.ajax({
                    type: "POST",
                    url: "ajax-service",
                    data: { ajaxDataServiceType: action_type, Entry: entry_id },
                    success: function(result) {
                        //$('#rate-notify-' + entry_id).html(result);
                        jAlert(result);
                        updateEntryRatings(entry_id);
                    }
                });
                break;
            case 'AddLocationToFavorites':
                //$('#entry-notify-' + locationWebID).html('...');
                $.ajax({
                    type: "POST",
                    url: "ajax-service",
                    data: { ajaxDataServiceType: action_type, Location: locationWebID },
                    success: function(result) {
                        if (result == "ok")
                            reloadPage();
                        else
                        //$('#entry-notify-' + locationWebID).html(result);
                            jAlert(result);
                    }
                });
                break;
            case 'AddLocationComment':
                //'var comment = $("#location-comment").val();
                //$('#comment-notify-' + locationWebID).html('...');
                var comment = $('#location-comment').htmlarea('toHtmlString');

                $.ajax({
                    type: "POST",
                    url: "ajax-service",
                    data: { ajaxDataServiceType: action_type, Location: locationWebID, Comment: comment, LangId: LangID },
                    success: function(result) {
                        jAlert(result);
                        reloadPage();
                        //$('#comment-notify-' + locationWebID).html(result);
                    }
                });
                break;

            case 'CancelLocationPhotoAdd':
                $.fn.colorbox.close();
                break;
            default:
                break;
        }

        return false;
    });






    $("#top-left").mouseover(function() {
        $("#edit-information").show();
    });

    $("#top-left").mouseout(function() {
        $("#edit-information").hide();
    });





    //EOF: General

    if (PageName == "search-results" || PageName == "location-detail") {
        $('.location-star').rating({
            callback: function(value, link) {
                locationWebID = this.name.replace('location-star', '');
                rateValue = this.getAttribute('rate');
                //$('#entry-notify-' + locationWebID).html('...');
                $.ajax({
                    type: "POST",
                    url: "ajax-service",
                    data: { ajaxDataServiceType: 'StarLocation', Location: locationWebID, Value: rateValue },
                    success: function(result) {
                        if (result == "ok")
                            reloadPage();
                        else
                        //$('#entry-notify-' + locationWebID).html(result);
                            jAlert(result);
                    }
                });
            }
        });
    }

    if (PageName == "search-results" || PageName == "profile-details") {
        $('.location-star').rating();
    }



    //BEGIN: Location Details
    if (PageName == "location-detail") {
        if (LocationWebID != "") {
            $('#sort-type').bind("change", function() {
                window.location.href = this.value;
            });
            $('#page-size').bind("change", function() {
                window.location.href = this.value;
            });

            $('#sort-type > option').each(function() {
                if (this.getAttribute('type') == sortBy.toString())
                    this.selected = true;
                else
                    this.selected = false;
            });
            $('#page-size > option').each(function() {
                if (this.getAttribute('type') == pageSize.toString())
                    this.selected = true;
                else
                    this.selected = false;
            });

            $(".rapor-et a").colorbox();
            $(".foto-galeri").colorbox({ iframe: true, innerWidth: 720, innerHeight: 550 });
            $(".foto-ekle-button").colorbox({ inline: true, href: "#add-photo-container", innerWidth: 460, innerHeight: 200 });
            $("a[rel='fotograflar']").colorbox();

        }
        else {
            $('#comment-container').remove();
            $('#add-comment-container').remove();
        }


        if (entryCount == 0)
            $('#sorter-comment').remove();

    }
    //EOF: Location Details

    //BEGIN: Logon
    $('#logon-form').bind("submit", function() {
    var email = $('#pegasusla-user').val();
        var password = $('#pegasusla-pass').val();
        var language = $('#language').val();
        var remember = $('#remember-me:checked').val() != null ? 'yep' : 'nope';

        $.ajax({
            type: "POST",
            url: "ajax-service",
            data: { ajaxDataServiceType: "ProcessLogon", Email: email, Password: password, Language: language, Remember: remember },
            success: function(result) {
                if (result == "ok")
                    redirectToURL(language + "/home");
                else
                    alerter(result);
            }
        });
        return false;
    });
    $('#member-logout').bind("click", function() {
        $.ajax({
            type: "POST",
            url: "ajax-service",
            data: { ajaxDataServiceType: "ProcessLogout" },
            success: function(result) {
                if (result == "ok")
                    reloadPage();
                else
                    alerter(result);
            }
        });
        return false;
    });
    //EOF: Logon

});



var resizer = function() {
    var width = $(window).width();
    $('#main-background').css("width", width);
};
var alerter = function(inMessage) {
    jAlert(inMessage);
};
var reloadPage = function() {
    var url = window.location.href;
    url = url.split('#')[0];
    window.location.href = url;
};



//bg resizer
$(window).load(function() {
    var width = $(window).width();
    $('#main-background').css("width", width);
});

$(window).bind('resize', resizer); 



