﻿$(document).ready(function(){
    // main navigation drop downs
    if (document.all){
        $("#navContainer li").hoverClass("sfHover");
    }
    
    // how it works info toggle
    $(".moreInfo").hide();
    $(".expander").children("a").html("+");
    
    // video player
    $('.videoPlayer').jqm({closeClass: 'closeBtn', onHide: endVideo});
    
    $(".btnPlayVideoSmall").hoverClass("btnPlayVideoSmallOver");
    $(".btnPlayVideoLarge").hoverClass("btnPlayVideoLargeOver");
});

$.fn.hoverClass = function(c){
    return this.each(function(){
        $(this).hover(
            function(){$(this).addClass(c);},
            function(){$(this).removeClass(c);}
        );
    });
};

function toggleInfo(rowId, callerItem){
    if (callerItem.parent().attr("class") == "expander")
	    callerItem = callerItem.parent();
    if ($("#" + rowId).css("display") == "none"){
	    // hide all
	    $(".moreInfo").hide();
	    $(".moreInfo").parent().children().removeClass("activeHeader");
	    $(".expander").children("a").html("+");
	    $(".noBottomBorder").removeClass("noBottomBorder");

	    // show selected
	    $("#" + rowId).show();
	    $(callerItem).parent().children(".expander").children("a").html("&ndash;");
	    $(callerItem).parent().parent().addClass("activeHeader");
	    if ($(callerItem).parent().parent().prev().attr("class") != "moreInfo")
		    $(callerItem).parent().parent().prev().addClass("noBottomBorder");
	    else $(callerItem).parent().parent().prev().prev().addClass("noBottomBorder");
    } else {
	    // hide selected
	    $("#" + rowId).hide();
	    $(callerItem).parent().children(".expander").children("a").html("+");
	    $(callerItem).parent().parent().removeClass("activeHeader");
	    $(".noBottomBorder").removeClass("noBottomBorder");
    }
}

function showVideo(videoName){
    $('#videoPlayer_' + videoName).jqmShow();
    return false;
}
function hideVideo(){
    $('.videoPlayer').jqmHide();
}
function endVideo(hash){
    try{
        var flashObj = document.getElementById(hash.w.attr("id").replace("videoPlayer", "flashVideoPlayer"));
        if (flashObj){
            flashObj.endVideo();
        }
    } catch (err){}
    hash.w.hide();
    hash.o.remove();
}

function submitHostedReceptionistDemoForm(){
    var strName = "";
    var strEmail = "";
    var strPhone = "";
    var btnValue = "";
    
    strName = $("#contactName").val();
    strPhone = $("#contactPhone").val();
    strEmail = $("#contactEmail").val();

    btnValue = $("#contactBtnSubmit").val();
    $("#contactBtnSubmit").val("Loading...");
    $("#contactBtnSubmit").attr("disabled", "true");

    $.ajax({
        type: "POST",
        url: "/Receptionist-LiveDemo-Form.aspx/SubmitReceptionistLiveDemo",
        data: "{ContactName: " + JSON.stringify(strName) + ", ContactPhone: " + JSON.stringify(strPhone) + ", ContactEmail: " + JSON.stringify(strEmail) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function(data){
            try{
                json = JSON.parse(data.d);

                if (json[0].Success){
                    return __doPostBack('SaveHostedReceptionistDemoSessionVar', $("#hostedReceptionistDemoURL").val());
                } else if (json[0].ErrorMessage){
                    $("#contactFormErrorMsg").html(json[0].ErrorMessage);
                    $("#contactFormErrorMsg").show();
                    $("#contactBtnSubmit").val(btnValue);
                    $("#contactBtnSubmit").attr("disabled", "");
                } else {
                    alert("Sorry, we were unable to submit your information.  Please try again later.");
                    $("#contactBtnSubmit").val(btnValue);
                    $("#contactBtnSubmit").attr("disabled", "");
                }
            } catch(err){
                alert("Error: " + err);
                $("#contactBtnSubmit").val(btnValue);
                $("#contactBtnSubmit").attr("disabled", "");
            }
        },
        
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert("Sorry, we were unable to submit your information.  Please try again later.");
            $("#contactBtnSubmit").val(btnValue);
            $("#contactBtnSubmit").attr("disabled", "");
        }
    });

}