
/**
 * General functons, event handlers for site
 */
$(document).ready(function() {  
 /**
   * Handles the add to bookmark click. For new browser where the featured is
   * provided by javascript, use this, otherwise, display a dialog show the user
   * how to add it in older browsers
  */
    if($('a#add-bookmarks') && $('a#add-bookmarks').length){
        $('a#add-bookmarks').click(function(){
            var bookmarkTitle = '123Chiropractors';
            var bookmarkURL   = 'http://www.123chiropractors.com/';
            if (window.sidebar) // Firefox
                window.sidebar.addPanel(bookmarkTitle, bookmarkURL, '');
            else if (window.external) // IE
                window.external.AddFavorite(bookmarkURL, bookmarkTitle);
            else  {// All other browsers
                $('.bookmarks-dialog').css("display", "block");
                $('.bookmarks-dialog').dialog('open');
            }
        });
	
        /**
     * Close the bookmark dialog
     */
        $('a.bookmarks-close').click(function(){
            $('.bookmarks-dialog').dialog('close');
        });
    }
  
    /**
	 * Sets up the various jQuery dialog boxes used
	 */
    //if($('.newsletter-dialog') && $('.newsletter-dialog').length)
    //  $('.newsletter-dialog').dialog({autoOpen:false, resizable:false, draggable:false, width:330, height: 191, modal:true, overlay:{opacity:0.7, background:"black"}});
    //if($('.sendtofriend-dialog') && $('.sendtofriend-dialog').length)
    //  $('.sendtofriend-dialog').dialog({autoOpen:false, resizable:false, draggable:false, width:330, height: 180, modal:true, overlay:{opacity:0.7, background:"black"}});
    if($('.billing-statement-dialog') && $('.billing-statement-dialog').length)
        $('.billing-statement-dialog').dialog({
            autoOpen:false,
            resizable:false,
            draggable:false,
            width:600,
            height:450,
            modal:true,
            overlay:{
                opacity:0.6,
                background:"black"
            }
            });
    if($('.bookmarks-dialog') && $('.bookmarks-dialog').length)
        $('.bookmarks-dialog').dialog({
            autoOpen:false,
            resizable:false,
            draggable:false,
            width:350,
            height:200,
            modal:true,
            overlay:{
                opacity:0.6,
                background:"black"
            }
            });
	  
    /**
	 * Close the billing statement dialog
	 */
    if($('a.billing-statement-close') && $('a.billing-statement-close').length)
        $('a.billing-statement-close').click(function(){
            $('.billing-statement-dialog').dialog('close');
        });

    /**
	 * Show the send profile to friend dialog
	 */
    /*
  if($('a#send-profile-to-friend') && $('a#send-profile-to-friend').length)
    $('a#send-profile-to-friend').click(function() {

    // Show the loading image, hide the content
    $("#sendtofriend-content").html("");
    $("#sendtofriend-content").css("display", "none");
    $("#sendtofriend-loading").css("display", "block");

    // Pull the page in over AJAX
    $.get(this.href, function(data) {

      // Place the page content into the popups content div
      $("#sendtofriend-content").html(data);

      // Hide the loading image, display the content
      $("#sendtofriend-loading").css("display", "none");
      $("#sendtofriend-content").css("display", "block");
    });

    $('.sendtofriend-dialog').css("display", "block");
    $('.sendtofriend-dialog').dialog('open');

    return false;
  });
	/**
	 * Close the send to friend dialog
	 */
    /*
  if($('a.sendtofriend-close') && $('a.sendtofriend-close').length)
	$('a.sendtofriend-close').click(function(){
		$('.sendtofriend-dialog').dialog('close');
	});
  */
  
    /**
	 * Show the newsletter dialog
	 */
    if($('a#newsletter-open') && $('a#newsletter-open').length)
        $('a#newsletter-open').click(function() {
    
            // Show the loading image, hide the content
            $("#newsletter-content").html("");
            $("#newsletter-content").css("display", "none");
            $("#newsletter-loading").css("display", "block");
    
            // Pull the page in over AJAX
            $.post(urlNewsletter, function(data) {
      
                // Place the page content into the popups content div
                $("#newsletter-content").html(data);
      
                // Hide the loading image, display the content
                $("#newsletter-loading").css("display", "none");
                $("#newsletter-content").css("display", "block");
      
                // Newsletter input boxes
                $("#newsletter-form input").hint();
            });
  
            $('.newsletter-dialog').css("display", "block");
            $('.newsletter-dialog').dialog('open');
    
		
        });

    /*
  if($('a#newsletter-open2') && $('a#newsletter-open2').length)
    $('a#newsletter-open2').click(function() {

    // Show the loading image, hide the content
    $("#newsletter-content").html("");
    $("#newsletter-content").css("display", "none");
    $("#newsletter-loading").css("display", "block");

    // Pull the page in over AJAX
    $.get("/ajax/getNewsletterDialog", function(data) {

      // Place the page content into the popups content div
      $("#newsletter-content").html(data);

      // Hide the loading image, display the content
      $("#newsletter-loading").css("display", "none");
      $("#newsletter-content").css("display", "block");

      // Newsletter input boxes
      $("#newsletter-form input").hint();
    });

    $('.newsletter-dialog').css("display", "block");
    $('.newsletter-dialog').dialog('open');

	});
*/

    /**
	 * Close the newsletter dialog
	 */
    if($('a.newsletter-close') && $('a.newsletter-close').length)
        $('a.newsletter-close').click(function(){
            $('.newsletter-dialog').dialog('close');
        });
	
    if($("input.location") && $("input.location").length)
        $("input.location").hint();
    if($("input.location-all") && $("input.location-all").length)
        $("input.location-all").hint();
    if($("input.doctor") && $("input.doctor").length)
        $("input.doctor").hint();

});

/**
 * Handles the newsletter signup click
 */
function sendProfileToFriend() {
    var errors = false;
    var email = document.getElementById("email").value;
    var doctor_id = document.getElementById("doctor_id").value;
    var sendButton = document.getElementById("sendToFriendButton");
  
    sendButton.disabled = true;
    sendButton.value = 'Sending...';

    $("#sendtofriend-error").css("display", "none");

    // TODO: validate email
    if (email.length == 0 || email == "Email Address") {
        errors = true;
        $("input#email").css("background", "#FCCCCC");
    }
    else
        $("input#email").css("background", "#E1FCCC");

    if (errors) {
        $("#sendtofriend-error").html("Please fill in all fields");
        $("#sendtofriend-error").css("display", "block");

        sendButton.value = 'Send';
        sendButton.disabled = false;
    }
    else {
        // Send the data via AJAX
        $.post("/ajax/sendProfileToFriend", {
            email: email,
            doctor_id: doctor_id
        }, function(data) {
            if (data == "success") {
                // Hide the form content
                $("#sendtofriend-form").css("display", "none");
                // Show the complete content
                $("#sendtofriend-complete").css("display", "block");
            }
            else {
                // An error occured
                error = data.split("|");

                // Display an error
                $("input#email").css("background", "#FCCCCC");
                $("#sendtofriend-error").html(error[1]);
                $("#sendtofriend-error").css("display", "block");
            }

            sendButton.value = 'Send';
            sendButton.disabled = false;
        });
    }
}

/**
 * Handles the newsletter signup click
 */
function newsletterSignup(url) {
    var errors = false;

    var inputs = $('#newsletter-form :input');
    for(var i=0; i<inputs.length; i++) {
        inputs[i].value = $.trim(inputs[i].value);
    }
    var firstName = document.getElementById("first-name").value;
    var lastName 	= document.getElementById("last-name").value;
    var email		= document.getElementById("email").value;
  
    // Perform some simple validation
    if (firstName.length == 0 || firstName == "First Name") {
        errors = true;
        $("input#first-name").css("background", "#FCCCCC");
    }
    else
        $("input#first-name").css("background", "#E1FCCC");
    
    if (lastName.length == 0 || lastName == "Last Name") {
        errors = true;
        $("input#last-name").css("background", "#FCCCCC");
    }
    else
        $("input#last-name").css("background", "#E1FCCC");
    
    // TODO: validate email
    if (email.length == 0 || email == "Email Address") {
        errors = true;
        $("input#email").css("background", "#FCCCCC");
    }
    else
        $("input#email").css("background", "#E1FCCC");
  
    if (errors) {
        alert("Please fill in all fields.");
    }
    else {
        // Send the data via AJAX
        $.post(url, {
            first_name: firstName,
            last_name: lastName,
            email: email
        }, function(data) {
      
            if (data == "success") {
                // Hide the form content
                $("#newsletter-form").css("display", "none");
                // Show the complete content
                $("#newsletter-complete").css("display", "block");
            }
            else {
                // An error occured
                // Hide the form content
                error = data.split("|");
                alert(error[1]);
                //$("#newsletter-form").css("display", "none");
                // Show the error content
                //$("#newsletter-error").css("display", "block");
        
                //error = data.split("|");
        
                // Display an error
                //$("#newsletter-error").html("<p>" + error[1] + "</p>");

            }
      
        });
    }
    return false;
}
  
/**
 * Loads in a billing statement. Page is pulled in over AJAX. Unique refNo is
 * passed over as the param. Displays a dialog on the screen
 */
function getBillingStatement(refNo,url) {
  
    // Show the loading image, hide the content
    $("#statement-content").html("");
    $("a.print-statement").css("display", "none");
    $("#statement-content").css("display", "none");
    $("#statement-loading").css("display", "block");
  
    // Pull the page in over AJAX
    $.post(url, {
        ref_no: refNo
    }, function(data) {
    
        // Check if there is an error
        var split = data.split(" | ");
        if (split[0] == "error") {
            // Display the error
            var content = "<p class=\"error-box\">" + split[1] + "</p>";
        }
        else {
            var content = data;
            $("a.print-statement").css("display", "block");
        }
      
        // Place the page content into the popups content div
        $("#statement-content").html(content);
    
        // Hide the loading image, display the content
        $("#statement-loading").css("display", "none");
        $("#statement-content").css("display", "block");
    
    });

    $('.billing-statement-dialog').css("display", "block");
    $('.billing-statement-dialog').dialog('open');
}

function disableButtons() {
    var anchors = $('a').unbind('click');
    for (var i=0; i<anchors.length; i++) {
        anchors[i].onclick="return false;";
    }
    $('a').click(function(){return false;});

    var inputs = $(':input');
    for (var j=0; j<inputs.length; j++) {
        inputs[j].onclick="return false;";
    }
}

function showCSCDialog() {
    document.getElementById('csc-dialog-box').style.display = 'block';
}

function hideCSCDialog() {
    document.getElementById('csc-dialog-box').style.display = '';
}

function ManageTabPanelDisplay() {
//
// Between the parenthesis, list the id's of the div's that
//     will be affected when tabs are clicked. List in any
//     order. Put the id's in single quotes (apostrophes)
//     and separate them with a comma -- all one line.
//
var idlist = new Array('tab1','tab2','tab3','tab1on','tab2on','tab3on','content1','content2','content3');

// No other customizations are necessary.
if(arguments.length < 1) { return; }
for(var i = 0; i < idlist.length; i++) {
   var block = false;
   for(var ii = 0; ii < arguments.length; ii++) {
      if(idlist[i] == arguments[ii]) {
         block = true;
         break;
         }
      }
   if(block) { document.getElementById(idlist[i]).style.display = "block"; }
   else { document.getElementById(idlist[i]).style.display = "none"; }
   }
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    if ( expires )
    {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}
Set_Cookie('screen', screen.width+'x'+screen.height, '', '/', '', '' );


function showVideoProgress(obj) {
    if (obj)
    {
        var $j = jQuery.noConflict();
        if(obj['error']) {
            $j('#uploadVideoResponse').hide();
            $j('#errorResponse').html('<ul class="form-errors"><li>The file that was just uploaded is not supported by our video player. Please contact customer support so that our technical team can convert it to a compatible format and append your video to your profile. Thank you.</li></ul>').show();
        } else {
            $j('#progressbar').show();
            $j("#progressbar").reportprogress(obj['progress']);
            if(obj['progress']!=100) {
                setTimeout("checkVideoProgress()",1500);
            } else {
                showVideo('video_'+ obj['video_id'], obj['video_path'], obj['thumbnail_path']);
            }
        }
    }
}
function checkVideoProgress() {
    var $j = jQuery.noConflict();
    if (document.getElementById('progress_video') != null) {
        $j.getJSON('/ajax/uploadVideoProgress/'+$j('#progress_video').html(),
            function JSON(json){
                showVideoProgress(json);
            }
        );
    }
}

function showVideo(container, file, preview) {
    var player_container = 'player_' + container;
    document.getElementById(container).innerHTML = '<a href="'+file+'" style="display:block;width:'+VIDEO_WIDTH+'px;height:'+VIDEO_HEIGHT+'px;" id="'+player_container+'"></a>';
    flowplayer(player_container, 
        {
            src:VIDEO_PLAYER,
            cachebusting: true
        }, {
            key: '#$a777753632c45e63e87',
            clip: {
                autoPlay: false,
                autoBuffering: true,
                scaling: 'fit'
            }
    });
}


function showVideoSection(container, file, width, height) {
    var player_container = 'player_' + container;
    document.getElementById(container).innerHTML = '<a href="'+file+'" style="display:block;width:'+width+'px;height:'+height+'px;" id="'+player_container+'"></a>';
    flowplayer(player_container,
        {
            src:VIDEO_PLAYER,
            cachebusting: true
        }, {
            key: '#$a777753632c45e63e87',
            clip: {
                autoPlay: false,
                autoBuffering: true,
                scaling: 'fit'
            }
    });
}