var SiteRoot = "/";

function GoToItem(CatName) {
    try {
        $j.ajax({
            type: 'POST',
            url: SiteRoot + 'WebService.asmx/GetItemLink',
            data: '{"title":"' + CatName + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(msg) {
                window.location = msg.d;
            }
        })
    }
    catch (err) {
        alert(err.description);
    }
}

function Add2List(ProfileID, ctrl, ListName) {
    WebService.AddToList(ProfileID, ListName,
            function(result) {
                $(ctrl).replace('<li>' + result + '</li>');
                if (result == "Please Sign In") window.location = "http://accounts.maxabout.com/sign_in.aspx?return=" + document.URL;
            },
            function() {
                alert("Problem adding.");
            });
}

function Vote(ProfileID, ctrl, VoteType) {
    WebService.Vote(ProfileID, VoteType,
            function(result) {
                $(ctrl).replace('<li>' + result + '</li>');
                if (result == "Please Sign In") window.location = "http://accounts.maxabout.com/sign_in.aspx?return=" + document.URL;
            },
            function() {
                alert("Problem voting.");
            });
}

$(document).ready(function() {
    $('.rounded').corner("5px #ccc");
    $("a[rel^=slide]").fancybox({ transition: "fade" });
    $("#submit-sms-box").fancybox();
    $("a[class^=macb]").fancybox();
    $("img").lazyload();
    
    //TEXTBOX WATERMARKS
    $(".watermarked").addClass("tbWaterMark").val($(".watermarked").attr("title"));

    $(".watermarked").focus(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == "" || $(this).val() == $(this).attr("title")

        }).removeClass("tbWaterMark").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    $(".watermarked").blur(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("tbWaterMark").val($(this).attr("title"));

    });
})


function escapeHTML(html) {
    return html.
    replace(/"/gmi, '&quot;');
}

function SaveReview(MovieID, ReviewTextBox, TargetCtrl, Container) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveReview',
        data: '{"MovieID": ' + MovieID +
    ', "Review":"' + escapeHTML($('#' + ReviewTextBox).val()) + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Submitted Successfully', 1) > 0)
                    $('#' + Container).html(result);
                else
                    $('#' + TargetCtrl).html(result);
            }
        }
    });
}

function SaveSMS(SMSTextBox, TargetCtrl, Container) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveSMS',
        data: '{"SMS":"' + escapeHTML($('#' + SMSTextBox).val()) + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Submitted', 1) > 0)
                    $('#' + SMSTextBox).val("");
                $('#' + TargetCtrl).html(result);
            }
        }
    });
}

$("#txtFindSMS").autocomplete(SiteRoot + 'WebService.asmx/GetAutocompleteListSms', {
    extraParams: {  },
    max: 20,
    delay: 400,
    scroll: true,
    matchContains: true,
    minChars: 3,
    width: 260,
    selectFirst: false,
    parse: function (xml) {
        var results = [];
        $(xml).find('AcList').each(function () {
            var text = $.trim($(this).find('Name').text());
            var value = $.trim($(this).find('Link').text());
            results[results.length] = { 'data': { text: text, value: value },
                'result': text, 'value': value
            }
        });
        return results;
    },
    dataType: "xml",
    formatItem: function (item) {
        return item.text;
    }
}).result(function (event, data, formatted) {
    window.location = data.value;
});

$(".MobilesAutocomplete").autocomplete(SiteRoot + "Handsets/Helpers/MobilesAutocomplete.ashx", {
    scrollHeight: 200,
    width: 200,
    cacheLength: 1,
    scroll: true
});

$(".MobilesAutocomplete").result(function(event, data, formatted) {
    try {
        $.ajax({
            type: 'POST',
            url: SiteRoot + 'WebService.asmx/GetMobileLink',
            data: '{"title":"' + data + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(msg) {
                window.location = msg.d;
            }
        })
    }
    catch (err) {
        alert(err.description);
    }

});



function checkMaxLen(textbox, maxLen, label) {
    try {
        $('#' + label).html(textbox.value.length);
        if (textbox.value.length > (maxLen - 1)) {
            var cont = textbox.value;
            textbox.value = cont.substring(0, (maxLen - 1));
            return false;
        };
    } catch (e) {
    }
}

function SaveReview(itemID, txtTitle, txtPros, txtCons, RatingCtrl, txtBody, MessageCtrl, ContainerCtrl) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveUserReview',
        data: '{"itemId": ' + itemID + ',"title":"' + escapeHTML($('#' + txtTitle).val()) +
    '", "pros": "' + escapeHTML($('#' + txtPros).val()) + '", "body":"' + escapeHTML($('#' + txtBody).val()) +
    '", "cons":"' + escapeHTML($('#' + txtCons).val()) + '", "rating":1}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Thanks', 0) >= 0)
                    $('#' + ContainerCtrl).html(result);
                else
                    $('#' + MessageCtrl).html(result);
            }
        }
    });
}

function SaveQues(QuesTextBoxID, DetailTextBoxID, CatID, TargetCtrlID, ContainerID, itemName, itemUrl) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveQues',
        data: '{"Ques": "' + $('#' + QuesTextBoxID).val() +
            '", "Detail":"' + escapeHTML($('#' + DetailTextBoxID).val()) + '","CatID":' + CatID +
            ',"itemName":"' + itemName + '","itemUrl":"' + itemUrl + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                if (result.toString().indexOf('Submitted Successfully', 1) > 0) {
                    $('#' + QuesTextBoxID).val('');
                    $('#' + DetailTextBoxID).val('');
                    $('#' + ContainerID).replaceWith(result);
                }
                else
                    $('#' + TargetCtrlID).html(result);
            }
        }
    });
}

function LikeSMS(SmsId, ctrl) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/LikeSms',
        data: '{"smsId": ' + SmsId + '}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            var result = msg.d;
            if (result != "") {
                $('#' + ctrl).replaceWith(result + "&nbsp;&nbsp;");
            }
        }
    });
}

$("#AddReview").click(function() {
    var ctrl = $(this);
    var itemId = $(this).attr("itemId");
    var link = SiteRoot + "Handsets/Helpers/AddComment.aspx?itemId=" + itemId;
    $.get(link, function(data) {
        $(ctrl).fadeOut("fast", function() {
            $(ctrl).parent().replaceWith(data);
            $(this).fadeIn("fast");
        });
    });
});

$("#AskQuestion").click(function() {
    var ctrl = $(this);
    var itemName = $(this).attr("itemName");
    var itemUrl = $(this).attr("itemUrl");
    var link = SiteRoot + "Handsets/Helpers/AskQuestion.aspx?itemName=" + itemName + "&itemUrl=" + itemUrl;
    $.get(link, function(data) {
        $(ctrl).fadeOut("fast", function() {
            $(ctrl).parent().replaceWith(data);
            $(this).fadeIn("fast");
        });
    });
});

function AddMobile2List(mobileId, ctrl, listName) {
    $.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/AddToList',
        data: '{"mobileId":' + mobileId + ', "listName":"' + listName + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(result) {
            $('#' + ctrl).html(result.d);
            $(".mobile_options>ul>li>a").removeAttr("onclick").removeAttr("href");
            $("#LeaveComment").click();
            if (result.d == "Please Sign In") window.location = "http://accounts.maxabout.com/sign_in.aspx?return=" + document.URL;
        },
        error: function() {
            alert("Problem adding.");
        }
    });
}
