﻿var SiteRoot = "/";

$(document).ready(function() {
    $('.rounded').corner('#cccccc 5px');
    $("a[rel='fbox']").facebox();
    $("a[rel='cb']").colorbox();

    // Create stars
    $("#rat").children().not(":radio").hide();
    $("#rat").stars({
        callback: function(ui, type, value) {
            $("#loader").fadeIn(function() {
                $.ajax({
                    type: 'POST',
                    url: SiteRoot + 'WebService.asmx/SaveRating',
                    data: '{"itemId":' + $("#rat").attr("title") + ',"ratedVal":' + value + '}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function(db) {
                        $("#rated-by").text("Thanks for rating.");

                        ui.select(Math.round(db.avg));
                        $("#avg").text(db.avg);
                        $("#votes").text(db.votes);
                        $("#loader").fadeOut();
                    }
                })
            })
        }
        , oneVoteOnly: true
    });
    //Create Stars Complete
});

$(".suggestCats").autocomplete(SiteRoot + "Helpers/FindCats.ashx", {
    scrollHeight: 200,
    width: 200,
    cacheLength: 1,
    scroll: true
});

$(".suggestCats").result(function(event, data, formatted) {
    try {
        $.ajax({
            type: 'POST',
            url: SiteRoot + 'WebService.asmx/GetItemLink',
            data: '{"title":"' + data + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(msg) {
                window.location = msg.d;
            }
        });
    }
    catch (err) {
        alert(err.description);
    }
});

function escapeHTML(html) {
    return html.
    replace(/"/gmi, '&quot;');
    //replace(/&/gmi, '&amp;').
    //replace(/>/gmi, '&gt;').
    //replace(/</gmi, '&lt;')
}

function SaveComment(itemId, ContentTextBox, MsgCtrl, Container) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveComment',
        data: '{"itemId": ' + itemId +
        ', "comment":"' + escapeHTML($('#' + ContentTextBox).val()) + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Successfully', 1) > 0)
                    $('#' + Container).html(result);
                else
                    $('#' + MsgCtrl).html(result);
            }
        }
    });
}

function SaveCaption(imageId, captionTextbox, msgCtrlId, containerId) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveCaption',
        data: '{"imageId": ' + imageId +
        ', "caption":"' + escapeHTML($('#' + captionTextbox).val()) + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Thanks', 0) >= 0)
                    $('#' + containerId).html(result);
                else
                    $('#' + msgCtrlId).html(result);
            }
        }
    });
}

function RandomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}


$("#AddCaption,#EditCaption").click(function() {
    var ctrl = $(this);
    var itemId = $(this).attr("itemId");
    var link = SiteRoot + "Helpers/SubmitCaption.aspx?itemId=" + itemId + "&r=" + RandomString();
    if (link != null) {
        $.get(link, function(data) {
            $(ctrl).fadeOut("fast", function() {
                $(ctrl).replaceWith(data);
                $(this).fadeIn("fast");
            });
        });
    }
});

$("#AddCatInfo,#EditCatInfo").click(function () {
    var ctrl = $(this);
    var itemId = $(this).attr("itemId");
    var link = SiteRoot + "Helpers/CatInfo.aspx?itemId=" + itemId + "&r=" + RandomString();
    if (link != null) {
        $.get(link, function (data) {
            $(ctrl).fadeOut("fast", function () {
                $(ctrl).replaceWith(data);
                $(this).fadeIn("fast");
            });
        });
    }
});

function SaveCatInfo(catId, infoTextbox, msgCtrlId, containerId) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveCatInfo',
        data: '{"catId": ' + catId +
        ', "info":"' + escapeHTML($('#' + infoTextbox).val()) + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Thanks', 0) >= 0)
                    $('#' + containerId).html(result);
                else
                    $('#' + msgCtrlId).html(result);
            }
        }
    });
}

$("#DeleteImage").click(function() {
    var imageId = $(this).attr("itemId");
    var ctrl = $(this);

    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/DeleteImage',
        data: '{"imageId": ' + imageId + '}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            $(ctrl).replaceWith(result);
        }
    });
});


//$("#SignUp").click(function() {
//    var link = SiteRoot + "Helpers/Proxy.ashx";
//    $.get(link, function(data) {
//        $.facebox(data);
//    });
//});
