﻿$(document).ready(function() {

    var $tabs = $("#tabs").tabs();

    $('input.PhoneMask').mask("(999) 999-9999");
    $("input.DateMask").mask("99/99/9999");
    $("input.ZipCodeMask").mask("99999");
    $('.error').hide();

    if (null != $("#stars-wrapper1")) {

        $("#stars-wrapper1").stars({
            cancelShow: false,
            oneVoteOnly: true,
            captionEl: $("#stars-cap"),
            callback: function(ui, type, value) {
                $.getJSON("ratebook.ashx", { rate: value, BookId: $('#BookId').val() }, function(json) {
                    $("#fake-stars-on").width(Math.round($("#fake-stars-off").width() / ui.options.items * parseFloat(json.avg)));
                    $("#fake-stars-cap").text(json.avg + " (" + json.votes + ")");
                });
            }
        });
    }

    imagePreview();

    var dThinking = $("#dThinking");
    var dReviewPosted = $("#dReviewPosted");
    var ContactResponse = $("#ContactResponse");

    if (null != ContactResponse) {
        ContactResponse.hide();
    }

    if (null != dReviewPosted) {
        dReviewPosted.hide();
    }

    if (null != dThinking) {
        dThinking.hide();
    }

    $("#txtPhone").mask("(999) 999-9999");
    $('.error').hide();

    $('#fReview').submit(function() {
        $("#dMakeReviewForm").hide();
        $("#dThinking").show();
        // submit the form
        $(this).ajaxSubmit({
            url: "PostReview.ashx",
            success: function(result) {
                $("#dThinking").hide();
                $("#dReviewPosted").show();
            }

        });

        // return false to prevent normal browser submit and page navigation 
        return false;
    });

    $("#ContactForm").validate({ submitHandler: function(form) {

        $('#ContactForm').submit(function() {
            $("#dMakeContact").hide();
            $("#dThinking").show();
            // submit the form
            $(this).ajaxSubmit({
                url: "ContactHandler.ashx",
                success: function(result) {
                    $("#dThinking").hide();
                    $("#ContactResponse").show();
                }
            });

            // return false to prevent normal browser submit and page navigation 
            return false;
        });
    }
    });

});



function GetBook() {

    $.ajax({
        type: "POST",
        url: "ASPNETBooksService.asmx/GetBookInfo",
        data: "{ 'Bookid': '1' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert('Yeah!!');
        }
    });

}


/*
* Image preview script 
* powered by jQuery (http://www.jquery.com)
* 
* written by Alen Grakalic (http://cssglobe.com)
* 
* for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
*
*/

this.imagePreview = function() {
    /* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.preview").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='" + this.rel + "' alt='Image preview' />" + c + "</p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function() {
	    this.title = this.t;
	    $("#preview").remove();
	});
    $("a.preview").mousemove(function(e) {
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });
};
