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.");
            });
}

jQuery.noConflict(); var $j = jQuery;
$j(document).ready(function() {
    $j('.rounded').corner("5px #ccc");
    $j(".macb").facebox();
    $j("#submit-sms-box").facebox();
    $j("img").lazyload();
    $j("a[rel='cb']").colorbox();

    //TEXTBOX WATERMARKS
    $j(".watermarked").addClass("tbWaterMark").val($j(".watermarked").attr("title"));

    $j(".watermarked").focus(function() {

        $j(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $j(this).val() == "" || $j(this).val() == $j(this).attr("title")

        }).removeClass("tbWaterMark").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    $j(".watermarked").blur(function() {

        $j(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $j(this).val() == ""

        }).addClass("tbWaterMark").val($j(this).attr("title"));

    });
})


function escapeHTML(html) {
    return html.
    replace(/"/gmi, '&quot;');
}

function SaveReview(MovieID, ReviewTextBox, TargetCtrl, Container) {
    $j.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveReview',
        data: '{"MovieID": ' + MovieID +
    ', "Review":"' + escapeHTML($j('#' + 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)
                    $j('#' + Container).html(result);
                else
                    $j('#' + TargetCtrl).html(result);
            }
        }
    });
}

function SaveSMS(SMSTextBox, TargetCtrl, Container) {
    $j.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveSMS',
        data: '{"SMS":"' + escapeHTML($j('#' + 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)
                    $j('#' + SMSTextBox).val("");
                $j('#' + TargetCtrl).html(result);
            }
        }
    });
}

$j(".autocomplete").autocomplete(SiteRoot + "sms/Helpers/FindCats.ashx", {
    scrollHeight: 200,
    width: 200,
    cacheLength: 1,
    scroll: true
});

$j(".autocomplete").result(function(event, data, formatted) {
    GoToItem(data);
});

$j(".MobilesAutocomplete").autocomplete(SiteRoot + "Handsets/Helpers/MobilesAutocomplete.ashx", {
    scrollHeight: 200,
    width: 200,
    cacheLength: 1,
    scroll: true
});

$j(".MobilesAutocomplete").result(function(event, data, formatted) {
    try {
        $j.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 {
        $j('#' + 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) {
    $j.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveUserReview',
        data: '{"itemID": ' + itemID + ',"title":"' + escapeHTML($j('#' + txtTitle).val()) +
    '", "pros": "' + escapeHTML($j('#' + txtPros).val()) + '", "body":"' + escapeHTML($j('#' + txtBody).val()) +
    '", "cons":"' + escapeHTML($j('#' + 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)
                    $j('#' + ContainerCtrl).html(result);
                else
                    $j('#' + MessageCtrl).html(result);
            }
        }
    });
}

function SaveQues(QuesTextBoxID, DetailTextBoxID, CatID, TargetCtrlID, ContainerID, itemName, itemUrl) {
    $j.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/SaveQues',
        data: '{"Ques": "' + $j('#' + QuesTextBoxID).val() +
            '", "Detail":"' + escapeHTML($j('#' + 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) {
                    $j('#' + QuesTextBoxID).val('');
                    $j('#' + DetailTextBoxID).val('');
                    $j('#' + ContainerID).replaceWith(result);
                }
                else
                    $j('#' + TargetCtrlID).html(result);
            }
        }
    });
}

function LikeSMS(SmsId, ctrl) {
    $j.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 != "") {
                $j('#' + ctrl).replaceWith(result + "&nbsp;&nbsp;");
            }
        }
    });
}

$j("#AddReview").click(function() {
    var ctrl = $j(this);
    var itemId = $j(this).attr("itemId");
    var link = SiteRoot + "Handsets/Helpers/AddComment.aspx?itemId=" + itemId;
    $j.get(link, function(data) {
        $j(ctrl).fadeOut("fast", function() {
            $j(ctrl).parent().replaceWith(data);
            $j(this).fadeIn("fast");

            $j("textarea").wysiwyg();
        });
    });
});

$j("#AskQuestion").click(function() {
    var ctrl = $j(this);
    var itemName = $j(this).attr("itemName");
    var itemUrl = $j(this).attr("itemUrl");
    var link = SiteRoot + "Handsets/Helpers/AskQuestion.aspx?itemName=" + itemName + "&itemUrl=" + itemUrl;
    $j.get(link, function(data) {
        $j(ctrl).fadeOut("fast", function() {
            $j(ctrl).parent().replaceWith(data);
            $j(this).fadeIn("fast");
        });
    });
});

function AddMobile2List(mobileId, ctrl, listName) {
    $j.ajax({
        type: 'POST',
        url: SiteRoot + 'WebService.asmx/AddToList',
        data: '{"mobileId":' + mobileId + ', "listName":"' + listName + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(result) {
            $j('#' + ctrl).html(result.d);
            $j(".mobile_options>ul>li>a").removeAttr("onclick").removeAttr("href");
            $j("#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.");
        }
    });
}